DORMANTno replies

[PATCH net-next] selftests: net: refactor reuseport_addr_any test

From: Peter Oskolkov <hidden>
Date: 2018-12-19 18:08:16
Subsystem: kernel selftest framework, networking [general], the rest · Maintainers: Shuah Khan, Shuah Khan, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds

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

---
 .../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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help