[PATCH 2/2] tests: check decoding of rtmsg netlink attributes
JingPiao Chen
chenjingpiao at gmail.com
Wed Aug 23 00:45:19 UTC 2017
* tests/nlattr_rtmsg.c (LWTUNNEL_ENCAP_NONE,
RTA_ENCAP_TYPE): New macros.
(main): Check decoding of RTA_DST, RTA_OIF, RTA_METRICS,
RTA_MULTIPATH, RTA_CACHEINFO, RTA_MFC_STATS,
RTA_VIA and RTA_ENCAP_TYPE.
---
tests/nlattr_rtmsg.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 112 insertions(+)
diff --git a/tests/nlattr_rtmsg.c b/tests/nlattr_rtmsg.c
index fd0ccde..a60db62 100644
--- a/tests/nlattr_rtmsg.c
+++ b/tests/nlattr_rtmsg.c
@@ -33,6 +33,9 @@
#include <linux/ip.h>
#include <linux/rtnetlink.h>
+#define RTA_ENCAP_TYPE 21
+#define LWTUNNEL_ENCAP_NONE 0
+
static void
init_rtmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
{
@@ -90,6 +93,115 @@ main(void)
4, pattern, 4,
print_quoted_hex(pattern, 4));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_DST, 4, pattern, 4,
+ print_quoted_hex(pattern, 4));
+
+ const uint32_t ifindex = ifindex_lo();
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_OIF, pattern, ifindex,
+ printf(IFINDEX_LO_STR));
+
+ static struct nlattr nla = {
+ .nla_type = RTAX_LOCK,
+ .nla_len = sizeof(nla)
+ };
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_METRICS, pattern, nla,
+ printf("{nla_len=%u, nla_type=RTAX_LOCK}",
+ nla.nla_len));
+ struct rtnexthop nh = {
+ .rtnh_len = sizeof(nh) - 1,
+ .rtnh_flags = RTNH_F_DEAD,
+ .rtnh_hops = 0xab,
+ .rtnh_ifindex = ifindex_lo()
+ };
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_MULTIPATH, pattern, nh,
+ printf("{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
+ ", rtnh_hops=%u"
+ ", rtnh_ifindex=" IFINDEX_LO_STR "}",
+ nh.rtnh_len, nh.rtnh_hops));
+
+ char buf[RTNH_ALIGN(sizeof(nh)) + sizeof(nla)];
+ nh.rtnh_len = sizeof(buf);
+ nla.nla_type = RTA_DST;
+ memcpy(buf, &nh, sizeof(nh));
+ memcpy(buf + RTNH_ALIGN(sizeof(nh)), &nla, sizeof(nla));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_MULTIPATH, sizeof(buf), buf, sizeof(buf),
+ printf("{rtnh_len=%u, rtnh_flags=RTNH_F_DEAD"
+ ", rtnh_hops=%u, rtnh_ifindex=" IFINDEX_LO_STR "}"
+ ", {nla_len=%u, nla_type=RTA_DST}",
+ nh.rtnh_len, nh.rtnh_hops, nla.nla_len));
+
+ static const struct rta_cacheinfo ci = {
+ .rta_clntref = 0xabcdefab,
+ .rta_lastuse = 0xbdadaedc,
+ .rta_expires = 0xcdadebad,
+ .rta_error = 0xdaedadeb,
+ .rta_used = 0xedfabdad,
+ .rta_id = 0xfeadbcda,
+ .rta_ts = 0xacdbaded,
+ .rta_tsage = 0xbadeadef
+ };
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_CACHEINFO, pattern, ci,
+ PRINT_FIELD_U("{", ci, rta_clntref);
+ PRINT_FIELD_U(", ", ci, rta_lastuse);
+ PRINT_FIELD_U(", ", ci, rta_expires);
+ PRINT_FIELD_U(", ", ci, rta_error);
+ PRINT_FIELD_U(", ", ci, rta_used);
+ PRINT_FIELD_X(", ", ci, rta_id);
+ PRINT_FIELD_U(", ", ci, rta_ts);
+ PRINT_FIELD_U(", ", ci, rta_tsage);
+ printf("}"));
+
+#ifdef HAVE_STRUCT_RTA_MFC_STATS
+ static const struct rta_mfc_stats mfcs = {
+ .mfcs_packets = 0xadcdedfdadefadcd,
+ .mfcs_bytes = 0xbaedadedcdedadbd,
+ .mfcs_wrong_if = 0xcddeabeedaedabfa
+ };
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_MFC_STATS, pattern, mfcs,
+ PRINT_FIELD_U("{", mfcs, mfcs_packets);
+ PRINT_FIELD_U(", ", mfcs, mfcs_bytes);
+ PRINT_FIELD_U(", ", mfcs, mfcs_wrong_if);
+ printf("}"));
+#endif
+
+#ifdef HAVE_STRUCT_RTVIA
+ static const struct rtvia via = {
+ .rtvia_family = AF_INET
+ };
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_VIA, pattern, via,
+ printf("{rtvia_family=AF_INET}"));
+ char rtviabuf[sizeof(struct rtvia) + 4];
+ memcpy(rtviabuf, &via, sizeof(via));
+ memcpy(rtviabuf + sizeof(via), "1234", 4);
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_VIA, sizeof(rtviabuf), rtviabuf, sizeof(rtviabuf),
+ printf("{rtvia_family=AF_INET"
+ ", rtvia_addr=\"\\x31\\x32\\x33\\x34\"}"));
+#endif
+
+ const uint16_t encap_type = LWTUNNEL_ENCAP_NONE;
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_rtmsg, print_rtmsg,
+ RTA_ENCAP_TYPE, pattern, encap_type,
+ printf("LWTUNNEL_ENCAP_NONE"));
+
puts("+++ exited with 0 +++");
return 0;
}
--
2.7.4
More information about the Strace-devel
mailing list