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 3e5d52616 testing/nettest: Add TUN cases for a signal in read() and 
write().
3e5d52616 is described below

commit 3e5d526164bd7886cb5624d5d2078c2ba84fb1c2
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 08:41:28 2026 +0100

    testing/nettest: Add TUN cases for a signal in read() and write().
    
    A read() of the TUN device that waits for a packet, and a write() that
    waits until the reader takes the reply of the stack (a RST to a SYN for
    a closed port), must end with EINTR when a signal comes (a SIGUSR1
    handler without SA_RESTART).  If the call does not end, the case takes
    the interface down after 2 seconds; the call then ends with ENETDOWN
    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        |   6 +-
 testing/nettest/Kconfig               |   2 +
 testing/nettest/Makefile              |   2 +-
 testing/nettest/tun/test_tun.c        |   6 +
 testing/nettest/tun/test_tun.h        |  24 ++
 testing/nettest/tun/test_tun_signal.c | 420 ++++++++++++++++++++++++++++++++++
 6 files changed, 457 insertions(+), 3 deletions(-)

diff --git a/testing/nettest/CMakeLists.txt b/testing/nettest/CMakeLists.txt
index 1bd9f7c4f..81968209d 100644
--- a/testing/nettest/CMakeLists.txt
+++ b/testing/nettest/CMakeLists.txt
@@ -299,8 +299,10 @@ if(CONFIG_TESTING_NET_TEST)
   endif()
 
   if(CONFIG_TESTING_NET_TUN)
-    set(SRCS ${CMAKE_CURRENT_LIST_DIR}/tun/test_tun.c
-             ${CMAKE_CURRENT_LIST_DIR}/tun/test_tun_tcp.c)
+    set(SRCS
+        ${CMAKE_CURRENT_LIST_DIR}/tun/test_tun.c
+        ${CMAKE_CURRENT_LIST_DIR}/tun/test_tun_tcp.c
+        ${CMAKE_CURRENT_LIST_DIR}/tun/test_tun_signal.c)
 
     nuttx_add_application(
       NAME
diff --git a/testing/nettest/Kconfig b/testing/nettest/Kconfig
index 1b2c222b9..ac415e46a 100644
--- a/testing/nettest/Kconfig
+++ b/testing/nettest/Kconfig
@@ -165,5 +165,7 @@ config TESTING_NET_TUN
                the whole stream with each byte at its own sequence number, and
                must not reset the connection.  The two cases take about one
                minute.
+               Two more cases check that a signal ends a read() and a write()
+               that wait on the TUN device (EINTR).
 
 endif
diff --git a/testing/nettest/Makefile b/testing/nettest/Makefile
index 26936f438..e5af4f50d 100644
--- a/testing/nettest/Makefile
+++ b/testing/nettest/Makefile
@@ -153,7 +153,7 @@ endif
 ifeq ($(CONFIG_TESTING_NET_TUN),y)
 MAINSRC  += tun/test_tun.c
 PROGNAME += cmocka_net_tun
-CSRCS    += tun/test_tun_tcp.c
+CSRCS    += tun/test_tun_tcp.c tun/test_tun_signal.c
 endif
 
 # One stack size per program: cmocka_net_local_dgram has its own
diff --git a/testing/nettest/tun/test_tun.c b/testing/nettest/tun/test_tun.c
index e86a4ef0d..f0b546da7 100644
--- a/testing/nettest/tun/test_tun.c
+++ b/testing/nettest/tun/test_tun.c
@@ -56,6 +56,12 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test_setup_teardown(test_tun_tcp_partack,
                                       test_tun_tcp_setup,
                                       test_tun_tcp_teardown),
+      cmocka_unit_test_setup_teardown(test_tun_signal_read,
+                                      test_tun_signal_setup,
+                                      test_tun_signal_teardown),
+      cmocka_unit_test_setup_teardown(test_tun_signal_write,
+                                      test_tun_signal_setup,
+                                      test_tun_signal_teardown),
     };
 
   return cmocka_run_group_tests(tun_tests, NULL, NULL);
