fix crash on shutdown
[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     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 != pmc_p1)
113   {
114     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
115     pmc_p1 = NULL;
116   }
117   if (NULL != pmc_p2)
118   {
119     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
120     pmc_p2 = NULL;
121   }
122
123
124
125   ok = 0;
126 }
127
128 static void
129 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 {
131   die_task = GNUNET_SCHEDULER_NO_TASK;
132
133   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
134
135
136   if (send_task != GNUNET_SCHEDULER_NO_TASK)
137     GNUNET_SCHEDULER_cancel (send_task);
138
139   if (cc != NULL)
140   {
141     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
142     GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
143     cc = NULL;
144   }
145
146   if (th != NULL)
147     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
148   else
149     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
150
151   if (s_started == GNUNET_NO)
152     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
153   else
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
155
156   if (s_connected == GNUNET_NO)
157     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
158   else
159     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
160
161   if (s_sending == GNUNET_NO)
162     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
163   else
164     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
165
166   th = NULL;
167
168   if (NULL != pmc_p1)
169   {
170     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
171     pmc_p1 = NULL;
172   }
173   if (NULL != pmc_p2)
174   {
175     GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
176     pmc_p2 = NULL;
177   }
178
179   if (p1 != NULL)
180     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
181   else
182     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
183   if (p2 != NULL)
184     GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
185   else
186     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
187
188   ok = GNUNET_SYSERR;
189 }
190
191
192 static void
193 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
194                 const struct GNUNET_MessageHeader *message)
195 {
196   struct PeerContext *p = cls;
197   struct PeerContext *t = NULL;
198
199   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
200     t = p1;
201   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
202     t = p2;
203   GNUNET_assert (t != NULL);
204
205   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
206
207   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
208               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
209               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
210               GNUNET_i2s (&t->id));
211   GNUNET_free (ps);
212 }
213
214
215 static size_t
216 notify_ready (void *cls, size_t size, void *buf)
217 {
218   struct PeerContext *p = cls;
219   struct GNUNET_MessageHeader *hdr;
220
221   th = NULL;
222
223   if (buf == NULL)
224   {
225     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
226                 "Timeout occurred while waiting for transmit_ready\n");
227     if (GNUNET_SCHEDULER_NO_TASK != die_task)
228       GNUNET_SCHEDULER_cancel (die_task);
229     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
230     ok = 42;
231     return 0;
232   }
233
234   GNUNET_assert (size >= TEST_MESSAGE_SIZE);
235   if (buf != NULL)
236   {
237     memset (buf, '\0', TEST_MESSAGE_SIZE);
238     hdr = buf;
239     hdr->size = htons (TEST_MESSAGE_SIZE);
240     hdr->type = htons (TEST_MESSAGE_TYPE);
241   }
242
243   char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
244   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
245               "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
246               p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
247               GNUNET_i2s (&p->id));
248   GNUNET_free (ps);
249
250   return TEST_MESSAGE_SIZE;
251 }
252
253
254 static void
255 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
256 {
257   send_task = GNUNET_SCHEDULER_NO_TASK;
258
259   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
260     return;
261   char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
262
263   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264               "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
265               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
266   GNUNET_free (receiver_s);
267   s_sending = GNUNET_YES;
268   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
269                                                TIMEOUT_TRANSMIT, &notify_ready,
270                                                p1);
271 }
272
273 static void done ()
274 {
275   if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
276   {
277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
278     ok = 0;
279     end();
280   }
281 }
282
283
284 static void
285 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
286 {
287   static int c;
288
289   c++;
290   struct PeerContext *p = cls;
291   struct PeerContext *t = NULL;
292
293   if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
294   {
295     p1_c_notify = GNUNET_YES;
296     t = p1;
297   }
298   if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
299   {
300     p2_c_notify = GNUNET_YES;
301     t = p2;
302   }
303   GNUNET_assert (t != NULL);
304
305   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
306
307   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
308               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
309               t->no, GNUNET_i2s (peer));
310   if (p1_c_notify && p2_c_notify)
311     GNUNET_SCHEDULER_add_now(&done, NULL);
312   GNUNET_free (ps);
313 }
314
315
316 static void
317 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
318 {
319   struct PeerContext *p = cls;
320   char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
321
322   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
323               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
324               GNUNET_i2s (peer));
325
326   GNUNET_free (ps);
327
328   if (th != NULL)
329     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
330   th = NULL;
331 }
332
333
334 static void
335 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
336 {
337   cc = NULL;
338   char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
339
340   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
341               p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
342   GNUNET_free (p1_c);
343
344   s_connected = GNUNET_YES;
345   send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
346 }
347
348
349 static void
350 start_cb (struct PeerContext *p, void *cls)
351 {
352   static int started;
353
354   started++;
355
356   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peer %u (`%s') started\n", p->no,
357               GNUNET_i2s (&p->id));
358
359   if (started != 2)
360     return;
361   else
362     s_started = GNUNET_YES;
363   char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
364
365   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
366               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
367               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
368   GNUNET_free (sender_c);
369
370   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
371                                                NULL);
372
373 }
374
375
376 static void monitor1_cb (void *cls,
377                         const struct GNUNET_PeerIdentity *peer,
378                         const struct GNUNET_HELLO_Address *address,
379                         enum GNUNET_TRANSPORT_PeerState state,
380                         struct GNUNET_TIME_Absolute state_timeout)
381 {
382   if ((NULL == peer) || (NULL == p1))
383     return;
384
385   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitor 1: %s %s %s\n",
386       GNUNET_i2s (peer), GNUNET_TRANSPORT_p2s(state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
387   if ((0 == memcmp (peer, &p2->id, sizeof (p2->id)) &&
388       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
389       GNUNET_NO == p1_c) )
390   {
391     p1_c = GNUNET_YES;
392     GNUNET_SCHEDULER_add_now (&done, NULL);
393   }
394
395 }
396
397
398 static void monitor2_cb (void *cls,
399                         const struct GNUNET_PeerIdentity *peer,
400                         const struct GNUNET_HELLO_Address *address,
401                         enum GNUNET_TRANSPORT_PeerState state,
402                         struct GNUNET_TIME_Absolute state_timeout)
403 {
404   if ((NULL == peer) || (NULL == p2))
405     return;
406
407   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Monitor 2: %s %s %s\n",
408       GNUNET_i2s (peer), GNUNET_TRANSPORT_p2s (state), GNUNET_STRINGS_absolute_time_to_string(state_timeout));
409   if ((0 == memcmp (peer, &p1->id, sizeof (p1->id)) &&
410       (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
411       GNUNET_NO == p2_c) )
412   {
413     p2_c = GNUNET_YES;
414     GNUNET_SCHEDULER_add_now (&done, NULL);
415   }
416 }
417
418
419
420 static void
421 run (void *cls, char *const *args, const char *cfgfile,
422      const struct GNUNET_CONFIGURATION_Handle *cfg)
423 {
424   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
425
426   s_started = GNUNET_NO;
427   s_connected = GNUNET_NO;
428   s_sending = GNUNET_NO;
429
430   p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
431                                             &notify_receive, &notify_connect,
432                                             &notify_disconnect, &start_cb,
433                                             NULL);
434   pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
435
436   p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
437                                             &notify_receive, &notify_connect,
438                                             &notify_disconnect, &start_cb,
439                                             NULL);
440   pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
441
442   if ((p1 == NULL) || (p2 == NULL))
443   {
444     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
445     if (die_task != GNUNET_SCHEDULER_NO_TASK)
446       GNUNET_SCHEDULER_cancel (die_task);
447     die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
448     return;
449   }
450 }
451
452
453 static int
454 check ()
455 {
456   static char *const argv[] = { "test-transport-api",
457     "-c",
458     "test_transport_api_data.conf",
459     NULL
460   };
461   static struct GNUNET_GETOPT_CommandLineOption options[] = {
462     GNUNET_GETOPT_OPTION_END
463   };
464
465   send_task = GNUNET_SCHEDULER_NO_TASK;
466
467   ok = 1;
468   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
469                       "nohelp", options, &run, &ok);
470
471   return ok;
472 }
473
474 int
475 main (int argc, char *argv[])
476 {
477   int ret;
478
479   ok = 1;
480
481   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
482   GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
483   GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
484                                                  &test_plugin);
485
486   GNUNET_log_setup (test_name,
487                     "WARNING",
488                     NULL);
489   tth = GNUNET_TRANSPORT_TESTING_init ();
490
491   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
492   GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
493
494   ret = check ();
495
496   GNUNET_free (cfg_file_p1);
497   GNUNET_free (cfg_file_p2);
498
499   GNUNET_free (test_source);
500   GNUNET_free (test_plugin);
501   GNUNET_free (test_name);
502
503   GNUNET_TRANSPORT_TESTING_done (tth);
504
505   if (0 != ret)
506     return ret;
507   else
508     return ok;
509 }
510
511 /* end of test_transport_api.c */