2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
21 * @file transport/test_transport_api_monitor_peers.c
22 * @brief base test case for transport peer monitor API
25 #include "gnunet_transport_service.h"
26 #include "transport-testing.h"
29 * How long until we give up on transmitting the message?
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
34 * How long until we give up on transmitting the message?
36 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
38 #define TEST_MESSAGE_SIZE 2600
40 #define TEST_MESSAGE_TYPE 12345
42 static char *test_source;
44 static char *test_plugin;
46 static char *test_name;
52 static int s_connected;
56 static struct GNUNET_SCHEDULER_Task * die_task;
58 static struct GNUNET_SCHEDULER_Task * send_task;
60 static struct PeerContext *p1;
62 static struct PeerContext *p2;
64 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
66 static struct GNUNET_TRANSPORT_TransmitHandle *th;
68 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
70 static char *cfg_file_p1;
72 static char *cfg_file_p2;
74 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
76 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
78 static int p1_c = GNUNET_NO;
80 static int p2_c = GNUNET_NO;
82 static int p1_c_notify = GNUNET_NO;
84 static int p2_c_notify = GNUNET_NO;
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
93 if (send_task != NULL)
95 GNUNET_SCHEDULER_cancel (send_task);
101 GNUNET_SCHEDULER_cancel (die_task);
106 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
110 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
113 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
118 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
123 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
132 end_badly (void *cls,
133 const struct GNUNET_SCHEDULER_TaskContext *tc)
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
138 if (send_task != NULL)
140 GNUNET_SCHEDULER_cancel (send_task);
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
147 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
152 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
156 if (s_started == GNUNET_NO)
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
161 if (s_connected == GNUNET_NO)
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
166 if (s_sending == GNUNET_NO)
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
175 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
180 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
185 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
189 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
198 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
199 const struct GNUNET_MessageHeader *message)
201 struct PeerContext *p = cls;
202 struct PeerContext *t = NULL;
204 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
206 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
208 GNUNET_assert (t != NULL);
210 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
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));
221 notify_ready (void *cls, size_t size, void *buf)
223 struct PeerContext *p = cls;
224 struct GNUNET_MessageHeader *hdr;
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Timeout occurred while waiting for transmit_ready\n");
232 if (NULL != die_task)
233 GNUNET_SCHEDULER_cancel (die_task);
234 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
240 memset (buf, '\0', TEST_MESSAGE_SIZE);
242 hdr->size = htons (TEST_MESSAGE_SIZE);
243 hdr->type = htons (TEST_MESSAGE_TYPE);
246 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
249 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
255 GNUNET_i2s (&p->id));
259 return TEST_MESSAGE_SIZE;
264 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
268 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
270 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
272 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
274 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
275 GNUNET_free (receiver_s);
276 s_sending = GNUNET_YES;
277 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
278 TIMEOUT_TRANSMIT, ¬ify_ready,
286 if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
296 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
301 struct PeerContext *p = cls;
302 struct PeerContext *t = NULL;
304 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
306 p1_c_notify = GNUNET_YES;
309 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
311 p2_c_notify = GNUNET_YES;
314 GNUNET_assert (t != NULL);
316 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
318 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
319 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
320 t->no, GNUNET_i2s (peer));
321 if (p1_c_notify && p2_c_notify)
322 GNUNET_SCHEDULER_add_now(&done, NULL);
328 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
330 struct PeerContext *p = cls;
331 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
333 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
334 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
340 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
346 testing_connect_cb (struct PeerContext *p1,
347 struct PeerContext *p2,
351 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 "Peers connected: %u (%s) <-> %u (%s)\n",
355 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
358 s_connected = GNUNET_YES;
359 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
364 start_cb (struct PeerContext *p, void *cls)
370 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
371 "Peer %u (`%s') started\n", p->no,
372 GNUNET_i2s (&p->id));
377 s_started = GNUNET_YES;
378 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
380 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
381 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
382 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
383 GNUNET_free (sender_c);
385 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
392 monitor1_cb (void *cls,
393 const struct GNUNET_PeerIdentity *peer,
394 const struct GNUNET_HELLO_Address *address,
395 enum GNUNET_TRANSPORT_PeerState state,
396 struct GNUNET_TIME_Absolute state_timeout)
398 if ((NULL == address) || (NULL == p1))
401 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
402 "Monitor 1: %s %s %s\n",
403 GNUNET_i2s (&address->peer),
404 GNUNET_TRANSPORT_ps2s (state),
405 GNUNET_STRINGS_absolute_time_to_string(state_timeout));
406 if ((0 == memcmp (&address->peer, &p2->id, sizeof (p2->id)) &&
407 (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
411 GNUNET_SCHEDULER_add_now (&done, NULL);
418 monitor2_cb (void *cls,
419 const struct GNUNET_PeerIdentity *peer,
420 const struct GNUNET_HELLO_Address *address,
421 enum GNUNET_TRANSPORT_PeerState state,
422 struct GNUNET_TIME_Absolute state_timeout)
424 if ((NULL == address) || (NULL == p2))
427 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
428 "Monitor 2: %s %s %s\n",
429 GNUNET_i2s (&address->peer),
430 GNUNET_TRANSPORT_ps2s (state),
431 GNUNET_STRINGS_absolute_time_to_string(state_timeout));
432 if ((0 == memcmp (&address->peer, &p1->id, sizeof (p1->id)) &&
433 (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
437 GNUNET_SCHEDULER_add_now (&done, NULL);
444 run (void *cls, char *const *args, const char *cfgfile,
445 const struct GNUNET_CONFIGURATION_Handle *cfg)
447 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
449 s_started = GNUNET_NO;
450 s_connected = GNUNET_NO;
451 s_sending = GNUNET_NO;
453 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
454 ¬ify_receive, ¬ify_connect,
455 ¬ify_disconnect, &start_cb,
457 pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
460 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
461 ¬ify_receive, ¬ify_connect,
462 ¬ify_disconnect, &start_cb,
464 pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
466 if ((p1 == NULL) || (p2 == NULL))
468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
469 if (die_task != NULL)
470 GNUNET_SCHEDULER_cancel (die_task);
471 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
480 static char *const argv[] = { "test-transport-api",
482 "test_transport_api_data.conf",
485 static struct GNUNET_GETOPT_CommandLineOption options[] = {
486 GNUNET_GETOPT_OPTION_END
492 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
493 "nohelp", options, &run, &ok);
500 main (int argc, char *argv[])
506 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
507 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
508 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
511 GNUNET_log_setup (test_name,
514 tth = GNUNET_TRANSPORT_TESTING_init ();
516 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
517 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
521 GNUNET_free (cfg_file_p1);
522 GNUNET_free (cfg_file_p2);
524 GNUNET_free (test_source);
525 GNUNET_free (test_plugin);
526 GNUNET_free (test_name);
528 GNUNET_TRANSPORT_TESTING_done (tth);
536 /* end of test_transport_api_monitor_peers.c */