[PATCH v4 2/4] tests: add check for NETLINK_NETFILTER parser

Chen Jingpiao chenjingpiao at gmail.com
Thu Mar 8 14:53:45 UTC 2018


* tests/netlink_netfilter.c: Include <netinet/in.h>, <arpa/inet.h>
and <linux/netfilter/nf_tables.h>.
Replace "netlink.h" with "test_netlink.h".
(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWTABLE): New macros.
(test_nlmsg_done, test_nfgenmsg): New functions.
(main): Use them.
---
 tests/netlink_netfilter.c | 102 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 100 insertions(+), 2 deletions(-)

diff --git a/tests/netlink_netfilter.c b/tests/netlink_netfilter.c
index db2622eb7..2826017d1 100644
--- a/tests/netlink_netfilter.c
+++ b/tests/netlink_netfilter.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 JingPiao Chen <chenjingpiao at gmail.com>
+ * Copyright (c) 2017, 2018 Chen Jingpiao <chenjingpiao at gmail.com>
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -32,9 +32,24 @@
 # include <stdio.h>
 # include <string.h>
 # include <unistd.h>
+# include <netinet/in.h>
+# include <arpa/inet.h>
 # include <sys/socket.h>
-# include "netlink.h"
+# include "test_netlink.h"
 # include <linux/netfilter/nfnetlink.h>
+# ifdef HAVE_LINUX_NETFILTER_NF_TABLES_H
+#  include <linux/netfilter/nf_tables.h>
+# endif
+
+# ifndef NFNETLINK_V0
+#  define NFNETLINK_V0 0
+# endif
+# ifndef NFNL_SUBSYS_NFTABLES
+#  define NFNL_SUBSYS_NFTABLES 10
+# endif
+# ifndef NFT_MSG_NEWTABLE
+#  define NFT_MSG_NEWTABLE 0
+# endif
 
 static void
 test_nlmsg_type(const int fd)
@@ -70,6 +85,87 @@ test_nlmsg_type(const int fd)
 	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
 }
 
+static void
+test_nlmsg_done(const int fd)
+{
+	void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+	const int num = 0xabcdefad;
+
+	TEST_NETLINK(fd, nlh0, NLMSG_DONE, NLM_F_REQUEST,
+		     sizeof(num), &num, sizeof(num),
+		     printf("%d", num));
+}
+
+static void
+test_nfgenmsg(const int fd)
+{
+	void *const nlh0 = tail_alloc(NLMSG_HDRLEN);
+
+	struct nfgenmsg msg = {
+		.nfgen_family = AF_UNIX,
+		.version = NFNETLINK_V0,
+		.res_id = NFNL_SUBSYS_NFTABLES
+	};
+
+	TEST_NETLINK_OBJECT_EX_(fd, nlh0,
+				NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
+				"NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
+				NLM_F_REQUEST, "NLM_F_REQUEST",
+				msg, print_quoted_hex,
+				printf("{nfgen_family=AF_UNIX");
+				printf(", version=NFNETLINK_V0");
+				printf(", res_id=");
+				if (htons(NFNL_SUBSYS_NFTABLES) == NFNL_SUBSYS_NFTABLES)
+					printf("htons(NFNL_SUBSYS_NFTABLES)");
+				else
+					printf("NFNL_SUBSYS_NFTABLES");
+				);
+
+	msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
+	TEST_NETLINK_(fd, nlh0,
+		      NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
+		      "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
+		      NLM_F_REQUEST, "NLM_F_REQUEST",
+		      sizeof(msg), &msg, sizeof(msg),
+		      printf("{nfgen_family=AF_UNIX");
+		      printf(", version=NFNETLINK_V0");
+		      printf(", res_id=htons(NFNL_SUBSYS_NFTABLES)"));
+
+	msg.res_id = htons(0xabcd);
+	TEST_NETLINK_(fd, nlh0,
+		      NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
+		      "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
+		      NLM_F_REQUEST, "NLM_F_REQUEST",
+		      sizeof(msg), &msg, sizeof(msg),
+		      printf("{nfgen_family=AF_UNIX");
+		      printf(", version=NFNETLINK_V0");
+		      printf(", res_id=htons(%d)", 0xabcd));
+
+# ifdef NFNL_MSG_BATCH_BEGIN
+	msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
+	TEST_NETLINK(fd, nlh0,
+		     NFNL_MSG_BATCH_BEGIN, NLM_F_REQUEST,
+		     sizeof(msg), &msg, sizeof(msg),
+		     printf("{nfgen_family=AF_UNIX");
+		     printf(", version=NFNETLINK_V0");
+		     printf(", res_id=htons(%d)", NFNL_SUBSYS_NFTABLES));
+
+	char str_buf[NLMSG_ALIGN(sizeof(msg)) + 4];
+
+	msg.res_id = htons(0xabcd);
+	memcpy(str_buf, &msg, sizeof(msg));
+	memcpy(str_buf + NLMSG_ALIGN(sizeof(msg)), "1234", 4);
+
+	TEST_NETLINK(fd, nlh0,
+		     NFNL_MSG_BATCH_BEGIN, NLM_F_REQUEST,
+		     sizeof(str_buf), str_buf, sizeof(str_buf),
+		     printf("{nfgen_family=AF_UNIX");
+		     printf(", version=NFNETLINK_V0");
+		     printf(", res_id=htons(%d)"
+			    ", \"\\x31\\x32\\x33\\x34\"", 0xabcd));
+# endif /* NFNL_MSG_BATCH_BEGIN */
+}
+
 int main(void)
 {
 	skip_if_unavailable("/proc/self/fd/");
@@ -77,6 +173,8 @@ int main(void)
 	int fd = create_nl_socket(NETLINK_NETFILTER);
 
 	test_nlmsg_type(fd);
+	test_nlmsg_done(fd);
+	test_nfgenmsg(fd);
 
 	printf("+++ exited with 0 +++\n");
 
-- 
2.16.2.348.g24cec2248.dirty



More information about the Strace-devel mailing list