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 ea25f8f99 testing/nettest: Test getsockname() and getpeername() with a
short buffer.
ea25f8f99 is described below
commit ea25f8f99477b82f496aacac0f2fd874267f0bf1
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 15:41:47 2026 +0100
testing/nettest: Test getsockname() and getpeername() with a short buffer.
Add test_udp_name_short4 and test_udp_name_short6 to cmocka_net_udp. A
UDP socket connects to the loopback address, and getsockname() and
getpeername() write its address into a buffer of 8 bytes. The calls
must succeed, the buffer gets the first 8 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/CMakeLists.txt | 3 +-
testing/nettest/Kconfig | 5 +-
testing/nettest/Makefile | 2 +-
testing/nettest/udp/test_udp.c | 2 +
testing/nettest/udp/test_udp.h | 12 +++
testing/nettest/udp/test_udp_name.c | 164 ++++++++++++++++++++++++++++++++++++
6 files changed, 184 insertions(+), 4 deletions(-)
diff --git a/testing/nettest/CMakeLists.txt b/testing/nettest/CMakeLists.txt
index 81968209d..b5402f6a7 100644
--- a/testing/nettest/CMakeLists.txt
+++ b/testing/nettest/CMakeLists.txt
@@ -89,7 +89,8 @@ if(CONFIG_TESTING_NET_TEST)
${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_v6only.c
${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_family.c
${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_bind.c
- ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_epoll.c)
+ ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_epoll.c
+ ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_name.c)
nuttx_add_application(
NAME
diff --git a/testing/nettest/Kconfig b/testing/nettest/Kconfig
index ac415e46a..cbf9c3786 100644
--- a/testing/nettest/Kconfig
+++ b/testing/nettest/Kconfig
@@ -64,8 +64,9 @@ config TESTING_NET_UDP
---help---
Test UDP sockets of AF_INET and AF_INET6 on one port over the
loopback device: IPV6_V6ONLY (also on TCP sockets), the delivery
- of IPv4 and IPv6 datagrams, EADDRINUSE from bind(), and the
- EPOLLOUT | EPOLLET events of a socket (cmocka_net_udp).
+ of IPv4 and IPv6 datagrams, EADDRINUSE from bind(), the
+ EPOLLOUT | EPOLLET events of a socket, and getsockname() and
+ getpeername() into a short buffer (cmocka_net_udp).
config TESTING_NET_LOCAL
bool "Enable cmocka net local test"
diff --git a/testing/nettest/Makefile b/testing/nettest/Makefile
index e5af4f50d..d9f576ca3 100644
--- a/testing/nettest/Makefile
+++ b/testing/nettest/Makefile
@@ -71,7 +71,7 @@ MAINSRC += udp/test_udp.c
PROGNAME += cmocka_net_udp
CSRCS += udp/test_udp_common.c udp/test_udp_v6only.c
CSRCS += udp/test_udp_family.c udp/test_udp_bind.c
-CSRCS += udp/test_udp_epoll.c
+CSRCS += udp/test_udp_epoll.c udp/test_udp_name.c
endif
ifeq ($(CONFIG_TESTING_NET_LOCAL),y)
diff --git a/testing/nettest/udp/test_udp.c b/testing/nettest/udp/test_udp.c
index ba3a87344..b559647ac 100644
--- a/testing/nettest/udp/test_udp.c
+++ b/testing/nettest/udp/test_udp.c
@@ -76,6 +76,8 @@ int main(int argc, FAR char *argv[])
TEST_UDP(test_udp_bind_v6addr),
TEST_UDP(test_udp_epollet_idle),
TEST_UDP(test_udp_epollet_send),
+ TEST_UDP(test_udp_name_short4),
+ TEST_UDP(test_udp_name_short6),
};
return cmocka_run_group_tests(udp_tests, NULL, NULL);
diff --git a/testing/nettest/udp/test_udp.h b/testing/nettest/udp/test_udp.h
index 99a0be775..991f15b1c 100644
--- a/testing/nettest/udp/test_udp.h
+++ b/testing/nettest/udp/test_udp.h
@@ -175,4 +175,16 @@ void test_udp_bind_v6addr(FAR void **state);
void test_udp_epollet_idle(FAR void **state);
void test_udp_epollet_send(FAR void **state);
+/****************************************************************************
+ * Name: test_udp_name_*
+ *
+ * Description:
+ * getsockname() and getpeername() into a buffer that is shorter than the
+ * address: the address is truncated, and *addrlen gives its length.
+ *
+ ****************************************************************************/
+
+void test_udp_name_short4(FAR void **state);
+void test_udp_name_short6(FAR void **state);
+
#endif /* __APPS_TESTING_NETTEST_UDP_TEST_UDP_H */
diff --git a/testing/nettest/udp/test_udp_name.c
b/testing/nettest/udp/test_udp_name.c
new file mode 100644
index 000000000..a3684f451
--- /dev/null
+++ b/testing/nettest/udp/test_udp_name.c
@@ -0,0 +1,164 @@
+/****************************************************************************
+ * apps/testing/nettest/udp/test_udp_name.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include <cmocka.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+
+#include "test_udp.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TEST_UDP_SHORT 8 /* A buffer shorter than each address */
+#define TEST_UDP_FILL 0xa5 /* The bytes after the buffer keep this */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+typedef int (*test_udp_name_t)(int sd, FAR struct sockaddr *addr,
+ FAR socklen_t *addrlen);
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/* Get the name with fn() into a big buffer, then into TEST_UDP_SHORT bytes:
+ * the call succeeds, the short buffer gets the first bytes of the address,
+ * the bytes after it do not change, and *addrlen gives the length of the
+ * whole address.
+ */
+
+static void test_udp_name_check(int sd, test_udp_name_t fn,
+ socklen_t want)
+{
+ struct sockaddr_storage full;
+ unsigned char buf[sizeof(struct sockaddr_storage)];
+ socklen_t len;
+ size_t i;
+ int ret;
+
+ memset(&full, 0, sizeof(full));
+ len = sizeof(full);
+ ret = fn(sd, (FAR struct sockaddr *)&full, &len);
+ assert_return_code(ret, errno);
+ assert_int_equal(len, want);
+
+ memset(buf, TEST_UDP_FILL, sizeof(buf));
+ len = TEST_UDP_SHORT;
+ ret = fn(sd, (FAR struct sockaddr *)buf, &len);
+ assert_return_code(ret, errno);
+ assert_int_equal(len, want);
+ assert_memory_equal(buf, &full, TEST_UDP_SHORT);
+
+ for (i = TEST_UDP_SHORT; i < sizeof(buf); i++)
+ {
+ assert_int_equal(buf[i], TEST_UDP_FILL);
+ }
+}
+
+/* A UDP socket of 'domain' on 'port', connected to the loopback address
+ * and port + 1: check getsockname() and getpeername().
+ */
+
+static void test_udp_name_short(FAR void **state, int domain, int port)
+{
+ struct sockaddr_in6 to6;
+ struct sockaddr_in to4;
+ FAR struct sockaddr *to;
+ socklen_t addrlen;
+ int sd;
+ int ret;
+ int err;
+
+ if (domain == AF_INET)
+ {
+ memset(&to4, 0, sizeof(to4));
+ to4.sin_family = AF_INET;
+ to4.sin_port = htons(port + 1);
+ to4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ to = (FAR struct sockaddr *)&to4;
+ addrlen = sizeof(to4);
+ }
+ else
+ {
+ memset(&to6, 0, sizeof(to6));
+ to6.sin6_family = AF_INET6;
+ to6.sin6_port = htons(port + 1);
+ to6.sin6_addr = in6addr_loopback;
+ to = (FAR struct sockaddr *)&to6;
+ addrlen = sizeof(to6);
+ }
+
+ sd = test_udp_socket(state, domain, SOCK_DGRAM, -1, false);
+ ret = test_udp_bind(sd, domain, port, &err);
+ assert_return_code(ret, err);
+ ret = connect(sd, to, addrlen);
+ assert_return_code(ret, errno);
+
+ test_udp_name_check(sd, getsockname, addrlen);
+ test_udp_name_check(sd, getpeername, addrlen);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_udp_name_short4
+ *
+ * Description:
+ * getsockname() and getpeername() of an AF_INET socket into a buffer of
+ * 8 bytes: the first 8 bytes of the address, and the whole length.
+ *
+ ****************************************************************************/
+
+void test_udp_name_short4(FAR void **state)
+{
+ test_udp_name_short(state, AF_INET, TEST_UDP_PORT + 40);
+}
+
+/****************************************************************************
+ * Name: test_udp_name_short6
+ *
+ * Description:
+ * As test_udp_name_short4(), for an AF_INET6 socket.
+ *
+ ****************************************************************************/
+
+void test_udp_name_short6(FAR void **state)
+{
+ test_udp_name_short(state, AF_INET6, TEST_UDP_PORT + 42);
+}