2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/test_transport_address_switch.c
22 * @brief base test case for transport implementations
24 * This test case tests if peers can successfully switch address when connected
25 * connected by monitoring statistic values
28 #include "gnunet_transport_service.h"
29 #include "gnunet_ats_service.h"
31 #include "transport-testing.h"
34 * Testcase specific declarations
37 GNUNET_NETWORK_STRUCT_BEGIN
40 struct GNUNET_MessageHeader header;
43 GNUNET_NETWORK_STRUCT_END
46 * Message type for test messages
51 * Message size for test messages
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
61 * How long until we give up on transmitting the message?
63 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
65 #define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
66 #define DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69 * Timeout task to send messages
71 static struct GNUNET_SCHEDULER_Task *die_task;
74 static struct GNUNET_SCHEDULER_Task *delayed_end_task;
77 * Measurement task to send messages
79 static struct GNUNET_SCHEDULER_Task *measure_task;
82 struct PeerContext *p1;
84 struct GNUNET_STATISTICS_Handle *p1_stat;
86 struct PeerContext *p2;
88 struct GNUNET_STATISTICS_Handle *p2_stat;
90 struct PeerContext *sender;
92 struct PeerContext *receiver;
94 struct GNUNET_TRANSPORT_TransmitHandle *th;
96 struct GNUNET_TRANSPORT_TESTING_handle *tth;
98 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
100 static int test_connected;
106 * Statistics about peer 1
108 static unsigned int p1_addresses_avail;
109 static unsigned int p1_switch_attempts;
110 static unsigned int p1_switch_success;
111 static unsigned int p1_switch_fail;
115 * Statistics about peer 2
117 static unsigned int p2_switch_attempts;
118 static unsigned int p2_switch_success;
119 static unsigned int p2_switch_fail;
120 static unsigned int p2_addresses_avail;
123 * Transmission statistics
126 /* Amount of data transfered since last switch attempt */
127 static unsigned long long bytes_sent_after_switch;
128 static unsigned long long bytes_recv_after_switch;
131 * END Testcase specific declarations
135 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
137 #define OKPP do { ok++; } while (0)
143 stat_start_attempt_cb (void *cls, const char *subsystem, const char *name,
144 uint64_t value, int is_persistent)
148 p1_switch_attempts++;
149 FPRINTF (stderr, "(1:s)");
153 p2_switch_attempts++;
154 FPRINTF (stderr, "(2:s)");
157 bytes_recv_after_switch = 0;
158 bytes_sent_after_switch = 0;
165 stat_success_attempt_cb (void *cls, const char *subsystem, const char *name,
166 uint64_t value, int is_persistent)
171 FPRINTF (stderr, "(1:+)");
176 FPRINTF (stderr, "(2:+)");
184 stat_fail_attempt_cb (void *cls, const char *subsystem, const char *name,
185 uint64_t value, int is_persistent)
193 FPRINTF (stderr, "(1:-)");
198 FPRINTF (stderr, "(2:-)");
205 stat_addresses_available (void *cls, const char *subsystem, const char *name,
206 uint64_t value, int is_persistent)
210 p1_addresses_avail++;
214 p2_addresses_avail++;
223 if (measure_task != NULL )
225 GNUNET_SCHEDULER_cancel (measure_task);
229 if (delayed_end_task != NULL )
231 GNUNET_SCHEDULER_cancel (delayed_end_task);
232 delayed_end_task = NULL;
235 if (die_task != NULL )
237 GNUNET_SCHEDULER_cancel (die_task);
243 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
244 "# Attempts to switch addresses",
245 stat_start_attempt_cb, p1);
246 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
247 "# Successful attempts to switch addresses",
248 stat_success_attempt_cb, p1);
249 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
250 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
251 stat_fail_attempt_cb, p1);
252 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
253 "# Failed attempts to switch addresses (failed to send CONNECT)",
254 stat_fail_attempt_cb, p1);
255 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
256 "# Failed attempts to switch addresses (no response)",
257 stat_fail_attempt_cb, p1);
258 GNUNET_STATISTICS_watch (p1_stat, "transport",
259 "# transport addresses",
260 stat_addresses_available, p1);
261 GNUNET_STATISTICS_destroy (p1_stat, GNUNET_NO);
266 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport",
267 "# Attempts to switch addresses", stat_start_attempt_cb, p2);
268 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport",
269 "# Successful attempts to switch addresses", stat_success_attempt_cb, p2);
270 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport",
271 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
272 stat_fail_attempt_cb, p2);
273 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport",
274 "# Failed attempts to switch addresses (failed to send CONNECT)",
275 stat_fail_attempt_cb, p2);
276 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport",
277 "# Failed attempts to switch addresses (no response)",
278 stat_fail_attempt_cb, p2);
279 GNUNET_STATISTICS_watch (p2_stat, "transport",
280 "# transport addresses",
281 stat_addresses_available, p2);
282 GNUNET_STATISTICS_destroy (p2_stat, GNUNET_NO);
288 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
293 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
298 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
303 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
314 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
316 delayed_end_task = NULL;
317 FPRINTF (stderr, "\n");
318 if (p1_switch_attempts > 0)
320 FPRINTF (stderr, "Peer 1 tried %u times to switch and succeeded %u times, failed %u times\n",
321 p1_switch_attempts, p1_switch_success, p1_switch_fail);
322 if (p1_switch_success != p1_switch_attempts)
328 else if (p1_addresses_avail > 1)
330 FPRINTF (stderr, "Peer 1 had %u addresses available, but did not try to switch\n",
333 if (p2_switch_attempts > 0)
335 FPRINTF (stderr, "Peer 2 tried %u times to switch and succeeded %u times, failed %u times\n",
336 p2_switch_attempts, p2_switch_success, p2_switch_fail);
337 if (p2_switch_success != p2_switch_attempts)
343 else if (p2_addresses_avail > 1)
345 FPRINTF (stderr, "Peer 2 had %u addresses available, but did not try to switch\n",
349 if ( ((p1_switch_attempts > 0) || (p2_switch_attempts > 0)) &&
350 (bytes_sent_after_switch == 0) )
352 FPRINTF (stderr, "No data sent after switching!\n");
356 if ( ((p1_switch_attempts > 0) || (p2_switch_attempts > 0)) &&
357 (bytes_recv_after_switch == 0) )
359 FPRINTF (stderr, "No data received after switching!\n");
374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
376 if (test_connected == GNUNET_YES)
377 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
379 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
388 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
389 const struct GNUNET_MessageHeader *message)
391 const struct TestMessage *hdr;
393 hdr = (const struct TestMessage *) message;
394 if (MTYPE != ntohs (message->type))
397 struct PeerContext *p = cls;
398 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
400 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
401 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n", p->no, ps,
402 ntohl (hdr->num), ntohs (message->size), GNUNET_i2s (peer));
404 if ( ((p1_switch_attempts >= 1) || (p2_switch_attempts >= 1)) &&
405 (p1_switch_attempts == p1_switch_fail + p1_switch_success) &&
406 (p2_switch_attempts == p2_switch_fail + p2_switch_success) )
408 bytes_recv_after_switch += ntohs(hdr->header.size);
409 if ((bytes_sent_after_switch > 0) && (bytes_recv_after_switch > 0))
411 /* A peer switched addresses and sent and received data after the
412 * switch operations */
423 notify_ready (void *cls, size_t size, void *buf)
426 struct TestMessage hdr;
432 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
433 "Timeout occurred while waiting for transmit_ready for message\n");
434 if (NULL != die_task)
435 GNUNET_SCHEDULER_cancel (die_task);
436 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL );
441 GNUNET_assert(size >= MSIZE);
442 GNUNET_assert(buf != NULL);
445 hdr.header.size = htons (MSIZE);
446 hdr.header.type = htons (MTYPE);
448 memcpy (&cbuf[0], &hdr, sizeof(struct TestMessage));
449 ret += sizeof(struct TestMessage);
450 memset (&cbuf[sizeof(struct TestMessage)], '0', MSIZE - sizeof(struct TestMessage));
454 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
455 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
456 "Sending message %u of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
457 n, s, sender->no, GNUNET_i2s (&sender->id), receiver->no, receiver_s);
458 GNUNET_free(receiver_s);
462 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE,
463 TIMEOUT_TRANSMIT, ¬ify_ready, NULL );
465 if ( ((p1_switch_attempts >= 1) || (p2_switch_attempts >= 1)) &&
466 (p1_switch_attempts == p1_switch_fail + p1_switch_success) &&
467 (p2_switch_attempts == p2_switch_fail + p2_switch_success) )
469 bytes_sent_after_switch += ret;
477 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
479 struct PeerContext *p = cls;
480 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
481 p->no, GNUNET_i2s (peer));
486 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
488 struct PeerContext *p = cls;
489 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
492 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
501 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE,
502 TIMEOUT_TRANSMIT, ¬ify_ready, NULL );
507 progress_indicator (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
512 if ((DURATION.rel_value_us / 1000 / 1000LL) < counter)
514 FPRINTF (stderr, "%s", ".\n");
518 FPRINTF (stderr, "%s", ".");
519 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
520 &progress_indicator, NULL );
526 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
528 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
530 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
531 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
535 test_connected = GNUNET_YES;
537 FPRINTF (stderr, "(i:s/+/-) \t i == peer 1/2, s/+/- : switch attempt/switch ok/switch fail\n");
539 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
540 &progress_indicator, NULL );
541 GNUNET_SCHEDULER_add_now (&sendtask, NULL );
546 start_cb (struct PeerContext *p, void *cls)
551 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
552 GNUNET_i2s (&p->id));
556 test_connected = GNUNET_NO;
560 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
561 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
562 "Test tries to send from %u (%s) -> peer %u (%s)\n", sender->no, sender_c,
563 receiver->no, GNUNET_i2s (&receiver->id));
564 GNUNET_free(sender_c);
565 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
571 run (void *cls, char * const *args, const char *cfgfile,
572 const struct GNUNET_CONFIGURATION_Handle *cfg)
574 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
576 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
577 ¬ify_receive, ¬ify_connect, ¬ify_disconnect, &start_cb, NULL );
579 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
580 ¬ify_receive, ¬ify_connect, ¬ify_disconnect, &start_cb, NULL );
582 if ((p1 == NULL )|| (p2 == NULL))
584 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
585 if (die_task != NULL)
586 GNUNET_SCHEDULER_cancel (die_task);
587 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
591 /* Start to watch statistics for peer 1 */
592 p1_stat = GNUNET_STATISTICS_create ("transport", p1->cfg);
593 GNUNET_STATISTICS_watch (p1_stat, "transport",
594 "# Attempts to switch addresses",
595 stat_start_attempt_cb, p1);
596 GNUNET_STATISTICS_watch (p1_stat, "transport",
597 "# Successful attempts to switch addresses",
598 stat_success_attempt_cb, p1);
599 GNUNET_STATISTICS_watch (p1_stat, "transport",
600 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
601 stat_fail_attempt_cb, p1);
602 GNUNET_STATISTICS_watch (p1_stat, "transport",
603 "# Failed attempts to switch addresses (failed to send CONNECT)",
604 stat_fail_attempt_cb, p1);
605 GNUNET_STATISTICS_watch (p1_stat, "transport",
606 "# Failed attempts to switch addresses (no response)",
607 stat_fail_attempt_cb, p1);
608 GNUNET_STATISTICS_watch (p1_stat, "transport",
609 "# transport addresses",
610 stat_addresses_available, p1);
612 /* Start to watch statistics for peer 2 */
613 p2_stat = GNUNET_STATISTICS_create ("transport", p2->cfg);
614 GNUNET_STATISTICS_watch (p2_stat, "transport",
615 "# Attempts to switch addresses",
616 stat_start_attempt_cb, p2);
617 GNUNET_STATISTICS_watch (p2_stat, "transport",
618 "# Successful attempts to switch addresses",
619 stat_success_attempt_cb, p2);
620 GNUNET_STATISTICS_watch (p2_stat, "transport",
621 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
622 stat_fail_attempt_cb, p2);
623 GNUNET_STATISTICS_watch (p2_stat, "transport",
624 "# Failed attempts to switch addresses (failed to send CONNECT)",
625 stat_fail_attempt_cb, p2);
626 GNUNET_STATISTICS_watch (p2_stat, "transport",
627 "# Failed attempts to switch addresses (no response)",
628 stat_fail_attempt_cb, p2);
629 GNUNET_STATISTICS_watch (p2_stat, "transport",
630 "# transport addresses",
631 stat_addresses_available, p2);
633 if ((p1_stat == NULL )|| (p2_stat == NULL))
635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not create statistics for peers!\n");
636 if (die_task != NULL)
637 GNUNET_SCHEDULER_cancel (die_task);
638 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
644 main (int argc, char *argv[])
650 static char *argv_new[] = { "test-transport-address-switch", "-c",
651 "test_transport_startonly.conf", NULL };
653 static struct GNUNET_GETOPT_CommandLineOption options[] = {
654 GNUNET_GETOPT_OPTION_END };
656 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
658 GNUNET_log_setup (test_name, "WARNING", NULL );
660 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
661 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
664 tth = GNUNET_TRANSPORT_TESTING_init ();
666 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
667 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using cfg [%u] : %s \n", 1, cfg_file_p1);
668 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
669 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using cfg [%u] : %s \n", 2, cfg_file_p2);
671 GNUNET_PROGRAM_run ((sizeof(argv_new) / sizeof(char *)) - 1, argv_new,
672 test_name, "nohelp", options, &run, NULL );
674 GNUNET_free(cfg_file_p1);
675 GNUNET_free(cfg_file_p2);
677 GNUNET_free(test_source);
678 GNUNET_free(test_plugin);
679 GNUNET_free(test_name);
681 GNUNET_TRANSPORT_TESTING_done (tth);
686 /* end of test_transport_address_switch.c */