[CRIU] [PATCH 18/21] zdtm: add a test for unconnected tcp sockets

Andrei Vagin avagin at openvz.org
Thu Dec 1 00:32:36 PST 2016


From: Andrei Vagin <avagin at virtuozzo.com>

Check source and destination addresses for closed tcp sockets.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 test/zdtm/static/Makefile                |   3 +
 test/zdtm/static/socket-tcp-unconn.c     | 117 +++++++++++++++++++++++++++++++
 test/zdtm/static/socket-tcp-unconn.desc  |   1 +
 test/zdtm/static/socket-tcp6-unconn.c    |   1 +
 test/zdtm/static/socket-tcp6-unconn.desc |   1 +
 5 files changed, 123 insertions(+)
 create mode 100644 test/zdtm/static/socket-tcp-unconn.c
 create mode 100644 test/zdtm/static/socket-tcp-unconn.desc
 create mode 120000 test/zdtm/static/socket-tcp6-unconn.c
 create mode 120000 test/zdtm/static/socket-tcp6-unconn.desc

diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index a04df8d..989e677 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -85,6 +85,8 @@ TST_NOFILE	:=				\
 		socket-tcp-closed		\
 		socket-tcp-closed-last-ack	\
 		socket-tcp6-closed		\
+		socket-tcp-unconn		\
+		socket-tcp6-unconn		\
 		sock_opts00			\
 		sock_opts01			\
 		sk-unix-unconn			\
@@ -440,6 +442,7 @@ socket-tcp6-close-wait:	override CFLAGS += -D ZDTM_TCP_CLOSE_WAIT -D ZDTM_IPV6
 socket-tcp-last-ack:	override CFLAGS += -D ZDTM_TCP_LAST_ACK
 socket-tcp6-last-ack:	override CFLAGS += -D ZDTM_TCP_LAST_ACK -D ZDTM_IPV6
 socket-tcp6-closing:	override CFLAGS += -D ZDTM_IPV6
+socket-tcp6-unconn:	override CFLAGS += -D ZDTM_IPV6
 
 $(LIB):	force
 	$(Q) $(MAKE) -C $(LIBDIR)
diff --git a/test/zdtm/static/socket-tcp-unconn.c b/test/zdtm/static/socket-tcp-unconn.c
new file mode 100644
index 0000000..124a733
--- /dev/null
+++ b/test/zdtm/static/socket-tcp-unconn.c
@@ -0,0 +1,117 @@
+#include "zdtmtst.h"
+
+#ifdef ZDTM_IPV6
+#define ZDTM_FAMILY AF_INET6
+#else
+#define ZDTM_FAMILY AF_INET
+#endif
+
+const char *test_doc = "Check unconnected tcp sockets\n";
+const char *test_author = "Andrey Vagin <avagin at openvz.org";
+
+#include <stdio.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+
+static int port = 8880;
+
+union sockaddr_inet {
+	struct sockaddr addr;
+	struct sockaddr_in v4;
+	struct sockaddr_in6 v6;
+};
+
+int main(int argc, char **argv)
+{
+	int fd, fd_s, sock, sk;
+	union sockaddr_inet addr, src_addr;
+	socklen_t aux;
+
+	test_init(argc, argv);
+
+	sk = socket(ZDTM_FAMILY, SOCK_STREAM, 0);
+	if (sk < 0) {
+		pr_perror("socket");
+		return 1;
+	}
+
+	if ((fd_s = tcp_init_server(ZDTM_FAMILY, &port)) < 0) {
+		pr_err("initializing server failed\n");
+		return 1;
+	}
+
+
+	if ((sock = socket(ZDTM_FAMILY, SOCK_STREAM, IPPROTO_TCP)) < 0) {
+		pr_perror("can't create socket");
+		return -1;
+	}
+
+	/* Construct the server address structure */
+	memset(&addr, 0, sizeof(addr));
+	if (ZDTM_FAMILY == AF_INET) {
+		addr.v4.sin_family      = AF_INET;
+		inet_pton(AF_INET, "localhost", &addr.v4.sin_addr);
+	} else {
+		addr.v6.sin6_family      = AF_INET6;
+		inet_pton(AF_INET6, "localhost", &addr.v6.sin6_addr);
+	}
+	if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		pr_perror("can't connect to server");
+		return -1;
+	}
+	aux = sizeof(src_addr);
+	memset(&src_addr, 0, sizeof(src_addr));
+	if (getsockname(sock, &src_addr.addr, &aux)) {
+		pr_perror("getsockname");
+		return 1;
+	}
+
+	test_daemon();
+	test_waitsig();
+
+	memset(&addr, 0, sizeof(addr));
+	if (getsockname(sock, &addr.addr, &aux)) {
+		pr_perror("getsockname");
+		return 1;
+	}
+	if (memcmp(&addr, &src_addr, aux)) {
+		pr_err("A source address mismatch");
+		return 1;
+	}
+
+	/* Construct the server address structure */
+	memset(&addr, 0, sizeof(addr));
+	if (ZDTM_FAMILY == AF_INET) {
+		addr.v4.sin_family      = AF_INET;
+		addr.v4.sin_port        = htons(port);
+		inet_pton(AF_INET, "localhost", &addr.v4.sin_addr);
+	} else {
+		addr.v6.sin6_family      = AF_INET6;
+		addr.v6.sin6_port        = htons(port);
+		inet_pton(AF_INET6, "localhost", &addr.v6.sin6_addr);
+	}
+	if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		pr_perror("can't connect to server");
+		return -1;
+	}
+
+	/*
+	 * parent is server of TCP connection
+	 */
+	fd = tcp_accept_server(fd_s);
+	if (fd < 0) {
+		pr_err("can't accept client connection\n");
+		return 1;
+	}
+	close(fd_s);
+
+
+	pass();
+	return 0;
+}
diff --git a/test/zdtm/static/socket-tcp-unconn.desc b/test/zdtm/static/socket-tcp-unconn.desc
new file mode 100644
index 0000000..155e103
--- /dev/null
+++ b/test/zdtm/static/socket-tcp-unconn.desc
@@ -0,0 +1 @@
+{'opts': '--tcp-established', 'flags': 'nouser samens', 'feature' : 'tcp_half_closed'}
diff --git a/test/zdtm/static/socket-tcp6-unconn.c b/test/zdtm/static/socket-tcp6-unconn.c
new file mode 120000
index 0000000..59efc05
--- /dev/null
+++ b/test/zdtm/static/socket-tcp6-unconn.c
@@ -0,0 +1 @@
+socket-tcp-unconn.c
\ No newline at end of file
diff --git a/test/zdtm/static/socket-tcp6-unconn.desc b/test/zdtm/static/socket-tcp6-unconn.desc
new file mode 120000
index 0000000..426e48c
--- /dev/null
+++ b/test/zdtm/static/socket-tcp6-unconn.desc
@@ -0,0 +1 @@
+socket-tcp-unconn.desc
\ No newline at end of file
-- 
2.7.4



More information about the CRIU mailing list