diff options
author | Peter Oskolkov <posk@google.com> | 2018-12-19 13:20:09 -0500 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2018-12-19 19:41:43 -0500 |
commit | 3f2eadb10886340f4d67fe683c2e53b4df005461 (patch) | |
tree | 30dac41744461a8978d5b30aea8e13277cdde425 /tools | |
parent | 9c7f37e5ca14f5b04894b1b699a9903885cdafa6 (diff) |
selftests: net: refactor reuseport_addr_any test
This patch refactors reuseport_add_any selftest a bit:
- makes it more modular (eliminates several copy/pasted blocks);
- skips DCCP tests if DCCP is not supported
V2: added "Signed-off-by" tag.
Signed-off-by: Peter Oskolkov <posk@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/testing/selftests/net/reuseport_addr_any.c | 121 |
1 files changed, 38 insertions, 83 deletions
diff --git a/tools/testing/selftests/net/reuseport_addr_any.c b/tools/testing/selftests/net/reuseport_addr_any.c index 4ac3e24b7cfc..6f54d425dba9 100644 --- a/tools/testing/selftests/net/reuseport_addr_any.c +++ b/tools/testing/selftests/net/reuseport_addr_any.c | |||
@@ -203,7 +203,9 @@ static void test(int *rcv_fds, int count, int family, int proto, int fd) | |||
203 | close(epfd); | 203 | close(epfd); |
204 | } | 204 | } |
205 | 205 | ||
206 | int main(void) | 206 | |
207 | static void run_one_test(int fam_send, int fam_rcv, int proto, | ||
208 | const char *addr_str) | ||
207 | { | 209 | { |
208 | /* Below we test that a socket listening on a specific address | 210 | /* Below we test that a socket listening on a specific address |
209 | * is always selected in preference over a socket listening | 211 | * is always selected in preference over a socket listening |
@@ -214,95 +216,48 @@ int main(void) | |||
214 | */ | 216 | */ |
215 | int rcv_fds[10], i; | 217 | int rcv_fds[10], i; |
216 | 218 | ||
217 | fprintf(stderr, "---- UDP IPv4 ----\n"); | 219 | build_rcv_fd(AF_INET, proto, rcv_fds, 2, NULL); |
218 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 2, NULL); | 220 | build_rcv_fd(AF_INET6, proto, rcv_fds + 2, 2, NULL); |
219 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 2, 2, NULL); | 221 | build_rcv_fd(fam_rcv, proto, rcv_fds + 4, 1, addr_str); |
220 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 4, 1, IP4_ADDR); | 222 | build_rcv_fd(AF_INET, proto, rcv_fds + 5, 2, NULL); |
221 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 5, 2, NULL); | 223 | build_rcv_fd(AF_INET6, proto, rcv_fds + 7, 2, NULL); |
222 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 7, 2, NULL); | 224 | test(rcv_fds, 9, fam_send, proto, rcv_fds[4]); |
223 | test(rcv_fds, 9, AF_INET, SOCK_DGRAM, rcv_fds[4]); | ||
224 | for (i = 0; i < 9; ++i) | ||
225 | close(rcv_fds[i]); | ||
226 | |||
227 | fprintf(stderr, "---- UDP IPv6 ----\n"); | ||
228 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 2, NULL); | ||
229 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 2, 2, NULL); | ||
230 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 4, 1, IP6_ADDR); | ||
231 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 5, 2, NULL); | ||
232 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 7, 2, NULL); | ||
233 | test(rcv_fds, 9, AF_INET6, SOCK_DGRAM, rcv_fds[4]); | ||
234 | for (i = 0; i < 9; ++i) | ||
235 | close(rcv_fds[i]); | ||
236 | |||
237 | fprintf(stderr, "---- UDP IPv4 mapped to IPv6 ----\n"); | ||
238 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 2, NULL); | ||
239 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 2, 2, NULL); | ||
240 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 4, 1, IP4_MAPPED6); | ||
241 | build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 5, 2, NULL); | ||
242 | build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 7, 2, NULL); | ||
243 | test(rcv_fds, 9, AF_INET, SOCK_DGRAM, rcv_fds[4]); | ||
244 | for (i = 0; i < 9; ++i) | ||
245 | close(rcv_fds[i]); | ||
246 | |||
247 | fprintf(stderr, "---- TCP IPv4 ----\n"); | ||
248 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 2, NULL); | ||
249 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 2, 2, NULL); | ||
250 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 4, 1, IP4_ADDR); | ||
251 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 5, 2, NULL); | ||
252 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 7, 2, NULL); | ||
253 | test(rcv_fds, 9, AF_INET, SOCK_STREAM, rcv_fds[4]); | ||
254 | for (i = 0; i < 9; ++i) | 225 | for (i = 0; i < 9; ++i) |
255 | close(rcv_fds[i]); | 226 | close(rcv_fds[i]); |
227 | fprintf(stderr, "pass\n"); | ||
228 | } | ||
256 | 229 | ||
257 | fprintf(stderr, "---- TCP IPv6 ----\n"); | 230 | static void test_proto(int proto, const char *proto_str) |
258 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 2, NULL); | 231 | { |
259 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 2, 2, NULL); | 232 | if (proto == SOCK_DCCP) { |
260 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 4, 1, IP6_ADDR); | 233 | int test_fd; |
261 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 5, 2, NULL); | 234 | |
262 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 7, 2, NULL); | 235 | test_fd = socket(AF_INET, proto, 0); |
263 | test(rcv_fds, 9, AF_INET6, SOCK_STREAM, rcv_fds[4]); | 236 | if (test_fd < 0) { |
264 | for (i = 0; i < 9; ++i) | 237 | if (errno == ESOCKTNOSUPPORT) { |
265 | close(rcv_fds[i]); | 238 | fprintf(stderr, "DCCP not supported: skipping DCCP tests\n"); |
239 | return; | ||
240 | } else | ||
241 | error(1, errno, "failed to create a DCCP socket"); | ||
242 | } | ||
243 | close(test_fd); | ||
244 | } | ||
266 | 245 | ||
267 | fprintf(stderr, "---- TCP IPv4 mapped to IPv6 ----\n"); | 246 | fprintf(stderr, "%s IPv4 ... ", proto_str); |
268 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 2, NULL); | 247 | run_one_test(AF_INET, AF_INET, proto, IP4_ADDR); |
269 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 2, 2, NULL); | ||
270 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 4, 1, IP4_MAPPED6); | ||
271 | build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 5, 2, NULL); | ||
272 | build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 7, 2, NULL); | ||
273 | test(rcv_fds, 9, AF_INET, SOCK_STREAM, rcv_fds[4]); | ||
274 | for (i = 0; i < 9; ++i) | ||
275 | close(rcv_fds[i]); | ||
276 | 248 | ||
277 | fprintf(stderr, "---- DCCP IPv4 ----\n"); | 249 | fprintf(stderr, "%s IPv6 ... ", proto_str); |
278 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds, 2, NULL); | 250 | run_one_test(AF_INET6, AF_INET6, proto, IP6_ADDR); |
279 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 2, 2, NULL); | ||
280 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 4, 1, IP4_ADDR); | ||
281 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 5, 2, NULL); | ||
282 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 7, 2, NULL); | ||
283 | test(rcv_fds, 9, AF_INET, SOCK_DCCP, rcv_fds[4]); | ||
284 | for (i = 0; i < 9; ++i) | ||
285 | close(rcv_fds[i]); | ||
286 | 251 | ||
287 | fprintf(stderr, "---- DCCP IPv6 ----\n"); | 252 | fprintf(stderr, "%s IPv4 mapped to IPv6 ... ", proto_str); |
288 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds, 2, NULL); | 253 | run_one_test(AF_INET, AF_INET6, proto, IP4_MAPPED6); |
289 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 2, 2, NULL); | 254 | } |
290 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 4, 1, IP6_ADDR); | ||
291 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 5, 2, NULL); | ||
292 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 7, 2, NULL); | ||
293 | test(rcv_fds, 9, AF_INET6, SOCK_DCCP, rcv_fds[4]); | ||
294 | for (i = 0; i < 9; ++i) | ||
295 | close(rcv_fds[i]); | ||
296 | 255 | ||
297 | fprintf(stderr, "---- DCCP IPv4 mapped to IPv6 ----\n"); | 256 | int main(void) |
298 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds, 2, NULL); | 257 | { |
299 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 2, 2, NULL); | 258 | test_proto(SOCK_DGRAM, "UDP"); |
300 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 4, 1, IP4_MAPPED6); | 259 | test_proto(SOCK_STREAM, "TCP"); |
301 | build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 5, 2, NULL); | 260 | test_proto(SOCK_DCCP, "DCCP"); |
302 | build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 7, 2, NULL); | ||
303 | test(rcv_fds, 9, AF_INET, SOCK_DCCP, rcv_fds[4]); | ||
304 | for (i = 0; i < 9; ++i) | ||
305 | close(rcv_fds[i]); | ||
306 | 261 | ||
307 | fprintf(stderr, "SUCCESS\n"); | 262 | fprintf(stderr, "SUCCESS\n"); |
308 | return 0; | 263 | return 0; |