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_reliability.c
22 * @brief base test case for transport implementations
24 * This test case serves as a base for tcp and http
25 * transport test cases to check that the transports
26 * achieve reliable message delivery.
29 #include "gnunet_common.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_transport_service.h"
38 #include "transport.h"
39 #include "transport-testing.h"
41 #define VERBOSE GNUNET_NO
43 #define VERBOSE_ARM GNUNET_NO
45 #define START_ARM GNUNET_YES
48 * Note that this value must not significantly exceed
49 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
50 * messages may be dropped even for a reliable transport.
52 #define TOTAL_MSGS (10000 * 2)
55 * How long until we give up on transmitting the message?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
61 static struct PeerContext p1;
63 static struct PeerContext p2;
69 static int is_tcp_nat;
83 static int test_failed;
85 static unsigned long long total_bytes;
87 static struct GNUNET_TIME_Absolute start_time;
89 static GNUNET_SCHEDULER_TaskIdentifier die_task;
91 static GNUNET_SCHEDULER_TaskIdentifier tct;
93 struct GNUNET_TRANSPORT_TransmitHandle * th_p2;
95 static char * key_file_p1;
96 static char * cert_file_p1;
98 static char * key_file_p2;
99 static char * cert_file_p2;
100 static char *test_name;
101 static int msg_scheduled;
103 static int msg_recv_expected;
106 static int p1_hello_canceled;
107 static int p2_hello_canceled;
110 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
112 #define OKPP do { ok++; } while (0)
119 unsigned long long delta;
122 if (die_task != GNUNET_SCHEDULER_NO_TASK)
123 GNUNET_SCHEDULER_cancel (die_task);
124 die_task = GNUNET_SCHEDULER_NO_TASK;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
130 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
133 GNUNET_TRANSPORT_disconnect (p1.th);
134 GNUNET_TRANSPORT_disconnect (p2.th);
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Transports disconnected, returning success!\n");
139 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
141 "\nThroughput was %llu kb/s\n",
142 total_bytes * 1000 / 1024 / delta);
143 GNUNET_asprintf(&value_name, "reliable_%s", test_name);
144 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
145 GNUNET_free(value_name);
153 stop_arm (struct PeerContext *p)
156 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
157 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
158 GNUNET_OS_process_wait (p->arm_proc);
159 GNUNET_OS_process_close (p->arm_proc);
162 GNUNET_CONFIGURATION_destroy (p->cfg);
167 end_badly (void *cls,
168 const struct GNUNET_SCHEDULER_TaskContext *tc)
170 if (test_failed == GNUNET_NO)
171 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
172 "Testcase timeout\n");
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
181 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
185 GNUNET_TRANSPORT_disconnect (p1.th);
186 GNUNET_TRANSPORT_disconnect (p2.th);
187 if (GNUNET_SCHEDULER_NO_TASK != tct)
189 GNUNET_SCHEDULER_cancel (tct);
190 tct = GNUNET_SCHEDULER_NO_TASK;
199 struct GNUNET_MessageHeader header;
205 get_size (unsigned int iter)
210 return iter + sizeof (struct TestMessage);
211 ret = (iter * iter * iter);
212 return sizeof (struct TestMessage) + (ret % 60000);
217 notify_receive (void *cls,
218 const struct GNUNET_PeerIdentity *peer,
219 const struct GNUNET_MessageHeader *message,
220 const struct GNUNET_TRANSPORT_ATS_Information *ats,
225 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
226 const struct TestMessage *hdr;
228 hdr = (const struct TestMessage*) message;
230 if (MTYPE != ntohs (message->type))
232 msg_recv_expected = n;
233 msg_recv = ntohl(hdr->num);
234 if (ntohs (message->size) != s)
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Expected message %u of size %u, got %u bytes of message %u\n",
239 ntohs (message->size),
241 if (die_task != GNUNET_SCHEDULER_NO_TASK)
242 GNUNET_SCHEDULER_cancel (die_task);
243 test_failed = GNUNET_YES;
244 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
247 if (ntohl (hdr->num) != n)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250 "Expected message %u of size %u, got %u bytes of message %u\n",
252 ntohs (message->size),
254 if (die_task != GNUNET_SCHEDULER_NO_TASK)
255 GNUNET_SCHEDULER_cancel (die_task);
256 test_failed = GNUNET_YES;
257 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
260 memset (cbuf, n, s - sizeof (struct TestMessage));
261 if (0 != memcmp (cbuf,
263 s - sizeof (struct TestMessage)))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 "Expected message %u with bits %u, but body did not match\n",
267 n, (unsigned char) n);
268 if (die_task != GNUNET_SCHEDULER_NO_TASK)
269 GNUNET_SCHEDULER_cancel (die_task);
270 test_failed = GNUNET_YES;
271 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
275 if (ntohl(hdr->num) % 5000 == 0)
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Got message %u of size %u\n",
280 ntohs (message->size));
284 if (0 == (n % (TOTAL_MSGS/100)))
286 fprintf (stderr, ".");
287 if (die_task != GNUNET_SCHEDULER_NO_TASK)
288 GNUNET_SCHEDULER_cancel (die_task);
289 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
299 notify_ready (void *cls, size_t size, void *buf)
303 struct TestMessage hdr;
316 GNUNET_assert (size >= s);
317 GNUNET_assert (buf != NULL);
321 hdr.header.size = htons (s);
322 hdr.header.type = htons (MTYPE);
325 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
326 ret += sizeof (struct TestMessage);
327 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
328 ret += s - sizeof (struct TestMessage);
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Sending message %u of size %u\n",
340 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
341 break; /* sometimes pack buffer full, sometimes not */
343 while (size - ret >= s);
347 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "Returning total message block of size %u\n",
366 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Peer `%4s' disconnected (%p)!\n",
372 GNUNET_i2s (peer), cls);
376 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
383 exchange_hello_last (void *cls,
384 const struct GNUNET_MessageHeader *message)
386 struct PeerContext *me = cls;
388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
389 "Exchanging HELLO of size %d with peer (%s)!\n",
390 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
391 GNUNET_i2s (&me->id));
392 GNUNET_assert (message != NULL);
393 GNUNET_assert (GNUNET_OK ==
394 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
396 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
402 exchange_hello (void *cls,
403 const struct GNUNET_MessageHeader *message)
405 struct PeerContext *me = cls;
407 GNUNET_assert (message != NULL);
408 GNUNET_assert (GNUNET_OK ==
409 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 "Exchanging HELLO of size %d from peer %s!\n",
413 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
414 GNUNET_i2s (&me->id));
415 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
420 notify_connect (void *cls,
421 const struct GNUNET_PeerIdentity *peer,
422 const struct GNUNET_TRANSPORT_ATS_Information *ats,
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Peer `%4s' connected to us (%p)!\n",
432 GNUNET_TRANSPORT_set_quota (p1.th,
434 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
435 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
436 start_time = GNUNET_TIME_absolute_get ();
440 GNUNET_TRANSPORT_set_quota (p2.th,
442 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
443 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
447 if (die_task != GNUNET_SCHEDULER_NO_TASK)
448 GNUNET_SCHEDULER_cancel (die_task);
449 if (tct != GNUNET_SCHEDULER_NO_TASK)
450 GNUNET_SCHEDULER_cancel (tct);
451 tct = GNUNET_SCHEDULER_NO_TASK;
452 if (p2_hello_canceled == GNUNET_NO)
454 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
455 p2_hello_canceled = GNUNET_YES;
457 if (p1_hello_canceled == GNUNET_NO)
459 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
460 p1_hello_canceled = GNUNET_YES;
462 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
464 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
466 get_size (0), 0, TIMEOUT,
475 setup_peer (struct PeerContext *p, const char *cfgname)
477 p->cfg = GNUNET_CONFIGURATION_create ();
478 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
479 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
480 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
481 GNUNET_DISK_directory_remove (p->servicehome);
484 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
485 "gnunet-service-arm",
486 "gnunet-service-arm",
490 "-c", cfgname, NULL);
498 if (GNUNET_CONFIGURATION_have_value (p->cfg,
499 "transport-https", "KEY_FILE"))
500 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
501 if (key_file_p1 == NULL)
502 GNUNET_asprintf(&key_file_p1,"https_p1.key");
503 if (0 == stat (key_file_p1, &sbuf ))
505 if (0 == remove(key_file_p1))
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Successfully removed existing private key file `%s'\n",
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Failed to remove private key file `%s'\n",
514 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
515 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
516 if (cert_file_p1 == NULL)
517 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
518 if (0 == stat (cert_file_p1, &sbuf ))
520 if (0 == remove(cert_file_p1))
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Successfully removed existing certificate file `%s'\n",
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Failed to remove existing certificate file `%s'\n",
532 if (GNUNET_CONFIGURATION_have_value (p->cfg,
533 "transport-https", "KEY_FILE"))
534 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
535 if (key_file_p2 == NULL)
536 GNUNET_asprintf(&key_file_p2,"https_p2.key");
537 if (0 == stat (key_file_p2, &sbuf ))
539 if (0 == remove(key_file_p2))
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Successfully removed existing private key file `%s'\n",
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Failed to remove private key file `%s'\n",
548 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
549 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
550 if (cert_file_p2 == NULL)
551 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
552 if (0 == stat (cert_file_p2, &sbuf ))
554 if (0 == remove(cert_file_p2))
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "Successfully removed existing certificate file `%s'\n",
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Failed to remove existing certificate file `%s'\n",
565 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
570 GNUNET_assert (p->th != NULL);
575 * Return the actual path to a file found in the current
576 * PATH environment variable.
578 * @param binary the name of the file to find
581 get_path_from_PATH (char *binary)
592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
593 _("PATH environment variable is unset.\n"));
596 path = GNUNET_strdup (p); /* because we write on it */
597 buf = GNUNET_malloc (strlen (path) + 20);
600 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
603 sprintf (buf, "%s/%s", pos, binary);
604 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
611 sprintf (buf, "%s/%s", pos, binary);
612 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
623 * Check whether the suid bit is set on a file.
624 * Attempts to find the file using the current
625 * PATH environment variable as a search path.
627 * @param binary the name of the file to check
629 * @return GNUNET_YES if the binary is found and
630 * can be run properly, GNUNET_NO otherwise
633 check_gnunet_nat_binary(char *binary)
643 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
644 p = get_path_from_PATH (binaryexe);
647 p = get_path_from_PATH (binary);
651 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
652 _("Could not find binary `%s' in PATH!\n"),
656 if (0 != STAT (p, &statbuf))
658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
659 _("stat (%s) failed: %s\n"),
663 return GNUNET_SYSERR;
667 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
668 (statbuf.st_uid == 0) )
672 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
673 if (INVALID_SOCKET == rawsock)
675 DWORD err = GetLastError ();
676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
677 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
678 return GNUNET_NO; /* not running as administrator */
680 closesocket (rawsock);
687 try_connect (void *cls,
688 const struct GNUNET_SCHEDULER_TaskContext *tc)
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 "Asking peers to connect...\n");
692 GNUNET_TRANSPORT_try_connect (p2.th,
694 GNUNET_TRANSPORT_try_connect (p1.th,
696 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
705 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
707 GNUNET_assert (ok == 1);
709 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
714 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
715 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
719 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
720 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
724 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
725 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
729 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
730 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
734 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
735 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
739 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
740 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
744 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
745 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
749 GNUNET_assert(p1.th != NULL);
750 GNUNET_assert(p2.th != NULL);
751 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
752 p1_hello_canceled = GNUNET_NO;
753 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
754 p2_hello_canceled = GNUNET_NO;
755 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
762 char *const argv[] = { "test-transport-api-reliability",
764 "test_transport_api_data.conf",
770 struct GNUNET_GETOPT_CommandLineOption options[] = {
771 GNUNET_GETOPT_OPTION_END
775 setTransportOptions("test_transport_api_data.conf");
779 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
781 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
782 "Not running NAT test case, binaries not properly installed.\n");
786 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
787 argv, "test-transport-api-reliability", "nohelp",
795 if (0 == stat (cert_file_p1, &sbuf ))
797 if (0 == remove(cert_file_p1))
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Successfully removed existing certificate file `%s'\n",
802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
803 "Failed to remove certfile `%s'\n",
807 if (0 == stat (key_file_p1, &sbuf ))
809 if (0 == remove(key_file_p1))
810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
811 "Successfully removed private key file `%s'\n",
814 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
815 "Failed to private key file `%s'\n",key_file_p1);
818 if (0 == stat (cert_file_p2, &sbuf ))
820 if (0 == remove(cert_file_p2))
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Successfully removed existing certificate file `%s'\n",
825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
826 "Failed to remove certfile `%s'\n",cert_file_p2);
829 if (0 == stat (key_file_p2, &sbuf ))
831 if (0 == remove(key_file_p2))
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
833 "Successfully removed private key file `%s'\n",
836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
837 "Failed to private key file `%s'\n",
840 GNUNET_free(key_file_p1);
841 GNUNET_free(key_file_p2);
842 GNUNET_free(cert_file_p1);
843 GNUNET_free(cert_file_p2);
844 GNUNET_free(p1.servicehome);
845 GNUNET_free(p2.servicehome);
853 main (int argc, char *argv[])
857 test_failed = GNUNET_NO;
859 if (strstr(argv[0], "tcp_nat") != NULL)
861 is_tcp_nat = GNUNET_YES;
862 GNUNET_asprintf(&test_name, "tcp_nat");
864 else if (strstr(argv[0], "tcp") != NULL)
867 GNUNET_asprintf(&test_name, "tcp");
869 else if (strstr(argv[0], "https") != NULL)
871 is_https = GNUNET_YES;
872 GNUNET_asprintf(&test_name, "https");
874 else if (strstr(argv[0], "http") != NULL)
876 is_http = GNUNET_YES;
877 GNUNET_asprintf(&test_name, "http");
879 else if (strstr(argv[0], "udp") != NULL)
882 GNUNET_asprintf(&test_name, "udp");
884 else if (strstr(argv[0], "unix") != NULL)
886 is_unix = GNUNET_YES;
887 GNUNET_asprintf(&test_name, "unix");
889 else if (strstr(argv[0], "wlan") != NULL)
891 is_wlan = GNUNET_YES;
893 GNUNET_log_setup ("test-transport-api-reliability",
901 GNUNET_DISK_directory_remove (p1.servicehome);
902 GNUNET_DISK_directory_remove (p2.servicehome);
906 /* end of test_transport_api_reliability.c */