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


The following commit(s) were added to refs/heads/master by this push:
     new db535791e testing/nettest: Test connect() while a local listener 
closes.
db535791e is described below

commit db535791e4d078680b5399a57a020d6111c98b58
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 15:41:48 2026 +0100

    testing/nettest: Test connect() while a local listener closes.
    
    Add test_local_dgram_stream_listenconn to cmocka_net_local_dgram.  A
    client waits in recv() on a connection that the listener did not accept,
    in a thread of higher priority.  When the listener closes, the client
    connects again to the same name.  This connect() must fail with
    ECONNREFUSED or ENOENT: it must not get a connection on the listener
    that closes.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/local/test_local_dgram.c         |   1 +
 testing/nettest/local/test_local_dgram.h         |   1 +
 testing/nettest/local/test_local_dgram_regress.c | 110 +++++++++++++++++++++++
 3 files changed, 112 insertions(+)

diff --git a/testing/nettest/local/test_local_dgram.c 
b/testing/nettest/local/test_local_dgram.c
index ededd6afa..b059d8693 100644
--- a/testing/nettest/local/test_local_dgram.c
+++ b/testing/nettest/local/test_local_dgram.c
@@ -135,6 +135,7 @@ int main(int argc, FAR char *argv[])
       DGRAM_TEST(test_local_dgram_stream_threads),
       DGRAM_TEST(test_local_dgram_stream_hup),
       DGRAM_TEST(test_local_dgram_stream_listenclose),
+      DGRAM_TEST(test_local_dgram_stream_listenconn),
       DGRAM_TEST(test_local_dgram_pipe_hup),
       DGRAM_TEST(test_local_dgram_fifo_hup),
       DGRAM_TEST(test_local_dgram_pair_send),
diff --git a/testing/nettest/local/test_local_dgram.h 
b/testing/nettest/local/test_local_dgram.h
index b5c4453f3..b61ad46c8 100644
--- a/testing/nettest/local/test_local_dgram.h
+++ b/testing/nettest/local/test_local_dgram.h
@@ -368,6 +368,7 @@ void test_local_dgram_stream_unspec(FAR void **state);
 void test_local_dgram_stream_threads(FAR void **state);
 void test_local_dgram_stream_hup(FAR void **state);
 void test_local_dgram_stream_listenclose(FAR void **state);
+void test_local_dgram_stream_listenconn(FAR void **state);
 void test_local_dgram_pipe_hup(FAR void **state);
 void test_local_dgram_fifo_hup(FAR void **state);
 
diff --git a/testing/nettest/local/test_local_dgram_regress.c 
b/testing/nettest/local/test_local_dgram_regress.c
index 6d9220ae9..698cbbd29 100644
--- a/testing/nettest/local/test_local_dgram_regress.c
+++ b/testing/nettest/local/test_local_dgram_regress.c
@@ -36,6 +36,7 @@
 #include <fcntl.h>
 #include <poll.h>
 #include <pthread.h>
+#include <sched.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stddef.h>
@@ -112,6 +113,44 @@ static int local_dgram_do_records(FAR struct 
local_dgram_job_s *job)
   return 0;
 }
 
+/* Wait in recv() until the connection ends, then connect() again to the
+ * same name with a new socket.  Return the result of the connect().
+ */
+
+static int local_dgram_do_reconnect(FAR struct local_dgram_job_s *job)
+{
+  struct sockaddr_un addr;
+  socklen_t addrlen = local_dgram_addr(&addr, job->path, false);
+  char buf[4];
+  int ret;
+  int err;
+  int fd;
+
+  recv(job->fd, buf, sizeof(buf), 0);
+
+  fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  if (fd < 0)
+    {
+      return -2;
+    }
+
+  ret = connect(fd, (FAR struct sockaddr *)&addr, addrlen);
+  err = errno;
+  close(fd);
+  errno = err;
+  return ret;
+}
+
+static FAR void *local_dgram_reconnect_main(FAR void *arg)
+{
+  FAR struct local_dgram_job_s *job = arg;
+
+  errno    = 0;
+  job->ret = job->action(job);
+  job->err = errno;
+  return NULL;
+}
+
 /* Make a connected pair of stream sockets: u, the client, and r2, the
  * accepted socket.  r3 is the listener.
  */
@@ -722,6 +761,77 @@ void test_local_dgram_stream_listenclose(FAR void **state)
 #endif
 }
 
+/****************************************************************************
+ * Name: test_local_dgram_stream_listenconn
+ *
+ * Description:
+ *   A client waits in recv() on a connection that the listener did not
+ *   accept, in a thread of higher priority.  The listener closes: that
+ *   ends the connection, and the client at once connects again to the
+ *   listener.  This connect() must wait for the close() and then fail: it
+ *   must not get a connection on the listener that closes.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_stream_listenconn(FAR void **state)
+{
+#ifdef CONFIG_NET_LOCAL_STREAM
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  struct local_dgram_job_s job;
+  struct sched_param param;
+  struct sockaddr_un addr;
+  pthread_attr_t attr;
+  socklen_t addrlen;
+  int policy;
+  int ret;
+
+  addrlen = local_dgram_addr(&addr, st->p3, false);
+  unlink(st->p3);
+
+  st->r3 = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  assert_return_code(st->r3, errno);
+  ret = bind(st->r3, (FAR struct sockaddr *)&addr, addrlen);
+  assert_return_code(ret, errno);
+  ret = listen(st->r3, 4);
+  assert_return_code(ret, errno);
+
+  st->u = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  assert_return_code(st->u, errno);
+  ret = connect(st->u, (FAR struct sockaddr *)&addr, addrlen);
+  assert_return_code(ret, errno);
+
+  /* The client thread runs before this thread when the close() wakes it */
+
+  ret = pthread_getschedparam(pthread_self(), &policy, &param);
+  assert_int_equal(ret, 0);
+  param.sched_priority++;
+  pthread_attr_init(&attr);
+  pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
+  pthread_attr_setschedpolicy(&attr, policy);
+  pthread_attr_setschedparam(&attr, &param);
+
+  memset(&job, 0, sizeof(job));
+  job.action = local_dgram_do_reconnect;
+  job.fd     = st->u;
+  job.path   = st->p3;
+  ret = pthread_create(&job.thread, &attr, local_dgram_reconnect_main, &job);
+  pthread_attr_destroy(&attr);
+  assert_int_equal(ret, 0);
+
+  /* Let the client reach its recv() also on another CPU */
+
+  usleep(100 * 1000);
+  ret = close(st->r3);
+  st->r3 = -1;
+  local_dgram_job_join(&job);
+  assert_int_equal(ret, 0);
+  assert_int_equal(job.ret, -1);
+  assert_true(job.err == ECONNREFUSED || job.err == ENOENT);
+#else
+  skip();
+#endif
+}
+
 /****************************************************************************
  * Name: test_local_dgram_pipe_hup
  *

Reply via email to