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 9cccc339c testing/nettest: Add cases for the errors of TCP connect().
9cccc339c is described below

commit 9cccc339ca92ce5d9ff315d24d408814f1933a94
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:43:47 2026 +0100

    testing/nettest: Add cases for the errors of TCP connect().
    
    cmocka_net_tcp:
    - test_tcp_connect_listener: connect() on a listening socket fails with
      EISCONN, and the socket still listens.
    - test_tcp_connect_family: connect() of an AF_INET socket to an address
      of another family fails with EAFNOSUPPORT.
    - test_tcp_reset_closewait: a RST after the FIN of the peer gives EPIPE
      in SO_ERROR, with and without poll(), not ECONNREFUSED.
    
    cmocka_net_tun (the peer on the TUN device answers the SYN late):
    - test_tun_tcp_connect_again: a second connect() while the handshake of
      a non-blocking connect() goes on fails with EALREADY.
    - test_tun_tcp_connect_intr: a signal interrupts a blocking connect():
      EINTR, the handshake goes on, a second connect() gives EALREADY, and
      the socket connects when the peer answers.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/tcp/test_tcp.c             |  11 ++
 testing/nettest/tcp/test_tcp.h             |  18 +++
 testing/nettest/tcp/test_tcp_bind_listen.c |  58 ++++++++
 testing/nettest/tcp/test_tcp_reset.c       |  53 ++++++++
 testing/nettest/tun/test_tun.c             |  10 ++
 testing/nettest/tun/test_tun.h             |  18 +++
 testing/nettest/tun/test_tun_tcp.c         | 212 +++++++++++++++++++++++++++++
 7 files changed, 380 insertions(+)

diff --git a/testing/nettest/tcp/test_tcp.c b/testing/nettest/tcp/test_tcp.c
index 493dc3df6..70ff80c64 100644
--- a/testing/nettest/tcp/test_tcp.c
+++ b/testing/nettest/tcp/test_tcp.c
@@ -112,6 +112,9 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test_setup_teardown(test_tcp_listen_connected,
                                       test_tcp_bind_listen_setup,
                                       test_tcp_bind_listen_teardown),
+      cmocka_unit_test_setup_teardown(test_tcp_connect_listener,
+                                      test_tcp_bind_listen_setup,
+                                      test_tcp_bind_listen_teardown),
       cmocka_unit_test_setup_teardown(test_tcp_close_shutdown_queued,
                                       test_tcp_close_setup,
                                       test_tcp_close_teardown),
@@ -160,6 +163,9 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test_setup_teardown(test_tcp_reset_refused,
                                       test_tcp_reset_setup,
                                       test_tcp_reset_teardown),
+      cmocka_unit_test_setup_teardown(test_tcp_reset_closewait,
+                                      test_tcp_reset_setup,
+                                      test_tcp_reset_teardown),
       cmocka_unit_test_setup_teardown(test_tcp_halfclose_answer,
                                       test_tcp_halfclose_setup,
                                       test_tcp_halfclose_teardown),
@@ -204,6 +210,11 @@ int main(int argc, FAR char *argv[])
                                       test_tcp_procfs_setup,
                                       test_tcp_procfs_teardown),
 #  endif
+#endif
+#ifdef CONFIG_NET_IPv4
+      cmocka_unit_test_setup_teardown(test_tcp_connect_family,
+                                      test_tcp_bind_listen_setup,
+                                      test_tcp_bind_listen_teardown),
 #endif
     };
 
