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