- fix error messages
[oweals/gnunet.git] / src / transport / test_transport_api_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  * @author Matthias Wachs
25  *
26  */
27 #include "platform.h"
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
30
31 struct PeerContext *p1;
32
33 struct PeerContext *p2;
34
35 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
36
37 struct GNUNET_TRANSPORT_TransmitHandle *th;
38
39 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 struct GNUNET_TRANSPORT_Blacklist * blacklist_p1;
62
63 struct GNUNET_TRANSPORT_Blacklist * blacklist_p2;
64
65 static GNUNET_SCHEDULER_TaskIdentifier die_task;
66
67 static GNUNET_SCHEDULER_TaskIdentifier send_task;
68
69 static GNUNET_SCHEDULER_TaskIdentifier 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 {
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n");
82
83   if (send_task != GNUNET_SCHEDULER_NO_TASK)
84     GNUNET_SCHEDULER_cancel (send_task);
85
86   if (die_task != GNUNET_SCHEDULER_NO_TASK)
87   {
88     GNUNET_SCHEDULER_cancel (die_task);
89     die_task = GNUNET_SCHEDULER_NO_TASK;
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 static void
140 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   if (send_task != GNUNET_SCHEDULER_NO_TASK)
143   {
144     GNUNET_SCHEDULER_cancel (send_task);
145     send_task = GNUNET_SCHEDULER_NO_TASK;
146   }
147
148   if (shutdown_task != GNUNET_SCHEDULER_NO_TASK)
149   {
150     GNUNET_SCHEDULER_cancel (shutdown_task);
151     shutdown_task = GNUNET_SCHEDULER_NO_TASK;
152   }
153
154   if (cc != NULL)
155   {
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
157     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
158     cc = NULL;
159   }
160
161   if (th != NULL)
162     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
163   th = NULL;
164
165   if (blacklist_p1 != NULL)
166     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
167
168   if (blacklist_p2 != NULL)
169     GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
170
171   if (p1 != NULL)
172     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
173   if (p2 != NULL)
174     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
175
176   ok = GNUNET_SYSERR;
177 }
178
179
180 static void
181 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
182                 const struct GNUNET_MessageHeader *message)
183 {
184   struct PeerContext *p = cls;
185   struct PeerContext *t = NULL;
186
187   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
188     t = p1;
189   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
190     t = p2;
191   GNUNET_assert (t != NULL);
192
193   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
194
195   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
197               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
198               GNUNET_i2s (&t->id));
199   GNUNET_free (ps);
200
201   if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
202       (TEST_MESSAGE_SIZE == ntohs (message->size)))
203   {
204     ok = 0;
205     shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
206   }
207   else
208   {
209     GNUNET_break (0);
210     ok = 1;
211     shutdown_task = GNUNET_SCHEDULER_add_now(&end, NULL);
212   }
213 }
214
215
216 static size_t
217 notify_ready (void *cls, size_t size, void *buf)
218 {
219   struct PeerContext *p = cls;
220   struct GNUNET_MessageHeader *hdr;
221
222   th = NULL;
223
224   if (buf == NULL)
225   {
226     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
227                 "Timeout occurred while waiting for transmit_ready\n");
228     if (GNUNET_SCHEDULER_NO_TASK != die_task)
229     {
230       GNUNET_SCHEDULER_cancel (die_task);
231       die_task = GNUNET_SCHEDULER_NO_TASK;
232     }
233     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
234     ok = 42;
235     return 0;
236   }
237
238   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
239
240   if (buf != NULL)
241   {
242     hdr = buf;
243     hdr->size = htons (TEST_MESSAGE_SIZE);
244     hdr->type = htons (TEST_MESSAGE_TYPE);
245   }
246
247   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
248   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
250               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
251               GNUNET_i2s (&p->id));
252   GNUNET_free (ps);
253
254   return TEST_MESSAGE_SIZE;
255 }
256
257 static void
258 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 {
260   send_task = GNUNET_SCHEDULER_NO_TASK;
261
262   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
263     return;
264   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
265
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
268               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
269   GNUNET_free (receiver_s);
270
271   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
272                                                TIMEOUT_TRANSMIT, &notify_ready,
273                                                p1);
274 }
275
276 static void
277 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
278 {
279   static int c;
280
281   c++;
282   struct PeerContext *p = cls;
283   struct PeerContext *t = NULL;
284
285   connected = GNUNET_YES;
286   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
287     t = p1;
288   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
289     t = p2;
290   GNUNET_assert (t != NULL);
291
292   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
293
294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
296               t->no, GNUNET_i2s (peer));
297   GNUNET_free (ps);
298 }
299
300
301 static void
302 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
303 {
304   struct PeerContext *p = cls;
305   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
306
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
309               GNUNET_i2s (peer));
310
311   GNUNET_free (ps);
312
313   if (th != NULL)
314     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
315   th = NULL;
316 }
317
318 static void
319 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
320 {
321   cc = NULL;
322   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
323
324   connected = GNUNET_YES;
325   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
326               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
327   GNUNET_free (p1_c);
328
329
330
331   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
332 }
333
334
335 int blacklist_cb (void *cls,
336                  const struct
337                  GNUNET_PeerIdentity * pid)
338 {
339   struct PeerContext * p = cls;
340   int res = GNUNET_SYSERR;
341   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer  %u : Blacklist request for peer `%s'\n", p->no, GNUNET_i2s (pid));
342
343   if (p == p1)
344   {
345     blacklist_request_p1 = GNUNET_YES;
346     res = GNUNET_OK;
347   }
348   else if (p == p2)
349   {
350     blacklist_request_p2 = GNUNET_YES;
351     res = GNUNET_SYSERR;
352   }
353
354   if (((blacklist_request_p2 == GNUNET_YES) && (blacklist_request_p1 == GNUNET_YES)) && (shutdown_task == GNUNET_SCHEDULER_NO_TASK))
355   {
356     shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3), &end, NULL);
357   }
358
359   return res;
360 }
361
362 void
363 start_cb (struct PeerContext *p, void *cls)
364 {
365   static int started;
366
367   started++;
368
369   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
370               GNUNET_i2s (&p->id));
371
372   if (started != 2)
373     return;
374
375   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
376
377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
379               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
380   GNUNET_free (sender_c);
381
382   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
383                                                NULL);
384
385 }
386
387 static void
388 run (void *cls, char *const *args, const char *cfgfile,
389      const struct GNUNET_CONFIGURATION_Handle *cfg)
390 {
391   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
392   connected = GNUNET_NO;
393   blacklist_request_p1 = GNUNET_NO;
394   blacklist_request_p2 = GNUNET_NO;
395
396   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1,
397                                             &notify_receive, &notify_connect,
398                                             &notify_disconnect, &start_cb,
399                                             NULL);
400
401   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2,
402                                             &notify_receive, &notify_connect,
403                                             &notify_disconnect, &start_cb,
404                                             NULL);
405
406   blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg,
407                               &blacklist_cb,
408                               p1);
409
410   blacklist_p2 = GNUNET_TRANSPORT_blacklist (p2->cfg,
411                               &blacklist_cb,
412                               p2);
413
414   GNUNET_assert (blacklist_p1 != NULL);
415   GNUNET_assert (blacklist_p2 != NULL);
416 }
417
418
419 static int
420 check ()
421 {
422   static char *const argv[] = { "test-transport-api-blacklisting",
423     "-c",
424     "test_transport_api_data.conf",
425     NULL
426   };
427   static struct GNUNET_GETOPT_CommandLineOption options[] = {
428     GNUNET_GETOPT_OPTION_END
429   };
430
431   ok = 1;
432   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-transport-api-blacklisting",
433                       "nohelp", options, &run, &ok);
434
435   return ok;
436 }
437
438 int
439 main (int argc, char *argv[])
440 {
441   int ret;
442
443   GNUNET_log_setup ("test-transport-api-blacklisting",
444                     "WARNING",
445                     NULL);
446
447   tth = GNUNET_TRANSPORT_TESTING_init ();
448
449   ret = check ();
450
451   GNUNET_TRANSPORT_TESTING_done (tth);
452
453   return ret;
454 }
455
456 /* end of transport_api_blacklisting.c */