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_ats.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, 90)
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 char * key_file_p1;
94 static char * cert_file_p1;
96 static char * key_file_p2;
97 static char * cert_file_p2;
100 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
102 #define OKPP do { ok++; } while (0)
110 GNUNET_assert (ok == 6);
111 GNUNET_SCHEDULER_cancel (die_task);
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
113 GNUNET_TRANSPORT_disconnect (p1.th);
114 GNUNET_TRANSPORT_disconnect (p2.th);
116 die_task = GNUNET_SCHEDULER_NO_TASK;
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
122 stop_arm (struct PeerContext *p)
125 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
126 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
127 GNUNET_OS_process_wait (p->arm_proc);
128 GNUNET_OS_process_close (p->arm_proc);
131 GNUNET_CONFIGURATION_destroy (p->cfg);
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
140 GNUNET_TRANSPORT_disconnect (p1.th);
141 GNUNET_TRANSPORT_disconnect (p2.th);
146 notify_receive (void *cls,
147 const struct GNUNET_PeerIdentity *peer,
148 const struct GNUNET_MessageHeader *message,
149 const struct GNUNET_TRANSPORT_ATS_Information *ats,
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
156 ntohs(message->type), cls);
158 GNUNET_assert (ok == 5);
161 GNUNET_assert (MTYPE == ntohs (message->type));
162 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
163 ntohs (message->size));
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
171 notify_ready (void *cls, size_t size, void *buf)
173 struct GNUNET_MessageHeader *hdr;
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
177 GNUNET_assert (size >= 256);
178 GNUNET_assert (ok == 4);
184 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
185 hdr->type = htons (MTYPE);
188 return sizeof (struct GNUNET_MessageHeader);
192 notify_ready_connect (void *cls, size_t size, void *buf)
199 notify_connect (void *cls,
200 const struct GNUNET_PeerIdentity *peer,
201 const struct GNUNET_TRANSPORT_ATS_Information *ats,
206 GNUNET_SCHEDULER_cancel (die_task);
207 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
210 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
212 256, 0, TIMEOUT, ¬ify_ready,
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
221 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Peer `%4s' disconnected (%p)!\n",
225 GNUNET_i2s (peer), cls);
230 setup_peer (struct PeerContext *p, const char *cfgname)
232 p->cfg = GNUNET_CONFIGURATION_create ();
234 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
235 "gnunet-service-arm",
239 "-c", cfgname, NULL);
241 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
248 if (GNUNET_CONFIGURATION_have_value (p->cfg,
249 "transport-https", "KEY_FILE"))
250 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p1);
251 if (key_file_p1==NULL)
252 GNUNET_asprintf(&key_file_p1,"https.key");
253 if (0 == stat (key_file_p1, &sbuf ))
255 if (0 == remove(key_file_p1))
256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p1);
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p1);
260 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
261 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p1);
262 if (cert_file_p1==NULL)
263 GNUNET_asprintf(&cert_file_p1,"https.cert");
264 if (0 == stat (cert_file_p1, &sbuf ))
266 if (0 == remove(cert_file_p1))
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p1);
274 if (GNUNET_CONFIGURATION_have_value (p->cfg,
275 "transport-https", "KEY_FILE"))
276 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "KEY_FILE", &key_file_p2);
277 if (key_file_p2==NULL)
278 GNUNET_asprintf(&key_file_p2,"https.key");
279 if (0 == stat (key_file_p2, &sbuf ))
281 if (0 == remove(key_file_p2))
282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing private key file `%s'\n",key_file_p2);
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove private key file `%s'\n",key_file_p2);
286 if (GNUNET_CONFIGURATION_have_value (p->cfg,"transport-https", "CERT_FILE"))
287 GNUNET_CONFIGURATION_get_value_string (p->cfg, "transport-https", "CERT_FILE", &cert_file_p2);
288 if (cert_file_p2==NULL)
289 GNUNET_asprintf(&cert_file_p2,"https.cert");
290 if (0 == stat (cert_file_p2, &sbuf ))
292 if (0 == remove(cert_file_p2))
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove existing certificate file `%s'\n",cert_file_p2);
300 p->th = GNUNET_TRANSPORT_connect (p->cfg,
303 ¬ify_connect, ¬ify_disconnect);
304 GNUNET_assert (p->th != NULL);
309 exchange_hello_last (void *cls,
310 const struct GNUNET_MessageHeader *message)
312 struct PeerContext *me = cls;
314 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Exchanging HELLO with peer (%p)!\n", cls);
317 GNUNET_assert (ok >= 3);
319 GNUNET_assert (message != NULL);
320 GNUNET_assert (GNUNET_OK ==
321 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
324 GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
326 sizeof (struct GNUNET_MessageHeader), 0,
328 ¬ify_ready_connect,
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
332 "Finished exchanging HELLOs, now waiting for transmission!\n");
336 exchange_hello (void *cls,
337 const struct GNUNET_MessageHeader *message)
339 struct PeerContext *me = cls;
341 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Exchanging HELLO with peer (%p)!\n", cls);
344 GNUNET_assert (ok >= 2);
346 GNUNET_assert (message != NULL);
347 GNUNET_assert (GNUNET_OK ==
348 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
354 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
355 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
362 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
364 GNUNET_assert (ok == 1);
366 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
369 setup_peer (&p1, "test_transport_ats_peer1.conf");
370 setup_peer (&p2, "test_transport_ats_peer2.conf");
372 GNUNET_assert(p1.th != NULL);
373 GNUNET_assert(p2.th != NULL);
375 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
381 static char *const argv[] = { "test-transport-api",
383 "test_transport_api_data.conf",
389 static struct GNUNET_GETOPT_CommandLineOption options[] = {
390 GNUNET_GETOPT_OPTION_END
394 setTransportOptions("test_transport_api_data.conf");
397 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
398 argv, "test-transport-api", "nohelp",
406 if (0 == stat (cert_file_p1, &sbuf ))
408 if (0 == remove(cert_file_p1))
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p1);
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p1);
414 if (0 == stat (key_file_p1, &sbuf ))
416 if (0 == remove(key_file_p1))
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p1);
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p1);
422 if (0 == stat (cert_file_p2, &sbuf ))
424 if (0 == remove(cert_file_p2))
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed existing certificate file `%s'\n",cert_file_p2);
427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to remove certfile `%s'\n",cert_file_p2);
430 if (0 == stat (key_file_p2, &sbuf ))
432 if (0 == remove(key_file_p2))
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully removed private key file `%s'\n",key_file_p2);
435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to private key file `%s'\n",key_file_p2);
437 GNUNET_free(key_file_p1);
438 GNUNET_free(key_file_p2);
439 GNUNET_free(cert_file_p1);
440 GNUNET_free(cert_file_p2);
446 * Return the actual path to a file found in the current
447 * PATH environment variable.
449 * @param binary the name of the file to find
452 get_path_from_PATH (char *binary)
463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
464 _("PATH environment variable is unset.\n"));
467 path = GNUNET_strdup (p); /* because we write on it */
468 buf = GNUNET_malloc (strlen (path) + 20);
471 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
474 sprintf (buf, "%s/%s", pos, binary);
475 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
482 sprintf (buf, "%s/%s", pos, binary);
483 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
494 * Check whether the suid bit is set on a file.
495 * Attempts to find the file using the current
496 * PATH environment variable as a search path.
498 * @param binary the name of the file to check
500 * @return GNUNET_YES if the binary is found and
501 * can be run properly, GNUNET_NO otherwise
504 check_gnunet_nat_binary(char *binary)
514 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
515 p = get_path_from_PATH (binaryexe);
518 p = get_path_from_PATH (binary);
522 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
523 _("Could not find binary `%s' in PATH!\n"),
527 if (0 != STAT (p, &statbuf))
529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
530 _("stat (%s) failed: %s\n"),
534 return GNUNET_SYSERR;
538 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
539 (statbuf.st_uid == 0) )
543 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
544 if (INVALID_SOCKET == rawsock)
546 DWORD err = GetLastError ();
547 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
548 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
549 return GNUNET_NO; /* not running as administrator */
551 closesocket (rawsock);
557 main (int argc, char *argv[])
561 return GNUNET_SYSERR;
564 GNUNET_log_setup ("test-transport-ats",
572 if (strstr(argv[0], "tcp_nat") != NULL)
574 is_tcp_nat = GNUNET_YES;
575 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
578 "`%s' not properly installed, cannot run NAT test!\n",
579 "gnunet-nat-server");
583 else if (strstr(argv[0], "tcp") != NULL)
587 else if (strstr(argv[0], "udp_nat") != NULL)
589 is_udp_nat = GNUNET_YES;
590 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
593 "`%s' not properly installed, cannot run NAT test!\n",
594 "gnunet-nat-server");
598 else if (strstr(argv[0], "udp") != NULL)
602 else if (strstr(argv[0], "unix") != NULL)
604 is_unix = GNUNET_YES;
606 else if (strstr(argv[0], "https") != NULL)
608 is_https = GNUNET_YES;
610 else if (strstr(argv[0], "http") != NULL)
612 is_http = GNUNET_YES;
614 else if (strstr(argv[0], "wlan") != NULL)
616 is_wlan = GNUNET_YES;
618 else if (strstr(argv[0], "multi") != NULL)
620 is_multi_protocol = GNUNET_YES;
624 if (is_multi_protocol)
626 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-1/");
627 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-2/");
631 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
632 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
640 /* end of test_transport_ats.c */