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;
92 static unsigned long long total_bytes;
94 static struct GNUNET_TIME_Absolute start_time;
96 static GNUNET_SCHEDULER_TaskIdentifier die_task;
98 static GNUNET_SCHEDULER_TaskIdentifier tct;
100 static char * key_file_p1;
101 static char * cert_file_p1;
103 static char * key_file_p2;
104 static char * cert_file_p2;
105 static char *test_name;
106 static int msg_scheduled;
108 static int msg_recv_expected;
112 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
114 #define OKPP do { ok++; } while (0)
121 unsigned long long delta;
124 GNUNET_SCHEDULER_cancel (die_task);
125 die_task = GNUNET_SCHEDULER_NO_TASK;
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
129 GNUNET_TRANSPORT_disconnect (p1.th);
130 GNUNET_TRANSPORT_disconnect (p2.th);
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "Transports disconnected, returning success!\n");
135 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
137 "\nThroughput was %llu kb/s\n",
138 total_bytes * 1000 / 1024 / delta);
139 GNUNET_asprintf(&value_name, "reliable_%s", test_name);
140 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
141 GNUNET_free(value_name);
149 stop_arm (struct PeerContext *p)
152 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
153 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
154 GNUNET_OS_process_wait (p->arm_proc);
155 GNUNET_OS_process_close (p->arm_proc);
158 GNUNET_CONFIGURATION_destroy (p->cfg);
163 end_badly (void *cls,
164 const struct GNUNET_SCHEDULER_TaskContext *tc)
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
173 GNUNET_TRANSPORT_disconnect (p1.th);
174 GNUNET_TRANSPORT_disconnect (p2.th);
175 if (GNUNET_SCHEDULER_NO_TASK != tct)
177 GNUNET_SCHEDULER_cancel (tct);
178 tct = GNUNET_SCHEDULER_NO_TASK;
186 struct GNUNET_MessageHeader header;
192 get_size (unsigned int iter)
197 return iter + sizeof (struct TestMessage);
198 ret = (iter * iter * iter);
199 return sizeof (struct TestMessage) + (ret % 60000);
204 notify_receive (void *cls,
205 const struct GNUNET_PeerIdentity *peer,
206 const struct GNUNET_MessageHeader *message,
207 const struct GNUNET_TRANSPORT_ATS_Information *ats,
212 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
213 const struct TestMessage *hdr;
215 hdr = (const struct TestMessage*) message;
217 if (MTYPE != ntohs (message->type))
219 msg_recv_expected = n;
220 msg_recv = ntohl(hdr->num);
221 if (ntohs (message->size) != s)
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
224 "Expected message %u of size %u, got %u bytes of message %u\n",
226 ntohs (message->size),
228 GNUNET_SCHEDULER_cancel (die_task);
229 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
232 if (ntohl (hdr->num) != n)
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 GNUNET_SCHEDULER_cancel (die_task);
240 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
243 memset (cbuf, n, s - sizeof (struct TestMessage));
244 if (0 != memcmp (cbuf,
246 s - sizeof (struct TestMessage)))
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 "Expected message %u with bits %u, but body did not match\n",
250 n, (unsigned char) n);
251 GNUNET_SCHEDULER_cancel (die_task);
252 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
256 if (ntohl(hdr->num) % 5000 == 0)
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Got message %u of size %u\n",
261 ntohs (message->size));
265 if (0 == (n % (TOTAL_MSGS/100)))
267 fprintf (stderr, ".");
268 GNUNET_SCHEDULER_cancel (die_task);
269 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
279 notify_ready (void *cls, size_t size, void *buf)
283 struct TestMessage hdr;
295 GNUNET_assert (size >= s);
296 GNUNET_assert (buf != NULL);
300 hdr.header.size = htons (s);
301 hdr.header.type = htons (MTYPE);
304 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
305 ret += sizeof (struct TestMessage);
306 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
307 ret += s - sizeof (struct TestMessage);
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Sending message %u of size %u\n",
319 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
320 break; /* sometimes pack buffer full, sometimes not */
322 while (size - ret >= s);
325 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Returning total message block of size %u\n",
344 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Peer `%4s' disconnected (%p)!\n",
350 GNUNET_i2s (peer), cls);
356 exchange_hello_last (void *cls,
357 const struct GNUNET_MessageHeader *message)
359 struct PeerContext *me = cls;
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Exchanging HELLO of size %d with peer (%s)!\n",
363 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
364 GNUNET_i2s (&me->id));
365 GNUNET_assert (message != NULL);
366 GNUNET_assert (GNUNET_OK ==
367 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
369 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
375 exchange_hello (void *cls,
376 const struct GNUNET_MessageHeader *message)
378 struct PeerContext *me = cls;
380 GNUNET_assert (message != NULL);
381 GNUNET_assert (GNUNET_OK ==
382 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Exchanging HELLO of size %d from peer %s!\n",
386 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
387 GNUNET_i2s (&me->id));
388 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
393 notify_connect (void *cls,
394 const struct GNUNET_PeerIdentity *peer,
395 const struct GNUNET_TRANSPORT_ATS_Information *ats,
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Peer `%4s' connected to us (%p)!\n",
405 GNUNET_TRANSPORT_set_quota (p1.th,
407 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
408 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
409 start_time = GNUNET_TIME_absolute_get ();
413 GNUNET_TRANSPORT_set_quota (p2.th,
415 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
416 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
420 GNUNET_SCHEDULER_cancel (die_task);
421 GNUNET_SCHEDULER_cancel (tct);
422 tct = GNUNET_SCHEDULER_NO_TASK;
423 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
424 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
425 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
427 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
429 get_size (0), 0, TIMEOUT,
438 setup_peer (struct PeerContext *p, const char *cfgname)
440 p->cfg = GNUNET_CONFIGURATION_create ();
442 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
443 "gnunet-service-arm",
444 "gnunet-service-arm",
448 "-c", cfgname, NULL);
450 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
457 if (GNUNET_CONFIGURATION_have_value (p->cfg,
458 "transport-https", "KEY_FILE"))
459 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
460 if (key_file_p1 == NULL)
461 GNUNET_asprintf(&key_file_p1,"https_p1.key");
462 if (0 == stat (key_file_p1, &sbuf ))
464 if (0 == remove(key_file_p1))
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 "Successfully removed existing private key file `%s'\n",
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
470 "Failed to remove private key file `%s'\n",
473 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
474 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
475 if (cert_file_p1 == NULL)
476 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
477 if (0 == stat (cert_file_p1, &sbuf ))
479 if (0 == remove(cert_file_p1))
480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481 "Successfully removed existing certificate file `%s'\n",
484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
485 "Failed to remove existing certificate file `%s'\n",
491 if (GNUNET_CONFIGURATION_have_value (p->cfg,
492 "transport-https", "KEY_FILE"))
493 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
494 if (key_file_p2 == NULL)
495 GNUNET_asprintf(&key_file_p2,"https_p2.key");
496 if (0 == stat (key_file_p2, &sbuf ))
498 if (0 == remove(key_file_p2))
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 "Successfully removed existing private key file `%s'\n",
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
504 "Failed to remove private key file `%s'\n",
507 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
508 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
509 if (cert_file_p2 == NULL)
510 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
511 if (0 == stat (cert_file_p2, &sbuf ))
513 if (0 == remove(cert_file_p2))
514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
515 "Successfully removed existing certificate file `%s'\n",
518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
519 "Failed to remove existing certificate file `%s'\n",
524 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
529 GNUNET_assert (p->th != NULL);
534 * Return the actual path to a file found in the current
535 * PATH environment variable.
537 * @param binary the name of the file to find
540 get_path_from_PATH (char *binary)
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
552 _("PATH environment variable is unset.\n"));
555 path = GNUNET_strdup (p); /* because we write on it */
556 buf = GNUNET_malloc (strlen (path) + 20);
559 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
562 sprintf (buf, "%s/%s", pos, binary);
563 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
570 sprintf (buf, "%s/%s", pos, binary);
571 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
582 * Check whether the suid bit is set on a file.
583 * Attempts to find the file using the current
584 * PATH environment variable as a search path.
586 * @param binary the name of the file to check
588 * @return GNUNET_YES if the binary is found and
589 * can be run properly, GNUNET_NO otherwise
592 check_gnunet_nat_binary(char *binary)
602 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
603 p = get_path_from_PATH (binaryexe);
606 p = get_path_from_PATH (binary);
610 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
611 _("Could not find binary `%s' in PATH!\n"),
615 if (0 != STAT (p, &statbuf))
617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
618 _("stat (%s) failed: %s\n"),
622 return GNUNET_SYSERR;
626 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
627 (statbuf.st_uid == 0) )
631 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
632 if (INVALID_SOCKET == rawsock)
634 DWORD err = GetLastError ();
635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
636 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
637 return GNUNET_NO; /* not running as administrator */
639 closesocket (rawsock);
646 try_connect (void *cls,
647 const struct GNUNET_SCHEDULER_TaskContext *tc)
649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
650 "Asking peers to connect...\n");
651 GNUNET_TRANSPORT_try_connect (p2.th,
653 GNUNET_TRANSPORT_try_connect (p1.th,
655 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
664 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
666 GNUNET_assert (ok == 1);
668 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
673 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
674 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
678 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
679 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
683 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
684 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
688 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
689 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
693 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
694 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
698 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
699 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
703 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
704 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
708 GNUNET_assert(p1.th != NULL);
709 GNUNET_assert(p2.th != NULL);
710 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
711 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
712 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
719 char *const argv[] = { "test-transport-api-reliability",
721 "test_transport_api_data.conf",
727 struct GNUNET_GETOPT_CommandLineOption options[] = {
728 GNUNET_GETOPT_OPTION_END
732 setTransportOptions("test_transport_api_data.conf");
736 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
738 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
739 "Not running NAT test case, binaries not properly installed.\n");
743 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
744 argv, "test-transport-api-reliability", "nohelp",
752 if (0 == stat (cert_file_p1, &sbuf ))
754 if (0 == remove(cert_file_p1))
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756 "Successfully removed existing certificate file `%s'\n",
759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
760 "Failed to remove certfile `%s'\n",
764 if (0 == stat (key_file_p1, &sbuf ))
766 if (0 == remove(key_file_p1))
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768 "Successfully removed private key file `%s'\n",
771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
772 "Failed to private key file `%s'\n",key_file_p1);
775 if (0 == stat (cert_file_p2, &sbuf ))
777 if (0 == remove(cert_file_p2))
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779 "Successfully removed existing certificate file `%s'\n",
782 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
783 "Failed to remove certfile `%s'\n",cert_file_p2);
786 if (0 == stat (key_file_p2, &sbuf ))
788 if (0 == remove(key_file_p2))
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Successfully removed private key file `%s'\n",
793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
794 "Failed to private key file `%s'\n",
797 GNUNET_free(key_file_p1);
798 GNUNET_free(key_file_p2);
799 GNUNET_free(cert_file_p1);
800 GNUNET_free(cert_file_p2);
808 main (int argc, char *argv[])
812 return GNUNET_SYSERR;
815 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
816 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
818 if (strstr(argv[0], "tcp_nat") != NULL)
820 is_tcp_nat = GNUNET_YES;
821 GNUNET_asprintf(&test_name, "tcp_nat");
823 else if (strstr(argv[0], "tcp") != NULL)
826 GNUNET_asprintf(&test_name, "tcp");
828 else if (strstr(argv[0], "https") != NULL)
830 is_https = GNUNET_YES;
831 GNUNET_asprintf(&test_name, "https");
833 else if (strstr(argv[0], "http") != NULL)
835 is_http = GNUNET_YES;
836 GNUNET_asprintf(&test_name, "http");
838 else if (strstr(argv[0], "udp") != NULL)
841 GNUNET_asprintf(&test_name, "udp");
843 else if (strstr(argv[0], "unix") != NULL)
845 is_unix = GNUNET_YES;
846 GNUNET_asprintf(&test_name, "unix");
848 else if (strstr(argv[0], "wlan") != NULL)
850 is_wlan = GNUNET_YES;
852 GNUNET_log_setup ("test-transport-api-reliability",
860 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
861 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
865 /* end of test_transport_api_reliability.c */