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;
431 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
432 "Timeout occurred while waiting for transmit_ready for message\n");
433 if (NULL != die_task)
434 GNUNET_SCHEDULER_cancel (die_task);
435 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL );
440 GNUNET_assert(size >= MSIZE);
441 GNUNET_assert(buf != NULL);
444 hdr.header.size = htons (MSIZE);
445 hdr.header.type = htons (MTYPE);
447 memcpy (&cbuf[0], &hdr, sizeof(struct TestMessage));
448 memset (&cbuf[sizeof(struct TestMessage)], '0', MSIZE - sizeof(struct TestMessage));
451 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
452 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
453 "Sending message %u of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
454 n, s, sender->no, GNUNET_i2s (&sender->id), receiver->no, receiver_s);
455 GNUNET_free(receiver_s);
459 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE,
460 TIMEOUT_TRANSMIT, ¬ify_ready, NULL );
462 if ( ((p1_switch_attempts >= 1) || (p2_switch_attempts >= 1)) &&
463 (p1_switch_attempts == p1_switch_fail + p1_switch_success) &&
464 (p2_switch_attempts == p2_switch_fail + p2_switch_success) )
466 bytes_sent_after_switch += MSIZE;
474 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
476 struct PeerContext *p = cls;
477 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
478 p->no, GNUNET_i2s (peer));
483 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
485 struct PeerContext *p = cls;
486 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
489 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
498 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE,
499 TIMEOUT_TRANSMIT, ¬ify_ready, NULL );
504 progress_indicator (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
509 if ((DURATION.rel_value_us / 1000 / 1000LL) < counter)
511 FPRINTF (stderr, "%s", ".\n");
515 FPRINTF (stderr, "%s", ".");
516 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
517 &progress_indicator, NULL );
523 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
525 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
527 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
528 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
532 test_connected = GNUNET_YES;
534 FPRINTF (stderr, "(i:s/+/-) \t i == peer 1/2, s/+/- : switch attempt/switch ok/switch fail\n");
536 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
537 &progress_indicator, NULL );
538 GNUNET_SCHEDULER_add_now (&sendtask, NULL );
543 start_cb (struct PeerContext *p, void *cls)
548 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
549 GNUNET_i2s (&p->id));
553 test_connected = GNUNET_NO;
557 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
558 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
559 "Test tries to send from %u (%s) -> peer %u (%s)\n", sender->no, sender_c,
560 receiver->no, GNUNET_i2s (&receiver->id));
561 GNUNET_free(sender_c);
562 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
568 run (void *cls, char * const *args, const char *cfgfile,
569 const struct GNUNET_CONFIGURATION_Handle *cfg)
571 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
573 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
574 ¬ify_receive, ¬ify_connect, ¬ify_disconnect, &start_cb, NULL );
576 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
577 ¬ify_receive, ¬ify_connect, ¬ify_disconnect, &start_cb, NULL );
579 if ((p1 == NULL )|| (p2 == NULL))
581 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
582 if (die_task != NULL)
583 GNUNET_SCHEDULER_cancel (die_task);
584 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
588 /* Start to watch statistics for peer 1 */
589 p1_stat = GNUNET_STATISTICS_create ("transport", p1->cfg);
590 GNUNET_STATISTICS_watch (p1_stat, "transport",
591 "# Attempts to switch addresses",
592 stat_start_attempt_cb, p1);
593 GNUNET_STATISTICS_watch (p1_stat, "transport",
594 "# Successful attempts to switch addresses",
595 stat_success_attempt_cb, p1);
596 GNUNET_STATISTICS_watch (p1_stat, "transport",
597 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
598 stat_fail_attempt_cb, p1);
599 GNUNET_STATISTICS_watch (p1_stat, "transport",
600 "# Failed attempts to switch addresses (failed to send CONNECT)",
601 stat_fail_attempt_cb, p1);
602 GNUNET_STATISTICS_watch (p1_stat, "transport",
603 "# Failed attempts to switch addresses (no response)",
604 stat_fail_attempt_cb, p1);
605 GNUNET_STATISTICS_watch (p1_stat, "transport",
606 "# transport addresses",
607 stat_addresses_available, p1);
609 /* Start to watch statistics for peer 2 */
610 p2_stat = GNUNET_STATISTICS_create ("transport", p2->cfg);
611 GNUNET_STATISTICS_watch (p2_stat, "transport",
612 "# Attempts to switch addresses",
613 stat_start_attempt_cb, p2);
614 GNUNET_STATISTICS_watch (p2_stat, "transport",
615 "# Successful attempts to switch addresses",
616 stat_success_attempt_cb, p2);
617 GNUNET_STATISTICS_watch (p2_stat, "transport",
618 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
619 stat_fail_attempt_cb, p2);
620 GNUNET_STATISTICS_watch (p2_stat, "transport",
621 "# Failed attempts to switch addresses (failed to send CONNECT)",
622 stat_fail_attempt_cb, p2);
623 GNUNET_STATISTICS_watch (p2_stat, "transport",
624 "# Failed attempts to switch addresses (no response)",
625 stat_fail_attempt_cb, p2);
626 GNUNET_STATISTICS_watch (p2_stat, "transport",
627 "# transport addresses",
628 stat_addresses_available, p2);
630 if ((p1_stat == NULL )|| (p2_stat == NULL))
632 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not create statistics for peers!\n");
633 if (die_task != NULL)
634 GNUNET_SCHEDULER_cancel (die_task);
635 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
641 main (int argc, char *argv[])
647 static char *argv_new[] = { "test-transport-address-switch", "-c",
648 "test_transport_startonly.conf", NULL };
650 static struct GNUNET_GETOPT_CommandLineOption options[] = {
651 GNUNET_GETOPT_OPTION_END };
653 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
655 GNUNET_log_setup (test_name, "WARNING", NULL );
657 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
658 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
661 tth = GNUNET_TRANSPORT_TESTING_init ();
663 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
664 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using cfg [%u] : %s \n", 1, cfg_file_p1);
665 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
666 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using cfg [%u] : %s \n", 2, cfg_file_p2);
668 GNUNET_PROGRAM_run ((sizeof(argv_new) / sizeof(char *)) - 1, argv_new,
669 test_name, "nohelp", options, &run, NULL );
671 GNUNET_free(cfg_file_p1);
672 GNUNET_free(cfg_file_p2);
674 GNUNET_free(test_source);
675 GNUNET_free(test_plugin);
676 GNUNET_free(test_name);
678 GNUNET_TRANSPORT_TESTING_done (tth);
683 /* end of test_transport_address_switch.c */