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 static char *key_file_p1;
95 static char *cert_file_p1;
97 static char *key_file_p2;
98 static char *cert_file_p2;
100 static char *test_name;
102 static char bitmap[TOTAL_MSGS / 8];
104 static int msg_scheduled;
106 static int msg_recv_expected;
110 * Sets a bit active in the bitmap.
112 * @param bitIdx which bit to set
115 set_bit (unsigned int bitIdx)
118 unsigned int targetBit;
119 if (bitIdx >= sizeof(bitmap) * 8)
121 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "tried to set bit %d of %d(!?!?)\n", bitIdx, sizeof(bitmap) * 8);
124 arraySlot = bitIdx / 8;
125 targetBit = (1L << (bitIdx % 8));
126 bitmap[arraySlot] |= targetBit;
130 * Obtain a bit from bitmap.
131 * @param map the bitmap
132 * @param bit index from bitmap
134 * @return Bit \a bit from hashcode \a code
137 get_bit (const char *map, unsigned int bit)
139 if (bit >= TOTAL_MSGS)
141 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "get bit %d of %d(!?!?)\n", bit, sizeof(bitmap) * 8);
144 return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
150 unsigned long long delta;
156 for (i = 0; i < TOTAL_MSGS; i++)
158 if (get_bit(bitmap, i) == 0)
160 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Did not receive message %d\n", i);
165 GNUNET_SCHEDULER_cancel (die_task);
166 die_task = GNUNET_SCHEDULER_NO_TASK;
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
170 GNUNET_TRANSPORT_disconnect (p1.th);
171 GNUNET_TRANSPORT_disconnect (p2.th);
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Transports disconnected, returning success!\n");
176 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
177 GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
178 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
179 GNUNET_free(value_name);
181 "\nThroughput was %llu kb/s\n",
182 total_bytes * 1000 / 1024 / delta);
190 unsigned long long delta;
195 for (i = 0; i < TOTAL_MSGS; i++)
197 if (get_bit(bitmap, i) == 0)
199 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Did not receive message %d\n", i);
204 die_task = GNUNET_SCHEDULER_NO_TASK;
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
208 GNUNET_TRANSPORT_disconnect (p1.th);
209 GNUNET_TRANSPORT_disconnect (p2.th);
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Transports disconnected, returning success!\n");
214 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
216 "\nThroughput was %llu kb/s\n",
217 total_bytes * 1000 / 1024 / delta);
218 GNUNET_asprintf(&value_name, "unreliable_%s", test_name);
219 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
220 GNUNET_free(value_name);
221 GNUNET_asprintf(&value_name, "unreliable_failed_%s", test_name);
222 GAUGER ("TRANSPORT", value_name, (int)num_failed, "msgs");
223 GNUNET_free(value_name);
224 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Had %d failed messages!\n", num_failed);
232 stop_arm (struct PeerContext *p)
235 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
236 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
237 GNUNET_OS_process_wait (p->arm_proc);
238 GNUNET_OS_process_close (p->arm_proc);
241 GNUNET_CONFIGURATION_destroy (p->cfg);
246 end_badly (void *cls,
247 const struct GNUNET_SCHEDULER_TaskContext *tc)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 "Reliability failed: Last message sent %u Next message scheduled %u Last message received %u Message expected %u\n",
256 GNUNET_TRANSPORT_disconnect (p1.th);
257 GNUNET_TRANSPORT_disconnect (p2.th);
259 if (GNUNET_SCHEDULER_NO_TASK != tct)
261 GNUNET_SCHEDULER_cancel (tct);
262 tct = GNUNET_SCHEDULER_NO_TASK;
270 struct GNUNET_MessageHeader header;
276 get_size (unsigned int iter)
281 return iter + sizeof (struct TestMessage);
282 ret = (iter * iter * iter);
283 return sizeof (struct TestMessage) + (ret % 60000);
288 notify_receive (void *cls,
289 const struct GNUNET_PeerIdentity *peer,
290 const struct GNUNET_MessageHeader *message,
291 const struct GNUNET_TRANSPORT_ATS_Information *ats,
296 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
297 const struct TestMessage *hdr;
299 hdr = (const struct TestMessage*) message;
301 if (MTYPE != ntohs (message->type))
303 msg_recv_expected = n;
304 msg_recv = ntohl(hdr->num);
305 s = get_size (ntohl(hdr->num));
307 if (ntohs (message->size) != s)
309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
310 "Expected message %u of size %u, got %u bytes of message %u\n",
312 ntohs (message->size),
314 GNUNET_SCHEDULER_cancel (die_task);
315 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
319 memset (cbuf, ntohl(hdr->num), s - sizeof (struct TestMessage));
320 if (0 != memcmp (cbuf,
322 s - sizeof (struct TestMessage)))
324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325 "Expected message %u with bits %u, but body did not match\n",
326 ntohl(hdr->num), (unsigned char) n);
327 GNUNET_SCHEDULER_cancel (die_task);
328 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
332 if (ntohl(hdr->num) % 5 == 0)
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Got message %u of size %u\n",
337 ntohs (message->size));
341 set_bit(ntohl(hdr->num));
342 if (0 == (n % (5000)))
344 fprintf (stderr, ".");
345 GNUNET_SCHEDULER_cancel (die_task);
346 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
356 notify_ready (void *cls, size_t size, void *buf)
360 struct TestMessage hdr;
372 GNUNET_assert (size >= s);
373 GNUNET_assert (buf != NULL);
377 hdr.header.size = htons (s);
378 hdr.header.type = htons (MTYPE);
381 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
382 ret += sizeof (struct TestMessage);
383 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
384 ret += s - sizeof (struct TestMessage);
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Sending message %u of size %u\n",
396 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
397 break; /* sometimes pack buffer full, sometimes not */
399 while (size - ret >= s);
402 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
411 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "All messages scheduled to be sent!!\n");
412 GNUNET_SCHEDULER_cancel(die_task);
413 die_task = GNUNET_SCHEDULER_add_delayed (UNRELIABLE_TIMEOUT, &end_unreliably, NULL);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Returning total message block of size %u\n",
428 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Peer `%4s' disconnected (%p)!\n",
434 GNUNET_i2s (peer), cls);
441 exchange_hello_last (void *cls,
442 const struct GNUNET_MessageHeader *message)
444 struct PeerContext *me = cls;
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Exchanging HELLO of size %d with peer (%s)!\n",
448 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
449 GNUNET_i2s (&me->id));
450 GNUNET_assert (message != NULL);
451 GNUNET_assert (GNUNET_OK ==
452 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
454 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
460 exchange_hello (void *cls,
461 const struct GNUNET_MessageHeader *message)
463 struct PeerContext *me = cls;
465 GNUNET_assert (message != NULL);
466 GNUNET_assert (GNUNET_OK ==
467 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Exchanging HELLO of size %d from peer %s!\n",
471 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
472 GNUNET_i2s (&me->id));
473 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
478 notify_connect (void *cls,
479 const struct GNUNET_PeerIdentity *peer,
480 const struct GNUNET_TRANSPORT_ATS_Information *ats,
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Peer `%4s' connected to us (%p)!\n",
489 GNUNET_TRANSPORT_set_quota (p1.th,
491 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
492 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
493 start_time = GNUNET_TIME_absolute_get ();
498 GNUNET_TRANSPORT_set_quota (p2.th,
500 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
501 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
506 GNUNET_SCHEDULER_cancel (die_task);
507 GNUNET_SCHEDULER_cancel (tct);
508 tct = GNUNET_SCHEDULER_NO_TASK;
509 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
510 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
511 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
513 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
515 get_size (0), 0, TIMEOUT,
524 setup_peer (struct PeerContext *p, const char *cfgname)
526 p->cfg = GNUNET_CONFIGURATION_create ();
528 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
529 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
530 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
531 GNUNET_DISK_directory_remove (p->servicehome);
535 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
536 "gnunet-service-arm",
537 "gnunet-service-arm",
541 "-c", cfgname, NULL);
544 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
549 GNUNET_assert (p->th != NULL);
554 * Return the actual path to a file found in the current
555 * PATH environment variable.
557 * @param binary the name of the file to find
560 get_path_from_PATH (char *binary)
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 _("PATH environment variable is unset.\n"));
575 path = GNUNET_strdup (p); /* because we write on it */
576 buf = GNUNET_malloc (strlen (path) + 20);
579 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
582 sprintf (buf, "%s/%s", pos, binary);
583 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
590 sprintf (buf, "%s/%s", pos, binary);
591 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
602 * Check whether the suid bit is set on a file.
603 * Attempts to find the file using the current
604 * PATH environment variable as a search path.
606 * @param binary the name of the file to check
608 * @return GNUNET_YES if the binary is found and
609 * can be run properly, GNUNET_NO otherwise
612 check_gnunet_nat_binary(char *binary)
622 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
623 p = get_path_from_PATH (binaryexe);
626 p = get_path_from_PATH (binary);
630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631 _("Could not find binary `%s' in PATH!\n"),
635 if (0 != STAT (p, &statbuf))
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638 _("stat (%s) failed: %s\n"),
642 return GNUNET_SYSERR;
646 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
647 (statbuf.st_uid == 0) )
651 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
652 if (INVALID_SOCKET == rawsock)
654 DWORD err = GetLastError ();
655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
656 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
657 return GNUNET_NO; /* not running as administrator */
659 closesocket (rawsock);
666 try_connect (void *cls,
667 const struct GNUNET_SCHEDULER_TaskContext *tc)
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
670 "Asking peers to connect...\n");
671 GNUNET_TRANSPORT_try_connect (p2.th,
673 GNUNET_TRANSPORT_try_connect (p1.th,
675 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
685 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
687 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
692 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
693 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
697 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
698 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
702 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
703 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
707 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
708 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
712 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
713 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
717 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
718 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
722 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
723 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
727 GNUNET_assert(p1.th != NULL);
728 GNUNET_assert(p2.th != NULL);
729 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
730 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
731 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
738 char *const argv[] = { "test-transport-api-reliability",
740 "test_transport_api_data.conf",
746 struct GNUNET_GETOPT_CommandLineOption options[] = {
747 GNUNET_GETOPT_OPTION_END
751 setTransportOptions("test_transport_api_data.conf");
755 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
757 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
761 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
762 argv, "test-transport-api-reliability", "nohelp",
770 if (0 == stat (cert_file_p1, &sbuf ))
772 if (0 == remove(cert_file_p1))
773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
778 if (0 == stat (key_file_p1, &sbuf ))
780 if (0 == remove(key_file_p1))
781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
786 if (0 == stat (cert_file_p2, &sbuf ))
788 if (0 == remove(cert_file_p2))
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
794 if (0 == stat (key_file_p2, &sbuf ))
796 if (0 == remove(key_file_p2))
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
799 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
801 GNUNET_free(key_file_p1);
802 GNUNET_free(key_file_p2);
803 GNUNET_free(cert_file_p1);
804 GNUNET_free(cert_file_p2);
807 if ((p1.servicehome != NULL) && (p2.servicehome != NULL))
809 GNUNET_DISK_directory_remove (p1.servicehome);
810 GNUNET_DISK_directory_remove (p2.servicehome);
811 GNUNET_free(p1.servicehome);
812 GNUNET_free(p2.servicehome);
821 main (int argc, char *argv[])
825 return GNUNET_SYSERR;
828 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
829 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
831 if (strstr(argv[0], "tcp_nat") != NULL)
833 is_tcp_nat = GNUNET_YES;
834 GNUNET_asprintf(&test_name, "tcp_nat");
836 else if (strstr(argv[0], "tcp") != NULL)
839 GNUNET_asprintf(&test_name, "tcp");
841 else if (strstr(argv[0], "https") != NULL)
843 is_https = GNUNET_YES;
844 GNUNET_asprintf(&test_name, "https");
846 else if (strstr(argv[0], "http") != NULL)
848 is_http = GNUNET_YES;
849 GNUNET_asprintf(&test_name, "http");
851 else if (strstr(argv[0], "udp") != NULL)
854 GNUNET_asprintf(&test_name, "udp");
856 else if (strstr(argv[0], "unix") != NULL)
858 is_unix = GNUNET_YES;
859 GNUNET_asprintf(&test_name, "unix");
861 else if (strstr(argv[0], "wlan") != NULL)
863 is_wlan = GNUNET_YES;
865 GNUNET_log_setup ("test-transport-api-reliability",
874 GNUNET_free_non_null(test_name);
878 /* end of test_transport_api_reliability.c */