Skip to content

Commit 0efd6e0

Browse files
committed
testing/nettest: Add TCP bind() and listen() cases.
Six cases in cmocka_net_tcp (IPv4 on the loopback address), with the rules of Linux: - test_tcp_bind_reuseaddr_timewait: a server closes first, so its connection stays in TIME_WAIT. bind() of the port fails with EADDRINUSE without SO_REUSEADDR and succeeds with it. - test_tcp_bind_reuseaddr_connected: a connection stays after its listener closed. bind() with SO_REUSEADDR succeeds only if the listener had SO_REUSEADDR too. - test_tcp_bind_reuseaddr_listener: bind() of the port of a listener fails with EADDRINUSE, also with SO_REUSEADDR. - test_tcp_accept_inherit: an accepted socket has the SO_KEEPALIVE, TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT values of its listener, and its MSS is not larger than TCP_MAXSEG of the listener. - test_tcp_listen_again: listen() on a listening socket returns 0, sets the new backlog and keeps the connection in the queue. - test_tcp_listen_connected: listen() on a connected socket fails with EINVAL. The three bind() cases and test_tcp_accept_inherit need CONFIG_NET_SOCKOPTS. Its TCP_MAXSEG check needs CONFIG_NET_TCPPROTO_OPTIONS and its TCP_KEEP* checks need CONFIG_NET_TCP_KEEPALIVE. A teardown closes the sockets that a failed case leaves open. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
1 parent 72e5a3c commit 0efd6e0

5 files changed

Lines changed: 600 additions & 2 deletions

File tree

‎testing/nettest/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ if(CONFIG_TESTING_NET_TEST)
2626
${CMAKE_CURRENT_LIST_DIR}/utils/nettest_tcpserver.c)
2727

2828
if(CONFIG_NET_IPv4)
29-
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv4.c)
29+
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv4.c
30+
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_bind_listen.c)
3031
endif()
3132

3233
if(CONFIG_NET_IPv6)

‎testing/nettest/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ PROGNAME += cmocka_net_tcp
3636
CSRCS += tcp/test_tcp_common.c utils/nettest_tcpserver.c
3737

3838
ifeq ($(CONFIG_NET_IPv4), y)
39-
CSRCS += tcp/test_tcp_connect_ipv4.c
39+
CSRCS += tcp/test_tcp_connect_ipv4.c tcp/test_tcp_bind_listen.c
4040
endif
4141

4242
ifeq ($(CONFIG_NET_IPv6), y)

‎testing/nettest/tcp/test_tcp.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,26 @@ int main(int argc, FAR char *argv[])
4242
cmocka_unit_test_setup_teardown(test_tcp_connect_ipv4,
4343
test_tcp_connect_ipv4_setup,
4444
test_tcp_common_teardown),
45+
# ifdef CONFIG_NET_SOCKOPTS
46+
cmocka_unit_test_setup_teardown(test_tcp_bind_reuseaddr_timewait,
47+
test_tcp_bind_listen_setup,
48+
test_tcp_bind_listen_teardown),
49+
cmocka_unit_test_setup_teardown(test_tcp_bind_reuseaddr_connected,
50+
test_tcp_bind_listen_setup,
51+
test_tcp_bind_listen_teardown),
52+
cmocka_unit_test_setup_teardown(test_tcp_bind_reuseaddr_listener,
53+
test_tcp_bind_listen_setup,
54+
test_tcp_bind_listen_teardown),
55+
cmocka_unit_test_setup_teardown(test_tcp_accept_inherit,
56+
test_tcp_bind_listen_setup,
57+
test_tcp_bind_listen_teardown),
58+
# endif
59+
cmocka_unit_test_setup_teardown(test_tcp_listen_again,
60+
test_tcp_bind_listen_setup,
61+
test_tcp_bind_listen_teardown),
62+
cmocka_unit_test_setup_teardown(test_tcp_listen_connected,
63+
test_tcp_bind_listen_setup,
64+
test_tcp_bind_listen_teardown),
4565
#endif
4666
#ifdef CONFIG_NET_IPv6
4767
cmocka_unit_test_setup_teardown(test_tcp_connect_ipv6,

‎testing/nettest/tcp/test_tcp.h‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,56 @@ int test_tcp_connect_ipv4_setup(FAR void **state);
7171
****************************************************************************/
7272

7373
void test_tcp_connect_ipv4(FAR void **state);
74+
75+
/****************************************************************************
76+
* Name: test_tcp_bind_listen_setup
77+
****************************************************************************/
78+
79+
int test_tcp_bind_listen_setup(FAR void **state);
80+
81+
/****************************************************************************
82+
* Name: test_tcp_bind_listen_teardown
83+
****************************************************************************/
84+
85+
int test_tcp_bind_listen_teardown(FAR void **state);
86+
87+
/****************************************************************************
88+
* Name: test_tcp_bind_reuseaddr_timewait
89+
****************************************************************************/
90+
91+
# ifdef CONFIG_NET_SOCKOPTS
92+
void test_tcp_bind_reuseaddr_timewait(FAR void **state);
93+
94+
/****************************************************************************
95+
* Name: test_tcp_bind_reuseaddr_connected
96+
****************************************************************************/
97+
98+
void test_tcp_bind_reuseaddr_connected(FAR void **state);
99+
100+
/****************************************************************************
101+
* Name: test_tcp_bind_reuseaddr_listener
102+
****************************************************************************/
103+
104+
void test_tcp_bind_reuseaddr_listener(FAR void **state);
105+
106+
/****************************************************************************
107+
* Name: test_tcp_accept_inherit
108+
****************************************************************************/
109+
110+
void test_tcp_accept_inherit(FAR void **state);
111+
# endif
112+
113+
/****************************************************************************
114+
* Name: test_tcp_listen_again
115+
****************************************************************************/
116+
117+
void test_tcp_listen_again(FAR void **state);
118+
119+
/****************************************************************************
120+
* Name: test_tcp_listen_connected
121+
****************************************************************************/
122+
123+
void test_tcp_listen_connected(FAR void **state);
74124
#endif
75125

76126
/****************************************************************************

0 commit comments

Comments
 (0)