This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git

commit bea79726b6f9053f9f1ac34044fa7e8cf613f6b4
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Nov 10 00:45:27 2026 +0100

    testing/nettest: Test sends of local sockets that wait for space.
    
    Add cases to cmocka_net_local_dgram:
    
    - poll_full, poll_fullwait: a non-blocking send to a full bound socket
      fails with EAGAIN and leaves no part of a datagram; a blocking send
      ends when the receiver reads.
    - send_intr, pair_intr, stream_send_intr: a signal ends a send() that
      waits for space in a full bound datagram socket, a full datagram socket
      pair and a full stream socket, with EINTR.  The send waits in a second
      thread, which gets the signal: in a flat build the handler runs on the
      stack of that thread, which has no cmocka frames.
    
    All the new cases pass on Linux.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/local/test_local_dgram.c      |   5 +
 testing/nettest/local/test_local_dgram.h      |   5 +
 testing/nettest/local/test_local_dgram_wait.c | 223 +++++++++++++++++++++++++-
 3 files changed, 229 insertions(+), 4 deletions(-)

diff --git a/testing/nettest/local/test_local_dgram.c 
b/testing/nettest/local/test_local_dgram.c
index 239754628..886f08032 100644
--- a/testing/nettest/local/test_local_dgram.c
+++ b/testing/nettest/local/test_local_dgram.c
@@ -143,6 +143,11 @@ int main(int argc, FAR char *argv[])
       DGRAM_TEST(test_local_dgram_poll_pair),
       DGRAM_TEST(test_local_dgram_recv_intr),
       DGRAM_TEST(test_local_dgram_stream_intr),
+      DGRAM_TEST(test_local_dgram_poll_full),
+      DGRAM_TEST(test_local_dgram_poll_fullwait),
+      DGRAM_TEST(test_local_dgram_send_intr),
+      DGRAM_TEST(test_local_dgram_pair_intr),
+      DGRAM_TEST(test_local_dgram_stream_send_intr),
     };
 
   if (argc > 1)
diff --git a/testing/nettest/local/test_local_dgram.h 
b/testing/nettest/local/test_local_dgram.h
index 9e19b7001..acaee84b5 100644
--- a/testing/nettest/local/test_local_dgram.h
+++ b/testing/nettest/local/test_local_dgram.h
@@ -382,5 +382,10 @@ void test_local_dgram_poll_pair(FAR void **state);
 
 void test_local_dgram_recv_intr(FAR void **state);
 void test_local_dgram_stream_intr(FAR void **state);
+void test_local_dgram_poll_full(FAR void **state);
+void test_local_dgram_poll_fullwait(FAR void **state);
+void test_local_dgram_send_intr(FAR void **state);
+void test_local_dgram_pair_intr(FAR void **state);
+void test_local_dgram_stream_send_intr(FAR void **state);
 
 #endif /* __APPS_TESTING_NETTEST_LOCAL_TEST_LOCAL_DGRAM_H */
diff --git a/testing/nettest/local/test_local_dgram_wait.c 
b/testing/nettest/local/test_local_dgram_wait.c
index c233ec3b5..aaabae127 100644
--- a/testing/nettest/local/test_local_dgram_wait.c
+++ b/testing/nettest/local/test_local_dgram_wait.c
@@ -20,10 +20,11 @@
  *
  ****************************************************************************/
 
