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 e6f6fc055 testing/nettest: Add accept() cases for SO_RCVTIMEO and
addrlen.
e6f6fc055 is described below
commit e6f6fc05587bc25edb6aa0491d9579bd6dc0ada7
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:45:05 2026 +0100
testing/nettest: Add accept() cases for SO_RCVTIMEO and addrlen.
cmocka_net_tcp:
- test_tcp_accept_rcvtimeo: accept() on a listener with SO_RCVTIMEO and
no client fails with EAGAIN after the timeout.
- test_tcp_accept_addrlen: accept() gives the address of the client cut
to the size of the buffer, and the whole length in addrlen, also for
a buffer shorter than struct sockaddr_in. It writes nothing after the
address.
cmocka_net_local:
- test_local_accept_rcvtimeo: the same timeout for a local stream
socket.
A client connects after 3 seconds, so that an accept() that ignores the
timeout still returns and the case fails instead of waiting for ever.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
testing/nettest/CMakeLists.txt | 4 +
testing/nettest/Makefile | 3 +
testing/nettest/local/test_local.c | 3 +
testing/nettest/local/test_local.h | 6 ++
testing/nettest/local/test_local_accept.c | 165 +++++++++++++++++++++++++++++
testing/nettest/tcp/test_tcp.c | 6 ++
testing/nettest/tcp/test_tcp.h | 12 +++
testing/nettest/tcp/test_tcp_bind_listen.c | 154 +++++++++++++++++++++++++++
8 files changed, 353 insertions(+)
diff --git a/testing/nettest/CMakeLists.txt b/testing/nettest/CMakeLists.txt
index 7a20cfa64..1bd9f7c4f 100644
--- a/testing/nettest/CMakeLists.txt
+++ b/testing/nettest/CMakeLists.txt
@@ -112,6 +112,10 @@ if(CONFIG_TESTING_NET_TEST)
${CMAKE_CURRENT_LIST_DIR}/local/test_local_common.c
${CMAKE_CURRENT_LIST_DIR}/local/test_local_scm_rights.c)
+ if(CONFIG_NET_LOCAL_STREAM AND CONFIG_NET_SOCKOPTS)
+ list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/local/test_local_accept.c)
+ endif()
+
if(CONFIG_TESTING_NET_LOCAL_PEERCRED)
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/local/test_local_peercred.c)
endif()
diff --git a/testing/nettest/Makefile b/testing/nettest/Makefile
index df9ef8fab..26936f438 100644
--- a/testing/nettest/Makefile
+++ b/testing/nettest/Makefile
@@ -78,6 +78,9 @@ ifeq ($(CONFIG_TESTING_NET_LOCAL),y)
MAINSRC += local/test_local.c
PROGNAME += cmocka_net_local
CSRCS += local/test_local_common.c local/test_local_scm_rights.c
+ifeq ($(CONFIG_NET_LOCAL_STREAM)$(CONFIG_NET_SOCKOPTS),yy)
+CSRCS += local/test_local_accept.c
+endif
ifeq ($(CONFIG_TESTING_NET_LOCAL_PEERCRED),y)
CSRCS += local/test_local_peercred.c
endif
diff --git a/testing/nettest/local/test_local.c
b/testing/nettest/local/test_local.c
index 526a1bc58..c73923db8 100644
--- a/testing/nettest/local/test_local.c
+++ b/testing/nettest/local/test_local.c
@@ -43,6 +43,9 @@ int main(int argc, FAR char *argv[])
const struct CMUnitTest local_tests[] =
{
cmocka_unit_test(test_local_scm_rights),
+#if defined(CONFIG_NET_LOCAL_STREAM) && defined(CONFIG_NET_SOCKOPTS)
+ cmocka_unit_test(test_local_accept_rcvtimeo),
+#endif
#ifdef CONFIG_TESTING_NET_LOCAL_PEERCRED
cmocka_unit_test(test_local_peercred_pair),
cmocka_unit_test(test_local_peercred_unconn),
diff --git a/testing/nettest/local/test_local.h
b/testing/nettest/local/test_local.h
index e067285ee..26db15fd0 100644
--- a/testing/nettest/local/test_local.h
+++ b/testing/nettest/local/test_local.h
@@ -51,6 +51,12 @@ int test_local_group_teardown(FAR void **state);
void test_local_scm_rights(FAR void **state);
+/****************************************************************************
+ * Name: test_local_accept_rcvtimeo
+ ****************************************************************************/
+
+void test_local_accept_rcvtimeo(FAR void **state);
+
/****************************************************************************
* Name: test_local_peercred_*
****************************************************************************/
diff --git a/testing/nettest/local/test_local_accept.c
b/testing/nettest/local/test_local_accept.c
new file mode 100644
index 000000000..3b637bab3
--- /dev/null
+++ b/testing/nettest/local/test_local_accept.c
@@ -0,0 +1,165 @@
+/****************************************************************************
+ * apps/testing/nettest/local/test_local_accept.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.
+ *
+ ****************************************************************************/
+
+/* accept() of a local stream socket with SO_RCVTIMEO */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <pthread.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <sys/un.h>
+#include <cmocka.h>
+
+#include "test_local.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TEST_ACCEPT_TIMEO 300 /* ms, SO_RCVTIMEO of accept() */
+#define TEST_ACCEPT_LATE 3000 /* ms before a client ends a wait */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+/* A client that connects after TEST_ACCEPT_LATE ms, unless the case is
+ * done: an accept() that ignores its timeout still returns.
+ */
+
+struct test_local_late_s
+{
+ struct sockaddr_un addr;
+ volatile bool done;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static FAR void *test_local_late(FAR void *arg)
+{
+ FAR struct test_local_late_s *late = arg;
+ int fd;
+ int i;
+
+ for (i = 0; i < TEST_ACCEPT_LATE / 100 && !late->done; i++)
+ {
+ usleep(100 * 1000);
+ }
+
+ if (!late->done)
+ {
+ fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+ if (fd >= 0)
+ {
+ connect(fd, (FAR struct sockaddr *)&late->addr,
+ sizeof(late->addr));
+ while (!late->done)
+ {
+ usleep(100 * 1000);
+ }
+
+ close(fd);
+ }
+ }
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_local_accept_rcvtimeo
+ *
+ * Description:
+ * accept() on a listener with SO_RCVTIMEO and no client fails with
+ * EAGAIN after the timeout (Linux).
+ *
+ ****************************************************************************/
+
+void test_local_accept_rcvtimeo(FAR void **state)
+{
+ struct test_local_late_s late;
+ struct timespec start;
+ struct timespec end;
+ struct timeval tv;
+ pthread_t tid;
+ int listener;
+ long ms;
+ int err;
+ int fd;
+
+ memset(&late, 0, sizeof(late));
+ late.addr.sun_family = AF_LOCAL;
+ snprintf(late.addr.sun_path, sizeof(late.addr.sun_path), "%s/accept.%d",
+ P_tmpdir, getpid());
+ unlink(late.addr.sun_path);
+
+ listener = socket(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ assert_true(listener >= 0);
+ assert_return_code(bind(listener, (FAR struct sockaddr *)&late.addr,
+ sizeof(late.addr)), errno);
+ assert_return_code(listen(listener, 1), errno);
+ tv.tv_sec = 0;
+ tv.tv_usec = TEST_ACCEPT_TIMEO * 1000;
+ assert_return_code(setsockopt(listener, SOL_SOCKET, SO_RCVTIMEO, &tv,
+ sizeof(tv)), errno);
+
+ assert_int_equal(pthread_create(&tid, NULL, test_local_late, &late), 0);
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ fd = accept4(listener, NULL, NULL, SOCK_CLOEXEC);
+ err = errno;
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ late.done = true;
+ pthread_join(tid, NULL);
+ if (fd >= 0)
+ {
+ close(fd);
+ }
+
+ close(listener);
+ unlink(late.addr.sun_path);
+
+ ms = (end.tv_sec - start.tv_sec) * 1000 +
+ (end.tv_nsec - start.tv_nsec) / 1000000;
+ assert_int_equal(fd, -1);
+ assert_int_equal(err, EAGAIN);
+ assert_in_range(ms, TEST_ACCEPT_TIMEO - 50, TEST_ACCEPT_LATE - 500);
+}
diff --git a/testing/nettest/tcp/test_tcp.c b/testing/nettest/tcp/test_tcp.c
index 70ff80c64..28beed55d 100644
--- a/testing/nettest/tcp/test_tcp.c
+++ b/testing/nettest/tcp/test_tcp.c
@@ -97,6 +97,9 @@ int main(int argc, FAR char *argv[])
cmocka_unit_test_setup_teardown(test_tcp_accept_inherit,
test_tcp_bind_listen_setup,
test_tcp_bind_listen_teardown),
+ cmocka_unit_test_setup_teardown(test_tcp_accept_rcvtimeo,
+ test_tcp_bind_listen_setup,
+ test_tcp_bind_listen_teardown),
# ifdef CONFIG_NET_TCP_WRITE_BUFFERS
cmocka_unit_test_setup_teardown(test_tcp_sndtimeo_iob,
test_tcp_sndtimeo_setup,
@@ -106,6 +109,9 @@ int main(int argc, FAR char *argv[])
cmocka_unit_test_setup_teardown(test_tcp_listen_again,
test_tcp_bind_listen_setup,
test_tcp_bind_listen_teardown),
+ cmocka_unit_test_setup_teardown(test_tcp_accept_addrlen,
+ test_tcp_bind_listen_setup,
+ test_tcp_bind_listen_teardown),
cmocka_unit_test_setup_teardown(test_tcp_listen_recv,
test_tcp_bind_listen_setup,
test_tcp_bind_listen_teardown),
diff --git a/testing/nettest/tcp/test_tcp.h b/testing/nettest/tcp/test_tcp.h
index 4ca9eddbe..9559cd1c6 100644
--- a/testing/nettest/tcp/test_tcp.h
+++ b/testing/nettest/tcp/test_tcp.h
@@ -215,6 +215,12 @@ void test_tcp_bind_reuseaddr_listener(FAR void **state);
void test_tcp_accept_inherit(FAR void **state);
+/****************************************************************************
+ * Name: test_tcp_accept_rcvtimeo
+ ****************************************************************************/
+
+void test_tcp_accept_rcvtimeo(FAR void **state);
+
# ifdef CONFIG_NET_TCP_WRITE_BUFFERS
/****************************************************************************
* Name: test_tcp_sndtimeo_setup
@@ -242,6 +248,12 @@ void test_tcp_sndtimeo_iob(FAR void **state);
void test_tcp_listen_again(FAR void **state);
+/****************************************************************************
+ * Name: test_tcp_accept_addrlen
+ ****************************************************************************/
+
+void test_tcp_accept_addrlen(FAR void **state);
+
/****************************************************************************
* Name: test_tcp_listen_recv
****************************************************************************/
diff --git a/testing/nettest/tcp/test_tcp_bind_listen.c
b/testing/nettest/tcp/test_tcp_bind_listen.c
index 3e2c0fab7..3a9f30066 100644
--- a/testing/nettest/tcp/test_tcp_bind_listen.c
+++ b/testing/nettest/tcp/test_tcp_bind_listen.c
@@ -33,6 +33,7 @@
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
+#include <pthread.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -40,6 +41,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <cmocka.h>
@@ -55,6 +57,8 @@
#define TEST_MAX_FDS 8
#define TEST_FILL_SIZE 1024
#define TEST_FILL_MAX 1024 /* sends of TEST_FILL_SIZE, at most */
+#define TEST_ACCEPT_TIMEO 300 /* ms, SO_RCVTIMEO of accept() */
+#define TEST_ACCEPT_LATE 3000 /* ms before a client ends a wait */
/****************************************************************************
* Private Types
@@ -69,6 +73,16 @@ struct tcp_bl_state_s
int fds[TEST_MAX_FDS];
};
+/* A client that connects after TEST_ACCEPT_LATE ms, unless the case is
+ * done: an accept() that ignores its timeout still returns.
+ */
+
+struct tcp_bl_late_s
+{
+ in_port_t port;
+ volatile bool done;
+};
+
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -221,6 +235,41 @@ static int tcp_bl_accept(FAR struct tcp_bl_state_s *st,
int listener)
return tcp_bl_keep(st, accept4(listener, NULL, NULL, SOCK_CLOEXEC));
}
+/****************************************************************************
+ * Name: tcp_bl_late
+ ****************************************************************************/
+
+#ifdef CONFIG_NET_SOCKOPTS
+static FAR void *tcp_bl_late(FAR void *arg)
+{
+ FAR struct tcp_bl_late_s *late = arg;
+ int fd;
+ int i;
+
+ for (i = 0; i < TEST_ACCEPT_LATE / 100 && !late->done; i++)
+ {
+ usleep(100 * 1000);
+ }
+
+ if (!late->done)
+ {
+ fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
+ if (fd >= 0)
+ {
+ tcp_bl_connect(fd, late->port);
+ while (!late->done)
+ {
+ usleep(100 * 1000);
+ }
+
+ close(fd);
+ }
+ }
+
+ return NULL;
+}
+#endif
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -465,8 +514,113 @@ void test_tcp_accept_inherit(FAR void **state)
assert_int_equal(value, cnt);
#endif
}
+
+/****************************************************************************
+ * Name: test_tcp_accept_rcvtimeo
+ *
+ * Description:
+ * accept() on a listener with SO_RCVTIMEO and no client fails with
+ * EAGAIN after the timeout (Linux).
+ *
+ ****************************************************************************/
+
+void test_tcp_accept_rcvtimeo(FAR void **state)
+{
+ FAR struct tcp_bl_state_s *st = *state;
+ struct tcp_bl_late_s late;
+ struct timespec start;
+ struct timespec end;
+ struct timeval tv;
+ pthread_t tid;
+ int listener;
+ long ms;
+ int err;
+ int fd;
+
+ listener = tcp_bl_listener(st, false, 1, &late.port);
+ tv.tv_sec = 0;
+ tv.tv_usec = TEST_ACCEPT_TIMEO * 1000;
+ assert_return_code(setsockopt(listener, SOL_SOCKET, SO_RCVTIMEO, &tv,
+ sizeof(tv)), errno);
+
+ late.done = false;
+ assert_int_equal(pthread_create(&tid, NULL, tcp_bl_late, &late), 0);
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ fd = accept4(listener, NULL, NULL, SOCK_CLOEXEC);
+ err = errno;
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ late.done = true;
+ assert_int_equal(pthread_join(tid, NULL), 0);
+ if (fd >= 0)
+ {
+ tcp_bl_keep(st, fd);
+ }
+
+ ms = (end.tv_sec - start.tv_sec) * 1000 +
+ (end.tv_nsec - start.tv_nsec) / 1000000;
+ assert_int_equal(fd, -1);
+ assert_int_equal(err, EAGAIN);
+ assert_in_range(ms, TEST_ACCEPT_TIMEO - 50, TEST_ACCEPT_LATE - 500);
+}
#endif /* CONFIG_NET_SOCKOPTS */
+/****************************************************************************
+ * Name: test_tcp_accept_addrlen
+ *
+ * Description:
+ * accept() gives the address of the client cut to the size of the
+ * buffer, and the whole length in addrlen (POSIX). It writes nothing
+ * after the address.
+ *
+ ****************************************************************************/
+
+void test_tcp_accept_addrlen(FAR void **state)
+{
+ FAR struct tcp_bl_state_s *st = *state;
+ struct sockaddr_storage addr;
+ struct sockaddr_in name;
+ FAR struct sockaddr_in *in = (FAR struct sockaddr_in *)&addr;
+ FAR const uint8_t *p = (FAR const uint8_t *)&addr;
+ socklen_t namelen;
+ socklen_t addrlen;
+ in_port_t port;
+ int listener;
+ int client;
+ size_t cut;
+ size_t i;
+
+ listener = tcp_bl_listener(st, false, 2, &port);
+ for (cut = 0; cut < 2; cut++)
+ {
+ client = tcp_bl_socket(st, 0, false);
+ assert_int_equal(tcp_bl_connect(client, port), 0);
+ namelen = sizeof(name);
+ assert_return_code(getsockname(client, (FAR struct sockaddr *)&name,
+ &namelen), errno);
+ tcp_bl_wait(listener, POLLIN);
+
+ /* Room for the family, the port and the IPv4 address only, or for
+ * more than the whole address
+ */
+
+ memset(&addr, 0xaa, sizeof(addr));
+ addrlen = cut ? offsetof(struct sockaddr_in, sin_zero) : sizeof(addr);
+ tcp_bl_keep(st, accept4(listener, (FAR struct sockaddr *)&addr,
+ &addrlen, SOCK_CLOEXEC));
+
+ assert_int_equal(addrlen, sizeof(struct sockaddr_in));
+ assert_int_equal(in->sin_family, AF_INET);
+ assert_int_equal(in->sin_port, name.sin_port);
+ assert_int_equal(in->sin_addr.s_addr, name.sin_addr.s_addr);
+ for (i = cut ? offsetof(struct sockaddr_in, sin_zero) :
+ sizeof(struct sockaddr_in);
+ i < sizeof(addr); i++)
+ {
+ assert_int_equal(p[i], 0xaa);
+ }
+ }
+}
+
/****************************************************************************
* Name: test_tcp_listen_again
*