Skip to content

testing/nettest: Add a test of Unix domain datagram sockets. - #3964

Merged
xiaoxiang781216 merged 3 commits into
apache:masterfrom
casaroli:apps-local-dgram-test-up
Nov 6, 2026
Merged

xiaoxiang781216 merged 3 commits into
apache:masterfrom
casaroli:apps-local-dgram-test-up

Conversation

@casaroli

@casaroli casaroli commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

No test in nuttx-apps covers connect() and poll() of Unix domain datagram sockets. This PR adds the cmocka test cmocka_net_local_dgram to testing/nettest/local (CONFIG_TESTING_NET_LOCAL_DGRAM), next to cmocka_net_local. It has 75 cases, so review is by commit:

  1. The first commit is the same as before: 20 cases of connect(), send(), getpeername(), poll() and select().
  2. The second commit has 21 cases of what works today and must not change: bind() errors, getsockname(), sendto(), recv(), recvfrom(), datagram limits and order, short reads, MSG_DONTWAIT, MSG_PEEK, sendmsg() and recvmsg(); names, connect(AF_UNSPEC), two senders and POLLHUP of stream sockets; POLLHUP and POLLERR of pipes and FIFOs. The datagram cases of this commit use neither connect() nor poll(), and they pass on master.
  3. The third commit has 34 more cases of what connect() and poll() must do: an empty name; what poll() reports for unbound and bound sockets, until the last datagram is read, for an array, one socket twice, a dup(); wake ups, timeout and EINTR; ppoll(), select(), pselect() and epoll; bind() and connect() by another thread during a poll(); two senders and one receiver; no POLLHUP from a FIFO with buffer policy 1.

Each case makes its own sockets in its setup, so a failed case does not change the next one. A watchdog (alarm()) stops the program when a case does not end. The first optional argument is a writable directory for the socket names (default P_tmpdir). The second is a cmocka filter, to run some of the cases.

Impact

New test only, default y under TESTING_NET_TEST, as TESTING_NET_LOCAL and TESTING_NET_OTHERS. It depends on NET_LOCAL && NET_LOCAL_DGRAM, !DISABLE_PTHREAD and !DISABLE_POSIX_TIMERS. No defconfig enables TESTING_NET_TEST, so no CI build changes. No change to the other nettest programs. The nettest page in Documentation/applications/testing describes the programs in general; no change.

Testing

QEMU qemu-armv8a with HVF, 1 CPU, with NET, NET_LOCAL, NET_LOCAL_STREAM, NET_LOCAL_DGRAM, PIPES, NETDEV_LATEINIT, NET_SOCKOPTS, the cmocka options and CONFIG_TESTING_NET_LOCAL_DGRAM=y. knsh runs the test as an ELF in /system/bin (names in /system), nsh (flat) as a builtin (names in /tmp). Each case ran alone through the filter, with apache/nuttx#21418 merged on the same master:

nuttx master 6e1126a2362 apache/nuttx#21418
cmocka_net_local_dgram on knsh (kernel) 24 PASS, 50 FAIL (connect() and poll() fail with ENOSYS), 1 does not end 75 PASS
cmocka_net_local_dgram on nsh (flat) 24 PASS, 50 FAIL (the same), 1 does not end 75 PASS
ostest on knsh and nsh status 0 status 0

The case that does not end on master is the one with two senders: it uses poll(), so nothing reads and the senders stay blocked.

mps2-an500:knsh (protected build, Cortex-M7, QEMU TCG, names in a tmpfs on /tmp) on apache/nuttx#21418: 75 PASS, ostest status 0.

Builds with CONFIG_TESTING_NET_LOCAL_DGRAM=y and EXTRAFLAGS="-Wno-cpp -Werror" on apache/nuttx#21418, each OK, no warning in the test: qemu-armv8a:nsh (make and CMake), rv-virt:nsh64, mps2-an500:knsh (protected). qemu-armv8a:nsh with CONFIG_NET_LOCAL_DGRAM off, also with apache/nuttx#21494, which fixes the build of the kernel in that case: the option is off and nothing of the test is built.

All cases also ran on Linux, built with the host libc: each passes, so the expected results are not specific to this implementation. One case, policy 1 of a FIFO, is NuttX only and is skipped there.

tools/checkpatch.sh -c -u -m -g passes.

Comment thread testing/nettest/Kconfig Resolved
No test in apps covers connect() and poll() of Unix domain datagram
sockets.  Add the cmocka test cmocka_net_local_dgram
(CONFIG_TESTING_NET_LOCAL_DGRAM).  It has 20 cases:

- connect() sets the destination, connect(AF_UNSPEC) removes it and a
  second connect() changes it.
- send(), write() and sendto() with a NULL address go to the
  destination.  getpeername() gives its name.
- A connected socket takes datagrams from its peer only.
- poll() and select() report POLLIN and POLLOUT.  poll() wakes up when
  a datagram arrives.  A datagram that is too large causes no POLLHUP.
- recv() with MSG_DONTWAIT or O_NONBLOCK fails with EAGAIN.
- connect() to a missing name fails with ENOENT, connect() to a regular
  file with ECONNREFUSED.  send() after the receiver is closed fails
  with ECONNREFUSED.
- connect() and send() to an abstract name.

A watchdog (alarm()) stops the program if a case does not end.  The
optional argument is a writable directory for the socket names.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the apps-local-dgram-test-up branch from 6830185 to 8e415df Compare November 4, 2026 09:33
xiaoxiang781216
xiaoxiang781216 previously approved these changes Nov 5, 2026
Add to cmocka_net_local_dgram the cases that work without connect() and
poll() of a datagram socket, so that a change to these sockets shows what
it breaks.  The datagram cases use neither connect() nor poll():

- bind() errors, getsockname(), sendto(), recv() and recvfrom(), datagram
  limits and order, short reads, datagrams with no
  data or too large, MSG_DONTWAIT, MSG_PEEK, sendmsg() and recvmsg().
- Stream sockets: names, connect(AF_UNSPEC), two threads that send on
  one socket, and POLLHUP.
- Pipes and FIFOs: POLLHUP and POLLERR.

The second optional argument of the program is a cmocka filter, to run
some of the cases.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Add to cmocka_net_local_dgram:

- connect() to an address with no name fails.
- poll() reports only what is true and what the caller asks for: an
  unbound and a bound socket, POLLIN until the last datagram is read,
  short reads, MSG_PEEK, an array of
  descriptors, one socket twice, a dup() of a socket, an abstract name.
- poll() wakes up for send() and sendto(), for a series of datagrams
  and in two threads at once.  It keeps its timeout and ends with EINTR.
- ppoll(), select() and pselect() with all sets, and epoll, also when a
  socket joins a set unbound or closes in a set.
- Another thread calls bind() or connect() during a poll().
- A failed send and many poll() calls leave nothing behind.
- Two senders and one receiver: each datagram comes out whole, once and
  in the order of its sender.
- A FIFO with buffer policy 1 gives no POLLHUP when its last writer closes:
  each send to a datagram socket closes one.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>

@cederom cederom left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow amazing work @casaroli !! :-)

@casaroli
casaroli marked this pull request as ready for review November 6, 2026 07:45
@xiaoxiang781216
xiaoxiang781216 merged commit 659f682 into apache:master Nov 6, 2026
51 of 52 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.

3 participants