summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Syromyatnikov <evgsyr@gmail.com>2021-09-30 16:01:42 +0200
committerEugene Syromyatnikov <evgsyr@gmail.com>2021-10-09 12:30:24 +0200
commit7564c3c2a1849aac8bab8a4417e5b8986b7b55df (patch)
treeac7fd32365348adcefd0ced21274e4032a069275
parent0c73fcccd6266db94c7ac1e53c29ce18a8d34506 (diff)
downloadstrace-7564c3c2a1849aac8bab8a4417e5b8986b7b55df.tar.gz
tests/nlattr_rtmsg.c: add checks for address-containing RTA_* attributes
* tests/nlattr_rtmsg.c (print_quoted_hex_ellipsis, init_rtmsg_inet6, print_rtmsg_inet6): New functions. (main): Add checks for RTA_DST, RTA_SRC, RTA_GATEWAY, RTA_PREFSRC, RTA_NEWDST attribute decoding.
-rw-r--r--tests/nlattr_rtmsg.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/tests/nlattr_rtmsg.c b/tests/nlattr_rtmsg.c
index cd477dd1d..fc5200b27 100644
--- a/tests/nlattr_rtmsg.c
+++ b/tests/nlattr_rtmsg.c
@@ -27,6 +27,20 @@ mempcpy(void *dest, const void *src, size_t n)
}
#endif
+static void
+print_quoted_hex_ellipsis(const void *const instr, const size_t len)
+{
+ const unsigned char *str = instr;
+ size_t i;
+
+ printf("\"");
+ for (i = 0; i < MIN(len, DEFAULT_STRLEN); i++)
+ printf("\\x%02x", str[i]);
+ printf("\"");
+ if (len > DEFAULT_STRLEN)
+ printf("...");
+}
+
#define LWTUNNEL_ENCAP_NONE 0
#define DEF_NLATTR_RTMSG_FUNCS(sfx_, af_) \
@@ -70,6 +84,7 @@ mempcpy(void *dest, const void *src, size_t n)
DEF_NLATTR_RTMSG_FUNCS(rtmsg, AF_UNIX)
DEF_NLATTR_RTMSG_FUNCS(rtmsg_inet, AF_INET)
+DEF_NLATTR_RTMSG_FUNCS(rtmsg_inet6, AF_INET6)
int
main(void)
@@ -98,6 +113,62 @@ main(void)
RTA_DST, 4, pattern, 4,
print_quoted_hex(pattern, 4));
+#define MAX_ADDR_SZ 35
+ static const struct {
+ uint8_t af;
+ uint8_t addr[MAX_ADDR_SZ];
+ const char *str;
+ void (* init_fn)(struct nlmsghdr *, unsigned int);
+ void (* print_fn)(unsigned int);
+ uint32_t len;
+ } addrs[] = {
+ { AF_UNIX, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
+ "\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09\"",
+ init_rtmsg, print_rtmsg, 10 },
+ { AF_UNIX,
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, [MAX_ADDR_SZ - 1] = 0xea },
+ "\"\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09"
+ "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
+ "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
+ "\\x00\\x00"
+#if DEFAULT_STRLEN == 32
+ "\"...",
+#else
+ "\\x00\\x00\\xea\"",
+#endif
+ init_rtmsg, print_rtmsg, MAX_ADDR_SZ },
+ { AF_INET, { 0xde, 0xca, 0xff, 0xed },
+ "inet_addr(\"222.202.255.237\")",
+ init_rtmsg_inet, print_rtmsg_inet, 4 },
+ { AF_INET6, { 0xfa, 0xce, 0xbe, 0xef, [15] = 0xda },
+ "inet_pton(AF_INET6, \"face:beef::da\")",
+ init_rtmsg_inet6, print_rtmsg_inet6, 16 },
+ };
+ static const struct strval32 addr_attrs[] = {
+ { ARG_STR(RTA_DST) },
+ { ARG_STR(RTA_SRC) },
+ { ARG_STR(RTA_GATEWAY) },
+ { ARG_STR(RTA_PREFSRC) },
+ { ARG_STR(RTA_NEWDST) },
+ };
+ for (size_t i = 0; i < ARRAY_SIZE(addrs); i++) {
+ for (size_t j = 0; j < ARRAY_SIZE(addr_attrs); j++) {
+ TEST_NLATTR_(fd, nlh0, hdrlen,
+ addrs[i].init_fn, addrs[i].print_fn,
+ addr_attrs[j].val, addr_attrs[j].str,
+ addrs[i].len - 1, addrs[i].addr,
+ addrs[i].len - 1,
+ print_quoted_hex_ellipsis(addrs[i].addr,
+ addrs[i].len - 1)
+ );
+ TEST_NLATTR_(fd, nlh0, hdrlen,
+ addrs[i].init_fn, addrs[i].print_fn,
+ addr_attrs[j].val, addr_attrs[j].str,
+ addrs[i].len, addrs[i].addr, addrs[i].len,
+ printf("%s", addrs[i].str));
+ }
+ }
+
const uint32_t ifindex = ifindex_lo();
TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
init_rtmsg, print_rtmsg,