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.
This commit is contained in:
Eugene Syromyatnikov 2018-02-08 14:23:49 +01:00 committed by Dmitry V. Levin
parent 85e2e24e19
commit d2f152e969
5 changed files with 70 additions and 35 deletions

1
tests/.gitignore vendored
View File

@ -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

View File

@ -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

View File

@ -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<TCP:[%lu]>\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<TCP:[%lu]>, {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<TCP:[%lu]>, 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<TCP:[127.0.0.1:%u]>, {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<TCP:[127.0.0.1:%u]>, 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<TCP:[%lu]>\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<TCP:[%lu]>, {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<TCP:[127.0.0.1:%u]>, {sa_family=AF_INET"
", sin_port=htons(%u), sin_addr=inet_addr(\"127.0.0.1\")}"
", [%u]) = %d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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(%d<TCP:[127.0.0.1:%u->127.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<TCP:[127.0.0.1:%u]>) = 0\n",
printf("close(%d<" TCP_STR ":[" LOOPBACK ":%u]>) = 0\n",
listen_fd, listen_port);
puts("+++ exited with 0 +++");

13
tests/net-yy-inet6.c Normal file
View File

@ -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"

View File

@ -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