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;
79 static int is_udp_nat;
85 static int is_multi_protocol;
89 static GNUNET_SCHEDULER_TaskIdentifier die_task;
91 static char * key_file_p1;
92 static char * cert_file_p1;
94 static char * key_file_p2;
95 static char * cert_file_p2;
98 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
100 #define OKPP do { ok++; } while (0)
108 GNUNET_assert (ok == 6);
109 GNUNET_SCHEDULER_cancel (die_task);
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
111 GNUNET_TRANSPORT_disconnect (p1.th);
112 GNUNET_TRANSPORT_disconnect (p2.th);
114 die_task = GNUNET_SCHEDULER_NO_TASK;
115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
120 stop_arm (struct PeerContext *p)
123 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
124 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
125 GNUNET_OS_process_wait (p->arm_proc);
126 GNUNET_OS_process_close (p->arm_proc);
129 GNUNET_CONFIGURATION_destroy (p->cfg);
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
138 GNUNET_TRANSPORT_disconnect (p1.th);
139 GNUNET_TRANSPORT_disconnect (p2.th);
144 notify_receive (void *cls,
145 const struct GNUNET_PeerIdentity *peer,
146 const struct GNUNET_MessageHeader *message,
147 const struct GNUNET_TRANSPORT_ATS_Information *ats,
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
154 ntohs(message->type), cls);
156 GNUNET_assert (ok == 5);
159 GNUNET_assert (MTYPE == ntohs (message->type));
160 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
161 ntohs (message->size));
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
169 notify_ready (void *cls, size_t size, void *buf)
171 struct GNUNET_MessageHeader *hdr;
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
175 GNUNET_assert (size >= 256);
176 GNUNET_assert (ok == 4);
182 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
183 hdr->type = htons (MTYPE);
186 return sizeof (struct GNUNET_MessageHeader);
190 notify_ready_connect (void *cls, size_t size, void *buf)
197 notify_connect (void *cls,
198 const struct GNUNET_PeerIdentity *peer,
199 const struct GNUNET_TRANSPORT_ATS_Information *ats,
204 GNUNET_SCHEDULER_cancel (die_task);
205 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
208 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
210 256, 0, TIMEOUT, ¬ify_ready,
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
219 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Peer `%4s' disconnected (%p)!\n",
223 GNUNET_i2s (peer), cls);
228 setup_peer (struct PeerContext *p, const char *cfgname)
230 p->cfg = GNUNET_CONFIGURATION_create ();
232 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
233 "gnunet-service-arm",
237 "-c", cfgname, NULL);
239 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
246 if (GNUNET_CONFIGURATION_have_value (p->cfg,
247 "transport-https", "KEY_FILE"))
248 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
249 if (key_file_p1==NULL)
250 GNUNET_asprintf(&key_file_p1,"https.key");
251 if (0 == stat (key_file_p1, &sbuf ))
253 if (0 == remove(key_file_p1))
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p1);
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p1);
258 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
259 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
260 if (cert_file_p1==NULL)
261 GNUNET_asprintf(&cert_file_p1,"https.cert");
262 if (0 == stat (cert_file_p1, &sbuf ))
264 if (0 == remove(cert_file_p1))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p1);
272 if (GNUNET_CONFIGURATION_have_value (p->cfg,
273 "transport-https", "KEY_FILE"))
274 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
275 if (key_file_p2==NULL)
276 GNUNET_asprintf(&key_file_p2,"https.key");
277 if (0 == stat (key_file_p2, &sbuf ))
279 if (0 == remove(key_file_p2))
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p2);
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p2);
284 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
285 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
286 if (cert_file_p2==NULL)
287 GNUNET_asprintf(&cert_file_p2,"https.cert");
288 if (0 == stat (cert_file_p2, &sbuf ))
290 if (0 == remove(cert_file_p2))
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p2);
298 p->th = GNUNET_TRANSPORT_connect (p->cfg,
301 ¬ify_connect, ¬ify_disconnect);
302 GNUNET_assert (p->th != NULL);
307 exchange_hello_last (void *cls,
308 const struct GNUNET_MessageHeader *message)
310 struct PeerContext *me = cls;
312 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Exchanging HELLO with peer (%p)!\n", cls);
315 GNUNET_assert (ok >= 3);
317 GNUNET_assert (message != NULL);
318 GNUNET_assert (GNUNET_OK ==
319 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
322 GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
324 sizeof (struct GNUNET_MessageHeader), 0,
326 ¬ify_ready_connect,
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Finished exchanging HELLOs, now waiting for transmission!\n");
334 exchange_hello (void *cls,
335 const struct GNUNET_MessageHeader *message)
337 struct PeerContext *me = cls;
339 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "Exchanging HELLO with peer (%p)!\n", cls);
342 GNUNET_assert (ok >= 2);
344 GNUNET_assert (message != NULL);
345 GNUNET_assert (GNUNET_OK ==
346 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
352 GNUNET_TRANSPORT_offer_hello (p2.th, message);
353 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
360 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
362 GNUNET_assert (ok == 1);
364 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
369 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
370 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
372 if (is_multi_protocol)
374 setup_peer (&p1, "test_transport_api_multi_peer1.conf");
375 setup_peer (&p2, "test_transport_api_multi_peer2.conf");
379 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
380 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
384 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
385 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
389 setup_peer (&p1, "test_transport_api_udp_nat_peer1.conf");
390 setup_peer (&p2, "test_transport_api_udp_nat_peer2.conf");
394 setup_peer (&p1, "test_transport_api_http_peer1.conf");
395 setup_peer (&p2, "test_transport_api_http_peer2.conf");
399 setup_peer (&p1, "test_transport_api_https_peer1.conf");
400 setup_peer (&p2, "test_transport_api_https_peer2.conf");
404 setup_peer (&p1, "test_transport_api_wlan_peer1.conf");
405 setup_peer (&p2, "test_transport_api_wlan_peer2.conf");
407 GNUNET_assert(p1.th != NULL);
408 GNUNET_assert(p2.th != NULL);
410 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
416 static char *const argv[] = { "test-transport-api",
418 "test_transport_api_data.conf",
424 static struct GNUNET_GETOPT_CommandLineOption options[] = {
425 GNUNET_GETOPT_OPTION_END
429 setTransportOptions("test_transport_api_data.conf");
432 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
433 argv, "test-transport-api", "nohelp",
441 if (0 == stat (cert_file_p1, &sbuf ))
443 if (0 == remove(cert_file_p1))
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
449 if (0 == stat (key_file_p1, &sbuf ))
451 if (0 == remove(key_file_p1))
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
457 if (0 == stat (cert_file_p2, &sbuf ))
459 if (0 == remove(cert_file_p2))
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
465 if (0 == stat (key_file_p2, &sbuf ))
467 if (0 == remove(key_file_p2))
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
472 GNUNET_free(key_file_p1);
473 GNUNET_free(key_file_p2);
474 GNUNET_free(cert_file_p1);
475 GNUNET_free(cert_file_p2);
481 * Return the actual path to a file found in the current
482 * PATH environment variable.
484 * @param binary the name of the file to find
487 get_path_from_PATH (char *binary)
498 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499 _("PATH environment variable is unset.\n"));
502 path = GNUNET_strdup (p); /* because we write on it */
503 buf = GNUNET_malloc (strlen (path) + 20);
506 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
509 sprintf (buf, "%s/%s", pos, binary);
510 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
517 sprintf (buf, "%s/%s", pos, binary);
518 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
529 * Check whether the suid bit is set on a file.
530 * Attempts to find the file using the current
531 * PATH environment variable as a search path.
533 * @param binary the name of the file to check
535 * @return GNUNET_YES if the binary is found and
536 * can be run properly, GNUNET_NO otherwise
539 check_gnunet_nat_binary(char *binary)
549 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
550 p = get_path_from_PATH (binaryexe);
553 p = get_path_from_PATH (binary);
557 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
558 _("Could not find binary `%s' in PATH!\n"),
562 if (0 != STAT (p, &statbuf))
564 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
565 _("stat (%s) failed: %s\n"),
569 return GNUNET_SYSERR;
573 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
574 (statbuf.st_uid == 0) )
578 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
579 if (INVALID_SOCKET == rawsock)
581 DWORD err = GetLastError ();
582 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
583 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
584 return GNUNET_NO; /* not running as administrator */
586 closesocket (rawsock);
592 main (int argc, char *argv[])
596 return GNUNET_SYSERR;
599 GNUNET_log_setup ("test-transport-api",
607 if (strstr(argv[0], "tcp_nat") != NULL)
609 is_tcp_nat = GNUNET_YES;
610 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
612 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
613 "`%s' not properly installed, cannot run NAT test!\n",
614 "gnunet-nat-server");
618 else if (strstr(argv[0], "tcp") != NULL)
622 else if (strstr(argv[0], "udp_nat") != NULL)
624 is_udp_nat = GNUNET_YES;
625 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
628 "`%s' not properly installed, cannot run NAT test!\n",
629 "gnunet-nat-server");
633 else if (strstr(argv[0], "udp") != NULL)
637 else if (strstr(argv[0], "https") != NULL)
639 is_https = GNUNET_YES;
641 else if (strstr(argv[0], "http") != NULL)
643 is_http = GNUNET_YES;
645 else if (strstr(argv[0], "wlan") != NULL)
647 is_wlan = GNUNET_YES;
649 else if (strstr(argv[0], "multi") != NULL)
651 is_multi_protocol = GNUNET_YES;
655 if (is_multi_protocol)
657 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-1/");
658 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-2/");
662 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
663 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
669 /* end of test_transport_api.c */