- fix coverity
[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 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 {
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 (tth, 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 (tth, 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 (tth, 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 PeerContext *p = cls;
201   struct 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 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 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,
454                                            NULL,
455                                            GNUNET_NO,
456                                            &monitor1_cb,
457                                            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,
465                                            NULL,
466                                            GNUNET_NO,
467                                            &monitor2_cb,
468                                            NULL);
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
470   if ((p1 == NULL) || (p2 == NULL))
471   {
472     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
473     if (die_task != NULL)
474       GNUNET_SCHEDULER_cancel (die_task);
475     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
476     return;
477   }
478 }
479
480
481 static int
482 check ()
483 {
484   static char *const argv[] = { "test-transport-api",
485     "-c",
486     "test_transport_api_data.conf",
487     NULL
488   };
489   static struct GNUNET_GETOPT_CommandLineOption options[] = {
490     GNUNET_GETOPT_OPTION_END
491   };
492
493   send_task = NULL;
494
495   ok = 1;
496   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
497                       "nohelp", options, &run, &ok);
498
499   return ok;
500 }
501
502
503 int
504 main (int argc, char *argv[])
505 {
506   int ret;
507
508   ok = 1;
509
510   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
511   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
512   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
513                                                  &test_plugin);
514
515   GNUNET_log_setup (test_name,
516                     "WARNING",
517                     NULL);
518   tth = GNUNET_TRANSPORT_TESTING_init ();
519
520   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
521   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
522
523   ret = check ();
524
525   GNUNET_free (cfg_file_p1);
526   GNUNET_free (cfg_file_p2);
527
528   GNUNET_free (test_source);
529   GNUNET_free (test_plugin);
530   GNUNET_free (test_name);
531
532   GNUNET_TRANSPORT_TESTING_done (tth);
533
534   if (0 != ret)
535     return ret;
536   else
537     return ok;
538 }
539
540 /* end of test_transport_api_monitor_peers.c */