Skip to content

testing/nettest: Add TCP bind() and listen() cases - #4021

Merged
acassis merged 1 commit into
apache:masterfrom
casaroli:apps-tcp-bind-listen-test-up
Nov 13, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
casaroli:apps-tcp-bind-listen-test-up

Conversation

@casaroli

@casaroli casaroli commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

depends-on: /pull/4033

Summary

This PR adds 6 cases to cmocka_net_tcp (testing/nettest/tcp, new file test_tcp_bind_listen.c) for bind() with SO_REUSEADDR, listen() and the options of an accepted socket, for IPv4 TCP sockets on the loopback address. The expected results are the ones of Linux:

  • test_tcp_bind_reuseaddr_timewait: a server closes its connection first, so the 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: the listener sets SO_KEEPALIVE, TCP_MAXSEG 1000, TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT. getsockopt() on the accepted socket gives the same SO_KEEPALIVE and TCP_KEEP* values, and an MSS of at most 1000.
  • 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 or accepted 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, so that the next cases have free connections. Every port comes from bind() to port 0, so the cases do not collide with the test server on CONFIG_TESTING_NET_TCP_PORT.

Impact

Test only. cmocka_net_tcp has 6 more cases when CONFIG_NET_IPv4 is set. No new Kconfig option (TESTING_NET_TCP is default y, as before). No defconfig in nuttx sets TESTING_NET_TEST, so CI does not run this test.

Testing

qemu-armv8a, HVF, 1 CPU, with NET_IPv4, NET_IPv6, NET_LOOPBACK, NET_TCP, NET_TCPBACKLOG, NET_SOCKOPTS, cmocka and TESTING_NET_TCP; "+KA" adds NET_TCP_WRITE_BUFFERS, NET_TCP_KEEPALIVE (with NET_TCPPROTO_OPTIONS) and NET_SOLINGER. knsh: kernel build, the program from /system/bin. nsh: flat build, the program as a builtin.

Configuration nuttx master 03aea786390 nuttx with apache/nuttx#21667
qemu-armv8a:knsh +KA 8 run, 3 passed, 5 failed 8 run, 8 passed
qemu-armv8a:nsh +KA 8 run, 3 passed, 5 failed 8 run, 8 passed
qemu-armv8a:nsh 8 run, 3 passed, 5 failed 8 run, 8 passed

On master, test_tcp_bind_reuseaddr_listener passes (the behaviour does not change) and the 5 other new cases fail: bind() with SO_REUSEADDR gives EADDRINUSE (timewait, connected), SO_KEEPALIVE of the accepted socket is 0 (accept inherit), listen() again gives EADDRINUSE, listen() on a connected socket returns 0.

On Linux (Fedora 44, kernel 7.2, gcc, cmocka 1.1.5, the same sources, with and without the TCP_MAXSEG and TCP_KEEP* checks): the 6 new cases pass. The existing test_tcp_connect_ipv4 fails there because its loopback server calls select() with tv_sec = -1 (EINVAL on Linux); not changed by this PR.

CMake (Ninja) build of qemu-armv8a:nsh with NET_IPv4, NET_TCP, NET_SOCKOPTS, NET_TCP_KEEPALIVE and the test: OK, no compiler warning.

tools/checkpatch.sh -c -u -m -g origin/master..HEAD passes.

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>
@casaroli
casaroli force-pushed the apps-tcp-bind-listen-test-up branch from 57aec16 to 0efd6e0 Compare November 11, 2026 08:55
@casaroli
casaroli marked this pull request as ready for review November 11, 2026 19:21
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/43086412455

@casaroli
casaroli marked this pull request as draft November 12, 2026 14:09
@casaroli
casaroli marked this pull request as ready for review November 12, 2026 14:09
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/43234342242

@casaroli
casaroli marked this pull request as draft November 13, 2026 09:31
@casaroli
casaroli marked this pull request as ready for review November 13, 2026 09:31
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/43401992847

@acassis
acassis merged commit c1c3f1c into apache:master Nov 13, 2026
138 of 293 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants