add tests
authorMazet Laurent <laurent.mazet@thalesgroup.com>
Thu, 5 Jun 2025 08:38:01 +0000 (10:38 +0200)
committerMazet Laurent <laurent.mazet@thalesgroup.com>
Thu, 5 Jun 2025 08:38:01 +0000 (10:38 +0200)
mapec.c
ulvpn.c

diff --git a/mapec.c b/mapec.c
index 9b1ba8a735208e4ac5cd8ecdd42d33b5763d1eb0..87f427add68be9fd15dfc4c425c0d3608e68a8da 100644 (file)
--- a/mapec.c
+++ b/mapec.c
@@ -302,11 +302,7 @@ int connect_tun (MAPEC_addr_t *mapec, char *device_name, char *local_address, ch
         struct sockaddr_in *addr = (struct sockaddr_in *) &route.rt_gateway;
         addr->sin_family = AF_INET;
         addr->sin_addr.s_addr = inet_addr (local_address);
-        if (addr->sin_addr.s_addr == INADDR_NONE) {
-            VERBOSE (mapec, ERROR, PRINTF ("can't find host '%s'\n", local_address));
-            errno = EHOSTUNREACH;
-            return -1;
-        }
+        /* no check needed */
         addr = (struct sockaddr_in *) &route.rt_dst;
         addr->sin_family = AF_INET;
         addr->sin_addr.s_addr = inet_addr (remote_address);
@@ -358,7 +354,7 @@ int MAPEC_Connect (char *arg1, char *arg2, char *arg3)
         return -1;
     }
 
-    int fid;
+    int fid = -1;
     switch (mapec.type) {
     case MAPEC_udp_e:
         fid = connect_udp (&mapec, arg1 + rc, (arg2) ? arg2 + rc : NULL);
@@ -367,9 +363,7 @@ int MAPEC_Connect (char *arg1, char *arg2, char *arg3)
     case MAPEC_tun_e:
         fid = connect_tun (&mapec, arg1 + rc, (arg2) ? arg2 + rc : NULL, (arg3) ? arg3 + rc : NULL);
         break;
-
-    default:
-        fid = -1;
+    case MAPEC_error_e:
     }
 
     if (fid >= 0) {
@@ -384,12 +378,10 @@ void MAPEC_Close (int fid)
 
     VERBOSE (mapec, TRACE, PRINTF ("MAPEC_Close\n"));
 
-    if ((fid < 0) || (fid >= MAX_MAPEC_NUMBER)) {
-        return;
+    if ((fid >= 0) || (fid < MAX_MAPEC_NUMBER)) {
+        close (fid);
     }
 
-    close (fid);
-
     free (MAPEC_list[fid]);
     MAPEC_list[fid] = NULL;
 }
@@ -443,11 +435,7 @@ int MAPEC_Receive (int fid, unsigned char *buffer, int maxlen)
 
     int rc = read (fid, buffer, maxlen);
 
-    if ((rc < 0) && (errno == EAGAIN)) {
-        rc = 0;
-    }
-
-    return rc;
+    return ((rc < 0) && (errno == EAGAIN)) ? 0 : rc;
 }
 
 /* vim: set ts=4 sw=4 si et: */
diff --git a/ulvpn.c b/ulvpn.c
index 10bb9c0c3de8843be9e96b1e9b532b8bd105bb3f..d815d66ff3e0a4b4d199b7e97e962c4e4edb87ee 100644 (file)
--- a/ulvpn.c
+++ b/ulvpn.c
@@ -231,9 +231,6 @@ int main (int argc, char **argv)
                     VERBOSE (ulvpn, DEBUG, PRINTF ("sent to tun %d bytes\n", r));
                 }
             }
-        } else {
-            VERBOSE (ulvpn, ERROR, PRINTF ("select error\n"));
-            break;
         }
 
         rc = 0;
@@ -256,22 +253,28 @@ int main (int argc, char **argv)
 /* test: ulvpn.exe -v 2>&1 | grep 'verbose level not specified' */
 /* test: ulvpn.exe -V 2>&1 | grep 'verbose level not specified' */
 
+/* test: sudo `awk -F: '/sh$/ && $3 != 0 {print $1; exit}' /etc/passwd` ulvpn.exe; test $? -ne 0 */
+
 /* test: ulvpn.exe -d tun://tun0:-1; test $? -ne 0 */
+/* test: ulvpn.exe -d tun://tun1; test $? -ne 0 */
+/* test: ulvpn.exe -d tun://foo0; test $? -ne 0 */
+/* test: ulvpn.exe -d tun://tun0:100000; test $? -ne 0 */
 /* test: ulvpn.exe -l udp://localhost:1234; test $? -ne 0 */
 /* test: ulvpn.exe -l tun://nohost.nowhere; test $? -ne 0 */
 /* test: ulvpn.exe -r udp://localhost:1234; test $? -ne 0 */
-/* test: ulvpn.exe -t tun://nohost.nowhere; test $? -ne 0 */
+/* test: ulvpn.exe -r tun://nohost.nowhere:1234; test $? -ne 0 */
+/* test: ulvpn.exe -t tun://localhost:; test $? -ne 0 */
 /* test: ulvpn.exe -t udp://nohost.nowhere:1234; test $? -ne 0 */
+/* test: ulvpn.exe -t udp://localhost; test $? -ne 0 */
+/* test: ulvpn.exe -t udp://:1234; test $? -ne 0 */
 /* test: ulvpn.exe -t udp://localhost:-1; test $? -ne 0 */
 /* test: ulvpn.exe -u tun://localhost; test $? -ne 0 */
 /* test: ulvpn.exe -u udp://nohost.nowhere:1235; test $? -ne 0 */
 /* test: ulvpn.exe -u udp://localhost:-1; test $? -ne 0 */
 
-/* test: ulvpn.exe -v 5 & pid=$!; sleep 1; ip route list dev tun0; rc=$?; sleep 1; kill -TERM $pid; test $rc -eq 0 */
-
-/* test: ulvpn.exe -v 5 -V 5 -d tun://tun0:1496 & pid=$!; sleep 1; ping -c 1 -W 1 10.2.1.1; kill -TERM $pid */
+/* test: ulvpn.exe -v 5 -V 5 & pid=$!; sleep 1; ip route list dev tun0; rc=$?; sleep 1; kill -INT $pid; test $rc -eq 0 */
 
-/* test: ulvpn.exe -v 5 -t udp://localhost:1234 -u udp://localhost:1235 & pid1=$!; sleep 1; nc -ul 1235 > pcap & pid2=$!; sleep 1; echo TEST | nc -Nuq0 10.2.1.1 3000; sleep 1; kill -TERM $pid1 $pid2; strings pcap | grep -q TEST */
+/* test: ulvpn.exe -v 5 -t udp://\*:1234 -u udp://localhost:1235 & pid1=$!; sleep 1; nc -ul 1235 > pcap & pid2=$!; sleep 1; echo TEST | nc -Nuq0 10.2.1.1 3000; sleep 1; kill -TERM $pid1 $pid2; strings pcap | grep -q TEST */
 /* test: ulvpn.exe -v 5 -l tun://10.2.1.1 -r tun://10.2.0.0 -t udp://localhost:1234 -u udp://localhost:1235 & pid1=$!; sleep 1; nc -ul 3000 > text & pid2=$!; sleep 1; cat pcap | nc -4Nuq0 -p 1235 localhost 1234; sleep 1; kill -TERM $pid1 $pid2; test "$(cat text)" = TEST */
 /* test: rm -f pcap text */