diff --git a/testing/nettest/tun/test_tun.h b/testing/nettest/tun/test_tun.h
index de2fed467..d7f747a95 100644
--- a/testing/nettest/tun/test_tun.h
+++ b/testing/nettest/tun/test_tun.h
@@ -69,4 +69,28 @@ void test_tun_tcp_lossy(FAR void **state);
 
 void test_tun_tcp_partack(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tun_signal_setup
+ ****************************************************************************/
+
+int test_tun_signal_setup(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tun_signal_teardown
+ ****************************************************************************/
+
+int test_tun_signal_teardown(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tun_signal_read
+ ****************************************************************************/
+
+void test_tun_signal_read(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tun_signal_write
+ ****************************************************************************/
+
+void test_tun_signal_write(FAR void **state);
+
 #endif /* __APPS_TESTING_NETTEST_TUN_TEST_TUN_H */
diff --git a/testing/nettest/tun/test_tun_signal.c 
b/testing/nettest/tun/test_tun_signal.c
new file mode 100644
index 000000000..0f1c61ae2
--- /dev/null
+++ b/testing/nettest/tun/test_tun_signal.c
@@ -0,0 +1,420 @@
+/****************************************************************************
+ * apps/testing/nettest/tun/test_tun_signal.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 <nuttx/config.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <poll.h>
+#include <pthread.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <cmocka.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <netinet/in.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <nuttx/net/tun.h>
+
+#include "test_tun.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TUN_SIG_ADDR_A  0x0a4f0001  /* 10.79.0.1: the TUN interface */
+#define TUN_SIG_ADDR_P  0x0a4f0002  /* 10.79.0.2: the peer */
+#define TUN_SIG_PORT    5026
+#define TUN_SIG_KICK_US 200000      /* The signal comes after 200 ms */
+#define TUN_SIG_DOWN_MS 2000        /* The interface goes down after 2 s */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct tun_sig_s
+{
+  int           fd;
+  char          ifname[IFNAMSIZ];
+  pthread_t     waiter;
+  volatile bool done;
+  volatile bool down;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void tun_sig_handler(int signo)
+{
+}
+
+/* Set the flags of the interface: up or down.  Returns 0 or an errno
+ * value.
+ */
+
+static int tun_sig_ifflags(FAR struct tun_sig_s *st, bool up)
+{
+  struct ifreq ifr;
+  int err = 0;
+  int sd;
+
+  sd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+  if (sd < 0)
+    {
+      return errno;
+    }
+
+  memset(&ifr, 0, sizeof(ifr));
+  strlcpy(ifr.ifr_name, st->ifname, sizeof(ifr.ifr_name));
+  if (ioctl(sd, SIOCGIFFLAGS, (unsigned long)&ifr) < 0)
+    {
+      err = errno;
+    }
+  else
+    {
+      if (up)
+        {
+          ifr.ifr_flags |= IFF_UP;
+        }
+      else
+        {
+          ifr.ifr_flags &= ~IFF_UP;
+        }
+
+      if (ioctl(sd, SIOCSIFFLAGS, (unsigned long)&ifr) < 0)
+        {
+          err = errno;
+        }
+    }
+
+  close(sd);
+  return err;
+}
+
+/* Open the TUN device and set up its interface 10.79.0.1/24.  Returns 0
+ * or an errno value.
+ */
+
+static int tun_sig_open(FAR struct tun_sig_s *st)
+{
+  FAR struct sockaddr_in *sin;
+  struct ifreq ifr;
+  int err;
+  int sd;
+
+  st->fd = open("/dev/tun", O_RDWR | O_CLOEXEC);
+  if (st->fd < 0)
+    {
+      return errno;
+    }
+
+  memset(&ifr, 0, sizeof(ifr));
+  ifr.ifr_flags = IFF_TUN;
+  if (ioctl(st->fd, TUNSETIFF, (unsigned long)&ifr) < 0)
+    {
+      return errno;
+    }
+
+  strlcpy(st->ifname, ifr.ifr_name, sizeof(st->ifname));
+
+  sd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+  if (sd < 0)
+    {
+      return errno;
+    }
+
+  memset(&ifr, 0, sizeof(ifr));
+  strlcpy(ifr.ifr_name, st->ifname, sizeof(ifr.ifr_name));
+  sin                  = (FAR struct sockaddr_in *)&ifr.ifr_addr;
+  sin->sin_family      = AF_INET;
+  sin->sin_addr.s_addr = htonl(TUN_SIG_ADDR_A);
+  err = ioctl(sd, SIOCSIFADDR, (unsigned long)&ifr) < 0 ? errno : 0;
+  if (err == 0)
+    {
+      sin                  = (FAR struct sockaddr_in *)&ifr.ifr_netmask;
+      sin->sin_family      = AF_INET;
+      sin->sin_addr.s_addr = htonl(0xffffff00);
+      err = ioctl(sd, SIOCSIFNETMASK, (unsigned long)&ifr) < 0 ? errno : 0;
+    }
+
+  close(sd);
+  return err != 0 ? err : tun_sig_ifflags(st, true);
+}
+
+/* Send SIGUSR1 to the thread that waits in read() or write().  If the call
+ * does not end, take the interface down: that ends it with ENETDOWN, and
+ * the case fails instead of waiting for ever.
+ */
+
+static FAR void *tun_sig_kick(FAR void *arg)
+{
+  FAR struct tun_sig_s *st = arg;
+  int i;
+
+  usleep(TUN_SIG_KICK_US);
+  pthread_kill(st->waiter, SIGUSR1);
+
+  for (i = 0; i < TUN_SIG_DOWN_MS / 10 && !st->done; i++)
+    {
+      usleep(10000);
+    }
+
+  if (!st->done)
+    {
+      st->down = true;
+      tun_sig_ifflags(st, false);
+    }
+
+  return NULL;
+}
+
+/* The Internet checksum of n bytes at p, added to sum */
+
+static uint16_t tun_sig_csum(FAR const uint8_t *p, int n, uint32_t sum)
+{
+  int i;
+
+  for (i = 0; i + 1 < n; i += 2)
+    {
+      sum += (p[i] << 8) | p[i + 1];
+    }
+
+  while ((sum >> 16) != 0)
+    {
+      sum = (sum & 0xffff) + (sum >> 16);
+    }
+
+  return ~sum & 0xffff;
+}
+
+/* A TCP SYN (IPv4, 40 bytes) from the peer to a port of the interface
+ * where nobody listens.
+ */
+
+static void tun_sig_syn(FAR uint8_t *pkt)
+{
+  uint8_t pseudo[12];
+  uint16_t sum;
+
+  memset(pkt, 0, 40);
+  pkt[0]  = 0x45;                        /* IPv4, 20-byte header */
+  pkt[3]  = 40;                          /* Total length */
+  pkt[8]  = 64;                          /* TTL */
+  pkt[9]  = IPPROTO_TCP;
+  pkt[12] = TUN_SIG_ADDR_P >> 24;
+  pkt[13] = (TUN_SIG_ADDR_P >> 16) & 0xff;
+  pkt[14] = (TUN_SIG_ADDR_P >> 8) & 0xff;
+  pkt[15] = TUN_SIG_ADDR_P & 0xff;
+  pkt[16] = TUN_SIG_ADDR_A >> 24;
+  pkt[17] = (TUN_SIG_ADDR_A >> 16) & 0xff;
+  pkt[18] = (TUN_SIG_ADDR_A >> 8) & 0xff;
+  pkt[19] = TUN_SIG_ADDR_A & 0xff;
+  sum     = tun_sig_csum(pkt, 20, 0);
+  pkt[10] = sum >> 8;
+  pkt[11] = sum & 0xff;
+
+  pkt[20] = TUN_SIG_PORT >> 8;           /* Source port */
+  pkt[21] = TUN_SIG_PORT & 0xff;
+  pkt[22] = (TUN_SIG_PORT + 1) >> 8;     /* Destination port: closed */
+  pkt[23] = (TUN_SIG_PORT + 1) & 0xff;
+  pkt[27] = 1;                           /* Sequence number */
+  pkt[32] = 5 << 4;                      /* Data offset: 20 bytes */
+  pkt[33] = 0x02;                        /* SYN */
+  pkt[34] = 0x10;                        /* Window 4096 */
+
+  memcpy(pseudo, &pkt[12], 8);
+  pseudo[8]  = 0;
+  pseudo[9]  = IPPROTO_TCP;
+  pseudo[10] = 0;
+  pseudo[11] = 20;
+  sum     = tun_sig_csum(&pkt[20], 20,
+                         ~tun_sig_csum(pseudo, sizeof(pseudo), 0) & 0xffff);
+  pkt[36] = sum >> 8;
+  pkt[37] = sum & 0xff;
+}
+
+/* Start the thread that sends the signal.  SIGUSR1 has a handler without
+ * SA_RESTART, so the call must end with EINTR.
+ */
+
+static void tun_sig_start(FAR struct tun_sig_s *st, FAR pthread_t *kick)
+{
+  struct sigaction sa;
+
+  memset(&sa, 0, sizeof(sa));
+  sa.sa_handler = tun_sig_handler;
+  sigemptyset(&sa.sa_mask);
+  assert_int_equal(sigaction(SIGUSR1, &sa, NULL), 0);
+
+  st->waiter = pthread_self();
+  st->done   = false;
+  st->down   = false;
+  assert_int_equal(pthread_create(kick, NULL, tun_sig_kick, st), 0);
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_tun_signal_setup
+ ****************************************************************************/
+
+int test_tun_signal_setup(FAR void **state)
+{
+  FAR struct tun_sig_s *st;
+  int ret;
+
+  st = zalloc(sizeof(*st));
+  if (st == NULL)
+    {
+      return -1;
+    }
+
+  st->fd = -1;
+  *state = st;
+
+  /* No assert here: cmocka does not run the teardown after a failed
+   * setup.  Undo the setup instead.
+   */
+
+  ret = tun_sig_open(st);
+  if (ret != 0)
+    {
+      printf("test_tun_signal_setup: error %d\n", ret);
+      test_tun_signal_teardown(state);
+      return -1;
+    }
+
+  return 0;
+}
+
+/****************************************************************************
+ * Name: test_tun_signal_teardown
+ ****************************************************************************/
+
+int test_tun_signal_teardown(FAR void **state)
+{
+  FAR struct tun_sig_s *st = *state;
+
+  if (st->fd >= 0)
+    {
+      close(st->fd);
+      st->fd = -1;
+    }
+
+  signal(SIGUSR1, SIG_DFL);
+  free(st);
+  *state = NULL;
+  return 0;
+}
+
+/****************************************************************************
+ * Name: test_tun_signal_read
+ *
+ * Description:
+ *   A read() of the TUN device that waits for a packet ends with EINTR
+ *   when a signal comes.
+ *
+ ****************************************************************************/
+
+void test_tun_signal_read(FAR void **state)
+{
+  FAR struct tun_sig_s *st = *state;
+  uint8_t buf[CONFIG_NET_TUN_PKTSIZE];
+  pthread_t kick;
+  ssize_t n;
+  int err;
+
+  tun_sig_start(st, &kick);
+
+  /* Packets that the stack sends when the interface comes up are read
+   * and dropped.
+   */
+
+  do
+    {
+      n = read(st->fd, buf, sizeof(buf));
+    }
+  while (n > 0);
+
+  err = errno;
+  st->done = true;
+  pthread_join(kick, NULL);
+
+  assert_false(st->down);
+  assert_int_equal(n, -1);
+  assert_int_equal(err, EINTR);
+}
+
+/****************************************************************************
+ * Name: test_tun_signal_write
+ *
+ * Description:
+ *   A write() of the TUN device that waits for the reader ends with EINTR
+ *   when a signal comes.  A SYN to a closed port makes the stack put a RST
+ *   in the reply buffer of the device; nobody reads it, so the next write()
+ *   waits.
+ *
+ ****************************************************************************/
+
+void test_tun_signal_write(FAR void **state)
+{
+  FAR struct tun_sig_s *st = *state;
+  struct pollfd pfd;
+  uint8_t syn[40];
+  pthread_t kick;
+  ssize_t n;
+  int err;
+
+  tun_sig_syn(syn);
+  assert_int_equal(write(st->fd, syn, sizeof(syn)), sizeof(syn));
+
+  pfd.fd      = st->fd;
+  pfd.events  = POLLIN;
+  pfd.revents = 0;
+  assert_int_equal(poll(&pfd, 1, 1000), 1);
+
+  tun_sig_start(st, &kick);
+  n = write(st->fd, syn, sizeof(syn));
+  err = errno;
+  st->done = true;
+  pthread_join(kick, NULL);
+
+  assert_false(st->down);
+  assert_int_equal(n, -1);
+  assert_int_equal(err, EINTR);
+}

Reply via email to