summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2020-06-19 20:52:23 +0200
committerStefan Metzmacher <metze@samba.org>2020-06-19 22:59:00 +0200
commita37c0175492fb1b35257b785c71dea4e4f6d4750 (patch)
tree1cde90da57df98066efd7b6ad16778df8cd52861 /tests
parent300de6e099ea82ee5361918de8c3abb389e0782d (diff)
downloadsocket_wrapper-a37c0175492fb1b35257b785c71dea4e4f6d4750.tar.gz
socket_wrapper-a37c0175492fb1b35257b785c71dea4e4f6d4750.tar.xz
socket_wrapper-a37c0175492fb1b35257b785c71dea4e4f6d4750.zip
test_echo_tcp_socket_options.c: add tests for TCP_INFO
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/echo_srv.c32
-rw-r--r--tests/test_echo_tcp_socket_options.c50
2 files changed, 82 insertions, 0 deletions
diff --git a/tests/echo_srv.c b/tests/echo_srv.c
index 93778f2..87c85f7 100644
--- a/tests/echo_srv.c
+++ b/tests/echo_srv.c
@@ -9,6 +9,10 @@
#include <arpa/inet.h>
#include <netinet/in.h>
+#include <netinet/tcp.h>
+#ifdef HAVE_NETINET_TCP_FSM_H
+#include <netinet/tcp_fsm.h>
+#endif
#include <netdb.h>
#include <resolv.h>
@@ -315,6 +319,34 @@ static int setup_srv(struct echo_srv_opts *opts, int *_sock)
perror("listen");
return ret;
}
+#ifdef TCP_INFO
+ {
+ struct tcp_info info;
+ socklen_t optlen = sizeof(info);
+
+ ZERO_STRUCT(info);
+ ret = getsockopt(sock, IPPROTO_TCP, TCP_INFO, &info, &optlen);
+ if (ret == -1) {
+ ret = errno;
+ perror("TCP_INFO failed");
+ close(sock);
+ return ret;
+ }
+#ifdef HAVE_NETINET_TCP_FSM_H
+/* This is FreeBSD */
+# define __TCP_LISTEN TCPS_LISTEN
+#else
+/* This is Linux */
+# define __TCP_LISTEN TCP_LISTEN
+#endif
+ if (info.tcpi_state != __TCP_LISTEN) {
+ errno = ret = ERANGE;
+ perror("not __TCP_LISTEN => ERANGE...");
+ close(sock);
+ return ret;
+ }
+ }
+#endif /* TCP_INFO */
}
*_sock = sock;
diff --git a/tests/test_echo_tcp_socket_options.c b/tests/test_echo_tcp_socket_options.c
index dfa46fe..5e5661d 100644
--- a/tests/test_echo_tcp_socket_options.c
+++ b/tests/test_echo_tcp_socket_options.c
@@ -11,12 +11,25 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
+#ifdef HAVE_NETINET_TCP_FSM_H
+#include <netinet/tcp_fsm.h>
+#endif
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
+#ifdef HAVE_NETINET_TCP_FSM_H
+/* This is FreeBSD */
+# define __TCP_ESTABLISHED TCPS_ESTABLISHED
+# define __TCP_CLOSE TCPS_CLOSED
+#else
+/* This is Linux */
+# define __TCP_ESTABLISHED TCP_ESTABLISHED
+# define __TCP_CLOSE TCP_CLOSE
+#endif
+
#ifndef ZERO_STRUCT
#define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x))
#endif
@@ -300,6 +313,9 @@ static void test_sockopt_tcp(void **state)
.sa_socklen = sizeof(struct sockaddr_in),
};
int opt = -1;
+#ifdef TCP_INFO
+ struct tcp_info info;
+#endif
socklen_t optlen = sizeof(int);
int rc;
@@ -318,9 +334,27 @@ static void test_sockopt_tcp(void **state)
&addr.sa.in.sin_addr);
assert_int_equal(rc, 1);
+#ifdef TCP_INFO
+ ZERO_STRUCT(info);
+ optlen = sizeof(info);
+ rc = getsockopt(s, IPPROTO_TCP, TCP_INFO, &info, &optlen);
+ assert_return_code(rc, errno);
+ assert_int_equal(optlen, sizeof(info));
+ printf("info.tcpi_state=0x%x\n", info.tcpi_state);
+ printf("info.tcpi_rto=%u\n", info.tcpi_rto);
+ printf("info.tcpi_rtt=%u\n", info.tcpi_rtt);
+ printf("info.tcpi_rttvar=%u\n", info.tcpi_rttvar);
+ assert_int_equal(info.tcpi_state, __TCP_CLOSE);
+ assert_int_not_equal(info.tcpi_rto, 0);
+ assert_int_equal(info.tcpi_rtt, 0);
+ assert_int_not_equal(info.tcpi_rttvar, 0);
+#endif /* TCP_INFO */
+
rc = connect(s, &addr.sa.s, addr.sa_socklen);
assert_int_equal(rc, 0);
+ opt = -1;
+ optlen = sizeof(int);
rc = getsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen);
assert_return_code(rc, errno);
assert_int_equal(opt, 0);
@@ -336,6 +370,22 @@ static void test_sockopt_tcp(void **state)
assert_return_code(rc, errno);
assert_int_equal(opt, 1);
+#ifdef TCP_INFO
+ ZERO_STRUCT(info);
+ optlen = sizeof(info);
+ rc = getsockopt(s, IPPROTO_TCP, TCP_INFO, &info, &optlen);
+ assert_return_code(rc, errno);
+ assert_int_equal(optlen, sizeof(info));
+ printf("info.tcpi_state=0x%x\n", info.tcpi_state);
+ printf("info.tcpi_rto=%u\n", info.tcpi_rto);
+ printf("info.tcpi_rtt=%u\n", info.tcpi_rtt);
+ printf("info.tcpi_rttvar=%u\n", info.tcpi_rttvar);
+ assert_int_equal(info.tcpi_state, __TCP_ESTABLISHED);
+ assert_int_not_equal(info.tcpi_rto, 0);
+ assert_int_not_equal(info.tcpi_rtt, 0);
+ assert_int_not_equal(info.tcpi_rttvar, 0);
+#endif /* TCP_INFO */
+
close(s);
}