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 0a37af986 testing/nettest: Test getsockname() of a CAN_RAW socket with 
a short buffer.
0a37af986 is described below

commit 0a37af9866b7e558fe186da4de7d99cc3dda51d7
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 17:05:00 2026 +0100

    testing/nettest: Test getsockname() of a CAN_RAW socket with a short buffer.
    
    Add test_can_name_short to cmocka_net_can.  getsockname() of a bound
    CAN_RAW socket writes its address into a buffer of 2 bytes.  The call
    must succeed, the buffer gets the first 2 bytes of the address, the
    bytes after it do not change, and *addrlen gives the length of the whole
    address.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/can/test_can.c      |  1 +
 testing/nettest/can/test_can.h      | 11 +++++++++
 testing/nettest/can/test_can_bind.c | 45 +++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)

diff --git a/testing/nettest/can/test_can.c b/testing/nettest/can/test_can.c
index ddb2bf4ca..9f57dbde5 100644
--- a/testing/nettest/can/test_can.c
+++ b/testing/nettest/can/test_can.c
@@ -47,6 +47,7 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test(test_can_bind_can0),
       cmocka_unit_test(test_can_bind_todev),
       cmocka_unit_test(test_can_bind_sendto),
+      cmocka_unit_test(test_can_name_short),
       cmocka_unit_test(test_can_bind_lo),
     };
 
diff --git a/testing/nettest/can/test_can.h b/testing/nettest/can/test_can.h
index 2c1cb9ad0..1db205544 100644
--- a/testing/nettest/can/test_can.h
+++ b/testing/nettest/can/test_can.h
@@ -49,4 +49,15 @@ void test_can_bind_todev(FAR void **state);
 void test_can_bind_sendto(FAR void **state);
 void test_can_bind_lo(FAR void **state);
 
+/****************************************************************************
+ * Name: test_can_name_short
+ *
+ * Description:
+ *   getsockname() of a CAN_RAW socket into a buffer shorter than the
+ *   address: the address is truncated, and *addrlen gives its length.
+ *
+ ****************************************************************************/
+
+void test_can_name_short(FAR void **state);
+
 #endif /* __APPS_TESTING_NETTEST_CAN_TEST_CAN_H */
diff --git a/testing/nettest/can/test_can_bind.c 
b/testing/nettest/can/test_can_bind.c
index 267dc2a1f..a903695a8 100644
--- a/testing/nettest/can/test_can_bind.c
+++ b/testing/nettest/can/test_can_bind.c
@@ -47,6 +47,8 @@
 
 #define TEST_CAN_NODEV   99            /* An interface index with no device */
 #define TEST_CAN_WAIT_US (200 * 1000)  /* Time for the poll after a write */
+#define TEST_CAN_SHORT   2             /* A buffer shorter than the address */
+#define TEST_CAN_FILL    0xa5          /* The bytes after the buffer keep this 
*/
 
 /****************************************************************************
  * Private Functions
@@ -114,6 +116,49 @@ void test_can_bind_any(FAR void **state)
   close(sd);
 }
 
+/****************************************************************************
+ * Name: test_can_name_short
+ *
+ * Description:
+ *   getsockname() of a bound CAN_RAW socket into a buffer of 2 bytes: the
+ *   call succeeds, the buffer gets the first 2 bytes of the address, the
+ *   bytes after it do not change, and *addrlen gives the length of the
+ *   whole address.
+ *
+ ****************************************************************************/
+
+void test_can_name_short(FAR void **state)
+{
+  struct sockaddr_can full;
+  unsigned char buf[sizeof(struct sockaddr_can) + 4];
+  socklen_t len;
+  size_t i;
+  int sd = test_can_socket();
+  int err;
+
+  assert_int_equal(test_can_bindto(sd, 0, &err), 0);
+
+  memset(&full, 0, sizeof(full));
+  len = sizeof(full);
+  assert_return_code(getsockname(sd, (FAR struct sockaddr *)&full, &len),
+                     errno);
+  assert_int_equal(len, sizeof(full));
+  assert_int_equal(full.can_family, AF_CAN);
+
+  memset(buf, TEST_CAN_FILL, sizeof(buf));
+  len = TEST_CAN_SHORT;
+  assert_return_code(getsockname(sd, (FAR struct sockaddr *)buf, &len),
+                     errno);
+  assert_int_equal(len, sizeof(full));
+  assert_memory_equal(buf, &full, TEST_CAN_SHORT);
+  for (i = TEST_CAN_SHORT; i < sizeof(buf); i++)
+    {
+      assert_int_equal(buf[i], TEST_CAN_FILL);
+    }
+
+  close(sd);
+}
+
 /****************************************************************************
  * Name: test_can_bind_can0
  ****************************************************************************/

Reply via email to