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 (1024 * 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)
209 ret = (iter * iter * iter);
210 return sizeof (struct TestMessage) + (ret % 60000);
215 notify_receive (void *cls,
216 const struct GNUNET_PeerIdentity *peer,
217 const struct GNUNET_MessageHeader *message,
218 const struct GNUNET_TRANSPORT_ATS_Information *ats,
223 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
224 const struct TestMessage *hdr;
226 hdr = (const struct TestMessage*) message;
228 if (MTYPE != ntohs (message->type))
230 msg_recv_expected = n;
231 msg_recv = ntohl(hdr->num);
232 if (ntohs (message->size) != s)
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Expected message %u of size %u, got %u bytes of message %u\n",
237 ntohs (message->size),
239 if (die_task != GNUNET_SCHEDULER_NO_TASK)
240 GNUNET_SCHEDULER_cancel (die_task);
241 test_failed = GNUNET_YES;
242 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
245 if (ntohl (hdr->num) != n)
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Expected message %u of size %u, got %u bytes of message %u\n",
250 ntohs (message->size),
252 if (die_task != GNUNET_SCHEDULER_NO_TASK)
253 GNUNET_SCHEDULER_cancel (die_task);
254 test_failed = GNUNET_YES;
255 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
258 memset (cbuf, n, s - sizeof (struct TestMessage));
259 if (0 != memcmp (cbuf,
261 s - sizeof (struct TestMessage)))
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 "Expected message %u with bits %u, but body did not match\n",
265 n, (unsigned char) n);
266 if (die_task != GNUNET_SCHEDULER_NO_TASK)
267 GNUNET_SCHEDULER_cancel (die_task);
268 test_failed = GNUNET_YES;
269 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
273 if (ntohl(hdr->num) % 5000 == 0)
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Got message %u of size %u\n",
278 ntohs (message->size));
282 if (0 == (n % (TOTAL_MSGS/100)))
284 fprintf (stderr, ".");
285 if (die_task != GNUNET_SCHEDULER_NO_TASK)
286 GNUNET_SCHEDULER_cancel (die_task);
287 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
297 notify_ready (void *cls, size_t size, void *buf)
301 struct TestMessage hdr;
314 GNUNET_assert (size >= s);
315 GNUNET_assert (buf != NULL);
319 hdr.header.size = htons (s);
320 hdr.header.type = htons (MTYPE);
323 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
324 ret += sizeof (struct TestMessage);
325 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
326 ret += s - sizeof (struct TestMessage);
330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331 "Sending message %u of size %u\n",
338 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
339 break; /* sometimes pack buffer full, sometimes not */
341 while (size - ret >= s);
345 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Returning total message block of size %u\n",
364 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Peer `%4s' disconnected (%p)!\n",
370 GNUNET_i2s (peer), cls);
374 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
381 exchange_hello_last (void *cls,
382 const struct GNUNET_MessageHeader *message)
384 struct PeerContext *me = cls;
386 GNUNET_assert (message != NULL);
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Exchanging HELLO of size %d with peer (%s)!\n",
389 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
390 GNUNET_i2s (&me->id));
391 GNUNET_assert (GNUNET_OK ==
392 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
394 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
400 exchange_hello (void *cls,
401 const struct GNUNET_MessageHeader *message)
403 struct PeerContext *me = cls;
405 GNUNET_assert (message != NULL);
406 GNUNET_assert (GNUNET_OK ==
407 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "Exchanging HELLO of size %d from peer %s!\n",
411 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
412 GNUNET_i2s (&me->id));
413 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
418 notify_connect (void *cls,
419 const struct GNUNET_PeerIdentity *peer,
420 const struct GNUNET_TRANSPORT_ATS_Information *ats,
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "Peer `%4s' connected to us (%p)!\n",
430 GNUNET_TRANSPORT_set_quota (p1.th,
432 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
433 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
434 start_time = GNUNET_TIME_absolute_get ();
438 GNUNET_TRANSPORT_set_quota (p2.th,
440 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
441 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
445 if (die_task != GNUNET_SCHEDULER_NO_TASK)
446 GNUNET_SCHEDULER_cancel (die_task);
447 if (tct != GNUNET_SCHEDULER_NO_TASK)
448 GNUNET_SCHEDULER_cancel (tct);
449 tct = GNUNET_SCHEDULER_NO_TASK;
450 if (p2_hello_canceled == GNUNET_NO)
452 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
453 p2_hello_canceled = GNUNET_YES;
455 if (p1_hello_canceled == GNUNET_NO)
457 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
458 p1_hello_canceled = GNUNET_YES;
460 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
462 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
464 get_size (0), 0, TIMEOUT,
473 setup_peer (struct PeerContext *p, const char *cfgname)
475 p->cfg = GNUNET_CONFIGURATION_create ();
476 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
477 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
479 GNUNET_assert (GNUNET_OK ==
480 GNUNET_CONFIGURATION_get_value_string (p->cfg,
481 "PATHS", "SERVICEHOME",
483 GNUNET_DISK_directory_remove (p->servicehome);
487 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
488 "gnunet-service-arm",
489 "gnunet-service-arm",
493 "-c", cfgname, NULL);
501 if (GNUNET_CONFIGURATION_have_value (p->cfg,
502 "transport-https", "KEY_FILE"))
503 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
504 if (key_file_p1 == NULL)
505 GNUNET_asprintf(&key_file_p1,"https_p1.key");
506 if (0 == stat (key_file_p1, &sbuf ))
508 if (0 == remove(key_file_p1))
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Successfully removed existing private key file `%s'\n",
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Failed to remove private key file `%s'\n",
517 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
518 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
519 if (cert_file_p1 == NULL)
520 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
521 if (0 == stat (cert_file_p1, &sbuf ))
523 if (0 == remove(cert_file_p1))
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Successfully removed existing certificate file `%s'\n",
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Failed to remove existing certificate file `%s'\n",
535 if (GNUNET_CONFIGURATION_have_value (p->cfg,
536 "transport-https", "KEY_FILE"))
537 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
538 if (key_file_p2 == NULL)
539 GNUNET_asprintf(&key_file_p2,"https_p2.key");
540 if (0 == stat (key_file_p2, &sbuf ))
542 if (0 == remove(key_file_p2))
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "Successfully removed existing private key file `%s'\n",
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "Failed to remove private key file `%s'\n",
551 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
552 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
553 if (cert_file_p2 == NULL)
554 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
555 if (0 == stat (cert_file_p2, &sbuf ))
557 if (0 == remove(cert_file_p2))
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
559 "Successfully removed existing certificate file `%s'\n",
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
563 "Failed to remove existing certificate file `%s'\n",
568 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
573 GNUNET_assert (p->th != NULL);
578 * Return the actual path to a file found in the current
579 * PATH environment variable.
581 * @param binary the name of the file to find
584 get_path_from_PATH (char *binary)
595 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
596 _("PATH environment variable is unset.\n"));
599 path = GNUNET_strdup (p); /* because we write on it */
600 buf = GNUNET_malloc (strlen (path) + 20);
603 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
606 sprintf (buf, "%s/%s", pos, binary);
607 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
614 sprintf (buf, "%s/%s", pos, binary);
615 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
626 * Check whether the suid bit is set on a file.
627 * Attempts to find the file using the current
628 * PATH environment variable as a search path.
630 * @param binary the name of the file to check
632 * @return GNUNET_YES if the binary is found and
633 * can be run properly, GNUNET_NO otherwise
636 check_gnunet_nat_binary(char *binary)
646 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
647 p = get_path_from_PATH (binaryexe);
650 p = get_path_from_PATH (binary);
654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
655 _("Could not find binary `%s' in PATH!\n"),
659 if (0 != STAT (p, &statbuf))
661 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
662 _("stat (%s) failed: %s\n"),
666 return GNUNET_SYSERR;
670 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
671 (statbuf.st_uid == 0) )
675 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
676 if (INVALID_SOCKET == rawsock)
678 DWORD err = GetLastError ();
679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
680 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
681 return GNUNET_NO; /* not running as administrator */
683 closesocket (rawsock);
690 try_connect (void *cls,
691 const struct GNUNET_SCHEDULER_TaskContext *tc)
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694 "Asking peers to connect...\n");
695 GNUNET_TRANSPORT_try_connect (p2.th,
697 GNUNET_TRANSPORT_try_connect (p1.th,
699 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
708 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
710 GNUNET_assert (ok == 1);
712 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
717 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
718 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
722 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
723 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
727 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
728 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
732 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
733 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
737 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
738 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
742 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
743 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
747 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
748 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
752 GNUNET_assert(p1.th != NULL);
753 GNUNET_assert(p2.th != NULL);
754 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
755 p1_hello_canceled = GNUNET_NO;
756 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
757 p2_hello_canceled = GNUNET_NO;
758 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
765 char *const argv[] = { "test-transport-api-reliability",
767 "test_transport_api_data.conf",
773 struct GNUNET_GETOPT_CommandLineOption options[] = {
774 GNUNET_GETOPT_OPTION_END
778 setTransportOptions("test_transport_api_data.conf");
782 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
784 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
785 "Not running NAT test case, binaries not properly installed.\n");
789 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
790 argv, "test-transport-api-reliability", "nohelp",
798 if (0 == stat (cert_file_p1, &sbuf ))
800 if (0 == remove(cert_file_p1))
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Successfully removed existing certificate file `%s'\n",
805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
806 "Failed to remove certfile `%s'\n",
810 if (0 == stat (key_file_p1, &sbuf ))
812 if (0 == remove(key_file_p1))
813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
814 "Successfully removed private key file `%s'\n",
817 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
818 "Failed to private key file `%s'\n",key_file_p1);
821 if (0 == stat (cert_file_p2, &sbuf ))
823 if (0 == remove(cert_file_p2))
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
825 "Successfully removed existing certificate file `%s'\n",
828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
829 "Failed to remove certfile `%s'\n",cert_file_p2);
832 if (0 == stat (key_file_p2, &sbuf ))
834 if (0 == remove(key_file_p2))
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836 "Successfully removed private key file `%s'\n",
839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
840 "Failed to private key file `%s'\n",
843 GNUNET_free(key_file_p1);
844 GNUNET_free(key_file_p2);
845 GNUNET_free(cert_file_p1);
846 GNUNET_free(cert_file_p2);
854 main (int argc, char *argv[])
858 test_failed = GNUNET_NO;
860 if (strstr(argv[0], "tcp_nat") != NULL)
862 is_tcp_nat = GNUNET_YES;
863 GNUNET_asprintf(&test_name, "tcp_nat");
865 else if (strstr(argv[0], "tcp") != NULL)
868 GNUNET_asprintf(&test_name, "tcp");
870 else if (strstr(argv[0], "https") != NULL)
872 is_https = GNUNET_YES;
873 GNUNET_asprintf(&test_name, "https");
875 else if (strstr(argv[0], "http") != NULL)
877 is_http = GNUNET_YES;
878 GNUNET_asprintf(&test_name, "http");
880 else if (strstr(argv[0], "udp") != NULL)
883 GNUNET_asprintf(&test_name, "udp");
885 else if (strstr(argv[0], "unix") != NULL)
887 is_unix = GNUNET_YES;
888 GNUNET_asprintf(&test_name, "unix");
890 else if (strstr(argv[0], "wlan") != NULL)
892 is_wlan = GNUNET_YES;
894 GNUNET_log_setup ("test-transport-api-reliability",
902 if (p1.servicehome != NULL)
904 GNUNET_DISK_directory_remove (p1.servicehome);
905 GNUNET_free (p1.servicehome);
907 if (p2.servicehome != NULL)
909 GNUNET_DISK_directory_remove (p2.servicehome);
910 GNUNET_free (p2.servicehome);
915 /* end of test_transport_api_reliability.c */