2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/test_transport_api_unreliability.c
22 * @brief test case for transports; ensures messages get
23 * through, regardless of order
25 * This test case serves as a base for unreliable
26 * transport test cases to check that the transports
27 * achieve reliable message delivery.
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_server_lib.h"
37 #include "gnunet_transport_service.h"
39 #include "transport.h"
40 #include "transport-testing.h"
42 #define VERBOSE GNUNET_NO
44 #define VERBOSE_ARM GNUNET_NO
46 #define START_ARM GNUNET_YES
49 * Note that this value must not significantly exceed
50 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
51 * messages may be dropped even for a reliable transport.
53 #define TOTAL_MSGS (1024 * 3) /* Total messages should be divisible by 8, so we can make a nice bitmap */
56 * How long until we give up on transmitting the message?
58 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
60 #define UNRELIABLE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
64 static struct PeerContext p1;
66 static struct PeerContext p2;
72 static int is_tcp_nat;
86 static unsigned long long total_bytes;
88 static struct GNUNET_TIME_Absolute start_time;
90 static GNUNET_SCHEDULER_TaskIdentifier die_task;
92 static GNUNET_SCHEDULER_TaskIdentifier tct;
94 struct GNUNET_TRANSPORT_TransmitHandle *th_p2;
96 static char *key_file_p1;
97 static char *cert_file_p1;
99 static char *key_file_p2;
100 static char *cert_file_p2;
102 static char *test_name;
104 static char bitmap[TOTAL_MSGS / 8];
106 static int msg_scheduled;
108 static int msg_recv_expected;
111 static int p1_hello_canceled;
112 static int p2_hello_canceled;
114 static int test_failed;
117 * Sets a bit active in the bitmap.
119 * @param bitIdx which bit to set
122 set_bit (unsigned int bitIdx)
125 unsigned int targetBit;
127 if (bitIdx >= sizeof (bitmap) * 8)
129 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n",
130 bitIdx, sizeof (bitmap) * 8);
133 arraySlot = bitIdx / 8;
134 targetBit = (1L << (bitIdx % 8));
135 bitmap[arraySlot] |= targetBit;
139 * Obtain a bit from bitmap.
140 * @param map the bitmap
141 * @param bit index from bitmap
143 * @return Bit \a bit from hashcode \a code
146 get_bit (const char *map, unsigned int bit)
148 if (bit >= TOTAL_MSGS)
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit,
151 sizeof (bitmap) * 8);
154 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
160 unsigned long long delta;
166 for (i = 0; i < TOTAL_MSGS; i++)
168 if (get_bit (bitmap, i) == 0)
170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
175 if (GNUNET_SCHEDULER_NO_TASK != die_task)
176 GNUNET_SCHEDULER_cancel (die_task);
177 die_task = GNUNET_SCHEDULER_NO_TASK;
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
182 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th_p2);
185 GNUNET_TRANSPORT_disconnect (p1.th);
186 GNUNET_TRANSPORT_disconnect (p2.th);
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "Transports disconnected, returning success!\n");
191 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
192 GNUNET_asprintf (&value_name, "unreliable_%s", test_name);
193 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
195 GNUNET_free (value_name);
196 fprintf (stderr, "\nThroughput was %llu kb/s\n",
197 total_bytes * 1000 / 1024 / delta);
205 unsigned long long delta;
211 for (i = 0; i < TOTAL_MSGS; i++)
213 if (get_bit (bitmap, i) == 0)
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Did not receive message %d\n", i);
220 die_task = GNUNET_SCHEDULER_NO_TASK;
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
225 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th_p2);
227 GNUNET_TRANSPORT_disconnect (p1.th);
228 GNUNET_TRANSPORT_disconnect (p2.th);
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "Transports disconnected, returning success!\n");
233 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
234 fprintf (stderr, "\nThroughput was %llu kb/s\n",
235 total_bytes * 1000 / 1024 / delta);
236 GNUNET_asprintf (&value_name, "unreliable_%s", test_name);
237 GAUGER ("TRANSPORT", value_name, (int) (total_bytes * 1000 / 1024 / delta),
239 GNUNET_free (value_name);
240 GNUNET_asprintf (&value_name, "unreliable_failed_%s", test_name);
241 GAUGER ("TRANSPORT", value_name, (int) num_failed, "msgs");
242 GNUNET_free (value_name);
243 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Had %d failed messages!\n",
252 stop_arm (struct PeerContext *p)
255 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
256 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
257 GNUNET_OS_process_wait (p->arm_proc);
258 GNUNET_OS_process_close (p->arm_proc);
261 GNUNET_CONFIGURATION_destroy (p->cfg);
267 exchange_hello_last (void *cls, const struct GNUNET_MessageHeader *message)
269 struct PeerContext *me = cls;
271 GNUNET_assert (message != NULL);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Exchanging HELLO of size %d with peer (%s)!\n",
274 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
275 message), GNUNET_i2s (&me->id));
276 GNUNET_assert (GNUNET_OK ==
277 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
279 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
285 exchange_hello (void *cls, const struct GNUNET_MessageHeader *message)
287 struct PeerContext *me = cls;
289 GNUNET_assert (message != NULL);
290 GNUNET_assert (GNUNET_OK ==
291 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Exchanging HELLO of size %d from peer %s!\n",
295 (int) GNUNET_HELLO_size ((const struct GNUNET_HELLO_Message *)
296 message), GNUNET_i2s (&me->id));
297 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
303 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
305 if (test_failed == GNUNET_NO)
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
310 msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
315 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th_p2);
318 if (p2_hello_canceled == GNUNET_NO)
320 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
321 p2_hello_canceled = GNUNET_YES;
323 if (p1_hello_canceled == GNUNET_NO)
325 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
326 p1_hello_canceled = GNUNET_YES;
328 GNUNET_TRANSPORT_disconnect (p1.th);
329 GNUNET_TRANSPORT_disconnect (p2.th);
330 if (GNUNET_SCHEDULER_NO_TASK != tct)
332 GNUNET_SCHEDULER_cancel (tct);
333 tct = GNUNET_SCHEDULER_NO_TASK;
342 struct GNUNET_MessageHeader header;
348 get_size (unsigned int iter)
352 ret = (iter * iter * iter);
353 return sizeof (struct TestMessage) + (ret % 60000);
358 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
359 const struct GNUNET_MessageHeader *message,
360 const struct GNUNET_TRANSPORT_ATS_Information *ats,
365 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
366 const struct TestMessage *hdr;
368 hdr = (const struct TestMessage *) message;
370 if (MTYPE != ntohs (message->type))
372 msg_recv_expected = n;
373 msg_recv = ntohl (hdr->num);
374 s = get_size (ntohl (hdr->num));
376 if (ntohs (message->size) != s)
378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
379 "Expected message %u of size %u, got %u bytes of message %u\n",
380 ntohl (hdr->num), s, ntohs (message->size), ntohl (hdr->num));
381 if (GNUNET_SCHEDULER_NO_TASK != die_task)
382 GNUNET_SCHEDULER_cancel (die_task);
383 test_failed = GNUNET_YES;
384 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
388 memset (cbuf, ntohl (hdr->num), s - sizeof (struct TestMessage));
389 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 "Expected message %u with bits %u, but body did not match\n",
393 ntohl (hdr->num), (unsigned char) n);
394 if (GNUNET_SCHEDULER_NO_TASK != die_task)
395 GNUNET_SCHEDULER_cancel (die_task);
396 test_failed = GNUNET_YES;
397 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
401 if (ntohl (hdr->num) % 5 == 0)
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
404 ntohl (hdr->num), ntohs (message->size));
408 set_bit (ntohl (hdr->num));
409 if (0 == (n % (TOTAL_MSGS / 100)))
411 fprintf (stderr, ".");
412 if (GNUNET_SCHEDULER_NO_TASK != die_task)
413 GNUNET_SCHEDULER_cancel (die_task);
414 test_failed = GNUNET_YES;
415 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
423 notify_ready (void *cls, size_t size, void *buf)
427 struct TestMessage hdr;
441 GNUNET_assert (size >= s);
442 GNUNET_assert (buf != NULL);
446 hdr.header.size = htons (s);
447 hdr.header.type = htons (MTYPE);
450 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
451 ret += sizeof (struct TestMessage);
452 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
453 ret += s - sizeof (struct TestMessage);
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
463 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
464 break; /* sometimes pack buffer full, sometimes not */
466 while (size - ret >= s);
470 GNUNET_TRANSPORT_notify_transmit_ready (p2.th, &p1.id, s, 0, TIMEOUT,
471 ¬ify_ready, NULL);
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "All messages scheduled to be sent!!\n");
478 if (GNUNET_SCHEDULER_NO_TASK != die_task)
479 GNUNET_SCHEDULER_cancel (die_task);
481 GNUNET_SCHEDULER_add_delayed (UNRELIABLE_TIMEOUT, &end_unreliably,
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Returning total message block of size %u\n", ret);
496 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
501 GNUNET_i2s (peer), cls);
505 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th_p2);
513 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
514 const struct GNUNET_TRANSPORT_ATS_Information *ats,
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
518 GNUNET_i2s (peer), cls);
521 GNUNET_TRANSPORT_set_quota (p1.th, &p2.id,
522 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
524 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
526 start_time = GNUNET_TIME_absolute_get ();
531 GNUNET_TRANSPORT_set_quota (p2.th, &p1.id,
532 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
534 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
540 if (GNUNET_SCHEDULER_NO_TASK != die_task)
542 GNUNET_SCHEDULER_cancel (die_task);
544 if (GNUNET_SCHEDULER_NO_TASK != tct)
546 GNUNET_SCHEDULER_cancel (tct);
547 tct = GNUNET_SCHEDULER_NO_TASK;
549 if (p2_hello_canceled == GNUNET_NO)
551 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
552 p2_hello_canceled = GNUNET_YES;
554 if (p1_hello_canceled == GNUNET_NO)
556 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
557 p1_hello_canceled = GNUNET_YES;
560 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
561 GNUNET_TRANSPORT_notify_transmit_ready (p2.th, &p1.id, get_size (0), 0,
562 TIMEOUT, ¬ify_ready, NULL);
569 setup_peer (struct PeerContext *p, const char *cfgname)
571 memset (p, 0, sizeof (*p));
572 p->cfg = GNUNET_CONFIGURATION_create ();
574 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
575 if (GNUNET_CONFIGURATION_have_value (p->cfg, "PATHS", "SERVICEHOME"))
577 GNUNET_assert (GNUNET_OK ==
578 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS",
581 GNUNET_DISK_directory_remove (p->servicehome);
587 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
588 "gnunet-service-arm",
592 "-c", cfgname, NULL);
596 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, ¬ify_receive,
597 ¬ify_connect, ¬ify_disconnect);
598 GNUNET_assert (p->th != NULL);
602 try_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking peers to connect...\n");
605 GNUNET_TRANSPORT_try_connect (p2.th, &p1.id);
606 GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
608 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &try_connect,
615 run (void *cls, char *const *args, const char *cfgfile,
616 const struct GNUNET_CONFIGURATION_Handle *cfg)
618 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
621 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
622 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
626 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
627 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
631 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
632 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
636 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
637 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
641 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
642 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
646 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
647 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
651 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
652 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
656 GNUNET_assert (p1.th != NULL);
657 GNUNET_assert (p2.th != NULL);
658 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
659 p1_hello_canceled = GNUNET_NO;
660 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
661 p2_hello_canceled = GNUNET_NO;
662 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
669 char *const argv[] = { "test-transport-api-unreliability",
671 "test_transport_api_data.conf",
677 struct GNUNET_GETOPT_CommandLineOption options[] = {
678 GNUNET_GETOPT_OPTION_END
682 setTransportOptions ("test_transport_api_data.conf");
686 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
687 "test-transport-api-unreliability", "nohelp", options,
696 if (0 == stat (cert_file_p1, &sbuf))
698 if (0 == remove (cert_file_p1))
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Successfully removed existing certificate file `%s'\n",
703 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",
707 if (0 == stat (key_file_p1, &sbuf))
709 if (0 == remove (key_file_p1))
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Successfully removed private key file `%s'\n",
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
715 "Failed to private key file `%s'\n", key_file_p1);
718 if (0 == stat (cert_file_p2, &sbuf))
720 if (0 == remove (cert_file_p2))
721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
722 "Successfully removed existing certificate file `%s'\n",
725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",
729 if (0 == stat (key_file_p2, &sbuf))
731 if (0 == remove (key_file_p2))
732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
733 "Successfully removed private key file `%s'\n",
736 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
737 "Failed to private key file `%s'\n", key_file_p2);
739 GNUNET_free (key_file_p1);
740 GNUNET_free (key_file_p2);
741 GNUNET_free (cert_file_p1);
742 GNUNET_free (cert_file_p2);
745 if (p1.servicehome != NULL)
747 GNUNET_DISK_directory_remove (p1.servicehome);
748 GNUNET_free (p1.servicehome);
750 if (p2.servicehome != NULL)
752 GNUNET_DISK_directory_remove (p2.servicehome);
753 GNUNET_free (p2.servicehome);
760 main (int argc, char *argv[])
765 test_failed = GNUNET_NO;
767 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
768 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
770 if (strstr (argv[0], "tcp_nat") != NULL)
772 is_tcp_nat = GNUNET_YES;
773 GNUNET_asprintf (&test_name, "tcp_nat");
775 else if (strstr (argv[0], "tcp") != NULL)
778 GNUNET_asprintf (&test_name, "tcp");
780 else if (strstr (argv[0], "https") != NULL)
782 is_https = GNUNET_YES;
783 GNUNET_asprintf (&test_name, "https");
785 else if (strstr (argv[0], "http") != NULL)
787 is_http = GNUNET_YES;
788 GNUNET_asprintf (&test_name, "http");
790 else if (strstr (argv[0], "udp") != NULL)
793 GNUNET_asprintf (&test_name, "udp");
795 else if (strstr (argv[0], "unix") != NULL)
797 is_unix = GNUNET_YES;
798 GNUNET_asprintf (&test_name, "unix");
800 else if (strstr (argv[0], "wlan") != NULL)
802 is_wlan = GNUNET_YES;
804 GNUNET_log_setup ("test-transport-api-unreliability",
813 if ((strstr (argv[0], "tcp_nat") != NULL) || (strstr (argv[0], "udp_nat") != NULL))
815 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
816 if (GNUNET_NO == nat_res)
818 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
819 "Cannot run NAT test: `%s' %s \n",
824 if (GNUNET_SYSERR == nat_res)
826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
827 "Cannot run NAT test: `%s' %s \n",
836 GNUNET_free_non_null (test_name);
840 /* end of test_transport_api_unreliability.c */