[CRIU] [PATCH 19/21] zdtm: add test cases on TCP_SYN_SENT
Andrei Vagin
avagin at openvz.org
Mon Nov 28 11:27:54 PST 2016
From: Andrei Vagin <avagin at virtuozzo.com>
Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
test/zdtm/static/Makefile | 1 +
test/zdtm/static/socket-tcp-syn-sent.c | 138 ++++++++++++++++++++++++++++++
test/zdtm/static/socket-tcp-syn-sent.desc | 9 ++
3 files changed, 148 insertions(+)
create mode 100644 test/zdtm/static/socket-tcp-syn-sent.c
create mode 100644 test/zdtm/static/socket-tcp-syn-sent.desc
diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index d056e97..6d2a67a 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -87,6 +87,7 @@ TST_NOFILE := \
socket-tcp6-closed \
socket-tcp-unconn \
socket-tcp6-unconn \
+ socket-tcp-syn-sent \
sock_opts00 \
sock_opts01 \
sk-unix-unconn \
diff --git a/test/zdtm/static/socket-tcp-syn-sent.c b/test/zdtm/static/socket-tcp-syn-sent.c
new file mode 100644
index 0000000..e68307e
--- /dev/null
+++ b/test/zdtm/static/socket-tcp-syn-sent.c
@@ -0,0 +1,138 @@
+#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 <fcntl.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;
+ char cmd[4096];
+
+ 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 | SOCK_NONBLOCK, 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;
+ }
+
+ snprintf(cmd, sizeof(cmd), "iptables -w -t filter --protocol tcp -A INPUT --dport %d -j DROP", port);
+ if (system(cmd))
+ 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);
+ }
+
+ errno = 0;
+ if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == 0 || errno != EINPROGRESS) {
+ pr_perror("can't connect to server");
+ return -1;
+ }
+
+ test_daemon();
+ test_waitsig();
+
+ snprintf(cmd, sizeof(cmd), "iptables -w -t filter --protocol tcp -D INPUT --dport %d -j DROP", port);
+ if (system(cmd))
+ 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);
+
+ fcntl(sock, F_SETFL, 0);
+
+ char c = 5;
+ if (write(sock, &c, 1) != 1) {
+ fail("Unable to send data");
+ return 1;
+ }
+
+ c = 0;
+ if (read(fd, &c, 1) != 1 || c != 5) {
+ fail("Unable to recv data");
+ return 1;
+ }
+
+ c = 6;
+ if (write(fd, &c, 1) != 1) {
+ fail("Unable to send data");
+ return 1;
+ }
+
+ c = 0;
+ if (read(sock, &c, 1) != 1 || c != 6) {
+ fail("Unable to recv data");
+ return 1;
+ }
+
+
+ pass();
+ return 0;
+}
diff --git a/test/zdtm/static/socket-tcp-syn-sent.desc b/test/zdtm/static/socket-tcp-syn-sent.desc
new file mode 100644
index 0000000..16ae496
--- /dev/null
+++ b/test/zdtm/static/socket-tcp-syn-sent.desc
@@ -0,0 +1,9 @@
+{ 'deps': [ '/bin/sh',
+ '/sbin/iptables',
+ '/usr/lib64/xtables/libxt_tcp.so|/lib/xtables/libxt_tcp.so|/usr/lib/powerpc64le-linux-gnu/xtables/libxt_tcp.so|/usr/lib/x86_64-linux-gnu/xtables/libxt_tcp.so',
+ '/usr/lib64/xtables/libxt_standard.so|/lib/xtables/libxt_standard.so|/usr/lib/powerpc64le-linux-gnu/xtables/libxt_standard.so|/usr/lib/x86_64-linux-gnu/xtables/libxt_standard.so',
+ ],
+ 'opts': '--tcp-established',
+ 'flags': 'suid nouser samens',
+ 'feature' : 'tcp_half_closed'
+}
--
2.7.4
More information about the CRIU
mailing list