small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[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   const struct GNUNET_SCHEDULER_TaskContext *tc;
266
267   send_task = NULL;
268   tc = GNUNET_SCHEDULER_get_task_context ();
269   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
270     return;
271   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
272
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
275               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
276   GNUNET_free (receiver_s);
277
278   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id,
279                                                TEST_MESSAGE_SIZE,
280                                                TIMEOUT_TRANSMIT, &notify_ready,
281                                                p1);
282 }
283
284
285 static void
286 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
287 {
288   static int c;
289
290   c++;
291   struct PeerContext *p = cls;
292   struct PeerContext *t = NULL;
293
294   connected = GNUNET_YES;
295   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
296     t = p1;
297   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
298     t = p2;
299   GNUNET_assert (t != NULL);
300
301   {
302     char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
303
304     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305                 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
306                 p->no,
307                 ps,
308                 t->no,
309                 GNUNET_i2s (peer));
310     GNUNET_free (ps);
311   }
312 }
313
314
315 static void
316 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
317 {
318   struct PeerContext *p = cls;
319   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
320
321   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
323               GNUNET_i2s (peer));
324
325   GNUNET_free (ps);
326
327   if (th != NULL)
328     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
329   th = NULL;
330 }
331
332
333 static void
334 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
335 {
336   cc = NULL;
337   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
338
339   connected = GNUNET_YES;
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
341               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
342   GNUNET_free (p1_c);
343
344
345
346   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
347 }
348
349
350 static int
351 blacklist_cb (void *cls,
352               const struct
353               GNUNET_PeerIdentity * pid)
354 {
355   struct PeerContext * p = cls;
356   int res = GNUNET_SYSERR;
357
358   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359               "Peer  %u : Blacklist request for peer `%s'\n",
360               p->no,
361               GNUNET_i2s (pid));
362
363   if (p == p1)
364   {
365     blacklist_request_p1 = GNUNET_YES;
366     res = GNUNET_OK;
367   }
368   else if (p == p2)
369   {
370     blacklist_request_p2 = GNUNET_YES;
371     res = GNUNET_SYSERR;
372   }
373
374   if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == NULL))
375   {
376     shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
377   }
378
379   return res;
380 }
381
382
383 static void
384 start_cb (struct PeerContext *p, void *cls)
385 {
386   static int started;
387
388   started++;
389
390   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
391               GNUNET_i2s (&p->id));
392
393   if (started != 2)
394     return;
395
396   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
397
398   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
400               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
401   GNUNET_free (sender_c);
402
403   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
404                                                NULL);
405
406 }
407
408
409 static void
410 run (void *cls, char *const *args, const char *cfgfile,
411      const struct GNUNET_CONFIGURATION_Handle *cfg)
412 {
413   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
414   connected = GNUNET_NO;
415   blacklist_request_p1 = GNUNET_NO;
416   blacklist_request_p2 = GNUNET_NO;
417
418   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
419                                             &notify_receive, &notify_connect,
420                                             &notify_disconnect, &start_cb,
421                                             NULL);
422
423   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
424                                             &notify_receive, &notify_connect,
425                                             &notify_disconnect, &start_cb,
426                                             NULL);
427
428   blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
429                               &blacklist_cb,
430                               p1);
431
432   blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
433                               &blacklist_cb,
434                               p2);
435
436   GNUNET_assert (blacklist_p1 != NULL);
437   GNUNET_assert (blacklist_p2 != NULL);
438 }
439
440
441 static int
442 check ()
443 {
444   static char *const argv[] = { "test-transport-api-blacklisting",
445     "-c",
446     "test_transport_api_data.conf",
447     NULL
448   };
449   static struct GNUNET_GETOPT_CommandLineOption options[] = {
450     GNUNET_GETOPT_OPTION_END
451   };
452
453   ok = 1;
454   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
455                       "nohelp", options, &run, &ok);
456
457   return ok;
458 }
459
460 int
461 main (int argc, char *argv[])
462 {
463   int ret;
464
465   GNUNET_log_setup ("test-transport-api-blacklisting",
466                     "WARNING",
467                     NULL);
468
469   tth = GNUNET_TRANSPORT_TESTING_init ();
470
471   ret = check ();
472
473   GNUNET_TRANSPORT_TESTING_done (tth);
474
475   return ret;
476 }
477
478 /* end of transport_api_blacklisting.c */