Check that you are not present in trail twice
[oweals/gnunet.git] / src / transport / test_transport_api_monitor_peers.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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  * @file transport/test_transport_api_monitor_peers.c
22  * @brief base test case for transport peer monitor API
23  */
24 #include "platform.h"
25 #include "gnunet_transport_service.h"
26 #include "transport-testing.h"
27
28 /**
29  * How long until we give up on transmitting the message?
30  */
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
32
33 /**
34  * How long until we give up on transmitting the message?
35  */
36 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38 #define TEST_MESSAGE_SIZE 2600
39
40 #define TEST_MESSAGE_TYPE 12345
41
42 static char *test_source;
43
44 static char *test_plugin;
45
46 static char *test_name;
47
48 static int ok;
49
50 static int s_started;
51
52 static int s_connected;
53
54 static int s_sending;
55
56 static GNUNET_SCHEDULER_TaskIdentifier die_task;
57
58 static GNUNET_SCHEDULER_TaskIdentifier send_task;
59
60 static struct PeerContext *p1;
61
62 static struct PeerContext *p2;
63
64 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
65
66 static struct GNUNET_TRANSPORT_TransmitHandle *th;
67
68 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
69
70 static char *cfg_file_p1;
71
72 static char *cfg_file_p2;
73
74 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
75
76 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
77
78 static int p1_c = GNUNET_NO;
79
80 static int p2_c = GNUNET_NO;
81
82 static int p1_c_notify = GNUNET_NO;
83
84 static int p2_c_notify = GNUNET_NO;
85
86
87 static void
88 end ()
89 {
90   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91               "Stopping peers\n");
92
93   if (send_task != GNUNET_SCHEDULER_NO_TASK)
94     GNUNET_SCHEDULER_cancel (send_task);
95
96   if (die_task != GNUNET_SCHEDULER_NO_TASK)
97   {
98     GNUNET_SCHEDULER_cancel (die_task);
99     die_task = GNUNET_SCHEDULER_NO_TASK;
100   }
101
102   if (th != NULL)
103     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
104   th = NULL;
105
106   if (NULL != p1)
107     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
108   p1 = NULL;
109   if (NULL != p2)
110     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
111   p2 = NULL;
112
113   if (NULL != pmc_p1)
114   {
115     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
116     pmc_p1 = NULL;
117   }
118   if (NULL != pmc_p2)
119   {
120     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
121     pmc_p2 = NULL;
122   }
123
124
125
126   ok = 0;
127 }
128
129
130 static void
131 end_badly (void *cls,
132            const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   die_task = GNUNET_SCHEDULER_NO_TASK;
135
136   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
137
138
139   if (send_task != GNUNET_SCHEDULER_NO_TASK)
140     GNUNET_SCHEDULER_cancel (send_task);
141
142   if (cc != NULL)
143   {
144     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
145     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
146     cc = NULL;
147   }
148
149   if (th != NULL)
150     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
151   else
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
153
154   if (s_started == GNUNET_NO)
155     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
156   else
157     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
158
159   if (s_connected == GNUNET_NO)
160     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
161   else
162     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
163
164   if (s_sending == GNUNET_NO)
165     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
166   else
167     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
168
169   th = NULL;
170
171   if (NULL != pmc_p1)
172   {
173     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
174     pmc_p1 = NULL;
175   }
176   if (NULL != pmc_p2)
177   {
178     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
179     pmc_p2 = NULL;
180   }
181
182   if (p1 != NULL)
183     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
184   else
185     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
186   if (p2 != NULL)
187     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
188   else
189     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
190
191   ok = GNUNET_SYSERR;
192 }
193
194
195 static void
196 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
197                 const struct GNUNET_MessageHeader *message)
198 {
199   struct PeerContext *p = cls;
200   struct PeerContext *t = NULL;
201
202   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
203     t = p1;
204   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
205     t = p2;
206   GNUNET_assert (t != NULL);
207
208   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
209
210   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
212               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
213               GNUNET_i2s (&t->id));
214   GNUNET_free (ps);
215 }
216
217
218 static size_t
219 notify_ready (void *cls, size_t size, void *buf)
220 {
221   struct PeerContext *p = cls;
222   struct GNUNET_MessageHeader *hdr;
223
224   th = NULL;
225
226   if (buf == NULL)
227   {
228     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229                 "Timeout occurred while waiting for transmit_ready\n");
230     if (GNUNET_SCHEDULER_NO_TASK != die_task)
231       GNUNET_SCHEDULER_cancel (die_task);
232     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
233     ok = 42;
234     return 0;
235   }
236
237   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
238   if (buf != NULL)
239   {
240     memset (buf, '\0', TEST_MESSAGE_SIZE);
241     hdr = buf;
242     hdr->size = htons (TEST_MESSAGE_SIZE);
243     hdr->type = htons (TEST_MESSAGE_TYPE);
244   }
245
246   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
247   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
249               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
250               GNUNET_i2s (&p->id));
251   GNUNET_free (ps);
252
253   return TEST_MESSAGE_SIZE;
254 }
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_INFO,
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   s_sending = GNUNET_YES;
271   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
272                                                TIMEOUT_TRANSMIT, &notify_ready,
273                                                p1);
274 }
275
276
277 static void
278 done ()
279 {
280   if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
281   {
282     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
283     ok = 0;
284     end();
285   }
286 }
287
288
289 static void
290 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
291 {
292   static int c;
293
294   c++;
295   struct PeerContext *p = cls;
296   struct PeerContext *t = NULL;
297
298   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
299   {
300     p1_c_notify = GNUNET_YES;
301     t = p1;
302   }
303   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
304   {
305     p2_c_notify = GNUNET_YES;
306     t = p2;
307   }
308   GNUNET_assert (t != NULL);
309
310   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
311
312   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
313               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
314               t->no, GNUNET_i2s (peer));
315   if (p1_c_notify && p2_c_notify)
316     GNUNET_SCHEDULER_add_now(&done, NULL);
317   GNUNET_free (ps);
318 }
319
320
321 static void
322 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
323 {
324   struct PeerContext *p = cls;
325   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
326
327   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
329               GNUNET_i2s (peer));
330
331   GNUNET_free (ps);
332
333   if (th != NULL)
334     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
335   th = NULL;
336 }
337
338
339 static void
340 testing_connect_cb (struct PeerContext *p1,
341                     struct PeerContext *p2,
342                     void *cls)
343 {
344   cc = NULL;
345   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
346
347   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348               "Peers connected: %u (%s) <-> %u (%s)\n",
349               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
350   GNUNET_free (p1_c);
351
352   s_connected = GNUNET_YES;
353   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
354 }
355
356
357 static void
358 start_cb (struct PeerContext *p, void *cls)
359 {
360   static int started;
361
362   started++;
363
364   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
365               "Peer %u (`%s') started\n", p->no,
366               GNUNET_i2s (&p->id));
367
368   if (started != 2)
369     return;
370   else
371     s_started = GNUNET_YES;
372   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
373
374   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
375               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
376               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
377   GNUNET_free (sender_c);
378
379   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
380                                                NULL);
381
382 }
383
384
385 static void
386 monitor1_cb (void *cls,
387              const struct GNUNET_PeerIdentity *peer,
388              const struct GNUNET_HELLO_Address *address,
389              enum GNUNET_TRANSPORT_PeerState state,
390              struct GNUNET_TIME_Absolute state_timeout)
391 {
392   if ((NULL == peer) || (NULL == p1))
393     return;
394
395   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
396               "Monitor 1: %s %s %s\n",
397               GNUNET_i2s (peer),
398               GNUNET_TRANSPORT_ps2s (state),
399               GNUNET_STRINGS_absolute_time_to_string(state_timeout));
400   if ((0 == memcmp (peer, &p2->id, sizeof (p2->id)) &&
401       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
402       GNUNET_NO == p1_c) )
403   {
404     p1_c = GNUNET_YES;
405     GNUNET_SCHEDULER_add_now (&done, NULL);
406   }
407
408 }
409
410
411 static void
412 monitor2_cb (void *cls,
413              const struct GNUNET_PeerIdentity *peer,
414              const struct GNUNET_HELLO_Address *address,
415              enum GNUNET_TRANSPORT_PeerState state,
416              struct GNUNET_TIME_Absolute state_timeout)
417 {
418   if ((NULL == peer) || (NULL == p2))
419     return;
420
421   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
422               "Monitor 2: %s %s %s\n",
423               GNUNET_i2s (peer),
424               GNUNET_TRANSPORT_ps2s (state),
425               GNUNET_STRINGS_absolute_time_to_string(state_timeout));
426   if ((0 == memcmp (peer, &p1->id, sizeof (p1->id)) &&
427       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
428       GNUNET_NO == p2_c) )
429   {
430     p2_c = GNUNET_YES;
431     GNUNET_SCHEDULER_add_now (&done, NULL);
432   }
433 }
434
435
436
437 static void
438 run (void *cls, char *const *args, const char *cfgfile,
439      const struct GNUNET_CONFIGURATION_Handle *cfg)
440 {
441   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
442
443   s_started = GNUNET_NO;
444   s_connected = GNUNET_NO;
445   s_sending = GNUNET_NO;
446
447   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
448                                             &notify_receive, &notify_connect,
449                                             &notify_disconnect, &start_cb,
450                                             NULL);
451   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
453
454   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
455                                             &notify_receive, &notify_connect,
456                                             &notify_disconnect, &start_cb,
457                                             NULL);
458   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
459   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
460   if ((p1 == NULL) || (p2 == NULL))
461   {
462     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
463     if (die_task != GNUNET_SCHEDULER_NO_TASK)
464       GNUNET_SCHEDULER_cancel (die_task);
465     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
466     return;
467   }
468 }
469
470
471 static int
472 check ()
473 {
474   static char *const argv[] = { "test-transport-api",
475     "-c",
476     "test_transport_api_data.conf",
477     NULL
478   };
479   static struct GNUNET_GETOPT_CommandLineOption options[] = {
480     GNUNET_GETOPT_OPTION_END
481   };
482
483   send_task = GNUNET_SCHEDULER_NO_TASK;
484
485   ok = 1;
486   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
487                       "nohelp", options, &run, &ok);
488
489   return ok;
490 }
491
492
493 int
494 main (int argc, char *argv[])
495 {
496   int ret;
497
498   ok = 1;
499
500   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
501   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
502   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
503                                                  &test_plugin);
504
505   GNUNET_log_setup (test_name,
506                     "WARNING",
507                     NULL);
508   tth = GNUNET_TRANSPORT_TESTING_init ();
509
510   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
511   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
512
513   ret = check ();
514
515   GNUNET_free (cfg_file_p1);
516   GNUNET_free (cfg_file_p2);
517
518   GNUNET_free (test_source);
519   GNUNET_free (test_plugin);
520   GNUNET_free (test_name);
521
522   GNUNET_TRANSPORT_TESTING_done (tth);
523
524   if (0 != ret)
525     return ret;
526   else
527     return ok;
528 }
529
530 /* end of test_transport_api_monitor_peers.c */