4731e41e2040a9f06c33abbc787998ecbaa07b94
[oweals/gnunet.git] / src / transport / test_transport_api_monitor_peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 GNUnet e.V.
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 GNUNET_TRANSPORT_TESTING_PeerContext *p1;
61
62 static struct GNUNET_TRANSPORT_TESTING_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 (p1);
111   p1 = NULL;
112   if (NULL != p2)
113     GNUNET_TRANSPORT_TESTING_stop_peer (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 {
134   die_task = NULL;
135   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
136
137   if (send_task != NULL)
138   {
139     GNUNET_SCHEDULER_cancel (send_task);
140     send_task = NULL;
141   }
142
143   if (cc != NULL)
144   {
145     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
146     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
147     cc = NULL;
148   }
149
150   if (th != NULL)
151     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
152   else
153     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
154
155   if (s_started == GNUNET_NO)
156     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
157   else
158     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
159
160   if (s_connected == GNUNET_NO)
161     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
162   else
163     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
164
165   if (s_sending == GNUNET_NO)
166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
167   else
168     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
169
170   th = NULL;
171
172   if (NULL != pmc_p1)
173   {
174     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
175     pmc_p1 = NULL;
176   }
177   if (NULL != pmc_p2)
178   {
179     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
180     pmc_p2 = NULL;
181   }
182
183   if (p1 != NULL)
184     GNUNET_TRANSPORT_TESTING_stop_peer (p1);
185   else
186     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
187   if (p2 != NULL)
188     GNUNET_TRANSPORT_TESTING_stop_peer (p2);
189   else
190     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
191
192   ok = GNUNET_SYSERR;
193 }
194
195
196 static void
197 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
198                 const struct GNUNET_MessageHeader *message)
199 {
200   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
201   struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL;
202
203   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
204     t = p1;
205   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
206     t = p2;
207   GNUNET_assert (t != NULL);
208
209   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
210
211   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
212               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
213               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
214               GNUNET_i2s (&t->id));
215   GNUNET_free (ps);
216 }
217
218
219 static size_t
220 notify_ready (void *cls, size_t size, void *buf)
221 {
222   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
223   struct GNUNET_MessageHeader *hdr;
224
225   th = NULL;
226
227   if (buf == NULL)
228   {
229     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230                 "Timeout occurred while waiting for transmit_ready\n");
231     if (NULL != die_task)
232       GNUNET_SCHEDULER_cancel (die_task);
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   memset (buf, '\0', TEST_MESSAGE_SIZE);
240   hdr = buf;
241   hdr->size = htons (TEST_MESSAGE_SIZE);
242   hdr->type = htons (TEST_MESSAGE_TYPE);
243
244   {
245     char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
246
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,
250                 ps,
251                 ntohs (hdr->type),
252                 ntohs (hdr->size),
253                 p->no,
254                 GNUNET_i2s (&p->id));
255     GNUNET_free (ps);
256   }
257
258   return TEST_MESSAGE_SIZE;
259 }
260
261
262 static void
263 sendtask (void *cls)
264 {
265   send_task = NULL;
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 GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
298   struct GNUNET_TRANSPORT_TESTING_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 GNUNET_TRANSPORT_TESTING_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 (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 GNUNET_TRANSPORT_TESTING_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 (p1,
380                                                p2,
381                                                &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,
450                                             cfg_file_p1,
451                                             1,
452                                             &notify_receive,
453                                             &notify_connect,
454                                             &notify_disconnect,
455                                             &start_cb,
456                                             NULL);
457   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg,
458                                            NULL,
459                                            GNUNET_NO,
460                                            &monitor1_cb,
461                                            NULL);
462   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
463
464   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
465                                             cfg_file_p2,
466                                             2,
467                                             &notify_receive,
468                                             &notify_connect,
469                                             &notify_disconnect,
470                                             &start_cb,
471                                             NULL);
472   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg,
473                                            NULL,
474                                            GNUNET_NO,
475                                            &monitor2_cb,
476                                            NULL);
477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
478   if ((p1 == NULL) || (p2 == NULL))
479   {
480     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
481     if (die_task != NULL)
482       GNUNET_SCHEDULER_cancel (die_task);
483     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
484     return;
485   }
486 }
487
488
489 static int
490 check ()
491 {
492   static char *const argv[] = { "test-transport-api",
493     "-c",
494     "test_transport_api_data.conf",
495     NULL
496   };
497   static struct GNUNET_GETOPT_CommandLineOption options[] = {
498     GNUNET_GETOPT_OPTION_END
499   };
500
501   send_task = NULL;
502
503   ok = 1;
504   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
505                       "nohelp", options, &run, &ok);
506
507   return ok;
508 }
509
510
511 int
512 main (int argc, char *argv[])
513 {
514   int ret;
515
516   ok = 1;
517
518   test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]);
519   test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__);
520   test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
521                                                                test_source);
522
523   GNUNET_log_setup (test_name,
524                     "WARNING",
525                     NULL);
526   tth = GNUNET_TRANSPORT_TESTING_init ();
527
528   cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1);
529   cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2);
530
531   ret = check ();
532
533   GNUNET_free (cfg_file_p1);
534   GNUNET_free (cfg_file_p2);
535
536   GNUNET_free (test_source);
537   GNUNET_free (test_plugin);
538   GNUNET_free (test_name);
539
540   GNUNET_TRANSPORT_TESTING_done (tth);
541
542   if (0 != ret)
543     return ret;
544   else
545     return ok;
546 }
547
548 /* end of test_transport_api_monitor_peers.c */