[PATCH v4 11/11] tests: check decoding of nlattr_inet_diag_req_v2 attributes
JingPiao Chen
chenjingpiao at gmail.com
Thu Jul 6 01:57:37 UTC 2017
* tests/gen_tests.in (nlattr_inet_diag_req_v2): New entry.
* tests/nlattr_inet_diag_req_v2.c: New file.
* tests/pure_executables.list: Add nlattr_inet_diag_req_v2.
* tests/.gitignore: Likewise.
---
tests/.gitignore | 1 +
tests/gen_tests.in | 1 +
tests/nlattr_inet_diag_req_v2.c | 436 ++++++++++++++++++++++++++++++++++++++++
tests/pure_executables.list | 1 +
4 files changed, 439 insertions(+)
create mode 100644 tests/nlattr_inet_diag_req_v2.c
diff --git a/tests/.gitignore b/tests/.gitignore
index 8dd0241..348ad14 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -212,6 +212,7 @@ newfstatat
nlattr
nlattr_inet_diag_msg
nlattr_inet_diag_req_compat
+nlattr_inet_diag_req_v2
nlattr_netlink_diag_msg
nlattr_packet_diag_msg
nlattr_smc_diag_msg
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index 2dec567..8394660 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -195,6 +195,7 @@ newfstatat -a32 -v -P stat.sample -P /dev/full
nlattr +netlink_sock_diag.test
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
nlattr_netlink_diag_msg +netlink_sock_diag.test
nlattr_packet_diag_msg +netlink_sock_diag.test
nlattr_smc_diag_msg +netlink_sock_diag.test
diff --git a/tests/nlattr_inet_diag_req_v2.c b/tests/nlattr_inet_diag_req_v2.c
new file mode 100644
index 0000000..d5005c2
--- /dev/null
+++ b/tests/nlattr_inet_diag_req_v2.c
@@ -0,0 +1,436 @@
+/*
+ * Copyright (c) 2017 JingPiao Chen <chenjingpiao at gmail.com>
+ * Copyright (c) 2017 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "tests.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/tcp.h>
+#include "test_nlattr.h"
+#include <linux/inet_diag.h>
+#include <linux/rtnetlink.h>
+#include <linux/sock_diag.h>
+
+static const char address[] = "10.11.12.13";
+static const unsigned int hdrlen = sizeof(struct inet_diag_req_v2);
+static void *nlh0;
+static char pattern[4096];
+
+static void
+init_inet_diag_req_v2(struct nlmsghdr *nlh, unsigned int msg_len)
+{
+ struct inet_diag_req_v2 *req;
+
+ SET_STRUCT(struct nlmsghdr, nlh,
+ .nlmsg_len = msg_len,
+ .nlmsg_type = SOCK_DIAG_BY_FAMILY,
+ .nlmsg_flags = NLM_F_REQUEST
+ );
+
+ req = NLMSG_DATA(nlh);
+ SET_STRUCT(struct inet_diag_req_v2, req,
+ .sdiag_family = AF_INET,
+ .idiag_ext = 1 << (INET_DIAG_CONG - 1),
+ .sdiag_protocol = IPPROTO_TCP,
+ .idiag_states = 1 << TCP_CLOSE
+ );
+
+ if (!inet_pton(AF_INET, address, req->id.idiag_src) ||
+ !inet_pton(AF_INET, address, req->id.idiag_dst))
+ perror_msg_and_skip("inet_pton");
+}
+
+static void
+print_inet_diag_req_v2(const unsigned int msg_len)
+{
+ printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
+ ", flags=NLM_F_REQUEST, seq=0, pid=0}"
+ ", {sdiag_family=AF_INET, sdiag_protocol=IPPROTO_TCP"
+ ", idiag_ext=1<<(INET_DIAG_CONG-1)"
+ ", idiag_states=1<<TCP_CLOSE"
+ ", id={idiag_sport=htons(0), idiag_dport=htons(0)"
+ ", inet_pton(AF_INET, \"%s\", &idiag_src)"
+ ", inet_pton(AF_INET, \"%s\", &idiag_dst)"
+ ", idiag_if=0, idiag_cookie=[0, 0]}}",
+ msg_len, address, address);
+}
+
+static void
+test_inet_diag_req_none(const int fd)
+{
+ char nla_type_str[256];
+ sprintf(nla_type_str, "%#x /* INET_DIAG_REQ_??? */",
+ INET_DIAG_REQ_BYTECODE + 1);
+ TEST_NLATTR_(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE + 1, nla_type_str,
+ 4, pattern, 4,
+ print_quoted_hex(pattern, 4));
+}
+
+static void
+test_inet_diag_bc_op(const int fd)
+{
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_S_COND,
+ .yes = 0xaf,
+ .no = 0xafcd
+ };
+ TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE, pattern, op,
+ printf("{code=INET_DIAG_BC_S_COND");
+ PRINT_FIELD_U(", ", op, yes);
+ PRINT_FIELD_U(", ", op, no);
+ printf("}"));
+}
+
+static void
+print_inet_diag_bc_op(const char *code)
+{
+ printf("{{code=%s, yes=0, no=0}, ", code);
+}
+
+static void
+test_inet_diag_bc_s_cond(const int fd)
+{
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ static const struct inet_diag_hostcond cond = {
+ .family = AF_UNSPEC,
+ .prefix_len = 0xad,
+ .port = 0xadfa
+ };
+ char buf[sizeof(op) + sizeof(cond)];
+ memcpy(buf, &op, sizeof(op));
+
+ const int plen = sizeof(cond) - 1 > DEFAULT_STRLEN ?
+ sizeof(op) + DEFAULT_STRLEN : sizeof(buf) - 1;
+ memcpy(buf + sizeof(op), &pattern, sizeof(cond));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ plen, buf, plen,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_quoted_hex(buf + sizeof(op), plen - sizeof(op));
+ printf("}"));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ printf("%p}", RTA_DATA(NLATTR) + sizeof(op)));
+
+ memcpy(buf + sizeof(op), &cond, sizeof(cond));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ printf("{family=AF_UNSPEC");
+ PRINT_FIELD_U(", ", cond, prefix_len);
+ PRINT_FIELD_U(", ", cond, port);
+ printf("}}"));
+}
+
+static void
+print_inet_diag_hostcond(const char *family)
+{
+ printf("{family=%s, prefix_len=0, port=0, ", family);
+}
+
+static void
+test_in_addr(const int fd)
+{
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ static const struct inet_diag_hostcond cond = {
+ .family = AF_INET,
+ };
+ struct in_addr in_addr;
+ if (!inet_pton(AF_INET, address, &in_addr.s_addr))
+ perror_msg_and_skip("inet_pton");
+
+ char buf[sizeof(op) + sizeof(cond) + sizeof(in_addr)];
+ memcpy(buf, &op, sizeof(op));
+ memcpy(buf + sizeof(op), &cond, sizeof(cond));
+
+ const int plen = sizeof(in_addr) - 1 > DEFAULT_STRLEN ?
+ sizeof(cond) + sizeof(cond) + DEFAULT_STRLEN : sizeof(buf) - 1;
+ memcpy(buf + sizeof(op) + sizeof(cond), &pattern, sizeof(in_addr));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ plen, buf, plen,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_inet_diag_hostcond("AF_INET");
+ printf("\"%.*s\"}}",
+ (int)(plen - sizeof(op) - sizeof(cond)), pattern));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_inet_diag_hostcond("AF_INET");
+ printf("%p}}",
+ RTA_DATA(NLATTR) + sizeof(op) + sizeof(cond)));
+
+ memcpy(buf + sizeof(op) + sizeof(cond), &in_addr, sizeof(in_addr));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_inet_diag_hostcond("AF_INET");
+ printf("inet_pton(AF_INET, \"%s\", &addr.s_addr)}}",
+ address));
+}
+
+static void
+test_in6_addr(const int fd)
+{
+ const char address6[] = "12:34:56:78:90:ab:cd:ef";
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_S_COND,
+ };
+ static const struct inet_diag_hostcond cond = {
+ .family = AF_INET6,
+ };
+ struct in6_addr in6_addr;
+ if (!inet_pton(AF_INET6, address6, &in6_addr.s6_addr))
+ perror_msg_and_skip("inet_pton");
+
+ char buf[sizeof(op) + sizeof(cond) + sizeof(in6_addr)];
+ memcpy(buf, &op, sizeof(op));
+ memcpy(buf + sizeof(op), &cond, sizeof(cond));
+
+ const int plen = sizeof(in6_addr) - 1 > DEFAULT_STRLEN ?
+ sizeof(cond) + sizeof(cond) + DEFAULT_STRLEN : sizeof(buf) - 1;
+ memcpy(buf + sizeof(op) + sizeof(cond), &pattern, sizeof(in6_addr));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ plen, buf, plen,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_inet_diag_hostcond("AF_INET6");
+ printf("\"%.*s\"}}",
+ (int)(plen - sizeof(op) - sizeof(cond)), pattern));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_inet_diag_hostcond("AF_INET6");
+ printf("%p}}",
+ RTA_DATA(NLATTR) + sizeof(op) + sizeof(cond)));
+
+ memcpy(buf + sizeof(op) + sizeof(cond), &in6_addr, sizeof(in6_addr));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_S_COND");
+ print_inet_diag_hostcond("AF_INET6");
+ printf("inet_pton(AF_INET6, \"%s\", &addr.s6_addr)}}",
+ address6));
+}
+
+static void
+test_inet_diag_bc_dev_cond(const int fd)
+{
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_DEV_COND,
+ };
+ static const uint32_t ifindex = 0xabafefaf;
+ char buf[sizeof(op) + sizeof(ifindex)];
+ memcpy(buf, &op, sizeof(op));
+ memcpy(buf + sizeof(op), pattern, sizeof(ifindex));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf) - 1, buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_DEV_COND");
+ print_quoted_hex(pattern, (int) sizeof(ifindex) - 1);
+ printf("}"));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_DEV_COND");
+ printf("%p}", RTA_DATA(NLATTR) + sizeof(op)));
+
+ memcpy(buf + sizeof(op), &ifindex, sizeof(ifindex));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_DEV_COND");
+ printf("%u}", ifindex));
+}
+
+static void
+test_inet_diag_bc_s_le(const int fd)
+{
+ static const struct inet_diag_bc_op op[] = {
+ {
+ .code = INET_DIAG_BC_S_LE,
+ },
+ {
+ .code = INET_DIAG_BC_DEV_COND,
+ .yes = 0xaf,
+ .no = 0xafcd
+ }
+ };
+
+ char buf[sizeof(op)];
+ memcpy(buf, op, sizeof(op[0]));
+ memcpy(buf + sizeof(op[0]), pattern, sizeof(op[1]));
+
+ const int plen = sizeof(op[1]) - 1 > DEFAULT_STRLEN ?
+ sizeof(op[0]) + DEFAULT_STRLEN : sizeof(buf) - 1;
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ plen, buf, plen,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_LE");
+ print_quoted_hex(buf + sizeof(op[0]), plen - sizeof(op[0]));
+ printf("}"));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_S_LE");
+ printf("%p}", RTA_DATA(NLATTR) + sizeof(op[0])));
+
+ memcpy(buf + sizeof(op[0]), &op[1], sizeof(op[1]));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_S_LE");
+ printf("{code=INET_DIAG_BC_DEV_COND");
+ PRINT_FIELD_U(", ", op[1], yes);
+ PRINT_FIELD_U(", ", op[1], no);
+ printf("}}"));
+};
+
+static void
+test_inet_diag_bc_mark_cond(const int fd)
+{
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_MARK_COND,
+ };
+ static const struct inet_diag_markcond markcond = {
+ .mark = 0xafbcafcd,
+ .mask = 0xbafaacda
+ };
+ char buf[sizeof(op) + sizeof(markcond)];
+ memcpy(buf, &op, sizeof(op));
+ memcpy(buf + sizeof(op), pattern, sizeof(markcond));
+
+ const int plen = sizeof(markcond) - 1 > DEFAULT_STRLEN ?
+ sizeof(markcond) + DEFAULT_STRLEN : sizeof(buf) - 1;
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ plen, buf, plen,
+ print_inet_diag_bc_op("INET_DIAG_BC_MARK_COND");
+ print_quoted_hex(buf + sizeof(op), plen - sizeof(op));
+ printf("}"));
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf) - 1,
+ print_inet_diag_bc_op("INET_DIAG_BC_MARK_COND");
+ printf("%p}", RTA_DATA(NLATTR) + sizeof(op)));
+
+ memcpy(buf + sizeof(op), &markcond, sizeof(markcond));
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_MARK_COND");
+ PRINT_FIELD_U("{", markcond, mark);
+ PRINT_FIELD_U(", ", markcond, mask);
+ printf("}}"));
+}
+
+static void
+test_inet_diag_bc_nop(const int fd)
+{
+ static const struct inet_diag_bc_op op = {
+ .code = INET_DIAG_BC_AUTO,
+ };
+ char buf[sizeof(op) + 4];
+ memcpy(buf, &op, sizeof(op));
+ memcpy(buf + sizeof(op), pattern, 4);
+
+ TEST_NLATTR(fd, nlh0, hdrlen,
+ init_inet_diag_req_v2, print_inet_diag_req_v2,
+ INET_DIAG_REQ_BYTECODE,
+ sizeof(buf), buf, sizeof(buf),
+ print_inet_diag_bc_op("INET_DIAG_BC_AUTO");
+ print_quoted_hex(buf + sizeof(op), 4);
+ printf("}"));
+}
+
+int
+main(void)
+{
+ skip_if_unavailable("/proc/self/fd/");
+
+ int fd = create_nl_socket(NETLINK_SOCK_DIAG);
+ nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
+ fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
+
+ test_inet_diag_req_none(fd);
+
+ test_inet_diag_bc_op(fd);
+ test_inet_diag_bc_s_cond(fd);
+ test_in_addr(fd);
+ test_in6_addr(fd);
+ test_inet_diag_bc_dev_cond(fd);
+ test_inet_diag_bc_s_le(fd);
+ test_inet_diag_bc_mark_cond(fd);
+ test_inet_diag_bc_nop(fd);
+
+ printf("+++ exited with 0 +++\n");
+
+ return 0;
+}
diff --git a/tests/pure_executables.list b/tests/pure_executables.list
index 759d2ac..8abee05 100755
--- a/tests/pure_executables.list
+++ b/tests/pure_executables.list
@@ -177,6 +177,7 @@ newfstatat
nlattr
nlattr_inet_diag_msg
nlattr_inet_diag_req_compat
+nlattr_inet_diag_req_v2
nlattr_netlink_diag_msg
nlattr_packet_diag_msg
nlattr_smc_diag_msg
--
2.7.4
More information about the Strace-devel
mailing list