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 char * key_file_p1;
99 static char * cert_file_p1;
101 static char * key_file_p2;
102 static char * cert_file_p2;
103 static char *test_name;
104 static int msg_scheduled;
106 static int msg_recv_expected;
108 static struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
111 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
113 #define OKPP do { ok++; } while (0)
120 unsigned long long delta;
123 GNUNET_SCHEDULER_cancel (die_task);
124 die_task = GNUNET_SCHEDULER_NO_TASK;
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
128 GNUNET_TRANSPORT_disconnect (p1.th);
129 GNUNET_TRANSPORT_disconnect (p2.th);
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Transports disconnected, returning success!\n");
134 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
136 "\nThroughput was %llu kb/s\n",
137 total_bytes * 1000 / 1024 / delta);
138 GNUNET_asprintf(&value_name, "reliable_%s", test_name);
139 GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
140 GNUNET_free(value_name);
148 stop_arm (struct PeerContext *p)
151 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
152 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
153 GNUNET_OS_process_wait (p->arm_proc);
154 GNUNET_OS_process_close (p->arm_proc);
157 GNUNET_CONFIGURATION_destroy (p->cfg);
162 end_badly (void *cls,
163 const struct GNUNET_SCHEDULER_TaskContext *tc)
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "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);
168 GNUNET_TRANSPORT_disconnect (p1.th);
169 GNUNET_TRANSPORT_disconnect (p2.th);
176 struct GNUNET_MessageHeader header;
182 get_size (unsigned int iter)
187 return iter + sizeof (struct TestMessage);
188 ret = (iter * iter * iter);
189 return sizeof (struct TestMessage) + (ret % 60000);
194 notify_receive (void *cls,
195 const struct GNUNET_PeerIdentity *peer,
196 const struct GNUNET_MessageHeader *message,
197 const struct GNUNET_TRANSPORT_ATS_Information *ats,
202 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
203 const struct TestMessage *hdr;
205 hdr = (const struct TestMessage*) message;
207 if (MTYPE != ntohs (message->type))
209 msg_recv_expected = n;
210 msg_recv = ntohl(hdr->num);
211 if (ntohs (message->size) != s)
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214 "Expected message %u of size %u, got %u bytes of message %u\n",
216 ntohs (message->size),
218 GNUNET_SCHEDULER_cancel (die_task);
219 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
222 if (ntohl (hdr->num) != n)
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 "Expected message %u of size %u, got %u bytes of message %u\n",
227 ntohs (message->size),
229 GNUNET_SCHEDULER_cancel (die_task);
230 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
233 memset (cbuf, n, s - sizeof (struct TestMessage));
234 if (0 != memcmp (cbuf,
236 s - sizeof (struct TestMessage)))
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Expected message %u with bits %u, but body did not match\n",
240 n, (unsigned char) n);
241 GNUNET_SCHEDULER_cancel (die_task);
242 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
246 if (ntohl(hdr->num) % 5000 == 0)
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Got message %u of size %u\n",
251 ntohs (message->size));
255 if (0 == (n % (TOTAL_MSGS/100)))
257 fprintf (stderr, ".");
258 GNUNET_SCHEDULER_cancel (die_task);
259 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
269 notify_ready (void *cls, size_t size, void *buf)
273 struct TestMessage hdr;
285 GNUNET_assert (size >= s);
286 GNUNET_assert (buf != NULL);
290 hdr.header.size = htons (s);
291 hdr.header.type = htons (MTYPE);
294 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
295 ret += sizeof (struct TestMessage);
296 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
297 ret += s - sizeof (struct TestMessage);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Sending message %u of size %u\n",
309 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
310 break; /* sometimes pack buffer full, sometimes not */
312 while (size - ret >= s);
315 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Returning total message block of size %u\n",
334 notify_connect (void *cls,
335 const struct GNUNET_PeerIdentity *peer,
336 const struct GNUNET_TRANSPORT_ATS_Information *ats,
341 GNUNET_TRANSPORT_set_quota (p1.th,
343 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
344 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
345 GNUNET_TIME_UNIT_FOREVER_REL,
347 start_time = GNUNET_TIME_absolute_get ();
352 GNUNET_TRANSPORT_set_quota (p2.th,
354 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
355 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
356 GNUNET_TIME_UNIT_FOREVER_REL,
364 if ((transmit_handle!=NULL) && (cls == NULL))
365 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
366 if ((transmit_handle!=NULL) && (cls == &transmit_handle))
367 transmit_handle=NULL;
368 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
370 get_size (0), 0, TIMEOUT,
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
382 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Peer `%4s' disconnected (%p)!\n",
387 GNUNET_i2s (peer), cls);
393 setup_peer (struct PeerContext *p, const char *cfgname)
395 p->cfg = GNUNET_CONFIGURATION_create ();
397 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
398 "gnunet-service-arm",
399 "gnunet-service-arm",
403 "-c", cfgname, NULL);
405 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
412 if (GNUNET_CONFIGURATION_have_value (p->cfg,
413 "transport-https", "KEY_FILE"))
414 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
415 if (key_file_p1 == NULL)
416 GNUNET_asprintf(&key_file_p1,"https_p1.key");
417 if (0 == stat (key_file_p1, &sbuf ))
419 if (0 == remove(key_file_p1))
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p1);
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p1);
424 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
425 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
426 if (cert_file_p1 == NULL)
427 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
428 if (0 == stat (cert_file_p1, &sbuf ))
430 if (0 == remove(cert_file_p1))
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p1);
438 if (GNUNET_CONFIGURATION_have_value (p->cfg,
439 "transport-https", "KEY_FILE"))
440 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
441 if (key_file_p2 == NULL)
442 GNUNET_asprintf(&key_file_p2,"https_p2.key");
443 if (0 == stat (key_file_p2, &sbuf ))
445 if (0 == remove(key_file_p2))
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p2);
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p2);
450 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
451 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
452 if (cert_file_p2 == NULL)
453 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
454 if (0 == stat (cert_file_p2, &sbuf ))
456 if (0 == remove(cert_file_p2))
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p2);
464 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
469 GNUNET_assert (p->th != NULL);
473 notify_ready_connect (void *cls, size_t size, void *buf)
479 exchange_hello_last (void *cls,
480 const struct GNUNET_MessageHeader *message)
482 struct PeerContext *me = cls;
483 transmit_handle = NULL;
484 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Exchanging HELLO with peer (%p)!\n", cls);
489 GNUNET_assert (ok >= 3);
491 GNUNET_assert (message != NULL);
492 GNUNET_assert (GNUNET_OK ==
493 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
496 GNUNET_assert(NULL != (transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
498 sizeof (struct GNUNET_MessageHeader), 0,
500 ¬ify_ready_connect,
503 /* both HELLOs exchanged, get ready to test transmission! */
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "Finished exchanging HELLOs, now waiting for transmission!\n");
510 exchange_hello (void *cls,
511 const struct GNUNET_MessageHeader *message)
513 struct PeerContext *me = cls;
515 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Exchanging HELLO with peer (%p)!\n", cls);
520 GNUNET_assert (ok >= 2);
522 GNUNET_assert (message != NULL);
523 GNUNET_assert (GNUNET_OK ==
524 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "Received HELLO size %d\n",
530 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
532 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
533 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
537 * Return the actual path to a file found in the current
538 * PATH environment variable.
540 * @param binary the name of the file to find
543 get_path_from_PATH (char *binary)
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 _("PATH environment variable is unset.\n"));
558 path = GNUNET_strdup (p); /* because we write on it */
559 buf = GNUNET_malloc (strlen (path) + 20);
562 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
565 sprintf (buf, "%s/%s", pos, binary);
566 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
573 sprintf (buf, "%s/%s", pos, binary);
574 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
585 * Check whether the suid bit is set on a file.
586 * Attempts to find the file using the current
587 * PATH environment variable as a search path.
589 * @param binary the name of the file to check
591 * @return GNUNET_YES if the binary is found and
592 * can be run properly, GNUNET_NO otherwise
595 check_gnunet_nat_binary(char *binary)
605 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
606 p = get_path_from_PATH (binaryexe);
609 p = get_path_from_PATH (binary);
613 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
614 _("Could not find binary `%s' in PATH!\n"),
618 if (0 != STAT (p, &statbuf))
620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
621 _("stat (%s) failed: %s\n"),
625 return GNUNET_SYSERR;
629 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
630 (statbuf.st_uid == 0) )
634 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
635 if (INVALID_SOCKET == rawsock)
637 DWORD err = GetLastError ();
638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
639 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
640 return GNUNET_NO; /* not running as administrator */
642 closesocket (rawsock);
650 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
652 GNUNET_assert (ok == 1);
654 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
659 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
660 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
664 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
665 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
669 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
670 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
674 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
675 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
679 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
680 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
684 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
685 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
689 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
690 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
694 GNUNET_assert(p1.th != NULL);
695 GNUNET_assert(p2.th != NULL);
696 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
703 char *const argv[] = { "test-transport-api-reliability",
705 "test_transport_api_data.conf",
711 struct GNUNET_GETOPT_CommandLineOption options[] = {
712 GNUNET_GETOPT_OPTION_END
716 setTransportOptions("test_transport_api_data.conf");
720 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
722 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
726 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
727 argv, "test-transport-api-reliability", "nohelp",
735 if (0 == stat (cert_file_p1, &sbuf ))
737 if (0 == remove(cert_file_p1))
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
743 if (0 == stat (key_file_p1, &sbuf ))
745 if (0 == remove(key_file_p1))
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
751 if (0 == stat (cert_file_p2, &sbuf ))
753 if (0 == remove(cert_file_p2))
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
756 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
759 if (0 == stat (key_file_p2, &sbuf ))
761 if (0 == remove(key_file_p2))
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
766 GNUNET_free(key_file_p1);
767 GNUNET_free(key_file_p2);
768 GNUNET_free(cert_file_p1);
769 GNUNET_free(cert_file_p2);
777 main (int argc, char *argv[])
781 return GNUNET_SYSERR;
784 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
785 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
787 if (strstr(argv[0], "tcp_nat") != NULL)
789 is_tcp_nat = GNUNET_YES;
790 GNUNET_asprintf(&test_name, "tcp_nat");
792 else if (strstr(argv[0], "tcp") != NULL)
795 GNUNET_asprintf(&test_name, "tcp");
797 else if (strstr(argv[0], "https") != NULL)
799 is_https = GNUNET_YES;
800 GNUNET_asprintf(&test_name, "https");
802 else if (strstr(argv[0], "http") != NULL)
804 is_http = GNUNET_YES;
805 GNUNET_asprintf(&test_name, "http");
807 else if (strstr(argv[0], "udp") != NULL)
810 GNUNET_asprintf(&test_name, "udp");
812 else if (strstr(argv[0], "unix") != NULL)
814 is_unix = GNUNET_YES;
815 GNUNET_asprintf(&test_name, "unix");
817 else if (strstr(argv[0], "wlan") != NULL)
819 is_wlan = GNUNET_YES;
821 GNUNET_log_setup ("test-transport-api-reliability",
829 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
830 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
834 /* end of test_transport_api_reliability.c */