38884f7d512ea2e853012d53c4f871e0e04210b8
[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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 struct 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   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
248     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249                 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
250                 p2->no,
251                 ps,
252                 ntohs (hdr->type),
253                 ntohs (hdr->size),
254                 p->no,
255                 GNUNET_i2s (&p->id));
256     GNUNET_free (ps);
257   }
258
259   return TEST_MESSAGE_SIZE;
260 }
261
262
263 static void
264 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 {
266   send_task = NULL;
267
268   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
269     return;
270   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
271
272   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
274               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
275   GNUNET_free (receiver_s);
276   s_sending = GNUNET_YES;
277   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
278                                                TIMEOUT_TRANSMIT, &notify_ready,
279                                                p1);
280 }
281
282
283 static void
284 done ()
285 {
286   if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
287   {
288     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
289     ok = 0;
290     end();
291   }
292 }
293
294
295 static void
296 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
297 {
298   static int c;
299
300   c++;
301   struct PeerContext *p = cls;
302   struct PeerContext *t = NULL;
303
304   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
305   {
306     p1_c_notify = GNUNET_YES;
307     t = p1;
308   }
309   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
310   {
311     p2_c_notify = GNUNET_YES;
312     t = p2;
313   }
314   GNUNET_assert (t != NULL);
315
316   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
317
318   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
319               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
320               t->no, GNUNET_i2s (peer));
321   if (p1_c_notify && p2_c_notify)
322     GNUNET_SCHEDULER_add_now(&done, NULL);
323   GNUNET_free (ps);
324 }
325
326
327 static void
328 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
329 {
330   struct PeerContext *p = cls;
331   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
332
333   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
334               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
335               GNUNET_i2s (peer));
336
337   GNUNET_free (ps);
338
339   if (th != NULL)
340     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
341   th = NULL;
342 }
343
344
345 static void
346 testing_connect_cb (struct PeerContext *p1,
347                     struct PeerContext *p2,
348                     void *cls)
349 {
350   cc = NULL;
351   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
352
353   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354               "Peers connected: %u (%s) <-> %u (%s)\n",
355               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
356   GNUNET_free (p1_c);
357
358   s_connected = GNUNET_YES;
359   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
360 }
361
362
363 static void
364 start_cb (struct PeerContext *p, void *cls)
365 {
366   static int started;
367
368   started++;
369
370   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371               "Peer %u (`%s') started\n", p->no,
372               GNUNET_i2s (&p->id));
373
374   if (started != 2)
375     return;
376   else
377     s_started = GNUNET_YES;
378   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
379
380   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
381               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
382               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
383   GNUNET_free (sender_c);
384
385   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
386                                                NULL);
387
388 }
389
390
391 static void
392 monitor1_cb (void *cls,
393              const struct GNUNET_PeerIdentity *peer,
394              const struct GNUNET_HELLO_Address *address,
395              enum GNUNET_TRANSPORT_PeerState state,
396              struct GNUNET_TIME_Absolute state_timeout)
397 {
398   if ((NULL == address) || (NULL == p1))
399     return;
400
401   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
402               "Monitor 1: %s %s %s\n",
403               GNUNET_i2s (&address->peer),
404               GNUNET_TRANSPORT_ps2s (state),
405               GNUNET_STRINGS_absolute_time_to_string(state_timeout));
406   if ((0 == memcmp (&address->peer, &p2->id, sizeof (p2->id)) &&
407       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
408       GNUNET_NO == p1_c) )
409   {
410     p1_c = GNUNET_YES;
411     GNUNET_SCHEDULER_add_now (&done, NULL);
412   }
413
414 }
415
416
417 static void
418 monitor2_cb (void *cls,
419              const struct GNUNET_PeerIdentity *peer,
420              const struct GNUNET_HELLO_Address *address,
421              enum GNUNET_TRANSPORT_PeerState state,
422              struct GNUNET_TIME_Absolute state_timeout)
423 {
424   if ((NULL == address) || (NULL == p2))
425     return;
426
427   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
428               "Monitor 2: %s %s %s\n",
429               GNUNET_i2s (&address->peer),
430               GNUNET_TRANSPORT_ps2s (state),
431               GNUNET_STRINGS_absolute_time_to_string(state_timeout));
432   if ((0 == memcmp (&address->peer, &p1->id, sizeof (p1->id)) &&
433       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
434       GNUNET_NO == p2_c) )
435   {
436     p2_c = GNUNET_YES;
437     GNUNET_SCHEDULER_add_now (&done, NULL);
438   }
439 }
440
441
442
443 static void
444 run (void *cls, char *const *args, const char *cfgfile,
445      const struct GNUNET_CONFIGURATION_Handle *cfg)
446 {
447   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
448
449   s_started = GNUNET_NO;
450   s_connected = GNUNET_NO;
451   s_sending = GNUNET_NO;
452
453   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
454                                             &notify_receive, &notify_connect,
455                                             &notify_disconnect, &start_cb,
456                                             NULL);
457   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
458   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
459
460   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
461                                             &notify_receive, &notify_connect,
462                                             &notify_disconnect, &start_cb,
463                                             NULL);
464   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
465   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
466   if ((p1 == NULL) || (p2 == NULL))
467   {
468     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
469     if (die_task != NULL)
470       GNUNET_SCHEDULER_cancel (die_task);
471     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
472     return;
473   }
474 }
475
476
477 static int
478 check ()
479 {
480   static char *const argv[] = { "test-transport-api",
481     "-c",
482     "test_transport_api_data.conf",
483     NULL
484   };
485   static struct GNUNET_GETOPT_CommandLineOption options[] = {
486     GNUNET_GETOPT_OPTION_END
487   };
488
489   send_task = NULL;
490
491   ok = 1;
492   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
493                       "nohelp", options, &run, &ok);
494
495   return ok;
496 }
497
498
499 int
500 main (int argc, char *argv[])
501 {
502   int ret;
503
504   ok = 1;
505
506   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
507   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
508   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
509                                                  &test_plugin);
510
511   GNUNET_log_setup (test_name,
512                     "WARNING",
513                     NULL);
514   tth = GNUNET_TRANSPORT_TESTING_init ();
515
516   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
517   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
518
519   ret = check ();
520
521   GNUNET_free (cfg_file_p1);
522   GNUNET_free (cfg_file_p2);
523
524   GNUNET_free (test_source);
525   GNUNET_free (test_plugin);
526   GNUNET_free (test_name);
527
528   GNUNET_TRANSPORT_TESTING_done (tth);
529
530   if (0 != ret)
531     return ret;
532   else
533     return ok;
534 }
535
536 /* end of test_transport_api_monitor_peers.c */