* [PATCH net-next v2] selftests: net: refactor reuseport_addr_any test
@ 2018-12-19 18:20 Peter Oskolkov
2018-12-20 0:42 ` David Miller
0 siblings, 1 reply; 2+ messages in thread
From: Peter Oskolkov @ 2018-12-19 18:20 UTC (permalink / raw)
To: David Miller, netdev; +Cc: Peter Oskolkov, Peter Oskolkov
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>
---
.../selftests/net/reuseport_addr_any.c | 121 ++++++------------
1 file 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)
close(epfd);
}
-int main(void)
+
+static void run_one_test(int fam_send, int fam_rcv, int proto,
+ const char *addr_str)
{
/* Below we test that a socket listening on a specific address
* is always selected in preference over a socket listening
@@ -214,95 +216,48 @@ int main(void)
*/
int rcv_fds[10], i;
- fprintf(stderr, "---- UDP IPv4 ----\n");
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 4, 1, IP4_ADDR);
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET, SOCK_DGRAM, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
-
- fprintf(stderr, "---- UDP IPv6 ----\n");
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 4, 1, IP6_ADDR);
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET6, SOCK_DGRAM, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
-
- fprintf(stderr, "---- UDP IPv4 mapped to IPv6 ----\n");
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 4, 1, IP4_MAPPED6);
- build_rcv_fd(AF_INET, SOCK_DGRAM, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DGRAM, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET, SOCK_DGRAM, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
-
- fprintf(stderr, "---- TCP IPv4 ----\n");
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 4, 1, IP4_ADDR);
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET, SOCK_STREAM, rcv_fds[4]);
+ build_rcv_fd(AF_INET, proto, rcv_fds, 2, NULL);
+ build_rcv_fd(AF_INET6, proto, rcv_fds + 2, 2, NULL);
+ build_rcv_fd(fam_rcv, proto, rcv_fds + 4, 1, addr_str);
+ build_rcv_fd(AF_INET, proto, rcv_fds + 5, 2, NULL);
+ build_rcv_fd(AF_INET6, proto, rcv_fds + 7, 2, NULL);
+ test(rcv_fds, 9, fam_send, proto, rcv_fds[4]);
for (i = 0; i < 9; ++i)
close(rcv_fds[i]);
+ fprintf(stderr, "pass\n");
+}
- fprintf(stderr, "---- TCP IPv6 ----\n");
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 4, 1, IP6_ADDR);
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET6, SOCK_STREAM, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
+static void test_proto(int proto, const char *proto_str)
+{
+ if (proto == SOCK_DCCP) {
+ int test_fd;
+
+ test_fd = socket(AF_INET, proto, 0);
+ if (test_fd < 0) {
+ if (errno == ESOCKTNOSUPPORT) {
+ fprintf(stderr, "DCCP not supported: skipping DCCP tests\n");
+ return;
+ } else
+ error(1, errno, "failed to create a DCCP socket");
+ }
+ close(test_fd);
+ }
- fprintf(stderr, "---- TCP IPv4 mapped to IPv6 ----\n");
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 4, 1, IP4_MAPPED6);
- build_rcv_fd(AF_INET, SOCK_STREAM, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_STREAM, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET, SOCK_STREAM, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
+ fprintf(stderr, "%s IPv4 ... ", proto_str);
+ run_one_test(AF_INET, AF_INET, proto, IP4_ADDR);
- fprintf(stderr, "---- DCCP IPv4 ----\n");
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 4, 1, IP4_ADDR);
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET, SOCK_DCCP, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
+ fprintf(stderr, "%s IPv6 ... ", proto_str);
+ run_one_test(AF_INET6, AF_INET6, proto, IP6_ADDR);
- fprintf(stderr, "---- DCCP IPv6 ----\n");
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 4, 1, IP6_ADDR);
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET6, SOCK_DCCP, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
+ fprintf(stderr, "%s IPv4 mapped to IPv6 ... ", proto_str);
+ run_one_test(AF_INET, AF_INET6, proto, IP4_MAPPED6);
+}
- fprintf(stderr, "---- DCCP IPv4 mapped to IPv6 ----\n");
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 2, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 4, 1, IP4_MAPPED6);
- build_rcv_fd(AF_INET, SOCK_DCCP, rcv_fds + 5, 2, NULL);
- build_rcv_fd(AF_INET6, SOCK_DCCP, rcv_fds + 7, 2, NULL);
- test(rcv_fds, 9, AF_INET, SOCK_DCCP, rcv_fds[4]);
- for (i = 0; i < 9; ++i)
- close(rcv_fds[i]);
+int main(void)
+{
+ test_proto(SOCK_DGRAM, "UDP");
+ test_proto(SOCK_STREAM, "TCP");
+ test_proto(SOCK_DCCP, "DCCP");
fprintf(stderr, "SUCCESS\n");
return 0;
--
2.20.0.405.gbc1bbc6f85-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH net-next v2] selftests: net: refactor reuseport_addr_any test
2018-12-19 18:20 [PATCH net-next v2] selftests: net: refactor reuseport_addr_any test Peter Oskolkov
@ 2018-12-20 0:42 ` David Miller
0 siblings, 0 replies; 2+ messages in thread
From: David Miller @ 2018-12-20 0:42 UTC (permalink / raw)
To: posk; +Cc: netdev, posk.devel
From: Peter Oskolkov <posk@google.com>
Date: Wed, 19 Dec 2018 10:20:09 -0800
> 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>
Applied, thanks Peter.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-12-20 0:42 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-19 18:20 [PATCH net-next v2] selftests: net: refactor reuseport_addr_any test Peter Oskolkov
2018-12-20 0:42 ` David Miller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).