[PATCH v1 1/3] tests: check decoding of setsockopt(PACKET_[ADD|DROP]_MEMBERSHIP)

Zhibin Li 08826794brmt at gmail.com
Tue Jul 24 08:59:14 UTC 2018


* tests/net-packet_mreq.c: New file.
* tests/.gitignore: Add net-packet_mreq.
* tests/pure_executables.list: Likewise.
* tests/gen_tests.in (net-packet_mreq): New test.
---
 tests/.gitignore            |   1 +
 tests/gen_tests.in          |   1 +
 tests/net-packet_mreq.c     | 100 ++++++++++++++++++++++++++++++++++++
 tests/pure_executables.list |   1 +
 4 files changed, 103 insertions(+)
 create mode 100644 tests/net-packet_mreq.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 2811afa0..b98d535c 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -251,6 +251,7 @@ munlockall
 nanosleep
 net-accept-connect
 net-icmp_filter
+net-packet_mreq
 net-sockaddr
 net-y-unix
 net-yy-inet
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 9e7dedab..44d6d715 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -234,6 +234,7 @@ msg_name	-a20 -e trace=recvmsg
 munlockall	-a13
 nanosleep	-a20
 net-icmp_filter	-e trace=getsockopt,setsockopt
+net-packet_mreq -e trace=setsockopt
 net-sockaddr	-a24 -e trace=connect
 net-yy-inet6	+net-yy-inet.test
 netlink_audit	+netlink_sock_diag.test
diff --git a/tests/net-packet_mreq.c b/tests/net-packet_mreq.c
new file mode 100644
index 00000000..3e970270
--- /dev/null
+++ b/tests/net-packet_mreq.c
@@ -0,0 +1,100 @@
+#include "tests.h"
+#include <stdio.h>
+#include <sys/socket.h>
+#include <linux/if_packet.h>
+
+static const char *errstr;
+
+static long
+packet_mreq_membership(int optname, void *optval, socklen_t len)
+{
+	long rc = setsockopt(-1, SOL_PACKET, optname, optval, len);
+	errstr = sprintrc(rc);
+	return rc;
+}
+
+static void
+test_packet_mreq(const int optname, const char *const optname_str)
+{
+	TAIL_ALLOC_OBJECT_CONST_PTR(struct packet_mreq, pmreq);
+	socklen_t len = sizeof(struct packet_mreq);
+
+	/* setsockopt with optname unknown */
+	packet_mreq_membership(-1, NULL, 0);
+	printf("setsockopt(-1, SOL_PACKET, %#x /* PACKET_??? */, NULL, 0) = %s\n",
+	       -1, errstr);
+
+	/* setsockopt with mr_type unknown */
+	pmreq->mr_ifindex = 0;
+	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address);
+	packet_mreq_membership(optname, pmreq, len);
+	printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
+	       " mr_type=%#x /* PACKET_MR_??? */, mr_alen=%d, mr_address=",
+	       optname_str, pmreq->mr_ifindex, pmreq->mr_type, pmreq->mr_alen);
+	for (unsigned int i = 0; i < ARRAY_SIZE(pmreq->mr_address); i++) {
+		printf("%02x", pmreq->mr_address[i]);
+	}
+	printf("}, %d) = %s\n", len, errstr);
+
+	/* setsockopt with mr_type unknown and mr_alen > sizeof(mr_address) */
+	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address) + 1;
+	packet_mreq_membership(optname, pmreq, len);
+	printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
+	       " mr_type=%#x /* PACKET_MR_??? */, mr_alen=%d, mr_address=",
+	       optname_str, pmreq->mr_ifindex, pmreq->mr_type, pmreq->mr_alen);
+	for (unsigned int i = 0; i < ARRAY_SIZE(pmreq->mr_address); i++) {
+		printf("%02x", pmreq->mr_address[i]);
+	}
+	printf("}, %d) = %s\n", len, errstr);
+
+	/* setsockopt with mr_type unknown and mr_alen < sizeof(mr_address) */
+	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address) - 1;
+	packet_mreq_membership(optname, pmreq, len);
+	printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
+	       " mr_type=%#x /* PACKET_MR_??? */, mr_alen=%d, mr_address=",
+	       optname_str, pmreq->mr_ifindex, pmreq->mr_type, pmreq->mr_alen);
+	for (unsigned int i = 0; i < pmreq->mr_alen; i++) {
+		printf("%02x", pmreq->mr_address[i]);
+	}
+	printf("}, %d) = %s\n", len, errstr);
+
+	/* setsockopt with valid mr_type */
+	pmreq->mr_alen = ARRAY_SIZE(pmreq->mr_address);
+	static const struct {
+		unsigned short type;
+		const char *const type_str;
+	} a[] = {
+		{ ARG_STR(PACKET_MR_MULTICAST) },
+		{ ARG_STR(PACKET_MR_PROMISC) },
+		{ ARG_STR(PACKET_MR_ALLMULTI) },
+		{ ARG_STR(PACKET_MR_UNICAST) },
+	};
+
+	for (unsigned int i = 0; i < ARRAY_SIZE(a); i++) {
+		pmreq->mr_type = a[i].type;
+		packet_mreq_membership(optname, pmreq, len);
+		printf("setsockopt(-1, SOL_PACKET, %s, {mr_ifindex=%d,"
+		       " mr_type=%s, mr_alen=%d, mr_address=",
+		       optname_str, pmreq->mr_ifindex, a[i].type_str, pmreq->mr_alen);
+		for (unsigned int i = 0; i < pmreq->mr_alen; i++) {
+			printf("%02x", pmreq->mr_address[i]);
+		}
+		printf("}, %d) = %s\n", len, errstr);
+	}
+
+	/* setsockopt with optlen larger than usual */
+	len = len + 1;
+	packet_mreq_membership(optname, pmreq, len);
+	printf("setsockopt(-1, SOL_PACKET, %s, %p,"
+	       " %d) = %s\n", optname_str, pmreq, len, errstr);
+}
+
+int
+main(void)
+{
+	test_packet_mreq(ARG_STR(PACKET_ADD_MEMBERSHIP));
+	test_packet_mreq(ARG_STR(PACKET_DROP_MEMBERSHIP));
+
+	puts("+++ exited with 0 +++");
+	return 0;
+}
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index 6926728a..af6883f1 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -205,6 +205,7 @@ msg_name
 munlockall
 nanosleep
 net-icmp_filter
+net-packet_mreq
 net-sockaddr
 net-y-unix
 net-yy-inet
-- 
2.17.1



More information about the Strace-devel mailing list