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"
37 #include "transport.h"
39 #define VERBOSE GNUNET_NO
41 #define VERBOSE_ARM GNUNET_NO
43 #define START_ARM GNUNET_YES
46 * Note that this value must not significantly exceed
47 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
48 * messages may be dropped even for a reliable transport.
50 #define TOTAL_MSGS (10000 * 2)
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
61 struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_TRANSPORT_Handle *th;
63 struct GNUNET_PeerIdentity id;
65 struct GNUNET_OS_Process *arm_proc;
69 static struct PeerContext p1;
71 static struct PeerContext p2;
77 static int is_tcp_nat;
87 static unsigned long long total_bytes;
89 static struct GNUNET_TIME_Absolute start_time;
91 static GNUNET_SCHEDULER_TaskIdentifier die_task;
93 static char * key_file_p1;
94 static char * cert_file_p1;
96 static char * key_file_p2;
97 static char * cert_file_p2;
99 static int msg_scheduled;
101 static int msg_recv_expected;
103 static struct GNUNET_TRANSPORT_TransmitHandle * transmit_handle;
106 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
108 #define OKPP do { ok++; } while (0)
115 unsigned long long delta;
117 GNUNET_SCHEDULER_cancel (die_task);
118 die_task = GNUNET_SCHEDULER_NO_TASK;
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
122 GNUNET_TRANSPORT_disconnect (p1.th);
123 GNUNET_TRANSPORT_disconnect (p2.th);
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
126 "Transports disconnected, returning success!\n");
128 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
130 "\nThroughput was %llu kb/s\n",
131 total_bytes * 1000 / 1024 / delta);
139 stop_arm (struct PeerContext *p)
142 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
143 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
144 GNUNET_OS_process_wait (p->arm_proc);
145 GNUNET_OS_process_close (p->arm_proc);
148 GNUNET_CONFIGURATION_destroy (p->cfg);
153 end_badly (void *cls,
154 const struct GNUNET_SCHEDULER_TaskContext *tc)
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 "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);
159 GNUNET_TRANSPORT_disconnect (p1.th);
160 GNUNET_TRANSPORT_disconnect (p2.th);
167 struct GNUNET_MessageHeader header;
173 get_size (unsigned int iter)
178 return iter + sizeof (struct TestMessage);
179 ret = (iter * iter * iter);
180 return sizeof (struct TestMessage) + (ret % 60000);
185 notify_receive (void *cls,
186 const struct GNUNET_PeerIdentity *peer,
187 const struct GNUNET_MessageHeader *message,
188 const struct GNUNET_TRANSPORT_ATS_Information *ats,
193 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
194 const struct TestMessage *hdr;
196 hdr = (const struct TestMessage*) message;
198 if (MTYPE != ntohs (message->type))
200 msg_recv_expected = n;
201 msg_recv = ntohl(hdr->num);
202 if (ntohs (message->size) != s)
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205 "Expected message %u of size %u, got %u bytes of message %u\n",
207 ntohs (message->size),
209 GNUNET_SCHEDULER_cancel (die_task);
210 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
213 if (ntohl (hdr->num) != n)
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 "Expected message %u of size %u, got %u bytes of message %u\n",
218 ntohs (message->size),
220 GNUNET_SCHEDULER_cancel (die_task);
221 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
224 memset (cbuf, n, s - sizeof (struct TestMessage));
225 if (0 != memcmp (cbuf,
227 s - sizeof (struct TestMessage)))
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
230 "Expected message %u with bits %u, but body did not match\n",
231 n, (unsigned char) n);
232 GNUNET_SCHEDULER_cancel (die_task);
233 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
237 if (ntohl(hdr->num) % 5000 == 0)
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Got message %u of size %u\n",
242 ntohs (message->size));
246 if (0 == (n % (TOTAL_MSGS/100)))
248 fprintf (stderr, ".");
249 GNUNET_SCHEDULER_cancel (die_task);
250 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
260 notify_ready (void *cls, size_t size, void *buf)
264 struct TestMessage hdr;
276 GNUNET_assert (size >= s);
277 GNUNET_assert (buf != NULL);
281 hdr.header.size = htons (s);
282 hdr.header.type = htons (MTYPE);
285 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
286 ret += sizeof (struct TestMessage);
287 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
288 ret += s - sizeof (struct TestMessage);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Sending message %u of size %u\n",
300 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
301 break; /* sometimes pack buffer full, sometimes not */
303 while (size - ret >= s);
306 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Returning total message block of size %u\n",
325 notify_connect (void *cls,
326 const struct GNUNET_PeerIdentity *peer,
327 const struct GNUNET_TRANSPORT_ATS_Information *ats,
332 GNUNET_TRANSPORT_set_quota (p1.th,
334 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
335 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
336 GNUNET_TIME_UNIT_FOREVER_REL,
338 start_time = GNUNET_TIME_absolute_get ();
343 GNUNET_TRANSPORT_set_quota (p2.th,
345 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
346 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
347 GNUNET_TIME_UNIT_FOREVER_REL,
355 if ((transmit_handle!=NULL) && (cls == NULL))
356 GNUNET_TRANSPORT_notify_transmit_ready_cancel(transmit_handle);
357 if ((transmit_handle!=NULL) && (cls == &transmit_handle))
358 transmit_handle=NULL;
359 GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
361 get_size (0), 0, TIMEOUT,
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
373 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "Peer `%4s' disconnected (%p)!\n",
378 GNUNET_i2s (peer), cls);
384 setup_peer (struct PeerContext *p, const char *cfgname)
386 p->cfg = GNUNET_CONFIGURATION_create ();
388 p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
389 "gnunet-service-arm",
390 "gnunet-service-arm",
394 "-c", cfgname, NULL);
396 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
403 if (GNUNET_CONFIGURATION_have_value (p->cfg,
404 "transport-https", "KEY_FILE"))
405 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
406 if (key_file_p1 == NULL)
407 GNUNET_asprintf(&key_file_p1,"https_p1.key");
408 if (0 == stat (key_file_p1, &sbuf ))
410 if (0 == remove(key_file_p1))
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p1);
413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p1);
415 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
416 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
417 if (cert_file_p1 == NULL)
418 GNUNET_asprintf(&cert_file_p1,"https_p1.cert");
419 if (0 == stat (cert_file_p1, &sbuf ))
421 if (0 == remove(cert_file_p1))
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p1);
429 if (GNUNET_CONFIGURATION_have_value (p->cfg,
430 "transport-https", "KEY_FILE"))
431 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
432 if (key_file_p2 == NULL)
433 GNUNET_asprintf(&key_file_p2,"https_p2.key");
434 if (0 == stat (key_file_p2, &sbuf ))
436 if (0 == remove(key_file_p2))
437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p2);
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p2);
441 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
442 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
443 if (cert_file_p2 == NULL)
444 GNUNET_asprintf(&cert_file_p2,"https_p2.cert");
445 if (0 == stat (cert_file_p2, &sbuf ))
447 if (0 == remove(cert_file_p2))
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
450 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p2);
455 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL,
460 GNUNET_assert (p->th != NULL);
464 notify_ready_connect (void *cls, size_t size, void *buf)
470 exchange_hello_last (void *cls,
471 const struct GNUNET_MessageHeader *message)
473 struct PeerContext *me = cls;
474 transmit_handle = NULL;
475 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
478 "Exchanging HELLO with peer (%p)!\n", cls);
480 GNUNET_assert (ok >= 3);
482 GNUNET_assert (message != NULL);
483 GNUNET_assert (GNUNET_OK ==
484 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
487 GNUNET_assert(NULL != (transmit_handle = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
489 sizeof (struct GNUNET_MessageHeader), 0,
491 ¬ify_ready_connect,
494 /* both HELLOs exchanged, get ready to test transmission! */
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Finished exchanging HELLOs, now waiting for transmission!\n");
501 exchange_hello (void *cls,
502 const struct GNUNET_MessageHeader *message)
504 struct PeerContext *me = cls;
506 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Exchanging HELLO with peer (%p)!\n", cls);
511 GNUNET_assert (ok >= 2);
513 GNUNET_assert (message != NULL);
514 GNUNET_assert (GNUNET_OK ==
515 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Received HELLO size %d\n",
521 GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
523 GNUNET_TRANSPORT_offer_hello (p2.th, message);
524 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
528 * Return the actual path to a file found in the current
529 * PATH environment variable.
531 * @param binary the name of the file to find
534 get_path_from_PATH (char *binary)
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 _("PATH environment variable is unset.\n"));
549 path = GNUNET_strdup (p); /* because we write on it */
550 buf = GNUNET_malloc (strlen (path) + 20);
553 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
556 sprintf (buf, "%s/%s", pos, binary);
557 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
564 sprintf (buf, "%s/%s", pos, binary);
565 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
576 * Check whether the suid bit is set on a file.
577 * Attempts to find the file using the current
578 * PATH environment variable as a search path.
580 * @param binary the name of the file to check
582 * @return GNUNET_YES if the binary is found and
583 * can be run properly, GNUNET_NO otherwise
586 check_gnunet_nat_binary(char *binary)
596 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
597 p = get_path_from_PATH (binaryexe);
600 p = get_path_from_PATH (binary);
604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
605 _("Could not find binary `%s' in PATH!\n"),
609 if (0 != STAT (p, &statbuf))
611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
612 _("stat (%s) failed: %s\n"),
616 return GNUNET_SYSERR;
620 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
621 (statbuf.st_uid == 0) )
625 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
626 if (INVALID_SOCKET == rawsock)
628 DWORD err = GetLastError ();
629 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
630 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
631 return GNUNET_NO; /* not running as administrator */
633 closesocket (rawsock);
641 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
643 GNUNET_assert (ok == 1);
645 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
650 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
651 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
655 setup_peer (&p1, "test_transport_api_rel_http_peer1.conf");
656 setup_peer (&p2, "test_transport_api_rel_http_peer2.conf");
660 setup_peer (&p1, "test_transport_api_rel_https_peer1.conf");
661 setup_peer (&p2, "test_transport_api_rel_https_peer2.conf");
665 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
666 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
670 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
671 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
675 GNUNET_assert(p1.th != NULL);
676 GNUNET_assert(p2.th != NULL);
677 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
684 char *const argv[] = { "test-transport-api-reliability",
686 "test_transport_api_data.conf",
692 struct GNUNET_GETOPT_CommandLineOption options[] = {
693 GNUNET_GETOPT_OPTION_END
697 setTransportOptions("test_transport_api_data.conf");
701 if ((GNUNET_YES == is_tcp_nat) && (check_gnunet_nat_binary("gnunet-nat-server") != GNUNET_YES))
703 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Not running NAT test case, binaries not properly installed.\n");
707 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
708 argv, "test-transport-api-reliability", "nohelp",
716 if (0 == stat (cert_file_p1, &sbuf ))
718 if (0 == remove(cert_file_p1))
719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
724 if (0 == stat (key_file_p1, &sbuf ))
726 if (0 == remove(key_file_p1))
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
732 if (0 == stat (cert_file_p2, &sbuf ))
734 if (0 == remove(cert_file_p2))
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
740 if (0 == stat (key_file_p2, &sbuf ))
742 if (0 == remove(key_file_p2))
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
745 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
747 GNUNET_free(key_file_p1);
748 GNUNET_free(key_file_p2);
749 GNUNET_free(cert_file_p1);
750 GNUNET_free(cert_file_p2);
758 main (int argc, char *argv[])
762 return GNUNET_SYSERR;
765 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
766 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
768 if (strstr(argv[0], "tcp_nat") != NULL)
770 is_tcp_nat = GNUNET_YES;
772 else if (strstr(argv[0], "tcp") != NULL)
776 else if (strstr(argv[0], "https") != NULL)
778 is_https = GNUNET_YES;
780 else if (strstr(argv[0], "http") != NULL)
782 is_http = GNUNET_YES;
784 else if (strstr(argv[0], "udp") != NULL)
788 GNUNET_log_setup ("test-transport-api-reliability",
796 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
797 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
801 /* end of test_transport_api_reliability.c */