missing test
[oweals/gnunet.git] / src / transport / test_transport_blacklisting_cfg.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 struct PeerContext *p1;
37
38 struct PeerContext *p2;
39
40 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
41
42 struct GNUNET_TRANSPORT_TESTING_handle *tth;
43
44 /**
45  * How long until we give up on transmitting the message?
46  */
47 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
48
49 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
50
51
52 static int stage;
53 static int ok;
54 static int connected;
55
56 static GNUNET_SCHEDULER_TaskIdentifier die_task;
57
58 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
59
60 static GNUNET_SCHEDULER_TaskIdentifier stage_task;
61
62 #if VERBOSE
63 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
64 #else
65 #define OKPP do { ok++; } while (0)
66 #endif
67
68 static void
69 run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
70
71 static void
72 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {
74   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
75
76   if (die_task != GNUNET_SCHEDULER_NO_TASK)
77   {
78     GNUNET_SCHEDULER_cancel (die_task);
79     die_task = GNUNET_SCHEDULER_NO_TASK;
80   }
81
82   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
83   {
84     GNUNET_SCHEDULER_cancel (timeout_task);
85     timeout_task = GNUNET_SCHEDULER_NO_TASK;
86   }
87
88   if (stage_task != GNUNET_SCHEDULER_NO_TASK)
89   {
90     GNUNET_SCHEDULER_cancel (stage_task);
91     stage_task = GNUNET_SCHEDULER_NO_TASK;
92   }
93
94   if (cc != NULL)
95   {
96     GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
97     cc = NULL;
98   }
99
100   if (p1 != NULL)
101   {
102     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
103     p1 = NULL;
104   }
105   if (p2 != NULL)
106   {
107     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
108     p2 = NULL;
109   }
110 }
111
112 static void
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 {
115   die_task = GNUNET_SCHEDULER_NO_TASK;
116
117   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
118   {
119     GNUNET_SCHEDULER_cancel (timeout_task);
120     timeout_task = GNUNET_SCHEDULER_NO_TASK;
121   }
122
123   if (stage_task != GNUNET_SCHEDULER_NO_TASK)
124   {
125     GNUNET_SCHEDULER_cancel (stage_task);
126     stage_task = GNUNET_SCHEDULER_NO_TASK;
127   }
128
129
130   if (cc != NULL)
131   {
132     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
133     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
134     cc = NULL;
135   }
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_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
152               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
153   GNUNET_free (p1_c);
154
155   if (1 == stage)
156   {
157     stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
158   }
159   else
160   {
161           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers connected, but they were blacklisted\n");
162                 stage_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
163   }
164
165 }
166
167 static void
168 connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 {
170   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers not connected, next stage\n");
171         timeout_task = GNUNET_SCHEDULER_NO_TASK;
172   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
173 }
174
175 static int started;
176
177 void
178 start_cb (struct PeerContext *p, void *cls)
179 {
180
181   started++;
182   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
183               GNUNET_i2s (&p->id));
184
185   if (started != 2)
186     return;
187
188   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
189
190   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
192               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
193   GNUNET_free (sender_c);
194
195   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
196                                                NULL);
197
198 }
199
200 static void
201 run_stage (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
202 {
203         stage_task = GNUNET_SCHEDULER_NO_TASK;
204         if (GNUNET_SCHEDULER_NO_TASK != die_task)
205                 GNUNET_SCHEDULER_cancel (die_task);
206   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
207   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Running stage %u\n", stage);
208         if (0 == stage)
209         {
210                         /* Try to connect peers successfully */
211                         started = 0;
212                         p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
213                                                             NULL, NULL, NULL, &start_cb, NULL);
214
215                   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
216                                                                                                                                                                                         NULL, NULL, NULL, &start_cb, NULL);
217                   stage ++;
218                   return;
219         }
220
221         if (1 == stage)
222         {
223                         /* Try to connect peers successfully with 2nd peer fully blacklisted*/
224                   if (p1 != NULL)
225                   {
226                     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
227                     p1 = NULL;
228                   }
229                   if (p2 != NULL)
230                   {
231                     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
232                     p2 = NULL;
233                   }
234                   started = 0;
235                   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer1.conf", 1,
236                                                                                                                                                                                                 NULL, NULL, NULL, &start_cb, NULL);
237
238                   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer2.conf", 2,
239                                                                                                                                                                                                 NULL, NULL, NULL, &start_cb, NULL);
240
241                   timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT, &connect_timeout, NULL);
242                   stage ++;
243                   return;
244         }
245         if (2 == stage)
246         {
247                         /* Try to connect peers successfully with 1st peer blacklisted on tcp */
248                   if (p1 != NULL)
249                   {
250                     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
251                     p1 = NULL;
252                   }
253                   if (p2 != NULL)
254                   {
255                     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
256                     p2 = NULL;
257                   }
258                   started = 0;
259                   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_blp_peer1.conf", 1,
260                                                                                                                                                                                                 NULL, NULL, NULL, &start_cb, NULL);
261
262                   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_blacklisting_cfg_peer2.conf", 2,
263                                                                                                                                                                                                 NULL, NULL, NULL, &start_cb, NULL);
264
265                   timeout_task = GNUNET_SCHEDULER_add_delayed (CONNECT_TIMEOUT, &connect_timeout, NULL);
266                   stage ++;
267                   return;
268         }
269   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Done!\n", stage);
270
271
272         ok = 0;
273         GNUNET_SCHEDULER_add_now (&end, NULL);
274 }
275
276 static void
277 run (void *cls, char *const *args, const char *cfgfile,
278      const struct GNUNET_CONFIGURATION_Handle *cfg)
279 {
280   connected = GNUNET_NO;
281   stage = 0;
282   stage_task = GNUNET_SCHEDULER_add_now (&run_stage, NULL);
283 }
284
285
286 static int
287 check ()
288 {
289   static char *const argv[] = { "test-transport-api-blacklisting",
290     "-c",
291     "test_transport_api_data.conf",
292     NULL
293   };
294   static struct GNUNET_GETOPT_CommandLineOption options[] = {
295     GNUNET_GETOPT_OPTION_END
296   };
297
298   ok = 1;
299   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
300                       "nohelp", options, &run, &ok);
301
302   return ok;
303 }
304
305 int
306 main (int argc, char *argv[])
307 {
308   int ret;
309
310   GNUNET_log_setup ("test-transport-api-blacklisting",
311                     "WARNING",
312                     NULL);
313
314   tth = GNUNET_TRANSPORT_TESTING_init ();
315
316   ret = check ();
317
318   GNUNET_TRANSPORT_TESTING_done (tth);
319
320   return ret;
321 }
322
323 /* end of transport_api_blacklisting.c */