-fix format warning
[oweals/gnunet.git] / src / transport / test_transport_api_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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file transport/transport_api_blacklisting.c
23  * @brief test for the blacklisting API
24  * @author Matthias Wachs
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
30
31 static struct PeerContext *p1;
32
33 static struct PeerContext *p2;
34
35 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
36
37 static struct GNUNET_TRANSPORT_TransmitHandle *th;
38
39 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
40
41 /**
42  * How long until we give up on transmitting the message?
43  */
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
45
46 /**
47  * How long until we give up on transmitting the message?
48  */
49 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
50
51 #define TEST_MESSAGE_SIZE 2600
52
53 #define TEST_MESSAGE_TYPE 12345
54
55
56 static int ok;
57 static int connected;
58 static int blacklist_request_p1;
59 static int blacklist_request_p2;
60
61 static struct GNUNET_TRANSPORT_Blacklist * blacklist_p1;
62
63 static struct GNUNET_TRANSPORT_Blacklist * blacklist_p2;
64
65 static struct GNUNET_SCHEDULER_Task * die_task;
66
67 static struct GNUNET_SCHEDULER_Task * send_task;
68
69 static struct GNUNET_SCHEDULER_Task * shutdown_task;
70
71 #if VERBOSE
72 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
73 #else
74 #define OKPP do { ok++; } while (0)
75 #endif
76
77
78 static void
79 end (void *cls)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
82
83   if (send_task != NULL)
84     GNUNET_SCHEDULER_cancel (send_task);
85
86   if (die_task != NULL)
87   {
88     GNUNET_SCHEDULER_cancel (die_task);
89     die_task = NULL;
90   }
91
92   if (cc != NULL)
93   {
94     GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
95     cc = NULL;
96   }
97
98   if (th != NULL)
99     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
100   th = NULL;
101
102   if (blacklist_p1 != NULL)
103   {
104     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
105     blacklist_p1 = NULL;
106   }
107
108   if (blacklist_p2 != NULL)
109   {
110     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
111     blacklist_p2 = NULL;
112   }
113
114   if (p1 != NULL)
115   {
116     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
117     p1 = NULL;
118   }
119   if (p2 != NULL)
120   {
121     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
122     p2 = NULL;
123   }
124
125   if ((blacklist_request_p1 == GNUNET_YES) &&
126       (blacklist_request_p2 == GNUNET_YES) &&
127       (connected == GNUNET_NO))
128   {
129     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Peers were not connected, success\n"));
130     ok = 0;
131   }
132   else
133   {
134     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Peers were not connected, fail\n"));
135     ok = 1;
136   }
137 }
138
139
140 static void
141 end_badly (void *cls)
142 {
143   if (send_task != NULL)
144   {
145     GNUNET_SCHEDULER_cancel (send_task);
146     send_task = NULL;
147   }
148
149   if (shutdown_task != NULL)
150   {
151     GNUNET_SCHEDULER_cancel (shutdown_task);
152     shutdown_task = NULL;
153   }
154
155   if (cc != NULL)
156   {
157     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
158     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
159     cc = NULL;
160   }
161
162   if (th != NULL)
163     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
164   th = NULL;
165
166   if (blacklist_p1 != NULL)
167     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
168
169   if (blacklist_p2 != NULL)
170     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
171
172   if (p1 != NULL)
173     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
174   if (p2 != NULL)
175     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
176
177   ok = GNUNET_SYSERR;
178 }
179
180
181 static void
182 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
183                 const struct GNUNET_MessageHeader *message)
184 {
185   struct PeerContext *p = cls;
186   struct PeerContext *t = NULL;
187
188   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
189     t = p1;
190   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
191     t = p2;
192   GNUNET_assert (t != NULL);
193
194   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
195
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
198               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
199               GNUNET_i2s (&t->id));
200   GNUNET_free (ps);
201
202   if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
203       (TEST_MESSAGE_SIZE == ntohs (message->size)))
204   {
205     ok = 0;
206     shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
207   }
208   else
209   {
210     GNUNET_break (0);
211     ok = 1;
212     shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
213   }
214 }
215
216
217 static size_t
218 notify_ready (void *cls, size_t size, void *buf)
219 {
220   struct PeerContext *p = cls;
221   struct GNUNET_MessageHeader *hdr;
222
223   th = NULL;
224
225   if (buf == NULL)
226   {
227     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228                 "Timeout occurred while waiting for transmit_ready\n");
229     if (NULL != die_task)
230     {
231       GNUNET_SCHEDULER_cancel (die_task);
232       die_task = NULL;
233     }
234     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
235     ok = 42;
236     return 0;
237   }
238
239   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
240   hdr = buf;
241   hdr->size = htons (TEST_MESSAGE_SIZE);
242   hdr->type = htons (TEST_MESSAGE_TYPE);
243
244   {
245     char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
246
247     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
249                 p2->no,
250                 ps,
251                 ntohs (hdr->type),
252                 ntohs (hdr->size),
253                 p->no,
254                 GNUNET_i2s (&p->id));
255     GNUNET_free (ps);
256   }
257
258   return TEST_MESSAGE_SIZE;
259 }
260
261
262 static void
263 sendtask (void *cls)
264 {
265   send_task = NULL;
266   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
267
268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
270               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
271   GNUNET_free (receiver_s);
272
273   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id,
274                                                TEST_MESSAGE_SIZE,
275                                                TIMEOUT_TRANSMIT, &notify_ready,
276                                                p1);
277 }
278
279
280 static void
281 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
282 {
283   static int c;
284
285   c++;
286   struct PeerContext *p = cls;
287   struct PeerContext *t = NULL;
288
289   connected = GNUNET_YES;
290   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
291     t = p1;
292   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
293     t = p2;
294   GNUNET_assert (t != NULL);
295
296   {
297     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
298
299     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300                 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
301                 p->no,
302                 ps,
303                 t->no,
304                 GNUNET_i2s (peer));
305     GNUNET_free (ps);
306   }
307 }
308
309
310 static void
311 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
312 {
313   struct PeerContext *p = cls;
314   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
318               GNUNET_i2s (peer));
319
320   GNUNET_free (ps);
321
322   if (th != NULL)
323     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
324   th = NULL;
325 }
326
327
328 static void
329 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
330 {
331   cc = NULL;
332   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
333
334   connected = GNUNET_YES;
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
336               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
337   GNUNET_free (p1_c);
338
339
340
341   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
342 }
343
344
345 static int
346 blacklist_cb (void *cls,
347               const struct
348               GNUNET_PeerIdentity * pid)
349 {
350   struct PeerContext * p = cls;
351   int res = GNUNET_SYSERR;
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354               "Peer  %u : Blacklist request for peer `%s'\n",
355               p->no,
356               GNUNET_i2s (pid));
357
358   if (p == p1)
359   {
360     blacklist_request_p1 = GNUNET_YES;
361     res = GNUNET_OK;
362   }
363   else if (p == p2)
364   {
365     blacklist_request_p2 = GNUNET_YES;
366     res = GNUNET_SYSERR;
367   }
368
369   if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == NULL))
370   {
371     shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
372   }
373
374   return res;
375 }
376
377
378 static void
379 start_cb (struct PeerContext *p, void *cls)
380 {
381   static int started;
382
383   started++;
384
385   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
386               GNUNET_i2s (&p->id));
387
388   if (started != 2)
389     return;
390
391   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
392
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
395               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
396   GNUNET_free (sender_c);
397
398   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
399                                                NULL);
400
401 }
402
403
404 static void
405 run (void *cls, char *const *args, const char *cfgfile,
406      const struct GNUNET_CONFIGURATION_Handle *cfg)
407 {
408   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
409   connected = GNUNET_NO;
410   blacklist_request_p1 = GNUNET_NO;
411   blacklist_request_p2 = GNUNET_NO;
412
413   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
414                                             &notify_receive, &notify_connect,
415                                             &notify_disconnect, &start_cb,
416                                             NULL);
417
418   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
419                                             &notify_receive, &notify_connect,
420                                             &notify_disconnect, &start_cb,
421                                             NULL);
422
423   blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
424                               &blacklist_cb,
425                               p1);
426
427   blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
428                               &blacklist_cb,
429                               p2);
430
431   GNUNET_assert (blacklist_p1 != NULL);
432   GNUNET_assert (blacklist_p2 != NULL);
433 }
434
435
436 static int
437 check ()
438 {
439   static char *const argv[] = { "test-transport-api-blacklisting",
440     "-c",
441     "test_transport_api_data.conf",
442     NULL
443   };
444   static struct GNUNET_GETOPT_CommandLineOption options[] = {
445     GNUNET_GETOPT_OPTION_END
446   };
447
448   ok = 1;
449   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
450                       "nohelp", options, &run, &ok);
451
452   return ok;
453 }
454
455 int
456 main (int argc, char *argv[])
457 {
458   int ret;
459
460   GNUNET_log_setup ("test-transport-api-blacklisting",
461                     "WARNING",
462                     NULL);
463
464   tth = GNUNET_TRANSPORT_TESTING_init ();
465
466   ret = check ();
467
468   GNUNET_TRANSPORT_TESTING_done (tth);
469
470   return ret;
471 }
472
473 /* end of transport_api_blacklisting.c */