summaryrefslogtreecommitdiff
path: root/src/tun/test_regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tun/test_regex.c')
-rw-r--r--src/tun/test_regex.c133
1 files changed, 99 insertions, 34 deletions
diff --git a/src/tun/test_regex.c b/src/tun/test_regex.c
index a0c3e99fd..341d8b9a0 100644
--- a/src/tun/test_regex.c
+++ b/src/tun/test_regex.c
@@ -27,8 +27,11 @@
27 27
28 28
29static int 29static int
30test_iptoregex (const char *ipv4, const char *netmask, const char *expectedv4, 30test_iptoregex (const char *ipv4,
31 const char *ipv6, unsigned int prefixlen, 31 uint16_t port,
32 const char *expectedv4,
33 const char *ipv6,
34 uint16_t port6,
32 const char *expectedv6) 35 const char *expectedv6)
33{ 36{
34 int error = 0; 37 int error = 0;
@@ -39,29 +42,82 @@ test_iptoregex (const char *ipv4, const char *netmask, const char *expectedv4,
39 char rxv6[GNUNET_TUN_IPV6_REGEXLEN]; 42 char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
40 43
41 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a)); 44 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
42 GNUNET_TUN_ipv4toregexsearch (&a, netmask, rxv4); 45 GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
43
44 46
45 if (0 != strcmp (rxv4, expectedv4)) 47 if (0 != strcmp (rxv4, expectedv4))
46 { 48 {
47 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n", 49 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
48 expectedv4, rxv4); 50 "Expected: %s but got: %s\n",
51 expectedv4,
52 rxv4);
49 error++; 53 error++;
50 } 54 }
51 55
52 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b)); 56 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
53 GNUNET_TUN_ipv6toregexsearch (&b, prefixlen, rxv6); 57 GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
54
55 if (0 != strcmp (rxv6, expectedv6)) 58 if (0 != strcmp (rxv6, expectedv6))
56 { 59 {
57 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expected: %s but got: %s\n", 60 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61 "Expected: %s but got: %s\n",
58 expectedv6, rxv6); 62 expectedv6, rxv6);
59 error++; 63 error++;
60 } 64 }
61
62 return error; 65 return error;
63} 66}
64 67
68
69static int
70test_policy4toregex (const char *policy,
71 const char *regex)
72{
73 char *r;
74 int ret;
75
76 ret = 0;
77 r = GNUNET_TUN_ipv4policy2regex (policy);
78 if (NULL == r)
79 {
80 GNUNET_break (0);
81 return 1;
82 }
83 if (0 != strcmp (regex, r))
84 {
85 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
86 "Expected: `%s' but got: `%s'\n",
87 regex, r);
88 ret = 2;
89 }
90 GNUNET_free (r);
91 return ret;
92}
93
94
95static int
96test_policy6toregex (const char *policy,
97 const char *regex)
98{
99 char *r;
100 int ret;
101
102 ret = 0;
103 r = GNUNET_TUN_ipv6policy2regex (policy);
104 if (NULL == r)
105 {
106 GNUNET_break (0);
107 return 1;
108 }
109 if (0 != strcmp (regex, r))
110 {
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 "Expected: `%s' but got: `%s'\n",
113 regex, r);
114 ret = 2;
115 }
116 GNUNET_free (r);
117 return ret;
118}
119
120
65int 121int
66main (int argc, char *argv[]) 122main (int argc, char *argv[])
67{ 123{
@@ -72,32 +128,41 @@ main (int argc, char *argv[])
72 error = 0; 128 error = 0;
73 129
74 error += 130 error +=
75 test_iptoregex ("192.0.0.0", "255.255.255.0", 131 test_iptoregex ("192.1.2.3", 2086,
76 "110000000000000000000000(0|1)+", "FFFF::0", 16, 132 "4-0826-C0010203",
77 "1111111111111111(0|1)+"); 133 "FFFF::1", 8080,
78 134 "6-1F90-FFFF0000000000000000000000000001");
79 error += 135 error +=
80 test_iptoregex ("187.238.225.0", "255.255.255.128", 136 test_iptoregex ("187.238.255.0", 80,
81 "1011101111101110111000010(0|1)+", "E1E1:73F9:51BE::0", 137 "4-0050-BBEEFF00",
82 49, 138 "E1E1:73F9:51BE::0", 49,
83 "1110000111100001011100111111100101010001101111100(0|1)+"); 139 "6-0031-E1E173F951BE00000000000000000000");
84
85 error += 140 error +=
86 test_iptoregex ("255.255.255.255", "255.255.255.255", 141 test_policy4toregex ("192.1.2.0/24:80;",
87 "11111111111111111111111111111111", 142 "4-0050-C00102..");
88 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", 128,
89 "11111111111111111111111111111111"
90 "11111111111111111111111111111111"
91 "11111111111111111111111111111111"
92 "11111111111111111111111111111111");
93
94 error += 143 error +=
95 test_iptoregex ("0.0.0.0", "255.255.255.255", 144 test_policy4toregex ("192.1.0.0/16;",
96 "00000000000000000000000000000000", "0::0", 128, 145 "4-....-C001....");
97 "00000000000000000000000000000000" 146 error +=
98 "00000000000000000000000000000000" 147 test_policy4toregex ("192.1.0.0/16:80-81;",
99 "00000000000000000000000000000000" 148 "4-(0050|0051)-C001....");
100 "00000000000000000000000000000000"); 149 error +=
101 150 test_policy4toregex ("192.1.0.0/8:!3-65535;",
151 "4-(0001|0002)-C0......");
152 error +=
153 test_policy6toregex ("E1E1::1;",
154 "6-....-E1E10000000000000000000000000001");
155 error +=
156 test_policy6toregex ("E1E1:ABCD::1/120;",
157 "6-....-E1E1ABCD0000000000000000000000..");
158 error +=
159 test_policy6toregex ("E1E1:ABCD::ABCD/126;",
160 "6-....-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
161 error +=
162 test_policy6toregex ("E1E1:ABCD::ABCD/127;",
163 "6-....-E1E1ABCD00000000000000000000ABC(C|D)");
164 error +=
165 test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
166 "6-0050-E1E1ABCD00000000000000000000ABCD");
102 return error; 167 return error;
103} 168}