From d2f152e969f34eb7437a87b0975dbdc321064ea8 Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Thu, 8 Feb 2018 14:23:49 +0100 Subject: [PATCH] tests: check decoding of ip6:port pairs associated with socket descriptors * tests/net-yy-inet.c: Generalise test. * tests/net-yy-inet6.c: New file. * tests/pure_executables.list: Add net-yy-inet6. * tests/.gitignore: Likewise. * tests/gen_tests.in (net-yy-inet6): New entry. --- tests/.gitignore | 1 + tests/gen_tests.in | 1 + tests/net-yy-inet.c | 89 ++++++++++++++++++++++--------------- tests/net-yy-inet6.c | 13 ++++++ tests/pure_executables.list | 1 + 5 files changed, 70 insertions(+), 35 deletions(-) create mode 100644 tests/net-yy-inet6.c diff --git a/tests/.gitignore b/tests/.gitignore index 2541ec24..e2f7c47f 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -208,6 +208,7 @@ net-icmp_filter net-sockaddr net-y-unix net-yy-inet +net-yy-inet6 net-yy-netlink net-yy-unix netlink_audit diff --git a/tests/gen_tests.in b/tests/gen_tests.in index ea33892f..57ed3fdc 100644 --- a/tests/gen_tests.in +++ b/tests/gen_tests.in @@ -195,6 +195,7 @@ munlockall -a13 nanosleep -a20 net-icmp_filter -e trace=getsockopt,setsockopt net-sockaddr -a24 -e trace=connect +net-yy-inet6 +net-yy-inet.test netlink_audit +netlink_sock_diag.test netlink_crypto +netlink_sock_diag.test netlink_generic +netlink_sock_diag.test diff --git a/tests/net-yy-inet.c b/tests/net-yy-inet.c index c69bb173..b32d547d 100644 --- a/tests/net-yy-inet.c +++ b/tests/net-yy-inet.c @@ -41,44 +41,60 @@ #include "accept_compat.h" +#ifndef ADDR_FAMILY +# define ADDR_FAMILY_FIELD sin_family +# define ADDR_FAMILY AF_INET +# define AF_STR "AF_INET" +# define LOOPBACK_FIELD .sin_addr.s_addr = htonl(INADDR_LOOPBACK) +# define LOOPBACK "127.0.0.1" +# define SOCKADDR_TYPE sockaddr_in +# define TCP_STR "TCP" +# define INPORT sin_port +# define INPORT_STR "sin_port" +# define INADDR_STR "sin_addr=inet_addr(\"" LOOPBACK "\")" +# define SA_FIELDS "" +#endif + int main(void) { skip_if_unavailable("/proc/self/fd/"); - const struct sockaddr_in addr = { - .sin_family = AF_INET, - .sin_addr.s_addr = htonl(INADDR_LOOPBACK) + const struct SOCKADDR_TYPE addr = { + .ADDR_FAMILY_FIELD = ADDR_FAMILY, + LOOPBACK_FIELD }; struct sockaddr * const listen_sa = tail_memdup(&addr, sizeof(addr)); TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len); *len = sizeof(addr); - const int listen_fd = socket(AF_INET, SOCK_STREAM, 0); + const int listen_fd = socket(ADDR_FAMILY, SOCK_STREAM, 0); if (listen_fd < 0) perror_msg_and_skip("socket"); const unsigned long listen_inode = inode_of_sockfd(listen_fd); - printf("socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = %d\n", + printf("socket(" AF_STR ", SOCK_STREAM, IPPROTO_IP) = %d<" TCP_STR + ":[%lu]>\n", listen_fd, listen_inode); if (bind(listen_fd, listen_sa, *len)) perror_msg_and_skip("bind"); - printf("bind(%d, {sa_family=AF_INET, sin_port=htons(0)" - ", sin_addr=inet_addr(\"127.0.0.1\")}, %u) = 0\n", + printf("bind(%d<" TCP_STR ":[%lu]>, {sa_family=" AF_STR ", " INPORT_STR + "=htons(0), " INADDR_STR SA_FIELDS "}, %u) = 0\n", listen_fd, listen_inode, (unsigned) *len); if (listen(listen_fd, 1)) perror_msg_and_skip("listen"); - printf("listen(%d, 1) = 0\n", listen_fd, listen_inode); + printf("listen(%d<" TCP_STR ":[%lu]>, 1) = 0\n", + listen_fd, listen_inode); memset(listen_sa, 0, sizeof(addr)); *len = sizeof(addr); if (getsockname(listen_fd, listen_sa, len)) perror_msg_and_fail("getsockname"); const unsigned int listen_port = - ntohs(((struct sockaddr_in *) listen_sa)->sin_port); - printf("getsockname(%d, {sa_family=AF_INET" - ", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}" + ntohs(((struct SOCKADDR_TYPE *) listen_sa)->INPORT); + printf("getsockname(%d<" TCP_STR ":[" LOOPBACK ":%u]>, {sa_family=" + AF_STR ", " INPORT_STR "=htons(%u), " INADDR_STR SA_FIELDS "}" ", [%u]) = 0\n", listen_fd, listen_port, listen_port, (unsigned) *len); @@ -86,22 +102,23 @@ main(void) *len = sizeof(*optval); if (getsockopt(listen_fd, SOL_TCP, TCP_MAXSEG, optval, len)) perror_msg_and_fail("getsockopt"); - printf("getsockopt(%d, SOL_TCP, TCP_MAXSEG" - ", [%u], [%u]) = 0\n", + printf("getsockopt(%d<" TCP_STR ":[" LOOPBACK ":%u]>, SOL_TCP, " + "TCP_MAXSEG, [%u], [%u]) = 0\n", listen_fd, listen_port, *optval, (unsigned) *len); - const int connect_fd = socket(AF_INET, SOCK_STREAM, 0); + const int connect_fd = socket(ADDR_FAMILY, SOCK_STREAM, 0); if (connect_fd < 0) perror_msg_and_fail("socket"); const unsigned long connect_inode = inode_of_sockfd(connect_fd); - printf("socket(AF_INET, SOCK_STREAM, IPPROTO_IP) = %d\n", + printf("socket(" AF_STR ", SOCK_STREAM, IPPROTO_IP) = %d<" TCP_STR + ":[%lu]>\n", connect_fd, connect_inode); *len = sizeof(addr); if (connect(connect_fd, listen_sa, *len)) perror_msg_and_fail("connect"); - printf("connect(%d, {sa_family=AF_INET, sin_port=htons(%u)" - ", sin_addr=inet_addr(\"127.0.0.1\")}, %u) = 0\n", + printf("connect(%d<" TCP_STR ":[%lu]>, {sa_family=" AF_STR ", " + INPORT_STR "=htons(%u), " INADDR_STR SA_FIELDS "}, %u) = 0\n", connect_fd, connect_inode, listen_port, (unsigned) *len); struct sockaddr * const accept_sa = tail_alloc(sizeof(addr)); @@ -111,10 +128,10 @@ main(void) if (accept_fd < 0) perror_msg_and_fail("accept"); const unsigned int connect_port = - ntohs(((struct sockaddr_in *) accept_sa)->sin_port); - printf("accept(%d, {sa_family=AF_INET" - ", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}" - ", [%u]) = %d127.0.0.1:%u]>\n", + ntohs(((struct SOCKADDR_TYPE *) accept_sa)->INPORT); + printf("accept(%d<" TCP_STR ":[" LOOPBACK ":%u]>, {sa_family=" AF_STR + ", " INPORT_STR "=htons(%u), " INADDR_STR SA_FIELDS "}" + ", [%u]) = %d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>\n", listen_fd, listen_port, connect_port, (unsigned) *len, accept_fd, listen_port, connect_port); @@ -122,9 +139,9 @@ main(void) *len = sizeof(addr); if (getpeername(accept_fd, accept_sa, len)) perror_msg_and_fail("getpeername"); - printf("getpeername(%d127.0.0.1:%u]>" - ", {sa_family=AF_INET, sin_port=htons(%u)" - ", sin_addr=inet_addr(\"127.0.0.1\")}, [%u]) = 0\n", + printf("getpeername(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>" + ", {sa_family=" AF_STR ", " INPORT_STR "=htons(%u)" + ", " INADDR_STR SA_FIELDS "}, [%u]) = 0\n", accept_fd, listen_port, connect_port, connect_port, (unsigned) *len); @@ -132,16 +149,16 @@ main(void) *len = sizeof(addr); if (getpeername(connect_fd, listen_sa, len)) perror_msg_and_fail("getpeername"); - printf("getpeername(%d127.0.0.1:%u]>" - ", {sa_family=AF_INET, sin_port=htons(%u)" - ", sin_addr=inet_addr(\"127.0.0.1\")}, [%u]) = 0\n", + printf("getpeername(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>" + ", {sa_family=" AF_STR ", " INPORT_STR "=htons(%u)" + ", " INADDR_STR SA_FIELDS "}, [%u]) = 0\n", connect_fd, connect_port, listen_port, listen_port, (unsigned) *len); *len = sizeof(*optval); if (setsockopt(connect_fd, SOL_TCP, TCP_MAXSEG, optval, *len)) perror_msg_and_fail("setsockopt"); - printf("setsockopt(%d127.0.0.1:%u]>" + printf("setsockopt(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>" ", SOL_TCP, TCP_MAXSEG, [%u], %u) = 0\n", connect_fd, connect_port, listen_port, *optval, (unsigned) *len); @@ -149,28 +166,30 @@ main(void) char text[] = "text"; assert(sendto(connect_fd, text, sizeof(text) - 1, MSG_DONTROUTE | MSG_DONTWAIT, NULL, 0) == sizeof(text) - 1); - printf("sendto(%d127.0.0.1:%u]>, \"%s\", %u" - ", MSG_DONTROUTE|MSG_DONTWAIT, NULL, 0) = %u\n", + printf("sendto(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>, " + "\"%s\", %u, MSG_DONTROUTE|MSG_DONTWAIT, NULL, 0) = %u\n", connect_fd, connect_port, listen_port, text, (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1); assert(close(connect_fd) == 0); - printf("close(%d127.0.0.1:%u]>) = 0\n", + printf("close(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>) = " + "0\n", connect_fd, connect_port, listen_port); assert(recvfrom(accept_fd, text, sizeof(text) - 1, MSG_WAITALL, NULL, NULL) == sizeof(text) - 1); - printf("recvfrom(%d127.0.0.1:%u]>, \"%s\", %u" - ", MSG_WAITALL, NULL, NULL) = %u\n", + printf("recvfrom(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>, " + "\"%s\", %u, MSG_WAITALL, NULL, NULL) = %u\n", accept_fd, listen_port, connect_port, text, (unsigned) sizeof(text) - 1, (unsigned) sizeof(text) - 1); assert(close(accept_fd) == 0); - printf("close(%d127.0.0.1:%u]>) = 0\n", + printf("close(%d<" TCP_STR ":[" LOOPBACK ":%u->" LOOPBACK ":%u]>) = " + "0\n", accept_fd, listen_port, connect_port); assert(close(listen_fd) == 0); - printf("close(%d) = 0\n", + printf("close(%d<" TCP_STR ":[" LOOPBACK ":%u]>) = 0\n", listen_fd, listen_port); puts("+++ exited with 0 +++"); diff --git a/tests/net-yy-inet6.c b/tests/net-yy-inet6.c new file mode 100644 index 00000000..c97979f7 --- /dev/null +++ b/tests/net-yy-inet6.c @@ -0,0 +1,13 @@ +#define ADDR_FAMILY_FIELD sin6_family +#define ADDR_FAMILY AF_INET6 +#define AF_STR "AF_INET6" +#define LOOPBACK_FIELD .sin6_addr = IN6ADDR_LOOPBACK_INIT +#define LOOPBACK "[::1]" +#define SOCKADDR_TYPE sockaddr_in6 +#define TCP_STR "TCPv6" +#define INPORT sin6_port +#define INPORT_STR "sin6_port" +#define INADDR_STR "inet_pton(AF_INET6, \"::1\", &sin6_addr)" +#define SA_FIELDS ", sin6_flowinfo=htonl(0), sin6_scope_id=0" + +#include "net-yy-inet.c" diff --git a/tests/pure_executables.list b/tests/pure_executables.list index 2f602a93..bf5f694c 100755 --- a/tests/pure_executables.list +++ b/tests/pure_executables.list @@ -169,6 +169,7 @@ net-icmp_filter net-sockaddr net-y-unix net-yy-inet +net-yy-inet6 net-yy-netlink net-yy-unix netlink_audit