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

commit bed39ab3336a3f55875d8c262de991a42d138b10
Author: Marco Casaroli <[email protected]>
AuthorDate: Sat Nov 21 17:16:43 2026 +0100

    testing/nettest: Add a TCP case for recv() with no connection.
    
    test_tcp_listen_recv: recv() on a listener and on a socket that never
    connected fails with ENOTCONN.  Before the receives, a loopback
    connection whose reader does not read takes the free I/O buffers down
    to the throttle: in this state the receive window code runs with few
    free buffers.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/tcp/test_tcp.c             |  3 ++
 testing/nettest/tcp/test_tcp.h             |  6 ++++
 testing/nettest/tcp/test_tcp_bind_listen.c | 54 ++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)

diff --git a/testing/nettest/tcp/test_tcp.c b/testing/nettest/tcp/test_tcp.c
index 5bc192491..284db6756 100644
--- a/testing/nettest/tcp/test_tcp.c
+++ b/testing/nettest/tcp/test_tcp.c
@@ -62,6 +62,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_listen_recv,
+                                      test_tcp_bind_listen_setup,
+                                      test_tcp_bind_listen_teardown),
       cmocka_unit_test_setup_teardown(test_tcp_listen_connected,
                                       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 6bc2720db..4739a850b 100644
--- a/testing/nettest/tcp/test_tcp.h
+++ b/testing/nettest/tcp/test_tcp.h
@@ -136,6 +136,12 @@ void test_tcp_accept_inherit(FAR void **state);
 
 void test_tcp_listen_again(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tcp_listen_recv
+ ****************************************************************************/
+
+void test_tcp_listen_recv(FAR void **state);
+
 /****************************************************************************
  * Name: test_tcp_listen_connected
  ****************************************************************************/
diff --git a/testing/nettest/tcp/test_tcp_bind_listen.c 
b/testing/nettest/tcp/test_tcp_bind_listen.c
index 7a902e9c4..6998cb854 100644
--- a/testing/nettest/tcp/test_tcp_bind_listen.c
+++ b/testing/nettest/tcp/test_tcp_bind_listen.c
@@ -39,6 +39,7 @@
 #include <stddef.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <cmocka.h>
@@ -52,6 +53,8 @@
 
 #define TEST_POLL_TIMEOUT 1000 /* ms, loopback answers at once */
 #define TEST_MAX_FDS      8
+#define TEST_FILL_SIZE    1024
+#define TEST_FILL_MAX     1024  /* sends of TEST_FILL_SIZE, at most */
 
 /****************************************************************************
  * Private Types
@@ -500,6 +503,57 @@ void test_tcp_listen_again(FAR void **state)
   tcp_bl_close(st, tcp_bl_accept(st, listener));
 }
 
+/****************************************************************************
+ * Name: test_tcp_listen_recv
+ *
+ * Description:
+ *   recv() on a listener and on a socket that never connected fails with
+ *   ENOTCONN.  A connection whose reader does not read first takes the
+ *   free I/O buffers down to the throttle.
+ *
+ ****************************************************************************/
+
+void test_tcp_listen_recv(FAR void **state)
+{
+  FAR struct tcp_bl_state_s *st = *state;
+  int client = tcp_bl_socket(st, SOCK_NONBLOCK, false);
+  int unconnected = tcp_bl_socket(st, 0, false);
+  FAR char *buf;
+  in_port_t port;
+  int listener;
+  int ret;
+  int i;
+  char c;
+
+  listener = tcp_bl_listener(st, false, 1, &port);
+  ret = tcp_bl_connect(client, port);
+  assert_true(ret == 0 || ret == -EINPROGRESS);
+  tcp_bl_wait(client, POLLOUT);
+  tcp_bl_accept(st, listener);
+
+  /* The accepted socket does not read: the sends stop when the free I/O
+   * buffers reach the throttle (EAGAIN).
+   */
+
+  buf = malloc(TEST_FILL_SIZE);
+  assert_non_null(buf);
+  memset(buf, 'f', TEST_FILL_SIZE);
+  for (i = 0; i < TEST_FILL_MAX; i++)
+    {
+      if (send(client, buf, TEST_FILL_SIZE, 0) < 0)
+        {
+          break;
+        }
+    }
+
+  free(buf);
+
+  assert_int_equal(recv(unconnected, &c, 1, MSG_DONTWAIT), -1);
+  assert_int_equal(errno, ENOTCONN);
+  assert_int_equal(recv(listener, &c, 1, MSG_DONTWAIT), -1);
+  assert_int_equal(errno, ENOTCONN);
+}
+
 /****************************************************************************
  * Name: test_tcp_listen_connected
  *

Reply via email to