[CRIU] [PATCH 2/2] test: Add testing of a unix socket with relative name

Cyrill Gorcunov gorcunov at openvz.org
Tue Jul 28 07:02:37 PDT 2015


Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
---
 test/zdtm.sh                        |   1 +
 test/zdtm/live/static/Makefile      |   1 +
 test/zdtm/live/static/sk-unix-rel.c | 110 ++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+)
 create mode 100644 test/zdtm/live/static/sk-unix-rel.c

diff --git a/test/zdtm.sh b/test/zdtm.sh
index 38f6612917d0..130ef0195f94 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -75,6 +75,7 @@ generate_test_list()
 		static/socket_queues
 		static/deleted_unix_sock
 		static/sk-unix-unconn
+		static/sk-unix-rel
 		static/pid00
 		static/pstree
 		static/caps00
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index 8be2929cdee2..fe636d69e3e7 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -137,6 +137,7 @@ TST_FILE	=				\
 		link10				\
 		file_attr			\
 		deleted_unix_sock		\
+		sk-unix-rel			\
 		deleted_dev			\
 		unlink_fstat00			\
 		unlink_fstat01			\
diff --git a/test/zdtm/live/static/sk-unix-rel.c b/test/zdtm/live/static/sk-unix-rel.c
new file mode 100644
index 000000000000..d341ce9afee9
--- /dev/null
+++ b/test/zdtm/live/static/sk-unix-rel.c
@@ -0,0 +1,110 @@
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+#include <limits.h>
+#include <fcntl.h>
+
+#include "zdtmtst.h"
+
+const char *test_doc	= "Test unix stream sockets with relative name\n";
+const char *test_author	= "Cyrill Gorcunov <gorcunov at openvz.org";
+
+#define SK_DATA "packet"
+
+char *filename;
+TEST_OPTION(filename, string, "socket file name", 1);
+
+#define TEST_MODE 0640
+
+int main(int argc, char *argv[])
+{
+	struct sockaddr_un addr;
+	unsigned int addrlen;
+	int sock[2];
+
+	char path[PATH_MAX];
+	char buf[64];
+	char *cwd;
+	int ret;
+
+	test_init(argc, argv);
+
+	cwd = get_current_dir_name();
+	if (!cwd) {
+		fail("getcwd\n");
+		exit(1);
+	}
+
+	snprintf(path, sizeof(path), "%s/%s", cwd, filename);
+	unlink(path);
+
+	addr.sun_family = AF_UNIX;
+	strncpy(addr.sun_path, filename, sizeof(addr.sun_path));
+	addrlen = sizeof(addr.sun_family) + strlen(filename);
+
+	sock[0] = socket(AF_UNIX, SOCK_STREAM, 0);
+	sock[1] = socket(AF_UNIX, SOCK_STREAM, 0);
+	if (sock[0] < 0 || sock[1] < 0) {
+		fail("socket\n");
+		exit(1);
+	}
+
+	if (setsockopt(sock[0], SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0 ||
+	    setsockopt(sock[1], SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0) {
+		fail("setsockopt\n");
+		exit(1);
+	}
+
+	ret = bind(sock[0], &addr, addrlen);
+	if (ret) {
+		fail("bind\n");
+		exit(1);
+	}
+
+	ret = listen(sock[0], 16);
+	if (ret) {
+		fail("bind\n");
+		exit(1);
+	}
+
+	test_daemon();
+	test_waitsig();
+
+	if (connect(sock[1], &addr, addrlen)) {
+		fail("connect\n");
+		exit(1);
+	}
+
+	ret = accept(sock[0], NULL, NULL);
+	if (ret < 0) {
+		fail("accept");
+		exit(1);
+	}
+
+	memset(buf, 0, sizeof(buf));
+	write(sock[1], SK_DATA, sizeof(SK_DATA));
+	read(ret, &buf, sizeof(buf));
+
+	if (strcmp(buf, SK_DATA)) {
+		fail("data corrupted\n");
+		exit(1);
+	}
+	test_msg("stream            : '%s'\n", buf);
+	close(sock[0]);
+	close(sock[1]);
+	unlink(path);
+
+	pass();
+	return 0;
+}
-- 
2.4.3



More information about the CRIU mailing list