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