[CRIU] [PATCH 2/2] zdtm: Add test for SOCK_PACKET sockets

the7winds at yandex.ru the7winds at yandex.ru
Tue Dec 20 06:17:35 PST 2016


From: Gleb Valin <the7winds at yandex.ru>

This one covers the following cases:
* socket is bound to an interface
* socket isn't bound to an interface

Signed-off-by: Gleb Valin <the7winds at yandex.ru>
Signed-off-by: Eugene Batalov <eabatalov89 at gmail.com>
---
 test/zdtm/static/Makefile              |  1 +
 test/zdtm/static/packet_sock_spkt.c    | 88 ++++++++++++++++++++++++++++++++++
 test/zdtm/static/packet_sock_spkt.desc |  1 +
 3 files changed, 90 insertions(+)
 create mode 100644 test/zdtm/static/packet_sock_spkt.c
 create mode 100644 test/zdtm/static/packet_sock_spkt.desc

diff --git a/test/zdtm/static/Makefile b/test/zdtm/static/Makefile
index 46bca200..3d8dde6d 100644
--- a/test/zdtm/static/Makefile
+++ b/test/zdtm/static/Makefile
@@ -41,6 +41,7 @@ TST_NOFILE	:=				\
 		socket_dgram_data		\
 		packet_sock			\
 		packet_sock_mmap		\
+		packet_sock_spkt		\
 		sock_filter			\
 		msgque				\
 		inotify_system			\
diff --git a/test/zdtm/static/packet_sock_spkt.c b/test/zdtm/static/packet_sock_spkt.c
new file mode 100644
index 00000000..4e9540fb
--- /dev/null
+++ b/test/zdtm/static/packet_sock_spkt.c
@@ -0,0 +1,88 @@
+#include "zdtmtst.h"
+
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
+#include <linux/if_ether.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+const char *test_doc = "Check bound and not bound SOCK_PACKET sockets";
+const char *test_author = "Gleb Valin <the7winds at yandex.ru>";
+
+struct ethframe {
+	struct ethhdr header;
+	char data[ETH_DATA_LEN];
+};
+
+static int do_bind(int sk)
+{
+	struct sockaddr addr = {};
+
+	addr.sa_family = AF_PACKET;
+	strcpy(addr.sa_data, "lo");
+
+	return bind(sk, &addr, sizeof(addr));
+}
+
+static int check_socket_binding(int sk, char *dev)
+{
+	struct sockaddr addr = {};
+
+	socklen_t l = sizeof(addr);
+
+	if (getsockname(sk, &addr, &l) < 0)
+		return -1;
+
+	if (addr.sa_family != AF_PACKET)
+		return -1;
+
+	if (strcmp(addr.sa_data, dev) != 0)
+		return -1;
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int sk1;
+	int sk2;
+
+	test_init(argc, argv);
+
+	sk1 = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ALL));
+
+	if (sk1  < 0) {
+		pr_perror("Can't create socket 1");
+		return 1;
+	}
+
+	if (do_bind(sk1) < 0) {
+		pr_perror("Can't bind sosket 1");
+		return 1;
+	}
+
+	sk2 = socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ALL));
+
+	if (sk2 < 0) {
+		pr_perror("Can't create socket 2");
+		return 1;
+	}
+
+	test_daemon();
+	test_waitsig();
+
+	if (check_socket_binding(sk1, "lo") < 0) {
+		fail("Socket 1 has wrong binding");
+		return 1;
+	}
+
+	if (check_socket_binding(sk2, "") < 0) {
+		fail("Socket 2 has wrong binding");
+		return 1;
+	}
+
+	pass();
+	return 0;
+}
diff --git a/test/zdtm/static/packet_sock_spkt.desc b/test/zdtm/static/packet_sock_spkt.desc
new file mode 100644
index 00000000..daba6421
--- /dev/null
+++ b/test/zdtm/static/packet_sock_spkt.desc
@@ -0,0 +1 @@
+{'flavor':'h', 'flags' : 'suid'}
-- 
2.11.0



More information about the CRIU mailing list