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