This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 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 f6bf8c58d testing/nettest: Add a TCP case with a large loopback
transfer.
f6bf8c58d is described below
commit f6bf8c58df2b409416f5f608052624c9f5702d46
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 20 01:25:22 2026 +0100
testing/nettest: Add a TCP case with a large loopback transfer.
test_tcp_loopback_bulk sends 4 MiB over 127.0.0.1: a thread sends with
blocking send() calls and fills the write buffers, and the test thread
reads. All bytes must arrive in order within 30 seconds.
With TCP write buffers, the received data must find IOBs while the
write buffers of the sender hold most of the pool. Since nuttx
ea5d2c9ae49 the receiver drops every segment in this case and the
transfer stops. The case then fails. Since nuttx a8fe8fc6e4e a reused
write buffer keeps its wb_sent, and the loopback then sends ACKs
without end in the high-priority work queue. The case then hangs.
Master has both fixes (nuttx#22089 and nuttx#22168), and the case
passes. Both sockets have 1 s send and receive time-outs, so the
sender thread sees the stop flag and ends before the case returns when
the transfer stops. The case needs CONFIG_NET_SOCKOPTS.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
testing/nettest/CMakeLists.txt | 1 +
testing/nettest/Makefile | 1 +
testing/nettest/tcp/test_tcp.c | 9 ++
testing/nettest/tcp/test_tcp.h | 20 +++
testing/nettest/tcp/test_tcp_bulk.c | 290 ++++++++++++++++++++++++++++++++++++
5 files changed, 321 insertions(+)
diff --git a/testing/nettest/CMakeLists.txt b/testing/nettest/CMakeLists.txt
index 910431704..fd787a587 100644
--- a/testing/nettest/CMakeLists.txt
+++ b/testing/nettest/CMakeLists.txt
@@ -37,6 +37,7 @@ if(CONFIG_TESTING_NET_TEST)
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv4.c
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_bind_listen.c
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_close.c)
+ list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_bulk.c)
endif()
if(CONFIG_NET_IPv6)
diff --git a/testing/nettest/Makefile b/testing/nettest/Makefile
index 69ec4d3fd..98b229fd4 100644
--- a/testing/nettest/Makefile
+++ b/testing/nettest/Makefile
@@ -46,6 +46,7 @@ endif
ifeq ($(CONFIG_NET_IPv4), y)
CSRCS += tcp/test_tcp_connect_ipv4.c tcp/test_tcp_bind_listen.c
CSRCS += tcp/test_tcp_close.c
+CSRCS += tcp/test_tcp_bulk.c
endif
ifeq ($(CONFIG_NET_IPv6), y)
diff --git a/testing/nettest/tcp/test_tcp.c b/testing/nettest/tcp/test_tcp.c
index 1813fdf15..83aafc92b 100644
--- a/testing/nettest/tcp/test_tcp.c
+++ b/testing/nettest/tcp/test_tcp.c
@@ -39,6 +39,15 @@ int main(int argc, FAR char *argv[])
const struct CMUnitTest tcp_tests[] =
{
#ifdef CONFIG_NET_IPv4
+# ifdef CONFIG_NET_SOCKOPTS
+ /* First: after the other cases, the devif callbacks that this case
+ * needs at once may not be free (NET_PREALLOC_DEVIF_CALLBACKS).
+ */
+
+ cmocka_unit_test_setup_teardown(test_tcp_loopback_bulk,
+ test_tcp_bulk_setup,
+ test_tcp_bulk_teardown),
+# endif
cmocka_unit_test_setup_teardown(test_tcp_closewait_accept_data,
test_tcp_closewait_setup,
test_tcp_closewait_teardown),
diff --git a/testing/nettest/tcp/test_tcp.h b/testing/nettest/tcp/test_tcp.h
index 05377cd21..3d5864027 100644
--- a/testing/nettest/tcp/test_tcp.h
+++ b/testing/nettest/tcp/test_tcp.h
@@ -277,6 +277,26 @@ void test_tcp_close_unread(FAR void **state);
****************************************************************************/
void test_tcp_close_timewait(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_bulk_setup
+ ****************************************************************************/
+
+# ifdef CONFIG_NET_SOCKOPTS
+int test_tcp_bulk_setup(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_bulk_teardown
+ ****************************************************************************/
+
+int test_tcp_bulk_teardown(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_loopback_bulk
+ ****************************************************************************/
+
+void test_tcp_loopback_bulk(FAR void **state);
+# endif
#endif
/****************************************************************************
diff --git a/testing/nettest/tcp/test_tcp_bulk.c
b/testing/nettest/tcp/test_tcp_bulk.c
new file mode 100644
index 000000000..21d45cc79
--- /dev/null
+++ b/testing/nettest/tcp/test_tcp_bulk.c
@@ -0,0 +1,290 @@
+/****************************************************************************
+ * apps/testing/nettest/tcp/test_tcp_bulk.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.
+ *
+ ****************************************************************************/
+
+/* A large transfer over an IPv4 TCP connection on the loopback address.
+ * A thread sends with blocking send() calls and fills the write buffers,
+ * so the received data must find IOBs while the write buffers hold most
+ * of them.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <netinet/in.h>
+#include <pthread.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <cmocka.h>
+
+#include "test_tcp.h"
+#include "utils.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TEST_BULK_SIZE (4 * 1024 * 1024) /* Bytes to transfer */
+#define TEST_BULK_CHUNK 4096 /* Bytes per send() and recv() */
+#define TEST_BULK_LIMIT 30 /* Seconds for the transfer */
+#define TEST_BULK_NFDS 3
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct tcp_bulk_state_s
+{
+ int fds[TEST_BULK_NFDS]; /* The listener, the client, the accepted */
+ volatile bool stop; /* Tells the sender to stop */
+ size_t sent; /* Bytes that send() took */
+ int senderr; /* errno of a failed send(), or 0 */
+ uint8_t sbuf[TEST_BULK_CHUNK]; /* Buffer of the sender */
+ uint8_t rbuf[TEST_BULK_CHUNK]; /* Buffer of the receiver */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tcp_bulk_byte
+ ****************************************************************************/
+
+static uint8_t tcp_bulk_byte(size_t off)
+{
+ return (uint8_t)(off % 251);
+}
+
+/****************************************************************************
+ * Name: tcp_bulk_timeouts
+ *
+ * Description:
+ * Let a blocking send() or recv() on fd return after one second.
+ *
+ ****************************************************************************/
+
+static void tcp_bulk_timeouts(int fd)
+{
+ struct timeval tv;
+
+ tv.tv_sec = 1;
+ tv.tv_usec = 0;
+ assert_return_code(setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
+ sizeof(tv)), errno);
+ assert_return_code(setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
+ sizeof(tv)), errno);
+}
+
+/****************************************************************************
+ * Name: tcp_bulk_sender
+ *
+ * Description:
+ * Send TEST_BULK_SIZE bytes with blocking send() calls until all are
+ * sent, send() fails or st->stop is set.
+ *
+ ****************************************************************************/
+
+static FAR void *tcp_bulk_sender(FAR void *arg)
+{
+ FAR struct tcp_bulk_state_s *st = arg;
+ ssize_t ret;
+ size_t len;
+ size_t i;
+
+ while (st->sent < TEST_BULK_SIZE && !st->stop)
+ {
+ len = TEST_BULK_SIZE - st->sent;
+ if (len > TEST_BULK_CHUNK)
+ {
+ len = TEST_BULK_CHUNK;
+ }
+
+ for (i = 0; i < len; i++)
+ {
+ st->sbuf[i] = tcp_bulk_byte(st->sent + i);
+ }
+
+ ret = send(st->fds[1], st->sbuf, len, 0);
+ if (ret > 0)
+ {
+ st->sent += ret;
+ }
+ else if (ret < 0 && errno != EAGAIN && errno != ETIMEDOUT)
+ {
+ st->senderr = errno;
+ break;
+ }
+ }
+
+ return NULL;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_tcp_bulk_setup
+ ****************************************************************************/
+
+int test_tcp_bulk_setup(FAR void **state)
+{
+ FAR struct tcp_bulk_state_s *st = malloc(sizeof(*st));
+ int i;
+
+ assert_non_null(st);
+ for (i = 0; i < TEST_BULK_NFDS; i++)
+ {
+ st->fds[i] = -1;
+ }
+
+ st->stop = false;
+ st->sent = 0;
+ st->senderr = 0;
+
+ *state = st;
+ return 0;
+}
+
+/****************************************************************************
+ * Name: test_tcp_bulk_teardown
+ ****************************************************************************/
+
+int test_tcp_bulk_teardown(FAR void **state)
+{
+ FAR struct tcp_bulk_state_s *st = *state;
+ int i;
+
+ for (i = 0; i < TEST_BULK_NFDS; i++)
+ {
+ if (st->fds[i] >= 0)
+ {
+ close(st->fds[i]);
+ }
+ }
+
+ free(st);
+ return 0;
+}
+
+/****************************************************************************
+ * Name: test_tcp_loopback_bulk
+ *
+ * Description:
+ * A thread sends TEST_BULK_SIZE bytes to another socket over 127.0.0.1
+ * with blocking send() calls; this thread reads them. All bytes must
+ * arrive in order within TEST_BULK_LIMIT seconds. The sender always
+ * stops before the case ends.
+ *
+ ****************************************************************************/
+
+void test_tcp_loopback_bulk(FAR void **state)
+{
+ FAR struct tcp_bulk_state_s *st = *state;
+ struct sockaddr_in addr;
+ struct timespec start;
+ struct timespec now;
+ socklen_t addrlen;
+ pthread_t sender;
+ size_t recvd = 0;
+ ssize_t bad = -1;
+ ssize_t ret;
+ int recverr = 0;
+ ssize_t i;
+
+ st->fds[0] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ assert_true(st->fds[0] >= 0);
+ addrlen = nettest_lo_addr((FAR struct sockaddr *)&addr, AF_INET);
+ addr.sin_port = 0;
+ assert_return_code(bind(st->fds[0], (FAR struct sockaddr *)&addr,
+ addrlen), errno);
+ assert_return_code(listen(st->fds[0], 1), errno);
+ assert_return_code(getsockname(st->fds[0], (FAR struct sockaddr *)&addr,
+ &addrlen), errno);
+
+ st->fds[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ assert_true(st->fds[1] >= 0);
+ assert_return_code(connect(st->fds[1], (FAR struct sockaddr *)&addr,
+ addrlen), errno);
+ st->fds[2] = accept4(st->fds[0], NULL, NULL, SOCK_CLOEXEC);
+ assert_true(st->fds[2] >= 0);
+ tcp_bulk_timeouts(st->fds[1]);
+ tcp_bulk_timeouts(st->fds[2]);
+
+ assert_int_equal(pthread_create(&sender, NULL, tcp_bulk_sender, st), 0);
+
+ /* No assert until the sender has stopped */
+
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ now = start;
+ while (recvd < TEST_BULK_SIZE)
+ {
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ if (now.tv_sec - start.tv_sec > TEST_BULK_LIMIT)
+ {
+ break;
+ }
+
+ ret = recv(st->fds[2], st->rbuf, TEST_BULK_CHUNK, 0);
+ if (ret < 0 && (errno == EAGAIN || errno == ETIMEDOUT))
+ {
+ continue;
+ }
+
+ if (ret <= 0)
+ {
+ recverr = ret < 0 ? errno : 0;
+ break;
+ }
+
+ for (i = 0; i < ret && bad < 0; i++)
+ {
+ if (st->rbuf[i] != tcp_bulk_byte(recvd + i))
+ {
+ bad = recvd + i;
+ }
+ }
+
+ recvd += ret;
+ }
+
+ st->stop = true;
+ pthread_join(sender, NULL);
+
+ if (recvd != TEST_BULK_SIZE || bad >= 0)
+ {
+ fail_msg("%zu of %d bytes received (recv errno %d), %zu sent "
+ "(send errno %d), first wrong byte %zd, after %d s",
+ recvd, TEST_BULK_SIZE, recverr, st->sent, st->senderr, bad,
+ (int)(now.tv_sec - start.tv_sec));
+ }
+}