- client-side implementation of peer queries
[oweals/gnunet.git] / src / transport / test_transport_api_monitoring.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_PeerMonitoringContext *pmc_p1;
80
81 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_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   {
102     GNUNET_SCHEDULER_cancel (die_task);
103     die_task = GNUNET_SCHEDULER_NO_TASK;
104   }
105
106   if (th != NULL)
107     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
108   th = NULL;
109
110   if (NULL != p1)
111     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
112   p1 = NULL;
113   if (NULL != p2)
114     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
115   p2 = NULL;
116
117   if (NULL != pmc_p1)
118   {
119     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
120     pmc_p1 = NULL;
121   }
122   if (NULL != pmc_p2)
123   {
124     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
125     pmc_p2 = NULL;
126   }
127
128
129
130   ok = 0;
131 }
132
133 static void
134 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   die_task = GNUNET_SCHEDULER_NO_TASK;
137
138   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
139
140
141   if (send_task != GNUNET_SCHEDULER_NO_TASK)
142     GNUNET_SCHEDULER_cancel (send_task);
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 (GNUNET_SCHEDULER_NO_TASK != 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   if (buf != NULL)
241   {
242     memset (buf, '\0', TEST_MESSAGE_SIZE);
243     hdr = buf;
244     hdr->size = htons (TEST_MESSAGE_SIZE);
245     hdr->type = htons (TEST_MESSAGE_TYPE);
246   }
247
248   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
249   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
250               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
251               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
252               GNUNET_i2s (&p->id));
253   GNUNET_free (ps);
254
255   return TEST_MESSAGE_SIZE;
256 }
257
258
259 static void
260 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 {
262   send_task = GNUNET_SCHEDULER_NO_TASK;
263
264   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
265     return;
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, 0,
274                                                TIMEOUT_TRANSMIT, &notify_ready,
275                                                p1);
276 }
277
278 static void done ()
279 {
280   if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
281   {
282     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
283     ok = 0;
284     end();
285   }
286 }
287
288
289 static void
290 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
291 {
292   static int c;
293
294   c++;
295   struct PeerContext *p = cls;
296   struct PeerContext *t = NULL;
297
298   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
299   {
300     p1_c_notify = GNUNET_YES;
301     t = p1;
302   }
303   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
304   {
305     p2_c_notify = GNUNET_YES;
306     t = p2;
307   }
308   GNUNET_assert (t != NULL);
309
310   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
311
312   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
313               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
314               t->no, GNUNET_i2s (peer));
315   if (p1_c_notify && p2_c_notify)
316     GNUNET_SCHEDULER_add_now(&done, NULL);
317   GNUNET_free (ps);
318 }
319
320
321 static void
322 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
323 {
324   struct PeerContext *p = cls;
325   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
326
327   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
329               GNUNET_i2s (peer));
330
331   GNUNET_free (ps);
332
333   if (th != NULL)
334     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
335   th = NULL;
336 }
337
338
339 static void
340 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
341 {
342   cc = NULL;
343   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
344
345   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
346               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
347   GNUNET_free (p1_c);
348
349   s_connected = GNUNET_YES;
350   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
351 }
352
353
354 static void
355 start_cb (struct PeerContext *p, void *cls)
356 {
357   static int started;
358
359   started++;
360
361   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
362               GNUNET_i2s (&p->id));
363
364   if (started != 2)
365     return;
366   else
367     s_started = GNUNET_YES;
368   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
369
370   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
372               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
373   GNUNET_free (sender_c);
374
375   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
376                                                NULL);
377
378 }
379
380
381 static void monitor1_cb (void *cls,
382                         const struct GNUNET_PeerIdentity *peer,
383                         const struct GNUNET_HELLO_Address *address,
384                         enum GNUNET_TRANSPORT_PeerState state,
385                         struct GNUNET_TIME_Absolute state_timeout)
386 {
387   if ((NULL == peer) || (NULL == p1))
388     return;
389
390   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Monitor 1: %s %s %s\n",
391       GNUNET_i2s (peer), GNUNET_TRANSPORT_ps2s(state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
392   if ((0 == memcmp (peer, &p2->id, sizeof (p2->id)) &&
393       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
394       GNUNET_NO == p1_c) )
395   {
396     p1_c = GNUNET_YES;
397     GNUNET_SCHEDULER_add_now (&done, NULL);
398   }
399
400 }
401
402
403 static void monitor2_cb (void *cls,
404                         const struct GNUNET_PeerIdentity *peer,
405                         const struct GNUNET_HELLO_Address *address,
406                         enum GNUNET_TRANSPORT_PeerState state,
407                         struct GNUNET_TIME_Absolute state_timeout)
408 {
409   if ((NULL == peer) || (NULL == p2))
410     return;
411
412   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Monitor 2: %s %s %s\n",
413       GNUNET_i2s (peer), GNUNET_TRANSPORT_ps2s (state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
414   if ((0 == memcmp (peer, &p1->id, sizeof (p1->id)) &&
415       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
416       GNUNET_NO == p2_c) )
417   {
418     p2_c = GNUNET_YES;
419     GNUNET_SCHEDULER_add_now (&done, NULL);
420   }
421 }
422
423
424
425 static void
426 run (void *cls, char *const *args, const char *cfgfile,
427      const struct GNUNET_CONFIGURATION_Handle *cfg)
428 {
429   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
430
431   s_started = GNUNET_NO;
432   s_connected = GNUNET_NO;
433   s_sending = GNUNET_NO;
434
435   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
436                                             &notify_receive, &notify_connect,
437                                             &notify_disconnect, &start_cb,
438                                             NULL);
439   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
441
442   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
443                                             &notify_receive, &notify_connect,
444                                             &notify_disconnect, &start_cb,
445                                             NULL);
446   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
448   if ((p1 == NULL) || (p2 == NULL))
449   {
450     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
451     if (die_task != GNUNET_SCHEDULER_NO_TASK)
452       GNUNET_SCHEDULER_cancel (die_task);
453     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
454     return;
455   }
456 }
457
458
459 static int
460 check ()
461 {
462   static char *const argv[] = { "test-transport-api",
463     "-c",
464     "test_transport_api_data.conf",
465     NULL
466   };
467   static struct GNUNET_GETOPT_CommandLineOption options[] = {
468     GNUNET_GETOPT_OPTION_END
469   };
470
471   send_task = GNUNET_SCHEDULER_NO_TASK;
472
473   ok = 1;
474   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
475                       "nohelp", options, &run, &ok);
476
477   return ok;
478 }
479
480 int
481 main (int argc, char *argv[])
482 {
483   int ret;
484
485   ok = 1;
486
487   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
488   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
489   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
490                                                  &test_plugin);
491
492   GNUNET_log_setup (test_name,
493                     "WARNING",
494                     NULL);
495   tth = GNUNET_TRANSPORT_TESTING_init ();
496
497   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
498   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
499
500   ret = check ();
501
502   GNUNET_free (cfg_file_p1);
503   GNUNET_free (cfg_file_p2);
504
505   GNUNET_free (test_source);
506   GNUNET_free (test_plugin);
507   GNUNET_free (test_name);
508
509   GNUNET_TRANSPORT_TESTING_done (tth);
510
511   if (0 != ret)
512     return ret;
513   else
514     return ok;
515 }
516
517 /* end of test_transport_api.c */