- fix error messages
[oweals/gnunet.git] / src / transport / test_transport_blacklisting.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 2011 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 /**
22  * @file transport/transport_api_blacklisting.c
23  * @brief test for the blacklisting API
24  *              stage 0: init
25  *              stage 1: connect peers and stop
26  *              stage 2: blacklist whole peer and connect
27  *              stage 3: blacklist tcp and try connect
28  *
29  * @author Matthias Wachs
30  *
31  */
32 #include "platform.h"
33 #include "gnunet_transport_service.h"
34 #include "transport-testing.h"
35
36 char *test_name;
37
38 struct PeerContext *p1;
39
40 struct PeerContext *p2;
41
42 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
43
44 struct GNUNET_TRANSPORT_TESTING_handle *tth;
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
50
51 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
52
53
54 static int stage;
55 static int ok;
56 static int connected;
57
58 static GNUNET_SCHEDULER_TaskIdentifier die_task;
59
60 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
61
62 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
63
64 #if VERBOSE
65 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
66 #else
67 #define OKPP do { ok++; } while (0)
68 #endif
69
70 static void
71 run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
72
73 static void
74 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
75 {
76   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
77
78   if (die_task != GNUNET_SCHEDULER_NO_TASK)
79   {
80     GNUNET_SCHEDULER_cancel (die_task);
81     die_task = GNUNET_SCHEDULER_NO_TASK;
82   }
83
84   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
85   {
86     GNUNET_SCHEDULER_cancel (timeout_task);
87     timeout_task = GNUNET_SCHEDULER_NO_TASK;
88   }
89
90   if (stage_task != GNUNET_SCHEDULER_NO_TASK)
91   {
92     GNUNET_SCHEDULER_cancel (stage_task);
93     stage_task = GNUNET_SCHEDULER_NO_TASK;
94   }
95
96   if (cc != NULL)
97   {
98     GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
99     cc = NULL;
100   }
101
102   if (p1 != NULL)
103   {
104     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
105     p1 = NULL;
106   }
107   if (p2 != NULL)
108   {
109     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
110     p2 = NULL;
111   }
112 }
113
114 static void
115 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   die_task = GNUNET_SCHEDULER_NO_TASK;
118
119   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
120   {
121     GNUNET_SCHEDULER_cancel (timeout_task);
122     timeout_task = GNUNET_SCHEDULER_NO_TASK;
123   }
124
125   if (stage_task != GNUNET_SCHEDULER_NO_TASK)
126   {
127     GNUNET_SCHEDULER_cancel (stage_task);
128     stage_task = GNUNET_SCHEDULER_NO_TASK;
129   }
130
131
132   if (cc != NULL)
133   {
134     GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
135     cc = NULL;
136   }
137   if (p1 != NULL)
138     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
139   if (p2 != NULL)
140     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
141
142   ok = GNUNET_SYSERR;
143 }
144
145 static void
146 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
147 {
148   cc = NULL;
149   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
150
151   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
152               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
153   GNUNET_free (p1_c);
154   connected = GNUNET_YES;
155   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
156 }
157
158 static void
159 connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers not connected, next stage\n");
162         timeout_task = GNUNET_SCHEDULER_NO_TASK;
163   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
164 }
165
166 static int started;
167
168 void
169 start_cb (struct PeerContext *p, void *cls)
170 {
171
172   started++;
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
174               GNUNET_i2s (&p->id));
175
176   if (started != 2)
177     return;
178
179   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
180
181   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
182               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
183               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
184   GNUNET_free (sender_c);
185
186   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
187                                                NULL);
188
189 }
190
191 static void
192 run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 {
194         stage_task = GNUNET_SCHEDULER_NO_TASK;
195         if (GNUNET_SCHEDULER_NO_TASK != die_task)
196                 GNUNET_SCHEDULER_cancel (die_task);
197   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
198   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running stage %u\n", stage);
199
200         if (0 == stage)
201         {
202                   started = GNUNET_NO;
203                   connected = GNUNET_NO;
204                         if (0 == strcmp(test_name, "test_transport_blacklisting_no_bl"))
205                         {
206                                         /* Try to connect peers successfully */
207                                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
208                                                             NULL, NULL, NULL, &start_cb, NULL);
209
210                                         p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
211                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
212                         }
213                         else if (0 == strcmp(test_name, "test_transport_blacklisting_outbound_bl_full"))
214                         {
215                                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1_full.conf", 1,
216                                                             NULL, NULL, NULL, &start_cb, NULL);
217
218                                         p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
219                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
220                         }
221                         else if (0 == strcmp(test_name, "test_transport_blacklisting_outbound_bl_plugin"))
222                         {
223                                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1_plugin.conf", 1,
224                                                             NULL, NULL, NULL, &start_cb, NULL);
225
226                                         p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
227                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
228                         }
229                         else if (0 == strcmp(test_name, "test_transport_blacklisting_inbound_bl_full"))
230                         {
231                                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
232                                                             NULL, NULL, NULL, &start_cb, NULL);
233
234                                         p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2_full.conf", 2,
235                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
236                         }
237                         else if (0 == strcmp(test_name, "test_transport_blacklisting_inbound_bl_plugin"))
238                         {
239                                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
240                                                             NULL, NULL, NULL, &start_cb, NULL);
241
242                                         p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2_plugin.conf", 2,
243                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
244                         }
245                         else if (0 == strcmp(test_name, "test_transport_blacklisting_multiple_plugins"))
246                         {
247                                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1_multiple_plugins.conf", 1,
248                                                             NULL, NULL, NULL, &start_cb, NULL);
249
250                                         p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2_multiple_plugins.conf", 2,
251                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
252                         }
253
254
255                         if ((NULL == p1) || (NULL == p2))
256                         {
257                                 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start peers\n");
258                                 ok = 1;
259                           GNUNET_SCHEDULER_add_now (&end, NULL);
260                         }
261
262                         timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT, &connect_timeout, NULL);
263                   stage ++;
264                   return;
265         }
266
267
268   if (cc != NULL)
269   {
270     GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
271     cc = NULL;
272   }
273
274   if (p1 != NULL)
275   {
276     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
277     p1 = NULL;
278   }
279   if (p2 != NULL)
280   {
281     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
282     p2 = NULL;
283   }
284
285   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Done in stage %u: Peers %s and %s!\n", stage,
286                 (GNUNET_NO == started) ? "NOT STARTED" : "STARTED",
287                 (GNUNET_YES == connected) ? "CONNECTED" : "NOT CONNECTED");
288
289         if ((0 == strcmp(test_name, "test_transport_blacklisting_no_bl")) ||
290                         (0 == strcmp(test_name, "test_transport_blacklisting_multiple_plugins")))
291         {
292                 if ((GNUNET_NO != started) && (GNUNET_YES == connected))
293                         ok = 0;
294                 else
295                 {
296                         GNUNET_break (0);
297                         ok = 1;
298                 }
299         }
300         else
301         {
302                         if ((GNUNET_NO != started) && (GNUNET_YES != connected))
303                                 ok = 0;
304                         else
305                         {
306                                 ok = 1;
307                         }
308         }
309   GNUNET_SCHEDULER_add_now (&end, NULL);
310 }
311
312 static void
313 run (void *cls, char *const *args, const char *cfgfile,
314      const struct GNUNET_CONFIGURATION_Handle *cfg)
315 {
316   connected = GNUNET_NO;
317   stage = 0;
318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running test `%s'!\n", test_name);
319   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
320 }
321
322
323 int
324 main (int argc, char *argv0[])
325 {
326   ok = 1;
327
328   GNUNET_TRANSPORT_TESTING_get_test_name (argv0[0], &test_name);
329
330   GNUNET_log_setup ("test-transport-api-blacklisting",
331                     "WARNING",
332                     NULL);
333
334   static char *const argv[] = { "date",
335     "-c",
336     "test_transport_api_data.conf",
337     NULL
338   };
339   static struct GNUNET_GETOPT_CommandLineOption options[] = {
340     GNUNET_GETOPT_OPTION_END
341   };
342
343   tth = GNUNET_TRANSPORT_TESTING_init ();
344
345   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
346                                                                                 "test-transport-api-blacklisting",
347                       "nohelp", options, &run, NULL);
348
349
350   GNUNET_TRANSPORT_TESTING_done (tth);
351
352   return ok;
353 }
354
355 /* end of transport_api_blacklisting.c */