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