fix warnings
[oweals/gnunet.git] / src / transport / test_transport_blacklisting.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2011 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 /**
22  * @file transport/transport_api_blacklisting.c
23  * @brief test for the blacklisting with blacklistings defined in cfg
24  *
25  * this file contains multiple tests:
26  *
27  * test_transport_blacklisting_no_bl:
28  *      no blacklisting entries
29  *      peers are expected to connect
30  * test_transport_blacklisting_outbound_bl_full:
31  *      both peers contain bl entries for full peer
32  *      test is expected to not connect
33  * test_transport_blacklisting_outbound_bl_plugin:
34  *      both peers contain bl entries for plugin
35  *      test is expected to not connect
36  * test_transport_blacklisting_inbound_bl_plugin:
37  *      peer 1 contains no bl entries
38  *      peer 2 contain bl entries for full peer
39  *      test is expected to not connect
40  * test_transport_blacklisting_inbound_bl_full:
41  *      peer 1 contains no bl entries
42  *      peer 2 contain bl entries for plugin
43  *      test is expected to not connect
44  * test_transport_blacklisting_multiple_plugins:
45  *      both peers contain bl entries for plugin
46  *      test is expected to  connect with not bl'ed plugin
47  *
48  * @author Matthias Wachs
49  *
50  */
51 #include "platform.h"
52 #include "gnunet_transport_service.h"
53 #include "transport-testing.h"
54
55 char *test_name;
56
57 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
58
59 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
60
61 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc;
62
63 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
64
65 /**
66  * How long until we give up on transmitting the message?
67  */
68 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
69
70 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
71                                                        10)
72
73 static int stage;
74 static int ok;
75 static int connected;
76
77 static struct GNUNET_SCHEDULER_Task *die_task;
78
79 static struct GNUNET_SCHEDULER_Task *timeout_task;
80
81 static struct GNUNET_SCHEDULER_Task *stage_task;
82
83 #if VERBOSE
84 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, \
85                                  __FILE__, __LINE__); } while (0)
86 #else
87 #define OKPP do { ok++; } while (0)
88 #endif
89
90
91 static void
92 run_stage (void *cls);
93
94
95 static void
96 end (void *cls)
97 {
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
99
100   if (die_task != NULL)
101   {
102     GNUNET_SCHEDULER_cancel (die_task);
103     die_task = NULL;
104   }
105
106   if (timeout_task != NULL)
107   {
108     GNUNET_SCHEDULER_cancel (timeout_task);
109     timeout_task = NULL;
110   }
111
112   if (stage_task != NULL)
113   {
114     GNUNET_SCHEDULER_cancel (stage_task);
115     stage_task = NULL;
116   }
117
118   if (cc != NULL)
119   {
120     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
121     cc = NULL;
122   }
123
124   if (p1 != NULL)
125   {
126     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
127     p1 = NULL;
128   }
129   if (p2 != NULL)
130   {
131     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
132     p2 = NULL;
133   }
134 }
135
136
137 static void
138 end_badly (void *cls)
139 {
140   die_task = NULL;
141
142   if (timeout_task != NULL)
143   {
144     GNUNET_SCHEDULER_cancel (timeout_task);
145     timeout_task = NULL;
146   }
147
148   if (stage_task != NULL)
149   {
150     GNUNET_SCHEDULER_cancel (stage_task);
151     stage_task = NULL;
152   }
153
154   if (cc != NULL)
155   {
156     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
157     cc = NULL;
158   }
159   if (p1 != NULL)
160     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
161   if (p2 != NULL)
162     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
163
164   ok = GNUNET_SYSERR;
165 }
166
167
168 static void
169 testing_connect_cb (void *cls)
170 {
171   cc = NULL;
172   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
173
174   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
175               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
176   GNUNET_free (p1_c);
177   connected = GNUNET_YES;
178   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
179 }
180
181
182 static void
183 connect_timeout (void *cls)
184 {
185   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186               "Peers not connected, next stage\n");
187   timeout_task = NULL;
188   stage_task = GNUNET_SCHEDULER_add_now (&run_stage,
189                                          NULL);
190 }
191
192
193 static int started;
194
195
196 static void
197 start_cb (void *cls)
198 {
199   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
200
201   started++;
202   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
203               "Peer %u (`%s') started\n",
204               p->no,
205               GNUNET_i2s_full (&p->id));
206
207   if (started != 2)
208     return;
209
210   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
211
212   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
214               p1->no,
215               sender_c,
216               p2->no,
217               GNUNET_i2s (&p2->id));
218   GNUNET_free (sender_c);
219
220   cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
221                                                p2,
222                                                &testing_connect_cb,
223                                                NULL);
224 }
225
226
227 static int
228 check_blacklist_config (const char *cfg_file,
229                         struct GNUNET_PeerIdentity *peer,
230                         struct GNUNET_PeerIdentity *bl_peer)
231 {
232   struct GNUNET_CONFIGURATION_Handle *cfg;
233   char *section;
234   char *peer_str;
235
236   cfg = GNUNET_CONFIGURATION_create ();
237   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfg_file))
238   {
239     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load configuration `%s'\n",
240                 cfg_file);
241     GNUNET_CONFIGURATION_destroy (cfg);
242     return GNUNET_SYSERR;
243   }
244
245   peer_str = GNUNET_strdup (GNUNET_i2s_full (peer));
246   GNUNET_asprintf (&section, "transport-blacklist-%s", peer_str);
247
248   if (GNUNET_NO == GNUNET_CONFIGURATION_have_value (cfg, section,
249                                                     GNUNET_i2s_full (bl_peer)))
250   {
251     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252                 "Configuration `%s' does not have blacklisting section for peer `%s' blacklisting `%s'\n",
253                 cfg_file, peer_str, GNUNET_i2s_full (bl_peer));
254     GNUNET_CONFIGURATION_destroy (cfg);
255     GNUNET_free (section);
256     GNUNET_free (peer_str);
257     return GNUNET_SYSERR;
258   }
259
260   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
261               "Configuration `%s' does have blacklisting section for peer `%s' blacklisting `%s'\n",
262               cfg_file, peer_str, GNUNET_i2s_full (bl_peer));
263
264   GNUNET_CONFIGURATION_destroy (cfg);
265   GNUNET_free (section);
266   GNUNET_free (peer_str);
267   return GNUNET_OK;
268 }
269
270
271 static void
272 run_stage (void *cls)
273 {
274   stage_task = NULL;
275   if (NULL != die_task)
276     GNUNET_SCHEDULER_cancel (die_task);
277   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
278   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
279
280   if (0 == stage)
281   {
282     started = GNUNET_NO;
283     connected = GNUNET_NO;
284     if (0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
285     {
286       /* Try to connect peers successfully */
287       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
288                                                 "test_transport_blacklisting_cfg_peer1.conf",
289                                                 1,
290                                                 NULL,
291                                                 NULL,
292                                                 NULL,
293                                                 NULL,
294                                                 &start_cb,
295                                                 NULL);
296
297       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
298                                                 "test_transport_blacklisting_cfg_peer2.conf",
299                                                 2,
300                                                 NULL,
301                                                 NULL,
302                                                 NULL,
303                                                 NULL,
304                                                 &start_cb,
305                                                 NULL);
306     }
307     else if (0 == strcmp (test_name,
308                           "test_transport_blacklisting_outbound_bl_full"))
309     {
310       const char *cfg_p1 =
311         "test_transport_blacklisting_cfg_blp_peer1_full.conf";
312       const char *cfg_p2 =
313         "test_transport_blacklisting_cfg_blp_peer2_full.conf";
314
315       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
316                                                 cfg_p1,
317                                                 1, NULL, NULL, NULL,
318                                                 NULL,
319                                                 &start_cb, NULL);
320       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
321                                                 cfg_p2, 2,
322                                                 NULL, NULL, NULL,
323                                                 NULL,
324                                                 &start_cb, NULL);
325
326       /* check if configuration contain correct blacklist entries */
327       if ((GNUNET_SYSERR ==
328            check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
329           (GNUNET_SYSERR ==
330            check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
331       {
332         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
333         p1 = NULL;
334         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
335         p2 = NULL;
336         ok = 1;
337         GNUNET_SCHEDULER_add_now (&end, NULL);
338       }
339     }
340     else if (0
341              == strcmp (test_name,
342                         "test_transport_blacklisting_outbound_bl_plugin"))
343     {
344       const char *cfg_p1 =
345         "test_transport_blacklisting_cfg_blp_peer1_plugin.conf";
346       const char *cfg_p2 =
347         "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
348
349       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
350                                                 cfg_p1,
351                                                 1,
352                                                 NULL,
353                                                 NULL,
354                                                 NULL,
355                                                 NULL,
356                                                 &start_cb,
357                                                 NULL);
358
359       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
360                                                 cfg_p2, 2,
361                                                 NULL,
362                                                 NULL,
363                                                 NULL,
364                                                 NULL,
365                                                 &start_cb,
366                                                 NULL);
367
368       /* check if configuration contain correct blacklist entries */
369       if ((GNUNET_SYSERR ==
370            check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
371           (GNUNET_SYSERR ==
372            check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
373       {
374         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
375         p1 = NULL;
376         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
377         p2 = NULL;
378         ok = 1;
379         GNUNET_SCHEDULER_add_now (&end, NULL);
380       }
381     }
382     else if (0 == strcmp (test_name,
383                           "test_transport_blacklisting_inbound_bl_full"))
384     {
385       const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
386       const char *cfg_p2 =
387         "test_transport_blacklisting_cfg_blp_peer2_full.conf";
388
389       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
390                                                 cfg_p1, 1,
391                                                 NULL,
392                                                 NULL, NULL, NULL,
393                                                 &start_cb, NULL);
394
395       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
396                                                 cfg_p2, 2,
397                                                 NULL,
398                                                 NULL, NULL, NULL,
399                                                 &start_cb, NULL);
400
401       /* check if configuration contain correct blacklist entries */
402       if ((GNUNET_SYSERR ==
403            check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
404       {
405         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
406         p1 = NULL;
407         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
408         p2 = NULL;
409         ok = 1;
410         GNUNET_SCHEDULER_add_now (&end, NULL);
411       }
412     }
413     else if (0 == strcmp (test_name,
414                           "test_transport_blacklisting_inbound_bl_plugin"))
415     {
416       const char *cfg_p1 = "test_transport_blacklisting_cfg_peer1.conf";
417       const char *cfg_p2 =
418         "test_transport_blacklisting_cfg_blp_peer2_plugin.conf";
419
420       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
421                                                 cfg_p1, 1,
422                                                 NULL,
423                                                 NULL, NULL, NULL,
424                                                 &start_cb, NULL);
425
426       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
427                                                 cfg_p2, 2,
428                                                 NULL,
429                                                 NULL, NULL,
430                                                 NULL,
431                                                 &start_cb, NULL);
432
433       /* check if configuration contain correct blacklist entries */
434       if ((GNUNET_SYSERR ==
435            check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
436       {
437         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
438         p1 = NULL;
439         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
440         p2 = NULL;
441         ok = 1;
442         GNUNET_SCHEDULER_add_now (&end, NULL);
443       }
444     }
445     else if (0 == strcmp (test_name,
446                           "test_transport_blacklisting_multiple_plugins"))
447     {
448       const char *cfg_p1 =
449         "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf";
450       const char *cfg_p2 =
451         "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf";
452
453       p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
454                                                 cfg_p1, 1,
455                                                 NULL,
456                                                 NULL, NULL, NULL,
457                                                 &start_cb, NULL);
458
459       p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
460                                                 cfg_p2, 2,
461                                                 NULL,
462                                                 NULL, NULL, NULL,
463                                                 &start_cb, NULL);
464
465       /* check if configuration contain correct blacklist entries */
466       if ((GNUNET_SYSERR ==
467            check_blacklist_config (cfg_p1, &p1->id, &p2->id)) ||
468           (GNUNET_SYSERR ==
469            check_blacklist_config (cfg_p2, &p2->id, &p1->id)))
470       {
471         GNUNET_TRANSPORT_TESTING_stop_peer (p1);
472         p1 = NULL;
473         GNUNET_TRANSPORT_TESTING_stop_peer (p2);
474         p2 = NULL;
475         ok = 1;
476         GNUNET_SCHEDULER_add_now (&end, NULL);
477       }
478     }
479     else
480     {
481       GNUNET_break (0);
482       GNUNET_SCHEDULER_add_now (&end, NULL);
483     }
484
485     if ((NULL == p1) || (NULL == p2))
486     {
487       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
488       ok = 1;
489       GNUNET_SCHEDULER_add_now (&end, NULL);
490     }
491
492     timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT,
493                                                  &connect_timeout,
494                                                  NULL);
495     stage++;
496     return;
497   }
498
499   if (cc != NULL)
500   {
501     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
502     cc = NULL;
503   }
504
505   if (p1 != NULL)
506   {
507     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
508     p1 = NULL;
509   }
510   if (p2 != NULL)
511   {
512     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
513     p2 = NULL;
514   }
515
516   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n",
517               stage, (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
518               (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
519
520   if ((0 == strcmp (test_name, "test_transport_blacklisting_no_bl"))
521       || (0 == strcmp (test_name,
522                        "test_transport_blacklisting_multiple_plugins")))
523   {
524     if ((GNUNET_NO != started) && (GNUNET_YES == connected))
525       ok = 0;
526     else
527     {
528       GNUNET_break (0);
529       ok = 1;
530     }
531   }
532   else
533   {
534     if ((GNUNET_NO != started) && (GNUNET_YES != connected))
535       ok = 0;
536     else
537     {
538       ok = 1;
539     }
540   }
541   GNUNET_SCHEDULER_add_now (&end, NULL);
542 }
543
544
545 static void
546 run (void *cls, char *const *args, const char *cfgfile,
547      const struct GNUNET_CONFIGURATION_Handle *cfg)
548 {
549   connected = GNUNET_NO;
550   stage = 0;
551   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
552   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
553 }
554
555
556 int
557 main (int argc, char *argv0[])
558 {
559   ok = 1;
560
561   test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0]);
562
563   GNUNET_log_setup ("test-transport-api-blacklisting", "WARNING", NULL);
564
565   static char *const argv[] =
566   { "date", "-c", "test_transport_api_data.conf", NULL };
567   static struct GNUNET_GETOPT_CommandLineOption options[] =
568   { GNUNET_GETOPT_OPTION_END };
569
570   tth = GNUNET_TRANSPORT_TESTING_init ();
571
572   GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1, argv,
573                       "test-transport-api-blacklisting", "nohelp", options,
574                       &run, NULL);
575
576   GNUNET_TRANSPORT_TESTING_done (tth);
577
578   return ok;
579 }
580
581
582 /* end of transport_api_blacklisting.c */