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

GUIDINGLI 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 52b959c3c testing/nettest: Test the address of the ends of a socket 
pair.
52b959c3c is described below

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

    testing/nettest: Test the address of the ends of a socket pair.
    
    Add test_local_dgram_pair_name to cmocka_net_local_dgram.  For a
    datagram and a stream socket pair, getsockname(), getpeername() and the
    source address of recvfrom() of each end must give an unnamed address:
    only sun_family, with an address length of sizeof(sa_family_t), as on
    Linux.
    
    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_pair.c | 80 +++++++++++++++++++++++++++
 3 files changed, 82 insertions(+)

diff --git a/testing/nettest/local/test_local_dgram.c 
b/testing/nettest/local/test_local_dgram.c
index 8118f145b..c731268b7 100644
--- a/testing/nettest/local/test_local_dgram.c
+++ b/testing/nettest/local/test_local_dgram.c
@@ -154,6 +154,7 @@ int main(int argc, FAR char *argv[])
       DGRAM_TEST(test_local_dgram_pair_iov),
       DGRAM_TEST(test_local_dgram_send_iov),
       DGRAM_TEST(test_local_dgram_pair_whole),
+      DGRAM_TEST(test_local_dgram_pair_name),
     };
 
   if (argc > 1)
diff --git a/testing/nettest/local/test_local_dgram.h 
b/testing/nettest/local/test_local_dgram.h
index bf353147c..7e020e0d4 100644
--- a/testing/nettest/local/test_local_dgram.h
+++ b/testing/nettest/local/test_local_dgram.h
@@ -383,6 +383,7 @@ 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);
 void test_local_dgram_pair_whole(FAR void **state);
+void test_local_dgram_pair_name(FAR void **state);
 
 /* The cases of test_local_dgram_wait.c */
 
diff --git a/testing/nettest/local/test_local_dgram_pair.c 
b/testing/nettest/local/test_local_dgram_pair.c
index fb34a3d75..63413898b 100644
--- a/testing/nettest/local/test_local_dgram_pair.c
+++ b/testing/nettest/local/test_local_dgram_pair.c
@@ -180,6 +180,41 @@ static int local_dgram_do_sendwhole(FAR struct 
local_dgram_job_s *job)
   return send(job->fd, buf, sizeof(buf), 0);
 }
 
+/* getsockname(), getpeername() and the source address of a recvfrom() of
+ * fd, the end of a pair, give an unnamed address.  peer is the other end.
+ */
+
+static void local_dgram_pairname(int fd, int peer)
+{
+  struct sockaddr_un addr;
+  socklen_t len;
+  char buf[4];
+  ssize_t ret;
+
+  memset(&addr, 0x5a, sizeof(addr));
+  len = sizeof(addr);
+  ret = getsockname(fd, (FAR struct sockaddr *)&addr, &len);
+  assert_return_code(ret, errno);
+  assert_int_equal(addr.sun_family, AF_UNIX);
+  assert_int_equal(len, sizeof(sa_family_t));
+
+  memset(&addr, 0x5a, sizeof(addr));
+  len = sizeof(addr);
+  ret = getpeername(fd, (FAR struct sockaddr *)&addr, &len);
+  assert_return_code(ret, errno);
+  assert_int_equal(addr.sun_family, AF_UNIX);
+  assert_int_equal(len, sizeof(sa_family_t));
+
+  assert_int_equal(send(peer, "n", 1, 0), 1);
+  memset(&addr, 0x5a, sizeof(addr));
+  len = sizeof(addr);
+  ret = recvfrom(fd, buf, sizeof(buf), 0, (FAR struct sockaddr *)&addr,
+                 &len);
+  assert_int_equal(ret, 1);
+  assert_int_equal(addr.sun_family, AF_UNIX);
+  assert_int_equal(len, sizeof(sa_family_t));
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -489,3 +524,48 @@ void test_local_dgram_pair_whole(FAR void **state)
       assert_int_equal(buf[i], 'w');
     }
 }
+
+/****************************************************************************
+ * Name: test_local_dgram_pair_name
+ *
+ * Description:
+ *   The ends of a socket pair have no name, as on Linux: getsockname(),
+ *   getpeername() and the source address of recvfrom() give only
+ *   sun_family, with an address length of sizeof(sa_family_t).  For a
+ *   datagram pair, and for a stream pair.
+ *
+ ****************************************************************************/
+
+void test_local_dgram_pair_name(FAR void **state)
+{
+  FAR struct nettest_local_dgram_state_s *st = *state;
+  int types[2];
+  int ntypes = 0;
+  int sv[2];
+  int ret;
+  int i;
+  int j;
+
+  types[ntypes++] = SOCK_DGRAM;
+#ifdef CONFIG_NET_LOCAL_STREAM
+  types[ntypes++] = SOCK_STREAM;
+#endif
+
+  for (i = 0; i < ntypes; i++)
+    {
+      ret = socketpair(AF_UNIX, types[i] | SOCK_CLOEXEC, 0, sv);
+      assert_return_code(ret, errno);
+      st->r2 = sv[0];
+      st->r3 = sv[1];
+
+      for (j = 0; j < 2; j++)
+        {
+          local_dgram_pairname(sv[j], sv[1 - j]);
+        }
+
+      close(st->r2);
+      close(st->r3);
+      st->r2 = -1;
+      st->r3 = -1;
+    }
+}

Reply via email to