-/* A recv() of a local socket that waits for data: a signal ends it with
- * EINTR.  Another thread sends SIGUSR1 (a handler without SA_RESTART) after
- * 200 ms.  If the signal does not end the call, data sent later ends the
- * wait, so that the case fails and does not hang.
+/* A send() or recv() of a local socket that waits: for space in a full
+ * receiver, and when a signal comes.  A signal ends the call with EINTR:
+ * another thread sends SIGUSR1 (a handler without SA_RESTART) after 200 ms.
+ * If the signal does not end the call, a later action ends the wait, so
+ * that the case fails and does not hang.
  */
 
 /****************************************************************************
@@ -33,6 +34,8 @@
 #include <nuttx/config.h>
 
 #include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
 #include <setjmp.h>
 #include <signal.h>
 #include <stdarg.h>
@@ -71,10 +74,115 @@ static void local_dgram_intr_setup(FAR struct sigaction 
*old)
   assert_return_code(ret, errno);
 }
 
+/* Read all that is in the socket without a wait.  Return the count. */
+
+static int local_dgram_drainall(int fd)
+{
+  char buf[LOCAL_DGRAM_FILLLEN];
+  int count = 0;
+
+  while (recv(fd, buf, sizeof(buf), MSG_DONTWAIT) >= 0)
+    {
+      count++;
+    }
+
+  return count;
+}
+
+/* Send one datagram of LOCAL_DGRAM_FILLLEN bytes and wait for space: to
+ * path, or to the peer if path is NULL.
+ */
+
+static int local_dgram_do_sendfull(FAR struct local_dgram_job_s *job)
+{
+  char buf[LOCAL_DGRAM_FILLLEN];
+
+  memset(buf, 'l', sizeof(buf));
+  if (job->path != NULL)
+    {
+      return local_dgram_sendto(job->fd, job->path, buf, sizeof(buf), 0);
+    }
+
+  return send(job->fd, buf, sizeof(buf), 0);
+}
+
+/* A thread sends to a full receiver, this thread sends it SIGUSR1 after
+ * 200 ms and reads the receiver empty 1.3 s later.  The send waits in the
+ * thread: a signal handler of a flat build runs on the stack of the thread
+ * that waits, and that stack has no cmocka frames.  Return the count that
+ * was read; job gets the result of the send.
+ */
+
+static int local_dgram_sendintr(FAR struct local_dgram_job_s *job, int fd,
+                                FAR const char *path, int rfd)
+{
+  struct sigaction old;
+  int count;
+
+  local_dgram_intr_setup(&old);
+  local_dgram_job_start(job, local_dgram_do_sendfull, fd, path, 0);
+  usleep(200000);
+  pthread_kill(job->thread, SIGUSR1);
+  usleep(1300000);
+  count = local_dgram_drainall(rfd);
+  local_dgram_job_join(job);
+  sigaction(SIGUSR1, &old, NULL);
+  return count;
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: test_local_dgram_poll_full
+ *
+ * Description:
+ *   A non-blocking send to a full receiver fails with EAGAIN and leaves no
+ *   part of a datagram: each datagram it took comes out whole.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_poll_full(FAR void **state)
+{
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  int count;
+
+  count = local_dgram_fill(st->s, st->p1);
+  assert_int_equal(local_dgram_revents(st->r1, POLLIN, 0), POLLIN);
+
+  local_dgram_unfill(st->r1, count);
+  assert_int_equal(local_dgram_revents(st->r1, POLLIN, 0), 0);
+  assert_int_equal(local_dgram_revents(st->s, POLLOUT, 0), POLLOUT);
+
+  assert_int_equal(local_dgram_sendto(st->s, st->p1, "next", 4, 0), 4);
+  local_dgram_expect(st->r1, "next");
+}
+
+/****************************************************************************
+ * Name: test_local_dgram_poll_fullwait
+ *
+ * Description:
+ *   A blocking send to a full receiver ends when the receiver reads.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_poll_fullwait(FAR void **state)
+{
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  struct local_dgram_job_s job;
+  int count;
+
+  count = local_dgram_fill(st->s, st->p1);
+
+  local_dgram_job_start(&job, local_dgram_do_sendto, st->s, st->p1, 0);
+  usleep(200000);
+  local_dgram_unfill(st->r1, count);
+  local_dgram_expect(st->r1, "later");
+  local_dgram_job_join(&job);
+  assert_int_equal(job.ret, 5);
+}
+
 /****************************************************************************
  * Name: test_local_dgram_recv_intr
  *
@@ -109,6 +217,63 @@ void test_local_dgram_recv_intr(FAR void **state)
   local_dgram_expect(st->r1, "later");
 }
 
+/****************************************************************************
+ * Name: test_local_dgram_send_intr
+ *
+ * Description:
+ *   A signal ends sendto() to a full bound datagram socket.  The receiver
+ *   gets no part of that datagram.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_send_intr(FAR void **state)
+{
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  struct local_dgram_job_s job;
+  int count;
+
+  local_dgram_fill(st->s, st->p1);
+  count = local_dgram_sendintr(&job, st->s, st->p1, st->r1);
+
+  assert_int_equal(job.ret, -1);
+  assert_int_equal(job.err, EINTR);
+  assert_true(count > 0);
+
+  assert_int_equal(local_dgram_sendto(st->s, st->p1, "next", 4, 0), 4);
+  local_dgram_expect(st->r1, "next");
+}
+
+/****************************************************************************
+ * Name: test_local_dgram_pair_intr
+ *
+ * Description:
+ *   A signal ends send() to the full peer of a datagram socket pair.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_pair_intr(FAR void **state)
+{
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  struct local_dgram_job_s job;
+  int count;
+  int sv[2];
+  int ret;
+
+  ret = socketpair(AF_UNIX, SOCK_DGRAM, 0, sv);
+  assert_return_code(ret, errno);
+  st->r2 = sv[0];
+  st->r3 = sv[1];
+  local_dgram_fill(st->r2, NULL);
+  count = local_dgram_sendintr(&job, st->r2, NULL, st->r3);
+
+  assert_int_equal(job.ret, -1);
+  assert_int_equal(job.err, EINTR);
+  assert_true(count > 0);
+
+  assert_int_equal(send(st->r2, "next", 4, 0), 4);
+  local_dgram_expect(st->r3, "next");
+}
+
 /****************************************************************************
  * Name: test_local_dgram_stream_intr
  *
@@ -151,3 +316,53 @@ void test_local_dgram_stream_intr(FAR void **state)
   skip();
 #endif
 }
+
+/****************************************************************************
+ * Name: test_local_dgram_stream_send_intr
+ *
+ * Description:
+ *   A signal ends send() to a full stream socket.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_stream_send_intr(FAR void **state)
+{
+#ifdef CONFIG_NET_LOCAL_STREAM
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  struct local_dgram_job_s job;
+  char buf[LOCAL_DGRAM_FILLLEN];
+  ssize_t ret;
+  int flags;
+  int count;
+  int sv[2];
+
+  ret = socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
+  assert_return_code(ret, errno);
+  st->r2 = sv[0];
+  st->r3 = sv[1];
+
+  /* Fill the peer with a socket that does not wait */
+
+  flags = fcntl(st->r2, F_GETFL);
+  assert_return_code(flags, errno);
+  assert_return_code(fcntl(st->r2, F_SETFL, flags | O_NONBLOCK), errno);
+  memset(buf, 's', sizeof(buf));
+  do
+    {
+      ret = send(st->r2, buf, sizeof(buf), 0);
+    }
+  while (ret > 0);
+
+  assert_int_equal(ret, -1);
+  assert_true(errno == EAGAIN || errno == EWOULDBLOCK);
+  assert_return_code(fcntl(st->r2, F_SETFL, flags), errno);
+
+  count = local_dgram_sendintr(&job, st->r2, NULL, st->r3);
+
+  assert_int_equal(job.ret, -1);
+  assert_int_equal(job.err, EINTR);
+  assert_true(count > 0);
+#else
+  skip();
+#endif
+}

Reply via email to