[PATCH v3 3/7] tests: check decoding of netlink protocol
Fabien Siron
fabien.siron at epita.fr
Wed Jun 15 12:43:01 UTC 2016
* tests/netlink_parsing.test: New test.
* tests/netlink_parsing.c: New file.
* tests/.gitignore: Add netlink_parsing.
* tests/Makefile.am (check_PROGRAMS): Likewise.
---
tests/.gitignore | 1 +
tests/Makefile.am | 2 +
tests/netlink_parsing.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++
tests/netlink_parsing.test | 48 +++++++++++++++++++++++
4 files changed, 147 insertions(+)
create mode 100644 tests/netlink_parsing.c
create mode 100755 tests/netlink_parsing.test
diff --git a/tests/.gitignore b/tests/.gitignore
index 67d0d66..bd18731 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -147,6 +147,7 @@ net-yy-netlink
net-yy-unix
netlink_inet_diag
netlink_netlink_diag
+netlink_parsing
netlink_unix_diag
newfstatat
nsyscalls
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7a029b6..3472023 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -200,6 +200,7 @@ check_PROGRAMS = \
net-yy-unix \
netlink_inet_diag \
netlink_netlink_diag \
+ netlink_parsing \
netlink_unix_diag \
newfstatat \
nsyscalls \
@@ -508,6 +509,7 @@ DECODER_TESTS = \
net-yy-netlink.test \
net-yy-unix.test \
net.test \
+ netlink_parsing.test \
newfstatat.test \
nsyscalls.test \
old_mmap.test \
diff --git a/tests/netlink_parsing.c b/tests/netlink_parsing.c
new file mode 100644
index 0000000..e7f996b
--- /dev/null
+++ b/tests/netlink_parsing.c
@@ -0,0 +1,96 @@
+/*
+ * Copyright (c) 2014-2016 Dmitry V. Levin <ldv at altlinux.org>
+ * Copyright (c) 2016 Fabien Siron <fabien.siron at epita.fr>
+ * 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 <errno.h>
+#include <string.h>
+#include <unistd.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <linux/netlink.h>
+#include <linux/sock_diag.h>
+#include <linux/netlink_diag.h>
+
+#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
+# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
+#endif
+
+static void
+send_query(const int fd, const unsigned int pid_magic)
+{
+ struct {
+ struct nlmsghdr nlh;
+ char magic[4];
+ } req = {
+ .nlh = {
+ .nlmsg_len = sizeof(req),
+ .nlmsg_type = NLMSG_NOOP,
+ .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
+ },
+ .magic = "abcd"
+ };
+
+ if (sendto(fd, &req, sizeof(req), MSG_DONTWAIT, NULL, 0) !=
+ (unsigned) sizeof(req))
+ perror_msg_and_skip("sendto");
+
+ printf("sendto(%d<NETLINK:[SOCK_DIAG:%u]>, {{len=%u, type=NLMSG_NOOP, "
+ "flags=NLM_F_REQUEST|0x%x, seq=0, pid=0}, \"abcd\"}, %u, "
+ "MSG_DONTWAIT, NULL, 0) = %u\n",
+ fd, pid_magic, (unsigned) sizeof(req), NLM_F_DUMP,
+ (unsigned) sizeof(req), (unsigned) sizeof(req));
+}
+
+int main(void)
+{
+ struct sockaddr_nl addr;
+ socklen_t len = sizeof(addr);
+ int fd;
+ long unsigned int inode;
+ const unsigned int pid_magic = 1234;
+
+ memset(&addr, 0, sizeof(addr));
+ addr.nl_family = AF_NETLINK;
+ addr.nl_pid = pid_magic;
+
+ if ((fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG)) == -1)
+ perror_msg_and_skip("socket AF_NETLINK");
+ inode = inode_of_sockfd(fd);
+ printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = "
+ "%d<NETLINK:[%lu]>\n", fd, inode);
+ if (bind(fd, (struct sockaddr *) &addr, len))
+ perror_msg_and_skip("bind");
+ printf("bind(%d<NETLINK:[%lu]>, {sa_family=AF_NETLINK, pid=%u, "
+ "groups=00000000}, %u) = 0\n", fd, inode, pid_magic, len);
+
+ send_query(fd, pid_magic);
+
+ printf("+++ exited with 0 +++\n");
+
+ return 0;
+}
diff --git a/tests/netlink_parsing.test b/tests/netlink_parsing.test
new file mode 100755
index 0000000..bacc428
--- /dev/null
+++ b/tests/netlink_parsing.test
@@ -0,0 +1,48 @@
+#!/bin/sh
+#
+# Check decoding of protocol:portid pairs associated with socket
+# descriptors
+#
+# Copyright (c) 2014 Masatake YAMATO <yamato at redhat.com>
+# Copyright (c) 2014-2016 Dmitry V. Levin <ldv at altlinux.org>
+# Copyright (c) 2016 Fabien Siron <fabien.siron at epita.fr>
+# 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.
+
+. "${srcdir=.}/init.sh"
+
+# strace -yy is implemented using /proc/self/fd
+[ -d /proc/self/fd/ ] ||
+ framework_skip_ '/proc/self/fd/ is not available'
+
+check_prog sed
+
+run_prog "./$NAME" > /dev/null
+
+run_strace -a22 -yy -eclose,network $args > "$EXP"
+# Filter out close() calls made by ld.so and libc.
+sed -n '/socket/,$p' < "$LOG" > "$OUT"
+
+match_diff "$OUT" "$EXP"
+rm -f "$EXP" "$OUT"
--
2.8.3
More information about the Strace-devel
mailing list