]> granicus.if.org Git - strace/commitdiff
tests: check decoding of setsockopt(PACKET_[ADD|DROP]_MEMBERSHIP)
authorZhibin Li <08826794brmt@gmail.com>
Tue, 24 Jul 2018 08:59:14 +0000 (16:59 +0800)
committerDmitry V. Levin <ldv@altlinux.org>
Wed, 1 Aug 2018 14:57:16 +0000 (14:57 +0000)
* 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
tests/gen_tests.in
tests/net-packet_mreq.c [new file with mode: 0644]
tests/pure_executables.list

index 2811afa014cd0bbbfcbec22d20d36e5a272c3a5c..b98d535c6eb7888a1d8793027b28b79bd6e6f338 100644 (file)
@@ -251,6 +251,7 @@ munlockall
 nanosleep
 net-accept-connect
 net-icmp_filter
+net-packet_mreq
 net-sockaddr
 net-y-unix
 net-yy-inet
index 9e7dedab088b9c929ca5f0d21dc0d4850e551b0d..44d6d715c6a6ec5aed4ac82ec5f297fd5a488293 100644 (file)
@@ -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 (file)
index 0000000..88423b7
--- /dev/null
@@ -0,0 +1,102 @@
+#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) },
+#ifdef PACKET_MR_UNICAST
+               { ARG_STR(PACKET_MR_UNICAST) },
+#endif
+       };
+
+       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;
+}
index 6926728ad1e7eec3899bc64435b059c3d2c08cfe..af6883f1a5fa87d7f52953747c87484e28d6e21a 100755 (executable)
@@ -205,6 +205,7 @@ msg_name
 munlockall
 nanosleep
 net-icmp_filter
+net-packet_mreq
 net-sockaddr
 net-y-unix
 net-yy-inet