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