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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file tun/test_regex.c
22 * @brief simple test for regex.c iptoregex functions
23 * @author Maximilian Szengel
26 #include "gnunet_util_lib.h"
29 * 'wildcard', matches all possible values (for HEX encoding).
31 #define DOT "(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"
35 test_iptoregex (const char *ipv4,
37 const char *expectedv4,
40 const char *expectedv6)
46 char rxv4[GNUNET_TUN_IPV4_REGEXLEN];
47 char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
49 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
50 GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
52 if (0 != strcmp (rxv4, expectedv4))
54 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
55 "Expected: %s but got: %s\n",
61 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
62 GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
63 if (0 != strcmp (rxv6, expectedv6))
65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
66 "Expected: %s but got: %s\n",
75 test_policy4toregex (const char *policy,
82 r = GNUNET_TUN_ipv4policy2regex (policy);
88 if (0 != strcmp (regex, r))
90 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
91 "Expected: `%s' but got: `%s'\n",
101 test_policy6toregex (const char *policy,
108 r = GNUNET_TUN_ipv6policy2regex (policy);
114 if (0 != strcmp (regex, r))
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
117 "Expected: `%s' but got: `%s'\n",
127 main (int argc, char *argv[])
132 GNUNET_log_setup ("test-regex", "WARNING", NULL);
135 /* this is just a performance test ... */
136 r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
137 GNUNET_break (NULL != r);
141 test_iptoregex ("192.1.2.3", 2086,
144 "6-1F90-FFFF0000000000000000000000000001");
146 test_iptoregex ("187.238.255.0", 80,
148 "E1E1:73F9:51BE::0", 49,
149 "6-0031-E1E173F951BE00000000000000000000");
151 test_policy4toregex ("192.1.2.0/24:80;",
152 "4-0050-C00102" DOT DOT);
154 test_policy4toregex ("192.1.0.0/16;",
155 "4-" DOT DOT DOT DOT "-C001" DOT DOT DOT DOT);
157 test_policy4toregex ("192.1.0.0/16:80-81;",
158 "4-(0050|0051)-C001" DOT DOT DOT DOT);
160 test_policy4toregex ("192.1.0.0/8:!3-65535;",
161 "4-000(0|1|2)-C0" DOT DOT DOT DOT DOT DOT);
163 test_policy4toregex ("192.1.0.0/8:!25-56;",
164 "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);
166 test_policy6toregex ("E1E1::1;",
167 "6-"DOT DOT DOT DOT"-E1E10000000000000000000000000001");
169 test_policy6toregex ("E1E1:ABCD::1/120;",
170 "6-"DOT DOT DOT DOT"-E1E1ABCD0000000000000000000000" DOT DOT);
172 test_policy6toregex ("E1E1:ABCD::ABCD/126;",
173 "6-"DOT DOT DOT DOT"-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
175 test_policy6toregex ("E1E1:ABCD::ABCD/127;",
176 "6-"DOT DOT DOT DOT"-E1E1ABCD00000000000000000000ABC(C|D)");
178 test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
179 "6-0050-E1E1ABCD00000000000000000000ABCD");