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"
40 #define VERBOSE GNUNET_NO
42 #define VERBOSE_ARM GNUNET_NO
44 #define START_ARM GNUNET_YES
47 * Note that this value must not significantly exceed
48 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
49 * messages may be dropped even for a reliable transport.
51 #define TOTAL_MSGS (10000 * 2)
54 * How long until we give up on transmitting the message?
56 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
62 struct GNUNET_CONFIGURATION_Handle *cfg;
63 struct GNUNET_TRANSPORT_Handle *th;
64 struct GNUNET_PeerIdentity id;
66 struct GNUNET_OS_Process *arm_proc;
70 static struct PeerContext p1;
72 static struct PeerContext p2;
78 static int is_tcp_nat;
90 static unsigned long long total_bytes;
92 static struct GNUNET_TIME_Absolute start_time;
94 static GNUNET_SCHEDULER_TaskIdentifier die_task;
96 static char * key_file_p1;
97 static char * cert_file_p1;
99 static char * key_file_p2;
100 static char * cert_file_p2;
101 static char *test_name;
102 static int msg_scheduled;
104 static int msg_recv_expected;
106 static struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
109 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
111 #define OKPP do { ok++; } while (0)
118 unsigned long long delta;
121 GNUNET_SCHEDULER_cancel (die_task);
122 die_task = GNUNET_SCHEDULER_NO_TASK;
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
126 GNUNET_TRANSPORT_disconnect (p1.th);
127 GNUNET_TRANSPORT_disconnect (p2.th);
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130 "Transports disconnected, returning success!\n");
132 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
134 "\nThroughput was %llu kb/s\n",
135 total_bytes * 1000 / 1024 / delta);
136 GNUNET_asprintf(&value_name, "reliable_kbs_%s", test_name);
137 GAUGER (value_name, (int)(total_bytes * 1000 / 1024 /delta));
138 GNUNET_free(value_name);
146 stop_arm (struct PeerContext *p)
149 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
150 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
151 GNUNET_OS_process_wait (p->arm_proc);
152 GNUNET_OS_process_close (p->arm_proc);
155 GNUNET_CONFIGURATION_destroy (p->cfg);
160 end_badly (void *cls,
161 const struct GNUNET_SCHEDULER_TaskContext *tc)
163 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164 "Reliability failed: \nLast message sent %u \nNext message scheduled %u\nLast message received %u\nMessage expected %u \n ", msg_sent, msg_scheduled, msg_recv, msg_recv_expected);
166 GNUNET_TRANSPORT_disconnect (p1.th);
167 GNUNET_TRANSPORT_disconnect (p2.th);
174 struct GNUNET_MessageHeader header;
180 get_size (unsigned int iter)
185 return iter + sizeof (struct TestMessage);
186 ret = (iter * iter * iter);
187 return sizeof (struct TestMessage) + (ret % 60000);
192 notify_receive (void *cls,
193 const struct GNUNET_PeerIdentity *peer,
194 const struct GNUNET_MessageHeader *message,
195 const struct GNUNET_TRANSPORT_ATS_Information *ats,
200 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
201 const struct TestMessage *hdr;
203 hdr = (const struct TestMessage*) message;
205 if (MTYPE != ntohs (message->type))
207 msg_recv_expected = n;
208 msg_recv = ntohl(hdr->num);
209 if (ntohs (message->size) != s)
211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212 "Expected message %u of size %u, got %u bytes of message %u\n",
214 ntohs (message->size),
216 GNUNET_SCHEDULER_cancel (die_task);
217 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
220 if (ntohl (hdr->num) != n)
222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223 "Expected message %u of size %u, got %u bytes of message %u\n",
225 ntohs (message->size),
227 GNUNET_SCHEDULER_cancel (die_task);
228 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
231 memset (cbuf, n, s - sizeof (struct TestMessage));
232 if (0 != memcmp (cbuf,
234 s - sizeof (struct TestMessage)))
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Expected message %u with bits %u, but body did not match\n",
238 n, (unsigned char) n);
239 GNUNET_SCHEDULER_cancel (die_task);
240 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
244 if (ntohl(hdr->num) % 5000 == 0)
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Got message %u of size %u\n",
249 ntohs (message->size));
253 if (0 == (n % (TOTAL_MSGS/100)))
255 fprintf (stderr, ".");
256 GNUNET_SCHEDULER_cancel (die_task);
257 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
267 notify_ready (void *cls, size_t size, void *buf)
271 struct TestMessage hdr;
283 GNUNET_assert (size >= s);
284 GNUNET_assert (buf != NULL);
288 hdr.header.size = htons (s);
289 hdr.header.type = htons (MTYPE);
292 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
293 ret += sizeof (struct TestMessage);
294 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
295 ret += s - sizeof (struct TestMessage);
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Sending message %u of size %u\n",
307 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
308 break; /* sometimes pack buffer full, sometimes not */
310 while (size - ret >= s);
313 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Returning total message block of size %u\n",
332 notify_connect (void *cls,
333 const struct GNUNET_PeerIdentity *peer,
334 const struct GNUNET_TRANSPORT_ATS_Information *ats,
339 GNUNET_TRANSPORT_set_quota (p1.th,
341 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
342 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
343 GNUNET_TIME_UNIT_FOREVER_REL,
345 start_time = GNUNET_TIME_absolute_get ();
350 GNUNET_TRANSPORT_set_quota (p2.th,
352 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
353 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
354 GNUNET_TIME_UNIT_FOREVER_REL,
362 if ((transmit_handle!=NULL) && (cls == NULL))
363 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
364 if ((transmit_handle!=NULL) && (cls == &transmit_handle))
365 transmit_handle=NULL;
366 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
368 get_size (0), 0, TIMEOUT,
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
380 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Peer `%4s' disconnected (%p)!\n",
385 GNUNET_i2s (peer), cls);
391 setup_peer (struct PeerContext *p, const char *cfgname)
393 p->cfg = GNUNET_CONFIGURATION_create ();
395 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
396 "gnunet-service-arm",
397 "gnunet-service-arm",
401 "-c", cfgname, NULL);
403 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
410 if (GNUNET_CONFIGURATION_have_value (p->cfg,
411 "transport-https", "KEY_FILE"))
412 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
413 if (key_file_p1 == NULL)
414 GNUNET_asprintf(&key_file_p1,"https_p1.key");
415 if (0 == stat (key_file_p1, &sbuf ))
417 if (0 == remove(key_file_p1))
418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p1);
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p1);
422 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
423 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
424 if (cert_file_p1 == NULL)
425 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
426 if (0 == stat (cert_file_p1, &sbuf ))
428 if (0 == remove(cert_file_p1))
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p1);
436 if (GNUNET_CONFIGURATION_have_value (p->cfg,
437 "transport-https", "KEY_FILE"))
438 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
439 if (key_file_p2 == NULL)
440 GNUNET_asprintf(&key_file_p2,"https_p2.key");
441 if (0 == stat (key_file_p2, &sbuf ))
443 if (0 == remove(key_file_p2))
444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p2);
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p2);
448 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
449 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
450 if (cert_file_p2 == NULL)
451 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
452 if (0 == stat (cert_file_p2, &sbuf ))
454 if (0 == remove(cert_file_p2))
455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p2);
462 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
467 GNUNET_assert (p->th != NULL);
471 notify_ready_connect (void *cls, size_t size, void *buf)
477 exchange_hello_last (void *cls,
478 const struct GNUNET_MessageHeader *message)
480 struct PeerContext *me = cls;
481 transmit_handle = NULL;
482 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Exchanging HELLO with peer (%p)!\n", cls);
487 GNUNET_assert (ok >= 3);
489 GNUNET_assert (message != NULL);
490 GNUNET_assert (GNUNET_OK ==
491 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
494 GNUNET_assert(NULL != (transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
496 sizeof (struct GNUNET_MessageHeader), 0,
498 ¬ify_ready_connect,
501 /* both HELLOs exchanged, get ready to test transmission! */
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Finished exchanging HELLOs, now waiting for transmission!\n");
508 exchange_hello (void *cls,
509 const struct GNUNET_MessageHeader *message)
511 struct PeerContext *me = cls;
513 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516 "Exchanging HELLO with peer (%p)!\n", cls);
518 GNUNET_assert (ok >= 2);
520 GNUNET_assert (message != NULL);
521 GNUNET_assert (GNUNET_OK ==
522 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Received HELLO size %d\n",
528 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
530 GNUNET_TRANSPORT_offer_hello (p2.th, message);
531 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
535 * Return the actual path to a file found in the current
536 * PATH environment variable.
538 * @param binary the name of the file to find
541 get_path_from_PATH (char *binary)
552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
553 _("PATH environment variable is unset.\n"));
556 path = GNUNET_strdup (p); /* because we write on it */
557 buf = GNUNET_malloc (strlen (path) + 20);
560 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
563 sprintf (buf, "%s/%s", pos, binary);
564 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
571 sprintf (buf, "%s/%s", pos, binary);
572 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
583 * Check whether the suid bit is set on a file.
584 * Attempts to find the file using the current
585 * PATH environment variable as a search path.
587 * @param binary the name of the file to check
589 * @return GNUNET_YES if the binary is found and
590 * can be run properly, GNUNET_NO otherwise
593 check_gnunet_nat_binary(char *binary)
603 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
604 p = get_path_from_PATH (binaryexe);
607 p = get_path_from_PATH (binary);
611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
612 _("Could not find binary `%s' in PATH!\n"),
616 if (0 != STAT (p, &statbuf))
618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
619 _("stat (%s) failed: %s\n"),
623 return GNUNET_SYSERR;
627 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
628 (statbuf.st_uid == 0) )
632 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
633 if (INVALID_SOCKET == rawsock)
635 DWORD err = GetLastError ();
636 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
637 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
638 return GNUNET_NO; /* not running as administrator */
640 closesocket (rawsock);
648 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
650 GNUNET_assert (ok == 1);
652 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
657 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
658 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
662 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
663 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
667 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
668 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
672 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
673 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
677 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
678 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
682 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
683 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
687 GNUNET_assert(p1.th != NULL);
688 GNUNET_assert(p2.th != NULL);
689 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
696 char *const argv[] = { "test-transport-api-reliability",
698 "test_transport_api_data.conf",
704 struct GNUNET_GETOPT_CommandLineOption options[] = {
705 GNUNET_GETOPT_OPTION_END
709 setTransportOptions("test_transport_api_data.conf");
713 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
715 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
719 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
720 argv, "test-transport-api-reliability", "nohelp",
728 if (0 == stat (cert_file_p1, &sbuf ))
730 if (0 == remove(cert_file_p1))
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
736 if (0 == stat (key_file_p1, &sbuf ))
738 if (0 == remove(key_file_p1))
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
741 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
744 if (0 == stat (cert_file_p2, &sbuf ))
746 if (0 == remove(cert_file_p2))
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
752 if (0 == stat (key_file_p2, &sbuf ))
754 if (0 == remove(key_file_p2))
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
759 GNUNET_free(key_file_p1);
760 GNUNET_free(key_file_p2);
761 GNUNET_free(cert_file_p1);
762 GNUNET_free(cert_file_p2);
770 main (int argc, char *argv[])
774 return GNUNET_SYSERR;
777 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
778 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
780 if (strstr(argv[0], "tcp_nat") != NULL)
782 is_tcp_nat = GNUNET_YES;
783 GNUNET_asprintf(&test_name, "tcp_nat");
785 else if (strstr(argv[0], "tcp") != NULL)
788 GNUNET_asprintf(&test_name, "tcp");
790 else if (strstr(argv[0], "https") != NULL)
792 is_https = GNUNET_YES;
793 GNUNET_asprintf(&test_name, "https");
795 else if (strstr(argv[0], "http") != NULL)
797 is_http = GNUNET_YES;
798 GNUNET_asprintf(&test_name, "http");
800 else if (strstr(argv[0], "udp") != NULL)
803 GNUNET_asprintf(&test_name, "udp");
805 else if (strstr(argv[0], "unix") != NULL)
807 is_unix = GNUNET_YES;
808 GNUNET_asprintf(&test_name, "unix");
810 GNUNET_log_setup ("test-transport-api-reliability",
818 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
819 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
823 /* end of test_transport_api_reliability.c */