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;
112 * Sets a bit active in the bitmap.
114 * @param bitIdx which bit to set
117 set_bit (unsigned int bitIdx)
120 unsigned int targetBit;
121 if (bitIdx >= sizeof(bitmap) * 8)
123 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n", bitIdx, sizeof(bitmap) * 8);
126 arraySlot = bitIdx / 8;
127 targetBit = (1L << (bitIdx % 8));
128 bitmap[arraySlot] |= targetBit;
132 * Obtain a bit from bitmap.
133 * @param map the bitmap
134 * @param bit index from bitmap
136 * @return Bit \a bit from hashcode \a code
139 get_bit (const char *map, unsigned int bit)
141 if (bit >= TOTAL_MSGS)
143 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit, sizeof(bitmap) * 8);
146 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
152 unsigned long long delta;
158 for (i = 0; i < TOTAL_MSGS; i++)
160 if (get_bit(bitmap, i) == 0)
162 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
167 if (GNUNET_SCHEDULER_NO_TASK != tct)
168 GNUNET_SCHEDULER_cancel (die_task);
169 die_task = GNUNET_SCHEDULER_NO_TASK;
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
174 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
177 GNUNET_TRANSPORT_disconnect (p1.th);
178 GNUNET_TRANSPORT_disconnect (p2.th);
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Transports disconnected, returning success!\n");
183 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
184 GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
185 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
186 GNUNET_free(value_name);
188 "\nThroughput was %llu kb/s\n",
189 total_bytes * 1000 / 1024 / delta);
197 unsigned long long delta;
202 for (i = 0; i < TOTAL_MSGS; i++)
204 if (get_bit(bitmap, i) == 0)
206 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Did not receive message %d\n", i);
211 die_task = GNUNET_SCHEDULER_NO_TASK;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
216 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
218 GNUNET_TRANSPORT_disconnect (p1.th);
219 GNUNET_TRANSPORT_disconnect (p2.th);
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Transports disconnected, returning success!\n");
224 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
226 "\nThroughput was %llu kb/s\n",
227 total_bytes * 1000 / 1024 / delta);
228 GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
229 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
230 GNUNET_free(value_name);
231 GNUNET_asprintf(&value_name, "unreliable_failed_%s", test_name);
232 GAUGER ("TRANSPORT", value_name, (int)num_failed, "msgs");
233 GNUNET_free(value_name);
234 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Had %d failed messages!\n", num_failed);
242 stop_arm (struct PeerContext *p)
245 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
246 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
247 GNUNET_OS_process_wait (p->arm_proc);
248 GNUNET_OS_process_close (p->arm_proc);
251 GNUNET_CONFIGURATION_destroy (p->cfg);
256 end_badly (void *cls,
257 const struct GNUNET_SCHEDULER_TaskContext *tc)
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 "Reliability failed: Last message sent %u Next message scheduled %u Last message received %u Message expected %u\n",
268 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
271 GNUNET_TRANSPORT_disconnect (p1.th);
272 GNUNET_TRANSPORT_disconnect (p2.th);
274 if (GNUNET_SCHEDULER_NO_TASK != tct)
276 GNUNET_SCHEDULER_cancel (tct);
277 tct = GNUNET_SCHEDULER_NO_TASK;
285 struct GNUNET_MessageHeader header;
291 get_size (unsigned int iter)
296 return iter + sizeof (struct TestMessage);
297 ret = (iter * iter * iter);
298 return sizeof (struct TestMessage) + (ret % 60000);
303 notify_receive (void *cls,
304 const struct GNUNET_PeerIdentity *peer,
305 const struct GNUNET_MessageHeader *message,
306 const struct GNUNET_TRANSPORT_ATS_Information *ats,
311 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
312 const struct TestMessage *hdr;
314 hdr = (const struct TestMessage*) message;
316 if (MTYPE != ntohs (message->type))
318 msg_recv_expected = n;
319 msg_recv = ntohl(hdr->num);
320 s = get_size (ntohl(hdr->num));
322 if (ntohs (message->size) != s)
324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325 "Expected message %u of size %u, got %u bytes of message %u\n",
327 ntohs (message->size),
329 if (GNUNET_SCHEDULER_NO_TASK != die_task)
330 GNUNET_SCHEDULER_cancel (die_task);
331 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
335 memset (cbuf, ntohl(hdr->num), s - sizeof (struct TestMessage));
336 if (0 != memcmp (cbuf,
338 s - sizeof (struct TestMessage)))
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 "Expected message %u with bits %u, but body did not match\n",
342 ntohl(hdr->num), (unsigned char) n);
343 if (GNUNET_SCHEDULER_NO_TASK != die_task)
344 GNUNET_SCHEDULER_cancel (die_task);
345 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
349 if (ntohl(hdr->num) % 5 == 0)
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Got message %u of size %u\n",
354 ntohs (message->size));
358 set_bit(ntohl(hdr->num));
359 if (0 == (n % (5000)))
361 fprintf (stderr, ".");
362 if (GNUNET_SCHEDULER_NO_TASK != die_task)
363 GNUNET_SCHEDULER_cancel (die_task);
364 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
374 notify_ready (void *cls, size_t size, void *buf)
378 struct TestMessage hdr;
392 GNUNET_assert (size >= s);
393 GNUNET_assert (buf != NULL);
397 hdr.header.size = htons (s);
398 hdr.header.type = htons (MTYPE);
401 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
402 ret += sizeof (struct TestMessage);
403 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
404 ret += s - sizeof (struct TestMessage);
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "Sending message %u of size %u\n",
416 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
417 break; /* sometimes pack buffer full, sometimes not */
419 while (size - ret >= s);
422 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
431 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent!!\n");
432 if (GNUNET_SCHEDULER_NO_TASK != die_task)
433 GNUNET_SCHEDULER_cancel(die_task);
434 die_task = GNUNET_SCHEDULER_add_delayed (UNRELIABLE_TIMEOUT, &end_unreliably, NULL);
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Returning total message block of size %u\n",
449 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "Peer `%4s' disconnected (%p)!\n",
455 GNUNET_i2s (peer), cls);
462 exchange_hello_last (void *cls,
463 const struct GNUNET_MessageHeader *message)
465 struct PeerContext *me = cls;
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Exchanging HELLO of size %d with peer (%s)!\n",
469 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
470 GNUNET_i2s (&me->id));
471 GNUNET_assert (message != NULL);
472 GNUNET_assert (GNUNET_OK ==
473 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
475 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
481 exchange_hello (void *cls,
482 const struct GNUNET_MessageHeader *message)
484 struct PeerContext *me = cls;
486 GNUNET_assert (message != NULL);
487 GNUNET_assert (GNUNET_OK ==
488 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Exchanging HELLO of size %d from peer %s!\n",
492 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
493 GNUNET_i2s (&me->id));
494 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
499 notify_connect (void *cls,
500 const struct GNUNET_PeerIdentity *peer,
501 const struct GNUNET_TRANSPORT_ATS_Information *ats,
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Peer `%4s' connected to us (%p)!\n",
510 GNUNET_TRANSPORT_set_quota (p1.th,
512 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
513 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
514 start_time = GNUNET_TIME_absolute_get ();
519 GNUNET_TRANSPORT_set_quota (p2.th,
521 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
522 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
527 if (GNUNET_SCHEDULER_NO_TASK != die_task)
528 GNUNET_SCHEDULER_cancel (die_task);
529 if (GNUNET_SCHEDULER_NO_TASK != die_task)
530 GNUNET_SCHEDULER_cancel (tct);
531 tct = GNUNET_SCHEDULER_NO_TASK;
532 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
533 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
534 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
536 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
538 get_size (0), 0, TIMEOUT,
547 setup_peer (struct PeerContext *p, const char *cfgname)
549 p->cfg = GNUNET_CONFIGURATION_create ();
551 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
552 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
553 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
554 GNUNET_DISK_directory_remove (p->servicehome);
558 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
559 "gnunet-service-arm",
560 "gnunet-service-arm",
564 "-c", cfgname, NULL);
567 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
572 GNUNET_assert (p->th != NULL);
577 * Return the actual path to a file found in the current
578 * PATH environment variable.
580 * @param binary the name of the file to find
583 get_path_from_PATH (char *binary)
594 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
595 _("PATH environment variable is unset.\n"));
598 path = GNUNET_strdup (p); /* because we write on it */
599 buf = GNUNET_malloc (strlen (path) + 20);
602 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
605 sprintf (buf, "%s/%s", pos, binary);
606 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
613 sprintf (buf, "%s/%s", pos, binary);
614 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
625 * Check whether the suid bit is set on a file.
626 * Attempts to find the file using the current
627 * PATH environment variable as a search path.
629 * @param binary the name of the file to check
631 * @return GNUNET_YES if the binary is found and
632 * can be run properly, GNUNET_NO otherwise
635 check_gnunet_nat_binary(char *binary)
645 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
646 p = get_path_from_PATH (binaryexe);
649 p = get_path_from_PATH (binary);
653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
654 _("Could not find binary `%s' in PATH!\n"),
658 if (0 != STAT (p, &statbuf))
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661 _("stat (%s) failed: %s\n"),
665 return GNUNET_SYSERR;
669 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
670 (statbuf.st_uid == 0) )
674 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
675 if (INVALID_SOCKET == rawsock)
677 DWORD err = GetLastError ();
678 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
679 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
680 return GNUNET_NO; /* not running as administrator */
682 closesocket (rawsock);
689 try_connect (void *cls,
690 const struct GNUNET_SCHEDULER_TaskContext *tc)
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
693 "Asking peers to connect...\n");
694 GNUNET_TRANSPORT_try_connect (p2.th,
696 GNUNET_TRANSPORT_try_connect (p1.th,
698 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
708 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
710 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
715 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
716 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
720 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
721 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
725 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
726 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
730 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
731 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
735 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
736 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
740 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
741 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
745 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
746 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
750 GNUNET_assert(p1.th != NULL);
751 GNUNET_assert(p2.th != NULL);
752 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
753 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
754 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
761 char *const argv[] = { "test-transport-api-reliability",
763 "test_transport_api_data.conf",
769 struct GNUNET_GETOPT_CommandLineOption options[] = {
770 GNUNET_GETOPT_OPTION_END
774 setTransportOptions("test_transport_api_data.conf");
778 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
780 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
784 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
785 argv, "test-transport-api-reliability", "nohelp",
793 if (0 == stat (cert_file_p1, &sbuf ))
795 if (0 == remove(cert_file_p1))
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
801 if (0 == stat (key_file_p1, &sbuf ))
803 if (0 == remove(key_file_p1))
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
809 if (0 == stat (cert_file_p2, &sbuf ))
811 if (0 == remove(cert_file_p2))
812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
817 if (0 == stat (key_file_p2, &sbuf ))
819 if (0 == remove(key_file_p2))
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
822 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
824 GNUNET_free(key_file_p1);
825 GNUNET_free(key_file_p2);
826 GNUNET_free(cert_file_p1);
827 GNUNET_free(cert_file_p2);
830 if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
832 GNUNET_DISK_directory_remove (p1.servicehome);
833 GNUNET_DISK_directory_remove (p2.servicehome);
834 GNUNET_free(p1.servicehome);
835 GNUNET_free(p2.servicehome);
844 main (int argc, char *argv[])
848 return GNUNET_SYSERR;
851 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
852 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
854 if (strstr(argv[0], "tcp_nat") != NULL)
856 is_tcp_nat = GNUNET_YES;
857 GNUNET_asprintf(&test_name, "tcp_nat");
859 else if (strstr(argv[0], "tcp") != NULL)
862 GNUNET_asprintf(&test_name, "tcp");
864 else if (strstr(argv[0], "https") != NULL)
866 is_https = GNUNET_YES;
867 GNUNET_asprintf(&test_name, "https");
869 else if (strstr(argv[0], "http") != NULL)
871 is_http = GNUNET_YES;
872 GNUNET_asprintf(&test_name, "http");
874 else if (strstr(argv[0], "udp") != NULL)
877 GNUNET_asprintf(&test_name, "udp");
879 else if (strstr(argv[0], "unix") != NULL)
881 is_unix = GNUNET_YES;
882 GNUNET_asprintf(&test_name, "unix");
884 else if (strstr(argv[0], "wlan") != NULL)
886 is_wlan = GNUNET_YES;
888 GNUNET_log_setup ("test-transport-api-reliability",
897 GNUNET_free_non_null(test_name);
901 /* end of test_transport_api_reliability.c */