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