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 7a279f160 testing/nettest: Test the name of a socket pair.
7a279f160 is described below

commit 7a279f1609a7e404f188d239cb892906a8b3d1a2
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 17:01:31 2026 +0100

    testing/nettest: Test the name of a socket pair.
    
    Add two cases to cmocka_net_local_dgram.  test_local_dgram_pair_path:
    F_GETPATH of the ends of a socket pair must give "local:[socketpairN]",
    with N a decimal number from a counter: the same N for both ends of a
    pair, and a different N for the next pair.  test_local_dgram_pair_noname:
    connect() and sendto() of a datagram socket to that name must fail with
    ENOENT, bind() of the name must succeed, and the pair must still work.
    The cases need CONFIG_FS_IOCTL_FILEPATH; without it they are skipped.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/local/test_local_dgram.c      |   2 +
 testing/nettest/local/test_local_dgram.h      |   2 +
 testing/nettest/local/test_local_dgram_pair.c | 120 ++++++++++++++++++++++++++
 3 files changed, 124 insertions(+)

diff --git a/testing/nettest/local/test_local_dgram.c 
b/testing/nettest/local/test_local_dgram.c
index b059d8693..8118f145b 100644
--- a/testing/nettest/local/test_local_dgram.c
+++ b/testing/nettest/local/test_local_dgram.c
@@ -141,6 +141,8 @@ int main(int argc, FAR char *argv[])
       DGRAM_TEST(test_local_dgram_pair_send),
       DGRAM_TEST(test_local_dgram_pair_full),
       DGRAM_TEST(test_local_dgram_pair_close),
+      DGRAM_TEST(test_local_dgram_pair_path),
+      DGRAM_TEST(test_local_dgram_pair_noname),
       DGRAM_TEST(test_local_dgram_poll_pair),
       DGRAM_TEST(test_local_dgram_recv_intr),
       DGRAM_TEST(test_local_dgram_stream_intr),
diff --git a/testing/nettest/local/test_local_dgram.h 
b/testing/nettest/local/test_local_dgram.h
index b61ad46c8..bf353147c 100644
--- a/testing/nettest/local/test_local_dgram.h
+++ b/testing/nettest/local/test_local_dgram.h
@@ -377,6 +377,8 @@ void test_local_dgram_fifo_hup(FAR void **state);
 void test_local_dgram_pair_send(FAR void **state);
 void test_local_dgram_pair_full(FAR void **state);
 void test_local_dgram_pair_close(FAR void **state);
+void test_local_dgram_pair_path(FAR void **state);
+void test_local_dgram_pair_noname(FAR void **state);
 void test_local_dgram_poll_pair(FAR void **state);
 void test_local_dgram_pair_iov(FAR void **state);
 void test_local_dgram_send_iov(FAR void **state);
diff --git a/testing/nettest/local/test_local_dgram_pair.c 
b/testing/nettest/local/test_local_dgram_pair.c
index d526f9b39..fb34a3d75 100644
--- a/testing/nettest/local/test_local_dgram_pair.c
+++ b/testing/nettest/local/test_local_dgram_pair.c
@@ -29,13 +29,16 @@
  * Included Files
  ****************************************************************************/
 
+#include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <poll.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 #include <cmocka.h>
@@ -50,6 +53,7 @@
 #define LOCAL_DGRAM_NIOV    12
 #define LOCAL_DGRAM_IOVLEN  500
 #define LOCAL_DGRAM_WHOLE   400
