[PATCH v2 2/2] tests: check decoding of ifinfomsg netlink attributes

JingPiao Chen chenjingpiao at gmail.com
Sat Aug 19 01:50:11 UTC 2017


* gen_tests.in (nlattr_ifinfomsg): Replace netlink_sock_diag.test
with netlink_sock_diag-v.sh, using verbose to check decoding of array.
* tests/nlattr_ifinfomsg.c: Include <stddef.h>, <arpa/inet.h>,
<linux/if_link.h> and <linux/netdevice.h>.
(SET_IFI_TYPE, IFLA_PHYS_PORT_ID, IFLA_LINK_NETNSID): New macros.
(init_ifinfomsg): Set ifinfomsg.ifi_type field.
(print_ifinfomsg): Print ifinfomsg.ifi_type field use %s format.
(main): Check decoding of IFLA_LINK_NETNSID, IFLA_STATS,
IFLA_MAP, IFLA_STATS64, IFLA_PHYS_PORT_ID and IFLA_ADDRESS.
---
 tests/gen_tests.in       |   2 +-
 tests/nlattr_ifinfomsg.c | 254 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 252 insertions(+), 4 deletions(-)

diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index ba156c3..e7c4620 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -203,7 +203,7 @@ nlattr_dcbmsg			+netlink_sock_diag.test
 nlattr_fib_rule_hdr		+netlink_sock_diag.test
 nlattr_ifaddrlblmsg		+netlink_sock_diag.test
 nlattr_ifaddrmsg		+netlink_sock_diag.test
-nlattr_ifinfomsg		+netlink_sock_diag.test
+nlattr_ifinfomsg		+netlink_sock_diag-v.sh
 nlattr_inet_diag_msg		+netlink_sock_diag.test
 nlattr_inet_diag_req_compat	+netlink_sock_diag.test
 nlattr_inet_diag_req_v2		+netlink_sock_diag.test
diff --git a/tests/nlattr_ifinfomsg.c b/tests/nlattr_ifinfomsg.c
index 7e42ed1..3c08e8b 100644
--- a/tests/nlattr_ifinfomsg.c
+++ b/tests/nlattr_ifinfomsg.c
@@ -29,12 +29,30 @@
 #include "tests.h"
 
 #include <stdio.h>
+#include <stddef.h>
 #include "test_nlattr.h"
 
+#include <arpa/inet.h>
 #include <linux/if.h>
 #include <linux/if_arp.h>
+#ifdef HAVE_LINUX_IF_LINK_H
+# include <linux/if_link.h>
+#endif
+#include <linux/netdevice.h>
 #include <linux/rtnetlink.h>
 
+#define IFLA_PHYS_PORT_ID 34
+#define IFLA_LINK_NETNSID 37
+#define SET_IFI_TYPE(type)		\
+	do {				\
+		ifi_type = type;	\
+		ifi_type_str = #type;	\
+	}				\
+	while (0)
+
+unsigned short ifi_type = ARPHRD_LOOPBACK;
+const char *ifi_type_str = "ARPHRD_LOOPBACK";
+
 static void
 init_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
 {
@@ -47,7 +65,7 @@ init_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
 	struct ifinfomsg *const msg = NLMSG_DATA(nlh);
 	SET_STRUCT(struct ifinfomsg, msg,
 		.ifi_family = AF_UNIX,
-		.ifi_type = ARPHRD_LOOPBACK,
+		.ifi_type = ifi_type,
 		.ifi_index = ifindex_lo(),
 		.ifi_flags = IFF_UP,
 	);
@@ -58,10 +76,10 @@ print_ifinfomsg(const unsigned int msg_len)
 {
 	printf("{len=%u, type=RTM_GETLINK, flags=NLM_F_DUMP"
 	       ", seq=0, pid=0}, {ifi_family=AF_UNIX"
-	       ", ifi_type=ARPHRD_LOOPBACK"
+	       ", ifi_type=%s"
 	       ", ifi_index=" IFINDEX_LO_STR
 	       ", ifi_flags=IFF_UP, ifi_change=0}",
-	       msg_len);
+	       msg_len, ifi_type_str);
 }
 
 int
