Support normal socket (non-NSP) name lookups in resolver (for testing)
[oweals/gnunet.git] / src / tun / test_regex.c
index 8af1abd0284ba875f9fcaf2ceed6b35eb7b778e7..3f0f898a92d38919335267bdf0bf2333c14a9e53 100644 (file)
 
 
 static int
-test_iptoregex (const char *ipv4, const char *netmask, const char *expectedv4,
-                const char *ipv6, unsigned int prefixlen,
+test_iptoregex (const char *ipv4,
+                uint16_t port,
+                const char *expectedv4,
+                const char *ipv6,
+                uint16_t port6,
                 const char *expectedv6)
 {
   int error = 0;
@@ -39,65 +42,132 @@ test_iptoregex (const char *ipv4, const char *netmask, const char *expectedv4,
   char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
 
   GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
-  GNUNET_TUN_ipv4toregex (&a, netmask, rxv4);
-
+  GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
 
   if (0 != strcmp (rxv4, expectedv4))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n",
-                expectedv4, rxv4);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Expected: %s but got: %s\n",
+                expectedv4,
+                rxv4);
     error++;
   }
 
   GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
-  GNUNET_TUN_ipv6toregex (&b, prefixlen, rxv6);
-
+  GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
   if (0 != strcmp (rxv6, expectedv6))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Expected: %s but got: %s\n",
                 expectedv6, rxv6);
     error++;
   }
-
   return error;
 }
 
+
+static int
+test_policy4toregex (const char *policy,
+                     const char *regex)
+{
+  char *r;
+  int ret;
+
+  ret = 0;
+  r = GNUNET_TUN_ipv4policy2regex (policy);
+  if (NULL == r)
+  {
+    GNUNET_break (0);
+    return 1;
+  }
+  if (0 != strcmp (regex, r))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Expected: `%s' but got: `%s'\n",
+                regex, r);
+    ret = 2;
+  }
+  GNUNET_free (r);
+  return ret;
+}
+
+
+static int
+test_policy6toregex (const char *policy,
+                     const char *regex)
+{
+  char *r;
+  int ret;
+
+  ret = 0;
+  r = GNUNET_TUN_ipv6policy2regex (policy);
+  if (NULL == r)
+  {
+    GNUNET_break (0);
+    return 1;
+  }
+  if (0 != strcmp (regex, r))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Expected: `%s' but got: `%s'\n",
+                regex, r);
+    ret = 2;
+  }
+  GNUNET_free (r);
+  return ret;
+}
+
+
 int
 main (int argc, char *argv[])
 {
-  GNUNET_log_setup ("test-regex", "WARNING", NULL);
-
   int error;
+  char *r;
 
+  GNUNET_log_setup ("test-regex", "WARNING", NULL);
   error = 0;
 
-  error +=
-      test_iptoregex ("192.0.0.0", "255.255.255.0",
-                      "110000000000000000000000(0|1)+", "FFFF::0", 16,
-                      "1111111111111111(0|1)+");
+  /* this is just a performance test ... */
+  r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
+  GNUNET_break (NULL != r);
+  GNUNET_free (r);
 
   error +=
-      test_iptoregex ("187.238.225.0", "255.255.255.128",
-                      "1011101111101110111000010(0|1)+", "E1E1:73F9:51BE::0",
-                      49,
-                      "1110000111100001011100111111100101010001101111100(0|1)+");
-
+    test_iptoregex ("192.1.2.3", 2086,
+                    "4-0826-C0010203",
+                    "FFFF::1", 8080,
+                    "6-1F90-FFFF0000000000000000000000000001");
   error +=
-      test_iptoregex ("255.255.255.255", "255.255.255.255",
-                      "11111111111111111111111111111111",
-                      "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 128,
-                      "11111111111111111111111111111111"
-                      "11111111111111111111111111111111"
-                      "11111111111111111111111111111111"
-                      "11111111111111111111111111111111");
-
+    test_iptoregex ("187.238.255.0", 80,
+                    "4-0050-BBEEFF00",
+                    "E1E1:73F9:51BE::0", 49,
+                    "6-0031-E1E173F951BE00000000000000000000");
   error +=
-      test_iptoregex ("0.0.0.0", "255.255.255.255",
-                      "00000000000000000000000000000000", "0::0", 128,
-                      "00000000000000000000000000000000"
-                      "00000000000000000000000000000000"
-                      "00000000000000000000000000000000"
-                      "00000000000000000000000000000000");
-
+    test_policy4toregex ("192.1.2.0/24:80;",
+                         "4-0050-C00102..");
+  error +=
+    test_policy4toregex ("192.1.0.0/16;",
+                         "4-....-C001....");
+  error +=
+    test_policy4toregex ("192.1.0.0/16:80-81;",
+                         "4-(0050|0051)-C001....");
+  error +=
+    test_policy4toregex ("192.1.0.0/8:!3-65535;",
+                         "4-(0001|0002)-C0......");
+  error +=
+    test_policy6toregex ("E1E1::1;",
+                         "6-....-E1E10000000000000000000000000001");
+  error +=
+    test_policy6toregex ("E1E1:ABCD::1/120;",
+                         "6-....-E1E1ABCD0000000000000000000000..");
+  error +=
+    test_policy6toregex ("E1E1:ABCD::ABCD/126;",
+                         "6-....-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
+  error +=
+    test_policy6toregex ("E1E1:ABCD::ABCD/127;",
+                         "6-....-E1E1ABCD00000000000000000000ABC(C|D)");
+  error +=
+    test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
+                         "6-0050-E1E1ABCD00000000000000000000ABCD");
   return error;
 }