+#define LOCAL_DGRAM_PNAME   16
 
 /****************************************************************************
  * Private Functions
@@ -68,6 +72,38 @@ static void local_dgram_pair(FAR struct 
nettest_local_dgram_state_s *st)
   st->r3 = sv[1];
 }
 
+#ifdef CONFIG_FS_IOCTL_FILEPATH
+/* F_GETPATH of fd, the end of a pair: check the form
+ * "local:[socketpairN]" with a decimal N, and return N in name.
+ */
+
+static void local_dgram_pairpath(int fd, FAR char *name, size_t size)
+{
+  static const char prefix[] = "local:[socketpair";
+  char path[PATH_MAX];
+  FAR char *num;
+  size_t len;
+  size_t i;
+  int ret;
+
+  memset(path, 0, sizeof(path));
+  ret = fcntl(fd, F_GETPATH, path);
+  assert_return_code(ret, errno);
+  assert_memory_equal(path, prefix, sizeof(prefix) - 1);
+
+  num = path + sizeof(prefix) - 1;
+  len = strlen(num);
+  assert_in_range(len, 2, size);
+  assert_int_equal(num[len - 1], ']');
+  for (i = 0; i < len - 1; i++)
+    {
+      assert_true(isdigit(num[i]));
+    }
+
+  strlcpy(name, num, len);
+}
+#endif
+
 /* recv() one datagram without a wait into size bytes, and check its length
  * and data.
  */
@@ -241,6 +277,90 @@ void test_local_dgram_pair_close(FAR void **state)
   assert_int_equal(errno, ECONNREFUSED);
 }
 
+/****************************************************************************
+ * Name: test_local_dgram_pair_path
+ *
+ * Description:
+ *   F_GETPATH of the ends of a socket pair gives "local:[socketpairN]",
+ *   with N a decimal number from a counter: the same N for both ends of a
+ *   pair, and a different N for the next pair.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_pair_path(FAR void **state)
+{
+#ifdef CONFIG_FS_IOCTL_FILEPATH
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  char name[3][LOCAL_DGRAM_PNAME];
+  int sv[2];
+  int ret;
+
+  ret = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv);
+  assert_return_code(ret, errno);
+  st->r2 = sv[0];
+  st->r3 = sv[1];
+  local_dgram_pairpath(st->r2, name[0], sizeof(name[0]));
+  local_dgram_pairpath(st->r3, name[1], sizeof(name[1]));
+  assert_string_equal(name[0], name[1]);
+
+  ret = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv);
+  assert_return_code(ret, errno);
+  st->u = sv[0];
+  close(sv[1]);
+  local_dgram_pairpath(st->u, name[2], sizeof(name[2]));
+  assert_string_not_equal(name[0], name[2]);
+#else
+  skip();
+#endif
+}
+
+/****************************************************************************
+ * Name: test_local_dgram_pair_noname
+ *
+ * Description:
+ *   The name of a socket pair is internal: connect() and sendto() of a
+ *   datagram socket to it fail with ENOENT, bind() of it succeeds, and the
+ *   pair still works.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_pair_noname(FAR void **state)
+{
+#ifdef CONFIG_FS_IOCTL_FILEPATH
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  char num[LOCAL_DGRAM_PNAME];
+  char name[LOCAL_DGRAM_PATHLEN];
+  int sv[2];
+  int fd;
+  int ret;
+
+  ret = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv);
+  assert_return_code(ret, errno);
+  st->r2 = sv[0];
+  st->r3 = sv[1];
+  local_dgram_pairpath(st->r2, num, sizeof(num));
+  snprintf(name, sizeof(name), "socketpair%s", num);
+
+  errno = 0;
+  assert_int_equal(local_dgram_connect_to(st->s, name, false), -1);
+  assert_int_equal(errno, ENOENT);
+
+  errno = 0;
+  assert_int_equal(local_dgram_sendto(st->s, name, "x", 1, MSG_DONTWAIT),
+                   -1);
+  assert_int_equal(errno, ENOENT);
+
+  fd = local_dgram_bind(name, false);
+  assert_return_code(fd, errno);
+  close(fd);
+
+  assert_int_equal(send(st->r2, "ok", 2, 0), 2);
+  local_dgram_pairrecv(st->r3, "ok", LOCAL_DGRAM_PAIRBUF);
+#else
+  skip();
+#endif
+}
+
 /****************************************************************************
  * Name: test_local_dgram_poll_pair
  *

Reply via email to