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