Support normal socket (non-NSP) name lookups in resolver (for testing)
[oweals/gnunet.git] / src / tun / test_regex.c
1 /*
2      This file is part of GNUnet
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
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_tun_lib.h"
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..");
148   error +=
149     test_policy4toregex ("192.1.0.0/16;",
150                          "4-....-C001....");
151   error +=
152     test_policy4toregex ("192.1.0.0/16:80-81;",
153                          "4-(0050|0051)-C001....");
154   error +=
155     test_policy4toregex ("192.1.0.0/8:!3-65535;",
156                          "4-(0001|0002)-C0......");
157   error +=
158     test_policy6toregex ("E1E1::1;",
159                          "6-....-E1E10000000000000000000000000001");
160   error +=
161     test_policy6toregex ("E1E1:ABCD::1/120;",
162                          "6-....-E1E1ABCD0000000000000000000000..");
163   error +=
164     test_policy6toregex ("E1E1:ABCD::ABCD/126;",
165                          "6-....-E1E1ABCD00000000000000000000ABC(C|D|E|F)");
166   error +=
167     test_policy6toregex ("E1E1:ABCD::ABCD/127;",
168                          "6-....-E1E1ABCD00000000000000000000ABC(C|D)");
169   error +=
170     test_policy6toregex ("E1E1:ABCD::ABCD/128:80;",
171                          "6-0050-E1E1ABCD00000000000000000000ABCD");
172   return error;
173 }