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.c
22 * @brief base test case for transport implementations
24 * This test case serves as a base for tcp, udp, and udp-nat
25 * transport test cases. Based on the executable being run
26 * the correct test case will be performed. Conservation of
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_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 * How long until we give up on transmitting the message?
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
51 * How long until we give up on transmitting the message?
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
59 struct GNUNET_CONFIGURATION_Handle *cfg;
60 struct GNUNET_TRANSPORT_Handle *th;
61 struct GNUNET_PeerIdentity id;
63 struct GNUNET_OS_Process *arm_proc;
67 static struct PeerContext p1;
69 static struct PeerContext p2;
75 static int is_tcp_nat;
81 static int is_udp_nat;
87 static int is_multi_protocol;
91 static GNUNET_SCHEDULER_TaskIdentifier die_task;
93 static GNUNET_SCHEDULER_TaskIdentifier tct;
95 static char * key_file_p1;
96 static char * cert_file_p1;
98 static char * key_file_p2;
99 static char * cert_file_p2;
102 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
104 #define OKPP do { ok++; } while (0)
111 GNUNET_assert (ok == 6);
112 GNUNET_SCHEDULER_cancel (die_task);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
114 "Disconnecting from transports!\n");
115 GNUNET_TRANSPORT_disconnect (p1.th);
116 GNUNET_TRANSPORT_disconnect (p2.th);
117 die_task = GNUNET_SCHEDULER_NO_TASK;
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "Transports disconnected, returning success!\n");
124 stop_arm (struct PeerContext *p)
127 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
128 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
129 GNUNET_OS_process_wait (p->arm_proc);
130 GNUNET_OS_process_close (p->arm_proc);
133 GNUNET_CONFIGURATION_destroy (p->cfg);
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
142 GNUNET_TRANSPORT_disconnect (p1.th);
143 GNUNET_TRANSPORT_disconnect (p2.th);
144 if (GNUNET_SCHEDULER_NO_TASK != tct)
146 GNUNET_SCHEDULER_cancel (tct);
147 tct = GNUNET_SCHEDULER_NO_TASK;
154 notify_receive (void *cls,
155 const struct GNUNET_PeerIdentity *peer,
156 const struct GNUNET_MessageHeader *message,
157 const struct GNUNET_TRANSPORT_ATS_Information *ats,
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Received message of type %d from peer %s!\n",
162 ntohs(message->type),
164 GNUNET_assert (ok == 5);
166 GNUNET_assert (MTYPE == ntohs (message->type));
167 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
168 ntohs (message->size));
174 notify_ready (void *cls, size_t size, void *buf)
176 struct PeerContext *p = cls;
177 struct GNUNET_MessageHeader *hdr;
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Transmitting message with %u bytes to peer %s\n",
181 sizeof (struct GNUNET_MessageHeader),
182 GNUNET_i2s (&p->id));
183 GNUNET_assert (size >= 256);
184 GNUNET_assert (ok == 4);
189 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
190 hdr->type = htons (MTYPE);
192 return sizeof (struct GNUNET_MessageHeader);
197 exchange_hello_last (void *cls,
198 const struct GNUNET_MessageHeader *message)
200 struct PeerContext *me = cls;
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Exchanging HELLO of size %d with peer (%s)!\n",
204 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
205 GNUNET_i2s (&me->id));
206 GNUNET_assert (message != NULL);
207 GNUNET_assert (GNUNET_OK ==
208 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
210 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
216 exchange_hello (void *cls,
217 const struct GNUNET_MessageHeader *message)
219 struct PeerContext *me = cls;
221 GNUNET_assert (message != NULL);
222 GNUNET_assert (GNUNET_OK ==
223 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Exchanging HELLO of size %d from peer %s!\n",
227 (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
228 GNUNET_i2s (&me->id));
229 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
235 notify_connect (void *cls,
236 const struct GNUNET_PeerIdentity *peer,
237 const struct GNUNET_TRANSPORT_ATS_Information *ats,
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Peer `%4s' connected to us (%p)!\n",
246 GNUNET_assert (ok >= 2);
249 GNUNET_SCHEDULER_cancel (die_task);
250 GNUNET_SCHEDULER_cancel (tct);
251 tct = GNUNET_SCHEDULER_NO_TASK;
252 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
253 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
254 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
256 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
258 256, 0, TIMEOUT, ¬ify_ready,
265 notify_disconnect (void *cls,
266 const struct GNUNET_PeerIdentity *peer)
268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269 "Peer `%4s' disconnected (%p)!\n",
270 GNUNET_i2s (peer), cls);
275 setup_peer (struct PeerContext *p,
278 p->cfg = GNUNET_CONFIGURATION_create ();
280 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
281 "gnunet-service-arm",
285 "-c", cfgname, NULL);
287 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
294 if (GNUNET_CONFIGURATION_have_value (p->cfg,
295 "transport-https", "KEY_FILE"))
296 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
297 if (key_file_p1==NULL)
298 GNUNET_asprintf(&key_file_p1,"https.key");
299 if (0 == stat (key_file_p1, &sbuf ))
301 if (0 == remove(key_file_p1))
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 "Successfully removed existing private key file `%s'\n",key_file_p1);
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306 "Failed to remove private key file `%s'\n",key_file_p1);
308 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
309 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
310 if (cert_file_p1==NULL)
311 GNUNET_asprintf(&cert_file_p1,"https.cert");
312 if (0 == stat (cert_file_p1, &sbuf ))
314 if (0 == remove(cert_file_p1))
315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
316 "Successfully removed existing certificate file `%s'\n",
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320 "Failed to remove existing certificate file `%s'\n",
326 if (GNUNET_CONFIGURATION_have_value (p->cfg,
327 "transport-https", "KEY_FILE"))
328 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
329 if (key_file_p2==NULL)
330 GNUNET_asprintf(&key_file_p2,"https.key");
331 if (0 == stat (key_file_p2, &sbuf ))
333 if (0 == remove(key_file_p2))
334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
335 "Successfully removed existing private key file `%s'\n",
338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339 "Failed to remove private key file `%s'\n",
342 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
343 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
344 if (cert_file_p2==NULL)
345 GNUNET_asprintf(&cert_file_p2,"https.cert");
346 if (0 == stat (cert_file_p2, &sbuf ))
348 if (0 == remove(cert_file_p2))
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 "Successfully removed existing certificate file `%s'\n",cert_file_p2);
352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
353 "Failed to remove existing certificate file `%s'\n",cert_file_p2);
358 p->th = GNUNET_TRANSPORT_connect (p->cfg,
361 ¬ify_connect, ¬ify_disconnect);
362 GNUNET_assert (p->th != NULL);
367 try_connect (void *cls,
368 const struct GNUNET_SCHEDULER_TaskContext *tc)
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Asking peers to connect...\n");
372 GNUNET_TRANSPORT_try_connect (p2.th,
374 GNUNET_TRANSPORT_try_connect (p1.th,
376 tct = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
385 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
387 GNUNET_assert (ok == 1);
389 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
394 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
395 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
399 setup_peer (&p1, "test_transport_api_unix_peer1.conf");
400 setup_peer (&p2, "test_transport_api_unix_peer2.conf");
402 if (is_multi_protocol)
404 setup_peer (&p1, "test_transport_api_multi_peer1.conf");
405 setup_peer (&p2, "test_transport_api_multi_peer2.conf");
409 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
410 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
414 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
415 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
419 setup_peer (&p1, "test_transport_api_udp_nat_peer1.conf");
420 setup_peer (&p2, "test_transport_api_udp_nat_peer2.conf");
424 setup_peer (&p1, "test_transport_api_http_peer1.conf");
425 setup_peer (&p2, "test_transport_api_http_peer2.conf");
429 setup_peer (&p1, "test_transport_api_https_peer1.conf");
430 setup_peer (&p2, "test_transport_api_https_peer2.conf");
434 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
435 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
437 GNUNET_assert(p1.th != NULL);
438 GNUNET_assert(p2.th != NULL);
440 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
441 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
442 tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
448 static char *const argv[] = { "test-transport-api",
450 "test_transport_api_data.conf",
456 static struct GNUNET_GETOPT_CommandLineOption options[] = {
457 GNUNET_GETOPT_OPTION_END
461 setTransportOptions("test_transport_api_data.conf");
464 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
465 argv, "test-transport-api", "nohelp",
473 if (0 == stat (cert_file_p1, &sbuf ))
475 if (0 == remove(cert_file_p1))
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Successfully removed existing certificate file `%s'\n",cert_file_p1);
479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480 "Failed to remove certfile `%s'\n",cert_file_p1);
483 if (0 == stat (key_file_p1, &sbuf ))
485 if (0 == remove(key_file_p1))
486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
487 "Successfully removed private key file `%s'\n",key_file_p1);
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490 "Failed to private key file `%s'\n",key_file_p1);
493 if (0 == stat (cert_file_p2, &sbuf ))
495 if (0 == remove(cert_file_p2))
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Successfully removed existing certificate file `%s'\n",cert_file_p2);
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 "Failed to remove certfile `%s'\n",cert_file_p2);
503 if (0 == stat (key_file_p2, &sbuf ))
505 if (0 == remove(key_file_p2))
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Successfully removed private key file `%s'\n",key_file_p2);
509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
510 "Failed to private key file `%s'\n",key_file_p2);
512 GNUNET_free(key_file_p1);
513 GNUNET_free(key_file_p2);
514 GNUNET_free(cert_file_p1);
515 GNUNET_free(cert_file_p2);
521 * Return the actual path to a file found in the current
522 * PATH environment variable.
524 * @param binary the name of the file to find
527 get_path_from_PATH (char *binary)
538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
539 _("PATH environment variable is unset.\n"));
542 path = GNUNET_strdup (p); /* because we write on it */
543 buf = GNUNET_malloc (strlen (path) + 20);
546 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
549 sprintf (buf, "%s/%s", pos, binary);
550 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
557 sprintf (buf, "%s/%s", pos, binary);
558 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
569 * Check whether the suid bit is set on a file.
570 * Attempts to find the file using the current
571 * PATH environment variable as a search path.
573 * @param binary the name of the file to check
575 * @return GNUNET_YES if the binary is found and
576 * can be run properly, GNUNET_NO otherwise
579 check_gnunet_nat_binary(char *binary)
589 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
590 p = get_path_from_PATH (binaryexe);
593 p = get_path_from_PATH (binary);
597 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
598 _("Could not find binary `%s' in PATH!\n"),
602 if (0 != STAT (p, &statbuf))
604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
605 _("stat (%s) failed: %s\n"),
609 return GNUNET_SYSERR;
613 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
614 (statbuf.st_uid == 0) )
618 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
619 if (INVALID_SOCKET == rawsock)
621 DWORD err = GetLastError ();
622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
623 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
624 return GNUNET_NO; /* not running as administrator */
626 closesocket (rawsock);
632 main (int argc, char *argv[])
636 return GNUNET_SYSERR;
639 GNUNET_log_setup ("test-transport-api",
647 if (strstr(argv[0], "tcp_nat") != NULL)
649 is_tcp_nat = GNUNET_YES;
650 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
652 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
653 "`%s' not properly installed, cannot run NAT test!\n",
654 "gnunet-nat-server");
658 else if (strstr(argv[0], "tcp") != NULL)
662 else if (strstr(argv[0], "udp_nat") != NULL)
664 is_udp_nat = GNUNET_YES;
665 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
668 "`%s' not properly installed, cannot run NAT test!\n",
669 "gnunet-nat-server");
673 else if (strstr(argv[0], "udp") != NULL)
677 else if (strstr(argv[0], "unix") != NULL)
679 is_unix = GNUNET_YES;
681 else if (strstr(argv[0], "https") != NULL)
683 is_https = GNUNET_YES;
685 else if (strstr(argv[0], "http") != NULL)
687 is_http = GNUNET_YES;
689 else if (strstr(argv[0], "wlan") != NULL)
691 is_wlan = GNUNET_YES;
693 else if (strstr(argv[0], "multi") != NULL)
695 is_multi_protocol = GNUNET_YES;
699 if (is_multi_protocol)
701 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-1/");
702 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-2/");
706 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
707 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
713 /* end of test_transport_api.c */