aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_regex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_regex.c')
-rw-r--r--src/util/test_regex.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/src/util/test_regex.c b/src/util/test_regex.c
new file mode 100644
index 000000000..2e7d52828
--- /dev/null
+++ b/src/util/test_regex.c
@@ -0,0 +1,179 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2012, 2013 GNUnet e.V.
4
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.
9
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.
14
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/>.
17*/
18/**
19 * @file tun/test_regex.c
20 * @brief simple test for regex.c iptoregex functions
21 * @author Maximilian Szengel
22 */
23#include "platform.h"
24#include "gnunet_tun_lib.h"
25
26/**
27 * 'wildcard', matches all possible values (for HEX encoding).
28 */
29#define DOT "(0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F)"
30
31
32static int
33test_iptoregex (const char *ipv4,
34 uint16_t port,
35 const char *expectedv4,
36 const char *ipv6,
37 uint16_t port6,
38 const char *expectedv6)
39{
40 int error = 0;
41
42 struct in_addr a;
43 struct in6_addr b;
44 char rxv4[GNUNET_TUN_IPV4_REGEXLEN];
45 char rxv6[GNUNET_TUN_IPV6_REGEXLEN];
46
47 GNUNET_assert (1 == inet_pton (AF_INET, ipv4, &a));
48 GNUNET_TUN_ipv4toregexsearch (&a, port, rxv4);
49
50 if (0 != strcmp (rxv4, expectedv4))
51 {
52 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
53 "Expected: %s but got: %s\n",
54 expectedv4,
55 rxv4);
56 error++;
57 }
58
59 GNUNET_assert (1 == inet_pton (AF_INET6, ipv6, &b));
60 GNUNET_TUN_ipv6toregexsearch (&b, port6, rxv6);
61 if (0 != strcmp (rxv6, expectedv6))
62 {
63 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
64 "Expected: %s but got: %s\n",
65 expectedv6, rxv6);
66 error++;
67 }
68 return error;
69}
70
71
72static int
73test_policy4toregex (const char *policy,
74 const char *regex)
75{
76 char *r;
77 int ret;
78
79 ret = 0;
80 r = GNUNET_TUN_ipv4policy2regex (policy);
81 if (NULL == r)
82 {
83 GNUNET_break (0);
84 return 1;
85 }
86 if (0 != strcmp (regex, r))
87 {
88 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89 "Expected: `%s' but got: `%s'\n",
90 regex, r);
91 ret = 2;
92 }
93 GNUNET_free (r);
94 return ret;
95}
96
97
98static int
99test_policy6toregex (const char *policy,
100 const char *regex)
101{
102 char *r;
103 int ret;
104
105 ret = 0;
106 r = GNUNET_TUN_ipv6policy2regex (policy);
107 if (NULL == r)
108 {
109 GNUNET_break (0);
110 return 1;
111 }
112 if (0 != strcmp (regex, r))
113 {
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
115 "Expected: `%s' but got: `%s'\n",
116 regex, r);
117 ret = 2;
118 }
119 GNUNET_free (r);
120 return ret;
121}
122
123
124int
125main (int argc, char *argv[])
126{
127 int error;
128 char *r;
129
130 GNUNET_log_setup ("test-regex", "WARNING", NULL);
131 error = 0;
132
133 /* this is just a performance test ... */
134 r = GNUNET_TUN_ipv4policy2regex ("1.2.3.4/16:!25;");
135 GNUNET_break (NULL != r);
136 GNUNET_free (r);
137
138 error +=
139 test_iptoregex ("192.1.2.3", 2086,
140 "4-0826-C0010203",
141 "FFFF::1", 8080,
142 "6-1F90-FFFF0000000000000000000000000001");
143 error +=
144 test_iptoregex ("187.238.255.0", 80,
145 "4-0050-BBEEFF00",
146 "E1E1:73F9:51BE::0", 49,
147 "6-0031-E1E173F951BE00000000000000000000");
148 error +=
149 test_policy4toregex ("192.1.2.0/24:80;",
150 "4-0050-C00102" DOT DOT);
151 error +=
152 test_policy4toregex ("192.1.0.0/16;",
153 "4-" DOT DOT DOT DOT "-C001" DOT DOT DOT DOT);
154 error +=
155 test_policy4toregex ("192.1.0.0/16:80-81;",
156 "4-(0050|0051)-C001" DOT DOT DOT DOT);
157 error +=
158 test_policy4toregex ("192.1.0.0/8:!3-65535;",
159 "4-000(0|1|2)-C0" DOT DOT DOT DOT DOT DOT);
160 error +=
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);
163 error +=
164 test_policy6toregex ("E1E1::1;",
165 "6-"DOT DOT DOT DOT"-E1E10000000000000000000000000001");
166 error +=
167 test_policy6toregex ("E1E1:ABCD::1/120;",
168 "6-"DOT DOT DOT DOT"-E1E1ABCD0000000000000000000000" DOT DOT);
169 error +=
170 test_policy6toregex ("E1E1:ABCD::ABCD/126;",
171 "6-"DOT DOT DOT DOT"-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
172 error +=
173 test_policy6toregex ("E1E1:ABCD::ABCD/127;",
174 "6-"DOT DOT DOT DOT"-E1E1ABCD00000000000000000000ABC(C|D)");
175 error +=
176 test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
177 "6-0050-E1E1ABCD00000000000000000000ABCD");
178 return error;
179}