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