[PATCH v1 2/3] tests: check decoding of setsockopt(PACKET_[R|T]X_RING)

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


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

diff --git a/tests/.gitignore b/tests/.gitignore
index b98d535c..011e3132 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -253,6 +253,7 @@ net-accept-connect
 net-icmp_filter
 net-packet_mreq
 net-sockaddr
+net-tpacket_req
 net-y-unix
 net-yy-inet
 net-yy-inet6
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 44d6d715..c3cd1a0b 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -236,6 +236,7 @@ nanosleep	-a20
 net-icmp_filter	-e trace=getsockopt,setsockopt
 net-packet_mreq -e trace=setsockopt
 net-sockaddr	-a24 -e trace=connect
+net-tpacket_req -e trace=setsockopt
 net-yy-inet6	+net-yy-inet.test
 netlink_audit	+netlink_sock_diag.test
 netlink_crypto	+netlink_sock_diag.test
diff --git a/tests/net-tpacket_req.c b/tests/net-tpacket_req.c
new file mode 100644
index 00000000..f9c05043
--- /dev/null
+++ b/tests/net-tpacket_req.c
@@ -0,0 +1,50 @@
+#include "tests.h"
+#include <stdio.h>
+#include <sys/socket.h>
+#include <linux/if_packet.h>
+
+static const char *errstr;
+
+static long
+set_tpacket_req(int optname, void *optval, socklen_t len)
+{
+	long rc = setsockopt(-1, SOL_PACKET, optname, optval, len);
+	errstr = sprintrc(rc);
+	return rc;
+}
+
+static void
+test_tpacket_req(const int optname, const char *const optname_str)
+{
+	TAIL_ALLOC_OBJECT_CONST_PTR(struct tpacket_req, tpreq);
+	socklen_t len;
+
+	/* setsockopt with optname unknown */
+	set_tpacket_req(-1, NULL, 0);
+	printf("setsockopt(-1, SOL_PACKET, %#x /* PACKET_??? */, NULL, 0) = %s\n",
+	       -1, errstr);
+
+	/* classic setsockopt */
+	len = sizeof(struct tpacket_req);
+	set_tpacket_req(optname, tpreq, len);
+	printf("setsockopt(-1, SOL_PACKET, %s, {tp_block_size=%u,"
+	       " tp_block_nr=%u, tp_frame_size=%u, tp_frame_nr=%u}, %d) = %s\n",
+	       optname_str, tpreq->tp_block_size, tpreq->tp_block_nr,
+	       tpreq->tp_frame_size, tpreq->tp_frame_nr, len, errstr);
+
+	/* setsockopt with optlen larger than usual */
+	len = len + 1;
+	set_tpacket_req(optname, tpreq, len);
+	printf("setsockopt(-1, SOL_PACKET, %s, %p,"
+	       " %d) = %s\n", optname_str, tpreq, len, errstr);
+}
+
+int
+main(void)
+{
+	test_tpacket_req(ARG_STR(PACKET_RX_RING));
+	test_tpacket_req(ARG_STR(PACKET_TX_RING));
+
+	puts("+++ exited with 0 +++");
+	return 0;
+}
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index af6883f1..e8345252 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -207,6 +207,7 @@ nanosleep
 net-icmp_filter
 net-packet_mreq
 net-sockaddr
+net-tpacket_req
 net-y-unix
 net-yy-inet
 net-yy-inet6
-- 
2.17.1



More information about the Strace-devel mailing list