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 (80000 * 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;
115 * Sets a bit active in the bitmap.
117 * @param bitIdx which bit to set
120 set_bit (unsigned int bitIdx)
123 unsigned int targetBit;
124 if (bitIdx >= sizeof(bitmap) * 8)
126 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n", bitIdx, sizeof(bitmap) * 8);
129 arraySlot = bitIdx / 8;
130 targetBit = (1L << (bitIdx % 8));
131 bitmap[arraySlot] |= targetBit;
135 * Obtain a bit from bitmap.
136 * @param map the bitmap
137 * @param bit index from bitmap
139 * @return Bit \a bit from hashcode \a code
142 get_bit (const char *map, unsigned int bit)
144 if (bit >= TOTAL_MSGS)
146 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit, sizeof(bitmap) * 8);
149 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
155 unsigned long long delta;
161 for (i = 0; i < TOTAL_MSGS; i++)
163 if (get_bit(bitmap, i) == 0)
165 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
170 if (GNUNET_SCHEDULER_NO_TASK != die_task)
171 GNUNET_SCHEDULER_cancel (die_task);
172 die_task = GNUNET_SCHEDULER_NO_TASK;
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
177 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
180 GNUNET_TRANSPORT_disconnect (p1.th);
181 GNUNET_TRANSPORT_disconnect (p2.th);
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Transports disconnected, returning success!\n");
186 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
187 GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
188 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
189 GNUNET_free(value_name);
191 "\nThroughput was %llu kb/s\n",
192 total_bytes * 1000 / 1024 / delta);
200 unsigned long long delta;
205 for (i = 0; i < TOTAL_MSGS; i++)
207 if (get_bit(bitmap, i) == 0)
209 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Did not receive message %d\n", i);
214 die_task = GNUNET_SCHEDULER_NO_TASK;
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
219 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
221 GNUNET_TRANSPORT_disconnect (p1.th);
222 GNUNET_TRANSPORT_disconnect (p2.th);
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Transports disconnected, returning success!\n");
227 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
229 "\nThroughput was %llu kb/s\n",
230 total_bytes * 1000 / 1024 / delta);
231 GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
232 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
233 GNUNET_free(value_name);
234 GNUNET_asprintf(&value_name, "unreliable_failed_%s", test_name);
235 GAUGER ("TRANSPORT", value_name, (int)num_failed, "msgs");
236 GNUNET_free(value_name);
237 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Had %d failed messages!\n", num_failed);
245 stop_arm (struct PeerContext *p)
248 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
249 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
250 GNUNET_OS_process_wait (p->arm_proc);
251 GNUNET_OS_process_close (p->arm_proc);
254 GNUNET_CONFIGURATION_destroy (p->cfg);
259 end_badly (void *cls,
260 const struct GNUNET_SCHEDULER_TaskContext *tc)
262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
263 "Reliability failed: Last message sent %u Next message scheduled %u Last message received %u Message expected %u\n",
271 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
274 GNUNET_TRANSPORT_disconnect (p1.th);
275 GNUNET_TRANSPORT_disconnect (p2.th);
277 if (GNUNET_SCHEDULER_NO_TASK != tct)
279 GNUNET_SCHEDULER_cancel (tct);
280 tct = GNUNET_SCHEDULER_NO_TASK;
288 struct GNUNET_MessageHeader header;
294 get_size (unsigned int iter)
299 return iter + sizeof (struct TestMessage);
300 ret = (iter * iter * iter);
301 return sizeof (struct TestMessage) + (ret % 60000);
306 notify_receive (void *cls,
307 const struct GNUNET_PeerIdentity *peer,
308 const struct GNUNET_MessageHeader *message,
309 const struct GNUNET_TRANSPORT_ATS_Information *ats,
314 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
315 const struct TestMessage *hdr;
317 hdr = (const struct TestMessage*) message;
319 if (MTYPE != ntohs (message->type))
321 msg_recv_expected = n;
322 msg_recv = ntohl(hdr->num);
323 s = get_size (ntohl(hdr->num));
325 if (ntohs (message->size) != s)
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 "Expected message %u of size %u, got %u bytes of message %u\n",
330 ntohs (message->size),
332 if (GNUNET_SCHEDULER_NO_TASK != die_task)
333 GNUNET_SCHEDULER_cancel (die_task);
334 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
338 memset (cbuf, ntohl(hdr->num), s - sizeof (struct TestMessage));
339 if (0 != memcmp (cbuf,
341 s - sizeof (struct TestMessage)))
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 "Expected message %u with bits %u, but body did not match\n",
345 ntohl(hdr->num), (unsigned char) n);
346 if (GNUNET_SCHEDULER_NO_TASK != die_task)
347 GNUNET_SCHEDULER_cancel (die_task);
348 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
352 if (ntohl(hdr->num) % 5 == 0)
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Got message %u of size %u\n",
357 ntohs (message->size));
361 set_bit(ntohl(hdr->num));
362 if (0 == (n % (5000)))
364 fprintf (stderr, ".");
365 if (GNUNET_SCHEDULER_NO_TASK != die_task)
366 GNUNET_SCHEDULER_cancel (die_task);
367 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
377 notify_ready (void *cls, size_t size, void *buf)
381 struct TestMessage hdr;
395 GNUNET_assert (size >= s);
396 GNUNET_assert (buf != NULL);
400 hdr.header.size = htons (s);
401 hdr.header.type = htons (MTYPE);
404 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
405 ret += sizeof (struct TestMessage);
406 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
407 ret += s - sizeof (struct TestMessage);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Sending message %u of size %u\n",
419 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
420 break; /* sometimes pack buffer full, sometimes not */
422 while (size - ret >= s);
425 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
434 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent!!\n");
435 if (GNUNET_SCHEDULER_NO_TASK != die_task)
436 GNUNET_SCHEDULER_cancel(die_task);
437 die_task = GNUNET_SCHEDULER_add_delayed (UNRELIABLE_TIMEOUT, &end_unreliably, NULL);
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Returning total message block of size %u\n",
452 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "Peer `%4s' disconnected (%p)!\n",
458 GNUNET_i2s (peer), cls);
461 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
467 exchange_hello_last (void *cls,
468 const struct GNUNET_MessageHeader *message)
470 struct PeerContext *me = cls;
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Exchanging HELLO of size %d with peer (%s)!\n",
474 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
475 GNUNET_i2s (&me->id));
476 GNUNET_assert (message != NULL);
477 GNUNET_assert (GNUNET_OK ==
478 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
480 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
486 exchange_hello (void *cls,
487 const struct GNUNET_MessageHeader *message)
489 struct PeerContext *me = cls;
491 GNUNET_assert (message != NULL);
492 GNUNET_assert (GNUNET_OK ==
493 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Exchanging HELLO of size %d from peer %s!\n",
497 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
498 GNUNET_i2s (&me->id));
499 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
504 notify_connect (void *cls,
505 const struct GNUNET_PeerIdentity *peer,
506 const struct GNUNET_TRANSPORT_ATS_Information *ats,
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Peer `%4s' connected to us (%p)!\n",
515 GNUNET_TRANSPORT_set_quota (p1.th,
517 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
518 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
519 start_time = GNUNET_TIME_absolute_get ();
524 GNUNET_TRANSPORT_set_quota (p2.th,
526 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
527 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
532 if (GNUNET_SCHEDULER_NO_TASK != die_task)
534 GNUNET_SCHEDULER_cancel (die_task);
536 if (GNUNET_SCHEDULER_NO_TASK != tct)
538 GNUNET_SCHEDULER_cancel (tct);
539 tct = GNUNET_SCHEDULER_NO_TASK;
541 if (p2_hello_canceled == GNUNET_NO)
543 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
544 p2_hello_canceled = GNUNET_YES;
546 if (p1_hello_canceled == GNUNET_NO)
548 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
549 p1_hello_canceled = GNUNET_YES;
552 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
554 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
556 get_size (0), 0, TIMEOUT,
565 setup_peer (struct PeerContext *p, const char *cfgname)
567 p->cfg = GNUNET_CONFIGURATION_create ();
569 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
570 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
571 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
572 GNUNET_DISK_directory_remove (p->servicehome);
576 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
577 "gnunet-service-arm",
578 "gnunet-service-arm",
582 "-c", cfgname, NULL);
585 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
590 GNUNET_assert (p->th != NULL);
595 * Return the actual path to a file found in the current
596 * PATH environment variable.
598 * @param binary the name of the file to find
601 get_path_from_PATH (char *binary)
612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
613 _("PATH environment variable is unset.\n"));
616 path = GNUNET_strdup (p); /* because we write on it */
617 buf = GNUNET_malloc (strlen (path) + 20);
620 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
623 sprintf (buf, "%s/%s", pos, binary);
624 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
631 sprintf (buf, "%s/%s", pos, binary);
632 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
643 * Check whether the suid bit is set on a file.
644 * Attempts to find the file using the current
645 * PATH environment variable as a search path.
647 * @param binary the name of the file to check
649 * @return GNUNET_YES if the binary is found and
650 * can be run properly, GNUNET_NO otherwise
653 check_gnunet_nat_binary(char *binary)
663 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
664 p = get_path_from_PATH (binaryexe);
667 p = get_path_from_PATH (binary);
671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
672 _("Could not find binary `%s' in PATH!\n"),
676 if (0 != STAT (p, &statbuf))
678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
679 _("stat (%s) failed: %s\n"),
683 return GNUNET_SYSERR;
687 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
688 (statbuf.st_uid == 0) )
692 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
693 if (INVALID_SOCKET == rawsock)
695 DWORD err = GetLastError ();
696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
697 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
698 return GNUNET_NO; /* not running as administrator */
700 closesocket (rawsock);
707 try_connect (void *cls,
708 const struct GNUNET_SCHEDULER_TaskContext *tc)
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
711 "Asking peers to connect...\n");
712 GNUNET_TRANSPORT_try_connect (p2.th,
714 GNUNET_TRANSPORT_try_connect (p1.th,
716 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
726 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
728 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
733 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
734 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
738 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
739 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
743 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
744 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
748 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
749 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
753 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
754 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
758 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
759 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
763 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
764 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
768 GNUNET_assert(p1.th != NULL);
769 GNUNET_assert(p2.th != NULL);
770 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
771 p1_hello_canceled = GNUNET_NO;
772 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
773 p2_hello_canceled = GNUNET_NO;
774 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
781 char *const argv[] = { "test-transport-api-unreliability",
783 "test_transport_api_data.conf",
789 struct GNUNET_GETOPT_CommandLineOption options[] = {
790 GNUNET_GETOPT_OPTION_END
794 setTransportOptions("test_transport_api_data.conf");
798 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
800 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
804 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
805 argv, "test-transport-api-unreliability", "nohelp",
813 if (0 == stat (cert_file_p1, &sbuf ))
815 if (0 == remove(cert_file_p1))
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
821 if (0 == stat (key_file_p1, &sbuf ))
823 if (0 == remove(key_file_p1))
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
829 if (0 == stat (cert_file_p2, &sbuf ))
831 if (0 == remove(cert_file_p2))
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
837 if (0 == stat (key_file_p2, &sbuf ))
839 if (0 == remove(key_file_p2))
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
842 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
844 GNUNET_free(key_file_p1);
845 GNUNET_free(key_file_p2);
846 GNUNET_free(cert_file_p1);
847 GNUNET_free(cert_file_p2);
850 if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
852 GNUNET_DISK_directory_remove (p1.servicehome);
853 GNUNET_DISK_directory_remove (p2.servicehome);
854 GNUNET_free(p1.servicehome);
855 GNUNET_free(p2.servicehome);
864 main (int argc, char *argv[])
868 return GNUNET_SYSERR;
871 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
872 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
874 if (strstr(argv[0], "tcp_nat") != NULL)
876 is_tcp_nat = GNUNET_YES;
877 GNUNET_asprintf(&test_name, "tcp_nat");
879 else if (strstr(argv[0], "tcp") != NULL)
882 GNUNET_asprintf(&test_name, "tcp");
884 else if (strstr(argv[0], "https") != NULL)
886 is_https = GNUNET_YES;
887 GNUNET_asprintf(&test_name, "https");
889 else if (strstr(argv[0], "http") != NULL)
891 is_http = GNUNET_YES;
892 GNUNET_asprintf(&test_name, "http");
894 else if (strstr(argv[0], "udp") != NULL)
897 GNUNET_asprintf(&test_name, "udp");
899 else if (strstr(argv[0], "unix") != NULL)
901 is_unix = GNUNET_YES;
902 GNUNET_asprintf(&test_name, "unix");
904 else if (strstr(argv[0], "wlan") != NULL)
906 is_wlan = GNUNET_YES;
908 GNUNET_log_setup ("test-transport-api-unreliability",
917 GNUNET_free_non_null(test_name);
921 /* end of test_transport_api_unreliability.c */