@@ -85,6 +103,236 @@ main(void)
 		     4, pattern, 4,
 		     print_quoted_hex(pattern, 4));
 
+	const int32_t netnsid = 0xacbdabda;
+	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+			   init_ifinfomsg, print_ifinfomsg,
+			   IFLA_LINK_NETNSID, pattern, netnsid,
+			   printf("%d", netnsid));
+
+	static const struct rtnl_link_stats st = {
+		.rx_packets = 0xabcdefac,
+		.tx_packets = 0xbcdacdab,
+		.rx_bytes = 0xcdbafaab,
+		.tx_bytes = 0xdafabadb,
+		.rx_errors = 0xeabcdaeb,
+		.tx_errors = 0xfefabeab,
+		.rx_dropped = 0xadbafafb,
+		.tx_dropped = 0xbdffabda,
+		.multicast = 0xcdabdfea,
+		.collisions = 0xefadbaeb,
+		.rx_length_errors = 0xfabffabd,
+		.rx_over_errors = 0xafbafabc,
+		.rx_crc_errors = 0xbfdabdad,
+		.rx_frame_errors = 0xcfdabfad,
+		.rx_fifo_errors = 0xddfdebad,
+		.rx_missed_errors = 0xefabdcba,
+		.tx_aborted_errors = 0xefdadbfa,
+		.tx_carrier_errors = 0xfaefbada,
+		.tx_fifo_errors = 0xaebdffab,
+		.tx_heartbeat_errors = 0xbadebaaf,
+		.tx_window_errors = 0xcdafbada,
+		.rx_compressed = 0xdeffadbd,
+		.tx_compressed = 0xefdadfab
+	};
+	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+			   init_ifinfomsg, print_ifinfomsg,
+			   IFLA_STATS, pattern, st,
+			   PRINT_FIELD_U("{", st, rx_packets);
+			   PRINT_FIELD_U(", ", st, tx_packets);
+			   PRINT_FIELD_U(", ", st, rx_bytes);
+			   PRINT_FIELD_U(", ", st, tx_bytes);
+			   PRINT_FIELD_U(", ", st, rx_errors);
+			   PRINT_FIELD_U(", ", st, tx_errors);
+			   PRINT_FIELD_U(", ", st, rx_dropped);
+			   PRINT_FIELD_U(", ", st, tx_dropped);
+			   PRINT_FIELD_U(", ", st, multicast);
+			   PRINT_FIELD_U(", ", st, collisions);
+			   PRINT_FIELD_U(", ", st, rx_length_errors);
+			   PRINT_FIELD_U(", ", st, rx_over_errors);
+			   PRINT_FIELD_U(", ", st, rx_crc_errors);
+			   PRINT_FIELD_U(", ", st, rx_frame_errors);
+			   PRINT_FIELD_U(", ", st, rx_fifo_errors);
+			   PRINT_FIELD_U(", ", st, rx_missed_errors);
+			   PRINT_FIELD_U(", ", st, tx_aborted_errors);
+			   PRINT_FIELD_U(", ", st, tx_carrier_errors);
+			   PRINT_FIELD_U(", ", st, tx_fifo_errors);
+			   PRINT_FIELD_U(", ", st, tx_heartbeat_errors);
+			   PRINT_FIELD_U(", ", st, tx_window_errors);
+			   PRINT_FIELD_U(", ", st, rx_compressed);
+			   PRINT_FIELD_U(", ", st, tx_compressed);
+#ifdef HAVE_STRUCT_RTNL_LINK_STATS_RX_NOHANDLER
+			   PRINT_FIELD_U(", ", st, rx_nohandler);
+#endif
+			   printf("}"));
+
+	static const struct rtnl_link_ifmap map = {
+		.mem_start = 0xadcbefedefbcdedb,
+		.mem_end = 0xefcbeabdecdcdefa,
+		.base_addr = 0xaddbeabdfaacdbae,
+		.irq = 0xefaf,
+		.dma = 0xab,
+		.port = 0xcd
+	};
+	const unsigned int sizeof_ifmap =
+		offsetofend(struct rtnl_link_ifmap, port);
+	const unsigned int plen = sizeof_ifmap - 1 > DEFAULT_STRLEN
+				  ? DEFAULT_STRLEN
+				  : (int) sizeof_ifmap - 1;
+	/* len < sizeof(map) */
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_MAP, plen, pattern, plen,
+		    print_quoted_hex(pattern, plen));
+
+	/* short read of sizeof(obj_) */
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_MAP, sizeof_ifmap, &map, sizeof_ifmap - 1,
+		    printf("%p", RTA_DATA(TEST_NLATTR_nla)));
+
+	/* sizeof(map) */
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_MAP, sizeof_ifmap, &map, sizeof_ifmap,
+		    PRINT_FIELD_X("{", map, mem_start);
+		    PRINT_FIELD_X(", ", map, mem_end);
+		    PRINT_FIELD_X(", ", map, base_addr);
+		    PRINT_FIELD_U(", ", map, irq);
+		    PRINT_FIELD_U(", ", map, dma);
+		    PRINT_FIELD_U(", ", map, port);
+		    printf("}"));
+
+#ifdef HAVE_STRUCT_RTNL_LINK_STATS64
+	static const struct rtnl_link_stats64 st64 = {
+		.rx_packets = 0xadcbefedefbcdedb,
+		.tx_packets = 0xbdabdedabdcdeabd,
+		.rx_bytes = 0xcdbaefbaeadfabec,
+		.tx_bytes = 0xdbaedbafabbeacdb,
+		.rx_errors = 0xefabfdaefabaefab,
+		.tx_errors = 0xfaebfabfabbaeabf,
+		.rx_dropped = 0xacdbaedbadbabeba,
+		.tx_dropped = 0xbcdeffebdabeadbe,
+		.multicast = 0xeeffbaeabaeffabe,
+		.collisions = 0xffbaefcefbafacef,
+		.rx_length_errors = 0xaabbdeabceffdecb,
+		.rx_over_errors = 0xbbdcdadebadeaeed,
+		.rx_crc_errors= 0xccdeabecefaedbef,
+		.rx_frame_errors = 0xddbedaedebcedaef,
+		.rx_fifo_errors = 0xeffbadefafdaeaab,
+		.rx_missed_errors = 0xfefaebccceadeecd,
+		.tx_aborted_errors = 0xabcdadefcdadef,
+		.tx_carrier_errors = 0xbccdafaeeaaefe,
+		.tx_fifo_errors = 0xcddefdbedeadce,
+		.tx_heartbeat_errors = 0xedaededdadcdea,
+		.tx_window_errors = 0xfdacdeaccedcda,
+		.rx_compressed = 0xacdbbcacdbccef,
+		.tx_compressed = 0xbcdadefcdedfea
+	};
+	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+			   init_ifinfomsg, print_ifinfomsg,
+			   IFLA_STATS64, pattern, st64,
+			   PRINT_FIELD_U("{", st64, rx_packets);
+			   PRINT_FIELD_U(", ", st64, tx_packets);
+			   PRINT_FIELD_U(", ", st64, rx_bytes);
+			   PRINT_FIELD_U(", ", st64, tx_bytes);
+			   PRINT_FIELD_U(", ", st64, rx_errors);
+			   PRINT_FIELD_U(", ", st64, tx_errors);
+			   PRINT_FIELD_U(", ", st64, rx_dropped);
+			   PRINT_FIELD_U(", ", st64, tx_dropped);
+			   PRINT_FIELD_U(", ", st64, multicast);
+			   PRINT_FIELD_U(", ", st64, collisions);
+			   PRINT_FIELD_U(", ", st64, rx_length_errors);
+			   PRINT_FIELD_U(", ", st64, rx_over_errors);
+			   PRINT_FIELD_U(", ", st64, rx_crc_errors);
+			   PRINT_FIELD_U(", ", st64, rx_frame_errors);
+			   PRINT_FIELD_U(", ", st64, rx_fifo_errors);
+			   PRINT_FIELD_U(", ", st64, rx_missed_errors);
+			   PRINT_FIELD_U(", ", st64, tx_aborted_errors);
+			   PRINT_FIELD_U(", ", st64, tx_carrier_errors);
+			   PRINT_FIELD_U(", ", st64, tx_fifo_errors);
+			   PRINT_FIELD_U(", ", st64, tx_heartbeat_errors);
+			   PRINT_FIELD_U(", ", st64, tx_window_errors);
+			   PRINT_FIELD_U(", ", st64, rx_compressed);
+			   PRINT_FIELD_U(", ", st64, tx_compressed);
+#ifdef HAVE_STRUCT_RTNL_LINK_STATS64_RX_NOHANDLER
+			   PRINT_FIELD_U(", ", st64, rx_nohandler);
+#endif
+			   printf("}"));
+#endif
+
+	static const uint8_t id[] = { 0xcd, 0xef };
+	TEST_NLATTR(fd, nlh0, hdrlen,
+			  init_ifinfomsg, print_ifinfomsg,
+			  IFLA_PHYS_PORT_ID, sizeof(id), id, sizeof(id),
+			  printf("[%u, %u]", id[0], id[1]));
+
+	static uint8_t bigid[33];
+	memcpy(bigid, pattern, sizeof(bigid));
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_PHYS_PORT_ID, sizeof(bigid), bigid, sizeof(bigid),
+		    size_t i;
+		    for (i = 0; i < 32; ++i) {
+			printf(i ? ", " : "[");
+			printf("%u", bigid[i]);
+		    }
+		    printf(", ...]"));
+
+	static uint8_t addr[] = { 0xeb, 0xfe };
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS, sizeof(addr), addr, sizeof(addr),
+		    printf("%02x:%02x", addr[0], addr[1]));
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS, sizeof(addr), addr, sizeof(addr) - 1,
+		    printf("%02x: ...", addr[0]));
+
+	static uint8_t long_addr[MAX_ADDR_LEN + 1];
+	memcpy(long_addr, pattern, sizeof(long_addr));
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS,
+		    sizeof(long_addr), long_addr, sizeof(long_addr),
+		    size_t i;
+		    for (i = 0; i < MAX_ADDR_LEN; ++i) {
+			printf(i ? ":" : "");
+			printf("%02x", long_addr[i]);
+		    }
+		    printf(": ..."));
+
+	const char address[] = "12.34.56.78";
+	struct in_addr a4;
+	SET_IFI_TYPE(ARPHRD_TUNNEL);
+
+	if (!inet_pton(AF_INET, address, &a4))
+		perror_msg_and_skip("inet_pton");
+
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS, sizeof(a4), &a4, sizeof(a4) - 1,
+		    printf("%p", RTA_DATA(TEST_NLATTR_nla)));
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS, sizeof(a4), &a4, sizeof(a4),
+		    printf("%s", address));
+
+	const char address6[] = "12:34:56:78:90:ab:cd:ef";
+	struct in6_addr a6;
+	SET_IFI_TYPE(ARPHRD_TUNNEL6);
+
+	if (!inet_pton(AF_INET6, address6, &a6))
+		perror_msg_and_skip("inet_pton");
+
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS, sizeof(a6), &a6, sizeof(a6) - 1,
+		    printf("%p", RTA_DATA(TEST_NLATTR_nla)));
+	TEST_NLATTR(fd, nlh0, hdrlen,
+		    init_ifinfomsg, print_ifinfomsg,
+		    IFLA_ADDRESS, sizeof(a6), &a6, sizeof(a6),
+		    printf("%s", address6));
+
 	puts("+++ exited with 0 +++");
 	return 0;
 }
-- 
2.7.4





More information about the Strace-devel mailing list