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 "gnunet_statistics_service.h"
38 #include "transport.h"
41 #define VERBOSE GNUNET_NO
43 #define VERBOSE_ARM GNUNET_NO
45 #define START_ARM GNUNET_YES
48 * How long until we give up on transmitting the message?
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
53 * How long until we give up on transmitting the message?
55 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
61 struct GNUNET_CONFIGURATION_Handle *cfg;
62 struct GNUNET_TRANSPORT_Handle *th;
63 struct GNUNET_PeerIdentity id;
64 struct GNUNET_STATISTICS_Handle *stats;
66 struct GNUNET_OS_Process *arm_proc;
70 static struct PeerContext p1;
73 * Handle for reporting statistics.
78 static struct PeerContext p2;
84 static int is_tcp_nat;
90 static int is_udp_nat;
96 static int is_multi_protocol;
100 static GNUNET_SCHEDULER_TaskIdentifier die_task;
104 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
106 #define OKPP do { ok++; } while (0)
114 GNUNET_assert (ok == 6);
115 GNUNET_SCHEDULER_cancel (die_task);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
117 GNUNET_TRANSPORT_disconnect (p1.th);
118 GNUNET_TRANSPORT_disconnect (p2.th);
119 if (p1.stats != NULL)
120 GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
121 die_task = GNUNET_SCHEDULER_NO_TASK;
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
127 stop_arm (struct PeerContext *p)
130 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
131 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
132 GNUNET_OS_process_wait (p->arm_proc);
133 GNUNET_OS_process_close (p->arm_proc);
136 GNUNET_CONFIGURATION_destroy (p->cfg);
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
145 GNUNET_TRANSPORT_disconnect (p1.th);
146 if (p1.stats != NULL)
147 GNUNET_STATISTICS_destroy (p1.stats, GNUNET_NO);
148 //GNUNET_TRANSPORT_disconnect (p2.th);
153 notify_receive (void *cls,
154 const struct GNUNET_PeerIdentity *peer,
155 const struct GNUNET_MessageHeader *message,
156 const struct GNUNET_TRANSPORT_ATS_Information *ats,
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
163 ntohs(message->type), cls);
165 GNUNET_assert (ok == 5);
168 GNUNET_assert (MTYPE == ntohs (message->type));
169 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
170 ntohs (message->size));
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
178 notify_ready (void *cls, size_t size, void *buf)
180 struct GNUNET_MessageHeader *hdr;
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183 "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
184 GNUNET_assert (size >= 256);
185 GNUNET_assert (ok == 4);
191 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
192 hdr->type = htons (MTYPE);
195 return sizeof (struct GNUNET_MessageHeader);
199 notify_ready_connect (void *cls, size_t size, void *buf)
206 notify_connect (void *cls,
207 const struct GNUNET_PeerIdentity *peer,
208 const struct GNUNET_TRANSPORT_ATS_Information *ats,
214 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
216 256, 0, TIMEOUT, ¬ify_ready,
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
225 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Peer `%4s' disconnected (%p)!\n",
229 GNUNET_i2s (peer), cls);
232 int stat_cb (void *cls,
233 const char *subsystem,
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Execution time for 100 Peers: %llu\n",value);
245 setup_peer (struct PeerContext *p, const char *cfgname)
247 p->cfg = GNUNET_CONFIGURATION_create ();
249 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
250 "gnunet-service-arm",
254 "-c", cfgname, NULL);
256 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
257 p->stats = GNUNET_STATISTICS_create ("transport", p->cfg);
258 GNUNET_assert (p->stats != NULL);
259 GNUNET_assert (GNUNET_OK == GNUNET_STATISTICS_watch (p->stats,"transport","ATS execution time 100 peers", stat_cb, NULL));
261 p->th = GNUNET_TRANSPORT_connect (p->cfg,
264 ¬ify_connect, ¬ify_disconnect);
265 GNUNET_assert (p->th != NULL);
267 GNUNET_SCHEDULER_cancel (die_task);
268 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
274 exchange_hello_last (void *cls,
275 const struct GNUNET_MessageHeader *message)
277 struct PeerContext *me = cls;
279 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Exchanging HELLO with peer (%p)!\n", cls);
282 GNUNET_assert (ok >= 3);
284 GNUNET_assert (message != NULL);
285 GNUNET_assert (GNUNET_OK ==
286 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
289 GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
291 sizeof (struct GNUNET_MessageHeader), 0,
293 ¬ify_ready_connect,
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Finished exchanging HELLOs, now waiting for transmission!\n");
301 exchange_hello (void *cls,
302 const struct GNUNET_MessageHeader *message)
304 struct PeerContext *me = cls;
306 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Exchanging HELLO with peer (%p)!\n", cls);
309 GNUNET_assert (ok >= 2);
311 GNUNET_assert (message != NULL);
312 GNUNET_assert (GNUNET_OK ==
313 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
319 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
320 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
327 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
329 GNUNET_assert (ok == 1);
331 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
334 setup_peer (&p1, "test_transport_ats_peer1.conf");
335 //setup_peer (&p2, "test_transport_ats_peer2.conf");
337 GNUNET_assert(p1.th != NULL);
338 //GNUNET_assert(p2.th != NULL);
340 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
346 static char *const argv[] = { "test-transport-api",
348 "test_transport_api_data.conf",
354 static struct GNUNET_GETOPT_CommandLineOption options[] = {
355 GNUNET_GETOPT_OPTION_END
359 setTransportOptions("test_transport_api_data.conf");
362 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
363 argv, "test-transport-api", "nohelp",
372 * Return the actual path to a file found in the current
373 * PATH environment variable.
375 * @param binary the name of the file to find
378 get_path_from_PATH (char *binary)
389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390 _("PATH environment variable is unset.\n"));
393 path = GNUNET_strdup (p); /* because we write on it */
394 buf = GNUNET_malloc (strlen (path) + 20);
397 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
400 sprintf (buf, "%s/%s", pos, binary);
401 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
408 sprintf (buf, "%s/%s", pos, binary);
409 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
420 * Check whether the suid bit is set on a file.
421 * Attempts to find the file using the current
422 * PATH environment variable as a search path.
424 * @param binary the name of the file to check
426 * @return GNUNET_YES if the binary is found and
427 * can be run properly, GNUNET_NO otherwise
430 check_gnunet_nat_binary(char *binary)
440 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
441 p = get_path_from_PATH (binaryexe);
444 p = get_path_from_PATH (binary);
448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
449 _("Could not find binary `%s' in PATH!\n"),
453 if (0 != STAT (p, &statbuf))
455 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
456 _("stat (%s) failed: %s\n"),
460 return GNUNET_SYSERR;
464 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
465 (statbuf.st_uid == 0) )
469 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
470 if (INVALID_SOCKET == rawsock)
472 DWORD err = GetLastError ();
473 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
474 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
475 return GNUNET_NO; /* not running as administrator */
477 closesocket (rawsock);
483 main (int argc, char *argv[])
487 return GNUNET_SYSERR;
490 GNUNET_log_setup ("test-transport-ats",
498 if (strstr(argv[0], "tcp_nat") != NULL)
500 is_tcp_nat = GNUNET_YES;
501 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504 "`%s' not properly installed, cannot run NAT test!\n",
505 "gnunet-nat-server");
509 else if (strstr(argv[0], "tcp") != NULL)
513 else if (strstr(argv[0], "udp_nat") != NULL)
515 is_udp_nat = GNUNET_YES;
516 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
519 "`%s' not properly installed, cannot run NAT test!\n",
520 "gnunet-nat-server");
524 else if (strstr(argv[0], "udp") != NULL)
528 else if (strstr(argv[0], "unix") != NULL)
530 is_unix = GNUNET_YES;
532 else if (strstr(argv[0], "https") != NULL)
534 is_https = GNUNET_YES;
536 else if (strstr(argv[0], "http") != NULL)
538 is_http = GNUNET_YES;
540 else if (strstr(argv[0], "wlan") != NULL)
542 is_wlan = GNUNET_YES;
544 else if (strstr(argv[0], "multi") != NULL)
546 is_multi_protocol = GNUNET_YES;
550 if (is_multi_protocol)
552 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-1/");
553 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-multi-peer-2/");
557 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
558 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
566 /* end of test_transport_ats.c */