99287243ef8a8866e0b8e0ca6eb9f43dcfe6f69a
[oweals/gnunet.git] / src / regex / test_regex_integration.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2015 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 regex/test_regex_integration.c
20  * @brief base test case for regex integration with VPN;
21  *        tests that the regexes generated by the TUN API
22  *        for IP addresses work (for some simple cases)
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_applications.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_tun_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_regex_service.h"
31
32
33 /**
34  * How long until we really give up on a particular testcase portion?
35  */
36 #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
37
38 /**
39  * How long until we give up on any particular operation (and retry)?
40  */
41 #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
42
43
44 static struct GNUNET_REGEX_Announcement *a4;
45
46 static struct GNUNET_REGEX_Search *s4;
47
48 static struct GNUNET_REGEX_Announcement *a6;
49
50 static struct GNUNET_REGEX_Search *s6;
51
52 static int ok = 1;
53
54 static struct GNUNET_SCHEDULER_Task *die_task;
55
56
57 static void
58 end (void *cls)
59 {
60   die_task = NULL;
61   GNUNET_REGEX_announce_cancel (a4);
62   a4 = NULL;
63   GNUNET_REGEX_search_cancel (s4);
64   s4 = NULL;
65   GNUNET_REGEX_announce_cancel (a6);
66   a6 = NULL;
67   GNUNET_REGEX_search_cancel (s6);
68   s6 = NULL;
69   ok = 0;
70 }
71
72
73 static void
74 end_badly ()
75 {
76   FPRINTF (stderr, "%s",  "Testcase failed (timeout).\n");
77   end (NULL);
78   ok = 1;
79 }
80
81
82 /**
83  * Search callback function, invoked for every result that was found.
84  *
85  * @param cls Closure provided in #GNUNET_REGEX_search().
86  * @param id Peer providing a regex that matches the string.
87  * @param get_path Path of the get request.
88  * @param get_path_length Length of @a get_path.
89  * @param put_path Path of the put request.
90  * @param put_path_length Length of the @a put_path.
91  */
92 static void
93 found_cb (void *cls,
94           const struct GNUNET_PeerIdentity *id,
95           const struct GNUNET_PeerIdentity *get_path,
96           unsigned int get_path_length,
97           const struct GNUNET_PeerIdentity *put_path,
98           unsigned int put_path_length)
99 {
100   const char *str = cls;
101   static int found;
102
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104               "IPv%s-exit found\n",
105               str);
106   if (0 == strcmp (str, "4"))
107     found |= 4;
108   if (0 == strcmp (str, "6"))
109     found |= 2;
110   if ((4|2) == found)
111   {
112     GNUNET_SCHEDULER_cancel (die_task);
113     die_task =
114     GNUNET_SCHEDULER_add_now (&end, NULL);
115   }
116 }
117
118
119 static void
120 run (void *cls,
121      const struct GNUNET_CONFIGURATION_Handle *cfg,
122      struct GNUNET_TESTING_Peer *peer)
123 {
124   char rxstr4[GNUNET_TUN_IPV4_REGEXLEN];
125   char rxstr6[GNUNET_TUN_IPV6_REGEXLEN];
126   char *p4r;
127   char *p6r;
128   char *p4;
129   char *p6;
130   char *ss4;
131   char *ss6;
132   struct in_addr i4;
133   struct in6_addr i6;
134
135   die_task =
136     GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
137                                   &end_badly, NULL);
138   GNUNET_assert (1 ==
139                  inet_pton (AF_INET,
140                             "127.0.0.1",
141                             &i4));
142   GNUNET_assert (1 ==
143                  inet_pton (AF_INET6,
144                             "::1:5",
145                             &i6));
146   GNUNET_TUN_ipv4toregexsearch (&i4,
147                                 8080,
148                                 rxstr4);
149   GNUNET_TUN_ipv6toregexsearch (&i6,
150                                 8686,
151                                 rxstr6);
152   GNUNET_asprintf (&ss4,
153                    "%s%s",
154                    GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
155                    rxstr4);
156   GNUNET_asprintf (&ss6,
157                    "%s%s",
158                    GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
159                    rxstr6);
160   p4r = GNUNET_TUN_ipv4policy2regex ("0.0.0.0/0:!25;");
161   p6r = GNUNET_TUN_ipv6policy2regex ("::/0:!25;");
162   GNUNET_asprintf (&p4,
163                    "%s%s",
164                    GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
165                    p4r);
166   GNUNET_asprintf (&p6,
167                    "%s%s",
168                    GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
169                    p6r);
170   GNUNET_free (p4r);
171   GNUNET_free (p6r);
172   a4 = GNUNET_REGEX_announce (cfg,
173                               p4,
174                               GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
175                                                              5),
176                              1);
177   a6 = GNUNET_REGEX_announce (cfg,
178                               p6,
179                               GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
180                                                              5),
181                               1);
182   GNUNET_free (p4);
183   GNUNET_free (p6);
184
185   s4 = GNUNET_REGEX_search (cfg,
186                             ss4,
187                             &found_cb, "4");
188   s6 = GNUNET_REGEX_search (cfg,
189                             ss6,
190                             &found_cb, "6");
191   GNUNET_free (ss4);
192   GNUNET_free (ss6);
193 }
194
195
196 int
197 main (int argc, char *argv[])
198 {
199   if (0 != GNUNET_TESTING_peer_run ("test-regex-integration",
200                                     "test_regex_api_data.conf",
201                                     &run, NULL))
202     return 1;
203   return ok;
204 }
205
206 /* end of test_regex_integration.c */