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 unsigned long long total_bytes;
85 static struct GNUNET_TIME_Absolute start_time;
87 static GNUNET_SCHEDULER_TaskIdentifier die_task;
89 static GNUNET_SCHEDULER_TaskIdentifier tct;
91 struct GNUNET_TRANSPORT_TransmitHandle * th_p2;
93 static char * key_file_p1;
94 static char * cert_file_p1;
96 static char * key_file_p2;
97 static char * cert_file_p2;
98 static char *test_name;
99 static int msg_scheduled;
101 static int msg_recv_expected;
104 static int p1_hello_canceled;
105 static int p2_hello_canceled;
108 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
110 #define OKPP do { ok++; } while (0)
117 unsigned long long delta;
120 if (die_task != GNUNET_SCHEDULER_NO_TASK)
121 GNUNET_SCHEDULER_cancel (die_task);
122 die_task = GNUNET_SCHEDULER_NO_TASK;
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
128 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
131 GNUNET_TRANSPORT_disconnect (p1.th);
132 GNUNET_TRANSPORT_disconnect (p2.th);
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Transports disconnected, returning success!\n");
137 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
139 "\nThroughput was %llu kb/s\n",
140 total_bytes * 1000 / 1024 / delta);
141 GNUNET_asprintf(&value_name, "reliable_%s", test_name);
142 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
143 GNUNET_free(value_name);
151 stop_arm (struct PeerContext *p)
154 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
155 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
156 GNUNET_OS_process_wait (p->arm_proc);
157 GNUNET_OS_process_close (p->arm_proc);
160 GNUNET_CONFIGURATION_destroy (p->cfg);
165 end_badly (void *cls,
166 const struct GNUNET_SCHEDULER_TaskContext *tc)
168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
169 "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
175 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
179 GNUNET_TRANSPORT_disconnect (p1.th);
180 GNUNET_TRANSPORT_disconnect (p2.th);
181 if (GNUNET_SCHEDULER_NO_TASK != tct)
183 GNUNET_SCHEDULER_cancel (tct);
184 tct = GNUNET_SCHEDULER_NO_TASK;
192 struct GNUNET_MessageHeader header;
198 get_size (unsigned int iter)
203 return iter + sizeof (struct TestMessage);
204 ret = (iter * iter * iter);
205 return sizeof (struct TestMessage) + (ret % 60000);
210 notify_receive (void *cls,
211 const struct GNUNET_PeerIdentity *peer,
212 const struct GNUNET_MessageHeader *message,
213 const struct GNUNET_TRANSPORT_ATS_Information *ats,
218 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
219 const struct TestMessage *hdr;
221 hdr = (const struct TestMessage*) message;
223 if (MTYPE != ntohs (message->type))
225 msg_recv_expected = n;
226 msg_recv = ntohl(hdr->num);
227 if (ntohs (message->size) != s)
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Expected message %u of size %u, got %u bytes of message %u\n",
232 ntohs (message->size),
234 if (die_task != GNUNET_SCHEDULER_NO_TASK)
235 GNUNET_SCHEDULER_cancel (die_task);
236 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
239 if (ntohl (hdr->num) != n)
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 "Expected message %u of size %u, got %u bytes of message %u\n",
244 ntohs (message->size),
246 if (die_task != GNUNET_SCHEDULER_NO_TASK)
247 GNUNET_SCHEDULER_cancel (die_task);
248 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
251 memset (cbuf, n, s - sizeof (struct TestMessage));
252 if (0 != memcmp (cbuf,
254 s - sizeof (struct TestMessage)))
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257 "Expected message %u with bits %u, but body did not match\n",
258 n, (unsigned char) n);
259 if (die_task != GNUNET_SCHEDULER_NO_TASK)
260 GNUNET_SCHEDULER_cancel (die_task);
261 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
265 if (ntohl(hdr->num) % 5000 == 0)
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Got message %u of size %u\n",
270 ntohs (message->size));
274 if (0 == (n % (TOTAL_MSGS/100)))
276 fprintf (stderr, ".");
277 if (die_task != GNUNET_SCHEDULER_NO_TASK)
278 GNUNET_SCHEDULER_cancel (die_task);
279 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
289 notify_ready (void *cls, size_t size, void *buf)
293 struct TestMessage hdr;
306 GNUNET_assert (size >= s);
307 GNUNET_assert (buf != NULL);
311 hdr.header.size = htons (s);
312 hdr.header.type = htons (MTYPE);
315 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
316 ret += sizeof (struct TestMessage);
317 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
318 ret += s - sizeof (struct TestMessage);
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Sending message %u of size %u\n",
330 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
331 break; /* sometimes pack buffer full, sometimes not */
333 while (size - ret >= s);
337 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Returning total message block of size %u\n",
356 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361 "Peer `%4s' disconnected (%p)!\n",
362 GNUNET_i2s (peer), cls);
365 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
370 exchange_hello_last (void *cls,
371 const struct GNUNET_MessageHeader *message)
373 struct PeerContext *me = cls;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Exchanging HELLO of size %d with peer (%s)!\n",
377 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
378 GNUNET_i2s (&me->id));
379 GNUNET_assert (message != NULL);
380 GNUNET_assert (GNUNET_OK ==
381 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
383 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
389 exchange_hello (void *cls,
390 const struct GNUNET_MessageHeader *message)
392 struct PeerContext *me = cls;
394 GNUNET_assert (message != NULL);
395 GNUNET_assert (GNUNET_OK ==
396 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
399 "Exchanging HELLO of size %d from peer %s!\n",
400 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
401 GNUNET_i2s (&me->id));
402 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
407 notify_connect (void *cls,
408 const struct GNUNET_PeerIdentity *peer,
409 const struct GNUNET_TRANSPORT_ATS_Information *ats,
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Peer `%4s' connected to us (%p)!\n",
419 GNUNET_TRANSPORT_set_quota (p1.th,
421 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
422 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
423 start_time = GNUNET_TIME_absolute_get ();
427 GNUNET_TRANSPORT_set_quota (p2.th,
429 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
430 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
434 if (die_task != GNUNET_SCHEDULER_NO_TASK)
435 GNUNET_SCHEDULER_cancel (die_task);
436 if (tct != GNUNET_SCHEDULER_NO_TASK)
437 GNUNET_SCHEDULER_cancel (tct);
438 tct = GNUNET_SCHEDULER_NO_TASK;
439 if (p2_hello_canceled == GNUNET_NO)
441 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
442 p2_hello_canceled = GNUNET_YES;
444 if (p1_hello_canceled == GNUNET_NO)
446 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
447 p1_hello_canceled = GNUNET_YES;
449 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
451 th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
453 get_size (0), 0, TIMEOUT,
462 setup_peer (struct PeerContext *p, const char *cfgname)
464 p->cfg = GNUNET_CONFIGURATION_create ();
465 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
466 if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
467 GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
468 GNUNET_DISK_directory_remove (p->servicehome);
471 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
472 "gnunet-service-arm",
473 "gnunet-service-arm",
477 "-c", cfgname, NULL);
485 if (GNUNET_CONFIGURATION_have_value (p->cfg,
486 "transport-https", "KEY_FILE"))
487 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
488 if (key_file_p1 == NULL)
489 GNUNET_asprintf(&key_file_p1,"https_p1.key");
490 if (0 == stat (key_file_p1, &sbuf ))
492 if (0 == remove(key_file_p1))
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Successfully removed existing private key file `%s'\n",
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Failed to remove private key file `%s'\n",
501 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
502 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
503 if (cert_file_p1 == NULL)
504 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
505 if (0 == stat (cert_file_p1, &sbuf ))
507 if (0 == remove(cert_file_p1))
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Successfully removed existing certificate file `%s'\n",
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Failed to remove existing certificate file `%s'\n",
519 if (GNUNET_CONFIGURATION_have_value (p->cfg,
520 "transport-https", "KEY_FILE"))
521 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
522 if (key_file_p2 == NULL)
523 GNUNET_asprintf(&key_file_p2,"https_p2.key");
524 if (0 == stat (key_file_p2, &sbuf ))
526 if (0 == remove(key_file_p2))
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Successfully removed existing private key file `%s'\n",
531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
532 "Failed to remove private key file `%s'\n",
535 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
536 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
537 if (cert_file_p2 == NULL)
538 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
539 if (0 == stat (cert_file_p2, &sbuf ))
541 if (0 == remove(cert_file_p2))
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Successfully removed existing certificate file `%s'\n",
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Failed to remove existing certificate file `%s'\n",
552 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
557 GNUNET_assert (p->th != NULL);
562 * Return the actual path to a file found in the current
563 * PATH environment variable.
565 * @param binary the name of the file to find
568 get_path_from_PATH (char *binary)
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 _("PATH environment variable is unset.\n"));
583 path = GNUNET_strdup (p); /* because we write on it */
584 buf = GNUNET_malloc (strlen (path) + 20);
587 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
590 sprintf (buf, "%s/%s", pos, binary);
591 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
598 sprintf (buf, "%s/%s", pos, binary);
599 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
610 * Check whether the suid bit is set on a file.
611 * Attempts to find the file using the current
612 * PATH environment variable as a search path.
614 * @param binary the name of the file to check
616 * @return GNUNET_YES if the binary is found and
617 * can be run properly, GNUNET_NO otherwise
620 check_gnunet_nat_binary(char *binary)
630 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
631 p = get_path_from_PATH (binaryexe);
634 p = get_path_from_PATH (binary);
638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
639 _("Could not find binary `%s' in PATH!\n"),
643 if (0 != STAT (p, &statbuf))
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
646 _("stat (%s) failed: %s\n"),
650 return GNUNET_SYSERR;
654 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
655 (statbuf.st_uid == 0) )
659 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
660 if (INVALID_SOCKET == rawsock)
662 DWORD err = GetLastError ();
663 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
664 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
665 return GNUNET_NO; /* not running as administrator */
667 closesocket (rawsock);
674 try_connect (void *cls,
675 const struct GNUNET_SCHEDULER_TaskContext *tc)
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678 "Asking peers to connect...\n");
679 GNUNET_TRANSPORT_try_connect (p2.th,
681 GNUNET_TRANSPORT_try_connect (p1.th,
683 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
692 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
694 GNUNET_assert (ok == 1);
696 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
701 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
702 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
706 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
707 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
711 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
712 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
716 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
717 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
721 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
722 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
726 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
727 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
731 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
732 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
736 GNUNET_assert(p1.th != NULL);
737 GNUNET_assert(p2.th != NULL);
738 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
739 p1_hello_canceled = GNUNET_NO;
740 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
741 p2_hello_canceled = GNUNET_NO;
742 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
749 char *const argv[] = { "test-transport-api-reliability",
751 "test_transport_api_data.conf",
757 struct GNUNET_GETOPT_CommandLineOption options[] = {
758 GNUNET_GETOPT_OPTION_END
762 setTransportOptions("test_transport_api_data.conf");
766 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
768 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
769 "Not running NAT test case, binaries not properly installed.\n");
773 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
774 argv, "test-transport-api-reliability", "nohelp",
782 if (0 == stat (cert_file_p1, &sbuf ))
784 if (0 == remove(cert_file_p1))
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Successfully removed existing certificate file `%s'\n",
789 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
790 "Failed to remove certfile `%s'\n",
794 if (0 == stat (key_file_p1, &sbuf ))
796 if (0 == remove(key_file_p1))
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Successfully removed private key file `%s'\n",
801 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
802 "Failed to private key file `%s'\n",key_file_p1);
805 if (0 == stat (cert_file_p2, &sbuf ))
807 if (0 == remove(cert_file_p2))
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Successfully removed existing certificate file `%s'\n",
812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
813 "Failed to remove certfile `%s'\n",cert_file_p2);
816 if (0 == stat (key_file_p2, &sbuf ))
818 if (0 == remove(key_file_p2))
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
820 "Successfully removed private key file `%s'\n",
823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
824 "Failed to private key file `%s'\n",
827 GNUNET_free(key_file_p1);
828 GNUNET_free(key_file_p2);
829 GNUNET_free(cert_file_p1);
830 GNUNET_free(cert_file_p2);
831 GNUNET_free(p1.servicehome);
832 GNUNET_free(p2.servicehome);
840 main (int argc, char *argv[])
844 return GNUNET_SYSERR;
847 if (strstr(argv[0], "tcp_nat") != NULL)
849 is_tcp_nat = GNUNET_YES;
850 GNUNET_asprintf(&test_name, "tcp_nat");
852 else if (strstr(argv[0], "tcp") != NULL)
855 GNUNET_asprintf(&test_name, "tcp");
857 else if (strstr(argv[0], "https") != NULL)
859 is_https = GNUNET_YES;
860 GNUNET_asprintf(&test_name, "https");
862 else if (strstr(argv[0], "http") != NULL)
864 is_http = GNUNET_YES;
865 GNUNET_asprintf(&test_name, "http");
867 else if (strstr(argv[0], "udp") != NULL)
870 GNUNET_asprintf(&test_name, "udp");
872 else if (strstr(argv[0], "unix") != NULL)
874 is_unix = GNUNET_YES;
875 GNUNET_asprintf(&test_name, "unix");
877 else if (strstr(argv[0], "wlan") != NULL)
879 is_wlan = GNUNET_YES;
881 GNUNET_log_setup ("test-transport-api-reliability",
889 GNUNET_DISK_directory_remove (p1.servicehome);
890 GNUNET_DISK_directory_remove (p2.servicehome);
894 /* end of test_transport_api_reliability.c */