2 This file is part of GNUnet
3 Copyright (C) 2012, 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file tun/test_regex.c
20 * @brief simple test for regex.c iptoregex functions
21 * @author Maximilian Szengel
24 #include "gnunet_tun_lib.h"
27 * 'wildcard', matches all possible values (for HEX encoding).
29 #define DOT "(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"
33 test_iptoregex (const char *ipv4,
35 const char *expectedv4,
38 const char *expectedv6)
44 char rxv4[GNUNET_TUN_IPV4_REGEXLEN];
45 char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
47 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
48 GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
50 if (0 != strcmp (rxv4, expectedv4))
52 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
53 "Expected: %s but got: %s\n",
59 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
60 GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
61 if (0 != strcmp (rxv6, expectedv6))
63 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
64 "Expected: %s but got: %s\n",
73 test_policy4toregex (const char *policy,
80 r = GNUNET_TUN_ipv4policy2regex (policy);
86 if (0 != strcmp (regex, r))
88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89 "Expected: `%s' but got: `%s'\n",
99 test_policy6toregex (const char *policy,
106 r = GNUNET_TUN_ipv6policy2regex (policy);
112 if (0 != strcmp (regex, r))
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 "Expected: `%s' but got: `%s'\n",
125 main (int argc, char *argv[])
130 GNUNET_log_setup ("test-regex", "WARNING", NULL);
133 /* this is just a performance test ... */
134 r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
135 GNUNET_break (NULL != r);
139 test_iptoregex ("192.1.2.3", 2086,
142 "6-1F90-FFFF0000000000000000000000000001");
144 test_iptoregex ("187.238.255.0", 80,
146 "E1E1:73F9:51BE::0", 49,
147 "6-0031-E1E173F951BE00000000000000000000");
149 test_policy4toregex ("192.1.2.0/24:80;",
150 "4-0050-C00102" DOT DOT);
152 test_policy4toregex ("192.1.0.0/16;",
153 "4-" DOT DOT DOT DOT "-C001" DOT DOT DOT DOT);
155 test_policy4toregex ("192.1.0.0/16:80-81;",
156 "4-(0050|0051)-C001" DOT DOT DOT DOT);
158 test_policy4toregex ("192.1.0.0/8:!3-65535;",
159 "4-000(0|1|2)-C0" DOT DOT DOT DOT DOT DOT);
161 test_policy4toregex ("192.1.0.0/8:!25-56;",
162 "4-(0(0(0"DOT"|1(0|1|2|3|4|5|6|7|8)|3(9|A|B|C|D|E|F)|(4|5|6|7|8|9|A|B|C|D|E|F)"DOT")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT")|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"DOT DOT DOT")-C0"DOT DOT DOT DOT DOT DOT);
164 test_policy6toregex ("E1E1::1;",
165 "6-"DOT DOT DOT DOT"-E1E10000000000000000000000000001");
167 test_policy6toregex ("E1E1:ABCD::1/120;",
168 "6-"DOT DOT DOT DOT"-E1E1ABCD0000000000000000000000" DOT DOT);
170 test_policy6toregex ("E1E1:ABCD::ABCD/126;",
171 "6-"DOT DOT DOT DOT"-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
173 test_policy6toregex ("E1E1:ABCD::ABCD/127;",
174 "6-"DOT DOT DOT DOT"-E1E1ABCD00000000000000000000ABC(C|D)");
176 test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
177 "6-0050-E1E1ABCD00000000000000000000ABCD");