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