[PATCH v6] tests: extend for decoding of udev_monitor_netlink_header

Harsha Sharma harshasharmaiitr at gmail.com
Sun Apr 8 12:00:23 UTC 2018


* tests/netlink_kobject_uevent.c: Add test_nlmsg_type_udev.
---
Changes in v6:
* invoke sprintrc before printf call in existing testcases
* change argument of print_quoted_memory to DEFAULT_STRLEN
* Add case for len == offset

Changes in v5:
* invoke sprintrc before first printf call

Changes in v4:
* invoke sprintrc before first printf call
* minor fix

Changes in v3:
* change format type

Changes in v2:
* Add decoding of kernel messages
* initialize udev_monitor_netlink_header with non-zero values
* cover case for length exceeding size of structure

 tests/netlink_kobject_uevent.c | 92 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 90 insertions(+), 2 deletions(-)

diff --git a/tests/netlink_kobject_uevent.c b/tests/netlink_kobject_uevent.c
index cacab5fd..2c83324c 100644
--- a/tests/netlink_kobject_uevent.c
+++ b/tests/netlink_kobject_uevent.c
@@ -27,9 +27,92 @@
  */
 
 #include "tests.h"
+#include <string.h>
 #include <stdio.h>
 #include <sys/socket.h>
+#include <arpa/inet.h>
 #include "netlink.h"
+#include "netlink_kobject_uevent.h"
+
+static void
+test_nlmsg_type_udev(const int fd)
+{
+	long ret;
+	const char *err_rc;
+	unsigned int offset = 8;
+	struct udev_monitor_netlink_header uh = {
+		.prefix = "libudev",
+		.magic = htonl(0xfeedcafe),
+		.header_size = sizeof(uh),
+		.properties_off = 40,
+		.properties_len = 299,
+		.filter_subsystem_hash = htonl(0xc370b302),
+		.filter_devtype_hash = htonl(0x10800000),
+		.filter_tag_bloom_hi = htonl(0x2000400),
+		.filter_tag_bloom_lo = htonl(0x10800000),
+	};
+	unsigned int len = sizeof(uh);
+	char buf[len + offset];
+	memcpy(buf, &uh, len);
+	memcpy(buf + len, "12345678", offset);
+
+	ret = sendto(fd, &uh, len, MSG_DONTWAIT, NULL, 0);
+	err_rc = sprintrc(ret);
+	printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
+	       ", header_size=%u, properties_off=%u, properties_len=%u"
+	       ", filter_subsystem_hash=htonl(%#x)"
+	       ", filter_devtype_hash=htonl(%#x)"
+	       ", filter_tag_bloom_hi=htonl(%#x)"
+	       ", filter_tag_bloom_lo=htonl(%#x)}}, %u, MSG_DONTWAIT, NULL, "
+	       "0) = %s\n"
+	       , fd, uh.prefix,
+	       ntohl(uh.magic), uh.header_size, uh.properties_off,
+	       uh.properties_len, ntohl(uh.filter_subsystem_hash),
+	       ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
+	       ntohl(uh.filter_tag_bloom_lo), len, err_rc);
+
+	ret = sendto(fd, &buf, len + offset, MSG_DONTWAIT, NULL, 0);
+	err_rc = sprintrc(ret);
+	printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
+	       ", header_size=%u, properties_off=%u, properties_len=%u"
+	       ", filter_subsystem_hash=htonl(%#x)"
+	       ", filter_devtype_hash=htonl(%#x)"
+	       ", filter_tag_bloom_hi=htonl(%#x)"
+	       ", filter_tag_bloom_lo=htonl(%#x)}, "
+	       , fd, uh.prefix,
+	       ntohl(uh.magic), uh.header_size, uh.properties_off,
+	       uh.properties_len, ntohl(uh.filter_subsystem_hash),
+	       ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
+	       ntohl(uh.filter_tag_bloom_lo));
+	print_quoted_memory(buf + len, offset);
+	printf("}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       len + offset, err_rc);
+
+}
+
+static void
+test_nlmsg_type_kernel(const int fd)
+{
+	long ret;
+	const char *err_rc;
+	struct udev_monitor_netlink_header uh = {
+		.prefix = "change@",
+		.magic = htonl(0xfeedcafe),
+		.header_size = sizeof(uh),
+		.properties_off = 10,
+		.properties_len = 299,
+		.filter_subsystem_hash = htonl(0xfffffff),
+		.filter_devtype_hash = htonl(0x10000000),
+		.filter_tag_bloom_hi = htonl(0x2000400),
+	};
+
+	ret = sendto(fd, &uh, sizeof(uh), MSG_DONTWAIT, NULL, 0);
+	err_rc = sprintrc(ret);
+	printf("sendto(%d, ", fd);
+	print_quoted_memory(&uh, DEFAULT_STRLEN);
+	printf("..., %u, MSG_DONTWAIT, NULL, 0) = %s\n",
+	       (unsigned) sizeof(uh), err_rc);
+}
 
 int
 main(void)
@@ -37,24 +120,29 @@ main(void)
 	skip_if_unavailable("/proc/self/fd/");
 
 	long rc;
+	const char *err_rc;
 	int fd = create_nl_socket(NETLINK_KOBJECT_UEVENT);
 
+	test_nlmsg_type_udev(fd);
+	test_nlmsg_type_kernel(fd);
 	/* test using data that looks like a zero-length C string */
 	char *const buf = tail_alloc(DEFAULT_STRLEN + 1);
 	buf[0] = '=';
 	fill_memory_ex(buf + 1, DEFAULT_STRLEN, 0, DEFAULT_STRLEN);
 
 	rc = sendto(fd, buf + 1, DEFAULT_STRLEN, MSG_DONTWAIT, NULL, 0);
+	err_rc = sprintrc(rc);
 	printf("sendto(%d, ", fd);
 	print_quoted_memory(buf + 1, DEFAULT_STRLEN);
 	printf(", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
-	       DEFAULT_STRLEN, sprintrc(rc));
+	       DEFAULT_STRLEN, err_rc);
 
 	rc = sendto(fd, buf, DEFAULT_STRLEN + 1, MSG_DONTWAIT, NULL, 0);
+	err_rc = sprintrc(rc);
 	printf("sendto(%d, ", fd);
 	print_quoted_memory(buf, DEFAULT_STRLEN);
 	printf("..., %u, MSG_DONTWAIT, NULL, 0) = %s\n",
-	       DEFAULT_STRLEN + 1, sprintrc(rc));
+	       DEFAULT_STRLEN + 1, err_rc);
 
 	puts("+++ exited with 0 +++");
 	return 0;
-- 
2.14.1



More information about the Strace-devel mailing list