diff --git a/testing/nettest/tcp/test_tcp.h b/testing/nettest/tcp/test_tcp.h
index 9412dff3c..4ca9eddbe 100644
--- a/testing/nettest/tcp/test_tcp.h
+++ b/testing/nettest/tcp/test_tcp.h
@@ -254,6 +254,18 @@ void test_tcp_listen_recv(FAR void **state);
 
 void test_tcp_listen_connected(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tcp_connect_listener
+ ****************************************************************************/
+
+void test_tcp_connect_listener(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_connect_family
+ ****************************************************************************/
+
+void test_tcp_connect_family(FAR void **state);
+
 /****************************************************************************
  * Name: test_tcp_close_setup
  ****************************************************************************/
@@ -403,6 +415,12 @@ void test_tcp_reset_poll(FAR void **state);
 #  ifdef CONFIG_NET_SOCKOPTS
 void test_tcp_reset_refused(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tcp_reset_closewait
+ ****************************************************************************/
+
+void test_tcp_reset_closewait(FAR void **state);
+
 /****************************************************************************
  * Name: test_tcp_halfclose_setup
  ****************************************************************************/
diff --git a/testing/nettest/tcp/test_tcp_bind_listen.c 
b/testing/nettest/tcp/test_tcp_bind_listen.c
index 6998cb854..3e2c0fab7 100644
--- a/testing/nettest/tcp/test_tcp_bind_listen.c
+++ b/testing/nettest/tcp/test_tcp_bind_listen.c
@@ -579,3 +579,61 @@ void test_tcp_listen_connected(FAR void **state)
   assert_int_equal(listen(server, 1), -1);
   assert_int_equal(errno, EINVAL);
 }
+
+/****************************************************************************
+ * Name: test_tcp_connect_listener
+ *
+ * Description:
+ *   connect() on a listening socket fails with EISCONN (Linux), and the
+ *   socket still listens.
+ *
+ ****************************************************************************/
+
+void test_tcp_connect_listener(FAR void **state)
+{
+  FAR struct tcp_bl_state_s *st = *state;
+  int client = tcp_bl_socket(st, 0, false);
+  int other = tcp_bl_socket(st, 0, false);
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+  in_port_t free_port;
+  in_port_t port;
+  int listener;
+
+  /* A free port: bind a socket to port 0, then close it */
+
+  assert_int_equal(tcp_bl_bind(other, 0), 0);
+  assert_return_code(getsockname(other, (FAR struct sockaddr *)&addr,
+                                 &addrlen), errno);
+  free_port = addr.sin_port;
+  tcp_bl_close(st, other);
+
+  listener = tcp_bl_listener(st, false, 1, &port);
+  assert_int_equal(tcp_bl_connect(listener, free_port), -EISCONN);
+  assert_int_equal(tcp_bl_connect(listener, port), -EISCONN);
+
+  assert_int_equal(tcp_bl_connect(client, port), 0);
+  tcp_bl_accept(st, listener);
+}
+
+/****************************************************************************
+ * Name: test_tcp_connect_family
+ *
+ * Description:
+ *   connect() of an AF_INET socket to an address of another family fails
+ *   with EAFNOSUPPORT.
+ *
+ ****************************************************************************/
+
+void test_tcp_connect_family(FAR void **state)
+{
+  FAR struct tcp_bl_state_s *st = *state;
+  int fd = tcp_bl_socket(st, 0, false);
+  struct sockaddr_storage addr;
+
+  memset(&addr, 0, sizeof(addr));
+  addr.ss_family = AF_UNIX;
+  assert_int_equal(connect(fd, (FAR struct sockaddr *)&addr, sizeof(addr)),
+                   -1);
+  assert_int_equal(errno, EAFNOSUPPORT);
+}
diff --git a/testing/nettest/tcp/test_tcp_reset.c 
b/testing/nettest/tcp/test_tcp_reset.c
index 291b21364..a9e9a97dc 100644
--- a/testing/nettest/tcp/test_tcp_reset.c
+++ b/testing/nettest/tcp/test_tcp_reset.c
@@ -25,6 +25,7 @@
  * 1122, 4.2.2.13).  The first receive without data, the first send and
  * SO_ERROR give ECONNRESET, once; then a receive gives end-of-file and a
  * send EPIPE.  A RST that refuses a connection still gives ECONNREFUSED.
+ * A RST after the FIN of the peer gives EPIPE in SO_ERROR (CLOSE_WAIT).
  */
 
 /****************************************************************************
@@ -615,4 +616,56 @@ void test_tcp_reset_refused(FAR void **state)
   assert_int_equal(err, ECONNREFUSED);
   tcp_reset_fd(st, fd);
 }
+
+/****************************************************************************
+ * Name: test_tcp_reset_closewait
+ *
+ * Description:
+ *   The peer closes its half of the connection (FIN), then resets it.
+ *   SO_ERROR gives EPIPE once (Linux), not ECONNREFUSED or 0.  poll()
+ *   after it gives POLLHUP and does not set another error.  send() gives
+ *   EPIPE.
+ *
+ ****************************************************************************/
+
+void test_tcp_reset_closewait(FAR void **state)
+{
+  FAR struct tcp_reset_state_s *st = *state;
+  struct tcp_reset_pair_s pair;
+  socklen_t len;
+  int err;
+  char c;
+
+  /* The FIN of the server: the client is in CLOSE_WAIT */
+
+  tcp_reset_pair(st, &pair);
+  assert_return_code(shutdown(pair.server, SHUT_WR), errno);
+  assert_true((tcp_reset_poll(pair.client, POLLIN) & POLLIN) != 0);
+  assert_int_equal(recv(pair.client, &c, 1, 0), 0);
+
+  /* The byte that the server did not read: its close() resets the
+   * connection.
+   */
+
+  tcp_reset_fd(st, pair.server);
+  usleep(TCP_RESET_DELAY * 1000);
+
+  len = sizeof(err);
+  assert_return_code(getsockopt(pair.client, SOL_SOCKET, SO_ERROR, &err,
+                                &len), errno);
+  assert_int_equal(err, EPIPE);
+  assert_return_code(getsockopt(pair.client, SOL_SOCKET, SO_ERROR, &err,
+                                &len), errno);
+  assert_int_equal(err, 0);
+
+  assert_true((tcp_reset_poll(pair.client, POLLIN | POLLOUT) &
+               POLLHUP) != 0);
+  assert_return_code(getsockopt(pair.client, SOL_SOCKET, SO_ERROR, &err,
+                                &len), errno);
+  assert_int_equal(err, 0);
+
+  assert_int_equal(send(pair.client, "s", 1, MSG_NOSIGNAL), -1);
+  assert_int_equal(errno, EPIPE);
+  tcp_reset_fd(st, pair.client);
+}
 #endif
diff --git a/testing/nettest/tun/test_tun.c b/testing/nettest/tun/test_tun.c
index f5f784c52..e86a4ef0d 100644
--- a/testing/nettest/tun/test_tun.c
+++ b/testing/nettest/tun/test_tun.c
@@ -24,6 +24,8 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stddef.h>
@@ -40,6 +42,14 @@ int main(int argc, FAR char *argv[])
 {
   const struct CMUnitTest tun_tests[] =
     {
+      cmocka_unit_test_setup_teardown(test_tun_tcp_connect_again,
+                                      test_tun_tcp_client_setup,
+                                      test_tun_tcp_teardown),
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+      cmocka_unit_test_setup_teardown(test_tun_tcp_connect_intr,
+                                      test_tun_tcp_client_setup,
+                                      test_tun_tcp_teardown),
+#endif
       cmocka_unit_test_setup_teardown(test_tun_tcp_lossy,
                                       test_tun_tcp_setup,
                                       test_tun_tcp_teardown),
diff --git a/testing/nettest/tun/test_tun.h b/testing/nettest/tun/test_tun.h
index c5b4d48ac..de2fed467 100644
--- a/testing/nettest/tun/test_tun.h
+++ b/testing/nettest/tun/test_tun.h
@@ -33,12 +33,30 @@
 
 int test_tun_tcp_setup(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tun_tcp_client_setup
+ ****************************************************************************/
+
+int test_tun_tcp_client_setup(FAR void **state);
+
 /****************************************************************************
  * Name: test_tun_tcp_teardown
  ****************************************************************************/
 
 int test_tun_tcp_teardown(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tun_tcp_connect_again
+ ****************************************************************************/
+
+void test_tun_tcp_connect_again(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tun_tcp_connect_intr
+ ****************************************************************************/
+
+void test_tun_tcp_connect_intr(FAR void **state);
+
 /****************************************************************************
  * Name: test_tun_tcp_lossy
  ****************************************************************************/
diff --git a/testing/nettest/tun/test_tun_tcp.c 
b/testing/nettest/tun/test_tun_tcp.c
index e2cca51b7..206562247 100644
--- a/testing/nettest/tun/test_tun_tcp.c
+++ b/testing/nettest/tun/test_tun_tcp.c
@@ -32,6 +32,7 @@
 #include <poll.h>
 #include <pthread.h>
 #include <setjmp.h>
+#include <signal.h>
 #include <stdarg.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -98,6 +99,9 @@
 #define TCP_PSH        0x08
 #define TCP_ACK        0x10
 
+#define TUN_WAIT_MS    3000        /* For a segment or an event */
+#define TUN_INTR_MS    500         /* Before the signal to connect() */
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -114,6 +118,7 @@ struct tun_seg_s
 struct tun_state_s
 {
   int       fd;
+  int       fd2;            /* The socket of a client case */
   char      ifname[IFNAMSIZ];
   pthread_t server;
   bool      started;
@@ -660,6 +665,90 @@ static void tun_check(FAR struct tun_state_s *st, FAR 
const char *name,
   assert_int_equal(st->sent, TUN_TOTAL);
 }
 
+/* A socket of the TUN interface (port TUN_PORT_A) that connects to the
+ * peer.  The peer takes a new port.
+ */
+
+static int tun_client(FAR struct tun_state_s *st, int type,
+                      FAR struct sockaddr_in *peer)
+{
+  struct sockaddr_in addr;
+  int one = 1;
+  int fd;
+
+  fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC | type, 0);
+  assert_true(fd >= 0);
+  st->fd2 = fd;
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family      = AF_INET;
+  addr.sin_port        = htons(TUN_PORT_A);
+  addr.sin_addr.s_addr = htonl(TUN_ADDR_A);
+  assert_return_code(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
+                                sizeof(one)), errno);
+  assert_return_code(bind(fd, (FAR struct sockaddr *)&addr, sizeof(addr)),
+                     errno);
+
+  st->pport = 40000 + tun_now_ms() % 20000;
+  st->pisn  = TUN_ISN_P + tun_now_ms() * 64;
+  st->pseq  = st->pisn + 1;
+
+  memset(peer, 0, sizeof(*peer));
+  peer->sin_family      = AF_INET;
+  peer->sin_port        = htons(st->pport);
+  peer->sin_addr.s_addr = htonl(TUN_ADDR_P);
+  return fd;
+}
+
+/* The SYN of the client and the SYN-ACK of the peer */
+
+static void tun_client_synack(FAR struct tun_state_s *st)
+{
+  struct tun_seg_s seg;
+  bool again;
+
+  assert_int_equal(tun_recv_seg(st, &seg, TUN_WAIT_MS, &again), 1);
+  assert_int_equal(seg.flags & (TCP_SYN | TCP_ACK | TCP_RST), TCP_SYN);
+  tun_send_tcp(st, seg.seq + 1, TCP_SYN | TCP_ACK, 65535);
+}
+
+/* Wait for the end of the handshake: POLLOUT and no error */
+
+static void tun_client_connected(int fd)
+{
+  struct pollfd pfd;
+  socklen_t len;
+  int err;
+
+  pfd.fd      = fd;
+  pfd.events  = POLLOUT;
+  pfd.revents = 0;
+  assert_int_equal(poll(&pfd, 1, TUN_WAIT_MS), 1);
+  assert_int_equal(pfd.revents, POLLOUT);
+
+  len = sizeof(err);
+  assert_return_code(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len),
+                     errno);
+  assert_int_equal(err, 0);
+}
+
+/* A signal handler that does nothing: the signal ends a wait */
+
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+static void tun_onsignal(int signo)
+{
+}
+
+/* Send SIGUSR1 to the thread arg after TUN_INTR_MS */
+
+static FAR void *tun_interrupter(FAR void *arg)
+{
+  usleep(TUN_INTR_MS * 1000);
+  pthread_kill(*(FAR pthread_t *)arg, SIGUSR1);
+  return NULL;
+}
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -676,6 +765,7 @@ int test_tun_tcp_setup(FAR void **state)
 
   memset(st, 0, sizeof(*st));
   st->fd   = -1;
+  st->fd2  = -1;
   st->sent = -1;
   st->pmss = TUN_MSS;
   *state   = st;
@@ -707,6 +797,36 @@ int test_tun_tcp_setup(FAR void **state)
   return 0;
 }
 
+/****************************************************************************
+ * Name: test_tun_tcp_client_setup
+ *
+ * Description:
+ *   The setup of a client case: the TUN interface, no server.
+ *
+ ****************************************************************************/
+
+int test_tun_tcp_client_setup(FAR void **state)
+{
+  FAR struct tun_state_s *st = &g_tun;
+  int ret;
+
+  memset(st, 0, sizeof(*st));
+  st->fd   = -1;
+  st->fd2  = -1;
+  st->sent = -1;
+  *state   = st;
+
+  ret = tun_open(st);
+  if (ret != 0)
+    {
+      printf("test_tun_tcp_client_setup: error %d\n", ret);
+      test_tun_tcp_teardown(state);
+      return -1;
+    }
+
+  return 0;
+}
+
 /****************************************************************************
  * Name: test_tun_tcp_teardown
  ****************************************************************************/
@@ -716,6 +836,12 @@ int test_tun_tcp_teardown(FAR void **state)
   FAR struct tun_state_s *st = *state;
   int i;
 
+  if (st->fd2 >= 0)
+    {
+      close(st->fd2);
+      st->fd2 = -1;
+    }
+
   /* Closing the TUN device takes the interface away: a server that still
    * waits on its connection gets an error.
    */
@@ -740,6 +866,92 @@ int test_tun_tcp_teardown(FAR void **state)
   return 0;
 }
 
+/****************************************************************************
+ * Name: test_tun_tcp_connect_again
+ *
+ * Description:
+ *   A second connect() of a non-blocking socket while the handshake of the
+ *   first one goes on fails with EALREADY, and the handshake goes on.  The
+ *   peer answers the SYN only after the second connect().
+ *
+ ****************************************************************************/
+
+void test_tun_tcp_connect_again(FAR void **state)
+{
+  FAR struct tun_state_s *st = *state;
+  struct sockaddr_in peer;
+  struct pollfd pfd;
+  int fd;
+
+  fd = tun_client(st, SOCK_NONBLOCK, &peer);
+  assert_int_equal(connect(fd, (FAR struct sockaddr *)&peer, sizeof(peer)),
+                   -1);
+  assert_int_equal(errno, EINPROGRESS);
+  assert_int_equal(connect(fd, (FAR struct sockaddr *)&peer, sizeof(peer)),
+                   -1);
+  assert_int_equal(errno, EALREADY);
+
+  pfd.fd      = fd;
+  pfd.events  = POLLOUT;
+  pfd.revents = 0;
+  assert_int_equal(poll(&pfd, 1, 0), 0);
+
+  tun_client_synack(st);
+  tun_client_connected(fd);
+}
+
+/****************************************************************************
+ * Name: test_tun_tcp_connect_intr
+ *
+ * Description:
+ *   A signal interrupts a blocking connect(): connect() fails with EINTR,
+ *   but the handshake goes on (POSIX).  A second connect() fails with
+ *   EALREADY; when the peer answers, poll() gives POLLOUT and the socket
+ *   is connected.
+ *
+ ****************************************************************************/
+
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+void test_tun_tcp_connect_intr(FAR void **state)
+{
+  FAR struct tun_state_s *st = *state;
+  struct sigaction act;
+  struct sigaction old;
+  struct sockaddr_in peer;
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+  pthread_t self = pthread_self();
+  pthread_t tid;
+  int ret;
+  int err;
+  int fd;
+
+  memset(&act, 0, sizeof(act));
+  act.sa_handler = tun_onsignal;
+  sigemptyset(&act.sa_mask);
+  assert_return_code(sigaction(SIGUSR1, &act, &old), errno);
+
+  fd = tun_client(st, 0, &peer);
+  assert_int_equal(pthread_create(&tid, NULL, tun_interrupter, &self), 0);
+  ret = connect(fd, (FAR struct sockaddr *)&peer, sizeof(peer));
+  err = errno;
+  assert_int_equal(pthread_join(tid, NULL), 0);
+  sigaction(SIGUSR1, &old, NULL);
+
+  assert_int_equal(ret, -1);
+  assert_int_equal(err, EINTR);
+  assert_int_equal(connect(fd, (FAR struct sockaddr *)&peer, sizeof(peer)),
+                   -1);
+  assert_int_equal(errno, EALREADY);
+
+  tun_client_synack(st);
+  tun_client_connected(fd);
+  assert_return_code(getpeername(fd, (FAR struct sockaddr *)&addr,
+                                 &addrlen), errno);
+  assert_int_equal(ntohs(addr.sin_port), st->pport);
+}
+#endif
+
 /****************************************************************************
  * Name: test_tun_tcp_lossy
  *

Reply via email to