2 This file is part of GNUnet
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file tun/test_regex.c
22 * @brief simple test for regex.c iptoregex functions
23 * @author Maximilian Szengel
26 #include "gnunet_tun_lib.h"
30 test_iptoregex (const char *ipv4,
32 const char *expectedv4,
35 const char *expectedv6)
41 char rxv4[GNUNET_TUN_IPV4_REGEXLEN];
42 char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
44 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
45 GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
47 if (0 != strcmp (rxv4, expectedv4))
49 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
50 "Expected: %s but got: %s\n",
56 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
57 GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
58 if (0 != strcmp (rxv6, expectedv6))
60 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
61 "Expected: %s but got: %s\n",
70 test_policy4toregex (const char *policy,
77 r = GNUNET_TUN_ipv4policy2regex (policy);
83 if (0 != strcmp (regex, r))
85 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
86 "Expected: `%s' but got: `%s'\n",
96 test_policy6toregex (const char *policy,
103 r = GNUNET_TUN_ipv6policy2regex (policy);
109 if (0 != strcmp (regex, r))
111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
112 "Expected: `%s' but got: `%s'\n",
122 main (int argc, char *argv[])
127 GNUNET_log_setup ("test-regex", "WARNING", NULL);
130 /* this is just a performance test ... */
131 r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
132 GNUNET_break (NULL != r);
136 test_iptoregex ("192.1.2.3", 2086,
139 "6-1F90-FFFF0000000000000000000000000001");
141 test_iptoregex ("187.238.255.0", 80,
143 "E1E1:73F9:51BE::0", 49,
144 "6-0031-E1E173F951BE00000000000000000000");
146 test_policy4toregex ("192.1.2.0/24:80;",
149 test_policy4toregex ("192.1.0.0/16;",
152 test_policy4toregex ("192.1.0.0/16:80-81;",
153 "4-(0050|0051)-C001....");
155 test_policy4toregex ("192.1.0.0/8:!3-65535;",
156 "4-000(0|1|2)-C0......");
158 test_policy4toregex ("192.1.0.0/8:!25-56;",
159 "4-(0(0(0.|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).)|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)..)|(1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)...)-C0......");
161 test_policy6toregex ("E1E1::1;",
162 "6-....-E1E10000000000000000000000000001");
164 test_policy6toregex ("E1E1:ABCD::1/120;",
165 "6-....-E1E1ABCD0000000000000000000000..");
167 test_policy6toregex ("E1E1:ABCD::ABCD/126;",
168 "6-....-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
170 test_policy6toregex ("E1E1:ABCD::ABCD/127;",
171 "6-....-E1E1ABCD00000000000000000000ABC(C|D)");
173 test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
174 "6-0050-E1E1ABCD00000000000000000000ABCD");