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