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_YES
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;
67 static struct PeerContext p1;
69 static struct PeerContext p2;
71 static struct GNUNET_SCHEDULER_Handle *sched;
77 static int is_tcp_nat;
81 static int is_udp_nat;
87 static GNUNET_SCHEDULER_TaskIdentifier die_task;
90 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
92 #define OKPP do { ok++; } while (0)
100 GNUNET_assert (ok == 6);
101 GNUNET_SCHEDULER_cancel (sched, die_task);
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
103 GNUNET_TRANSPORT_disconnect (p1.th);
104 GNUNET_TRANSPORT_disconnect (p2.th);
106 die_task = GNUNET_SCHEDULER_NO_TASK;
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
112 stop_arm (struct PeerContext *p)
115 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
116 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
117 GNUNET_OS_process_wait (p->arm_pid);
119 GNUNET_CONFIGURATION_destroy (p->cfg);
127 GNUNET_TRANSPORT_disconnect (p1.th);
128 GNUNET_TRANSPORT_disconnect (p2.th);
133 notify_receive (void *cls,
134 const struct GNUNET_PeerIdentity *peer,
135 const struct GNUNET_MessageHeader *message,
136 struct GNUNET_TIME_Relative latency,
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
143 ntohs(message->type), cls);
145 GNUNET_assert (ok == 5);
148 GNUNET_assert (MTYPE == ntohs (message->type));
149 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
150 ntohs (message->size));
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
158 notify_ready (void *cls, size_t size, void *buf)
160 struct GNUNET_MessageHeader *hdr;
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "Transmitting message to peer (%p) - %u!\n", cls, size);
164 GNUNET_assert (size >= 256);
165 GNUNET_assert (ok == 4);
170 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
171 hdr->type = htons (MTYPE);
174 return sizeof (struct GNUNET_MessageHeader);
179 notify_connect (void *cls,
180 const struct GNUNET_PeerIdentity *peer,
181 struct GNUNET_TIME_Relative latency,
186 GNUNET_SCHEDULER_cancel (sched, die_task);
187 die_task = GNUNET_SCHEDULER_add_delayed (sched,
191 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
193 256, 0, TIMEOUT, ¬ify_ready,
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
202 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Peer `%4s' disconnected (%p)!\n",
206 GNUNET_i2s (peer), cls);
211 setup_peer (struct PeerContext *p, const char *cfgname)
213 p->cfg = GNUNET_CONFIGURATION_create ();
215 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
216 "gnunet-service-arm",
220 "-c", cfgname, NULL);
222 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
224 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
227 ¬ify_connect, ¬ify_disconnect);
228 GNUNET_assert (p->th != NULL);
233 exchange_hello_last (void *cls,
234 const struct GNUNET_MessageHeader *message)
236 struct PeerContext *me = cls;
238 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240 "Exchanging HELLO with peer (%p)!\n", cls);
241 GNUNET_assert (ok >= 3);
243 GNUNET_assert (message != NULL);
244 GNUNET_assert (GNUNET_OK ==
245 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Finished exchanging HELLOs, now waiting for transmission!\n");
252 exchange_hello (void *cls,
253 const struct GNUNET_MessageHeader *message)
255 struct PeerContext *me = cls;
257 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Exchanging HELLO with peer (%p)!\n", cls);
260 GNUNET_assert (ok >= 2);
262 GNUNET_assert (message != NULL);
263 GNUNET_assert (GNUNET_OK ==
264 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
270 GNUNET_TRANSPORT_offer_hello (p2.th, message);
271 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
277 struct GNUNET_SCHEDULER_Handle *s,
279 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
281 GNUNET_assert (ok == 1);
284 die_task = GNUNET_SCHEDULER_add_delayed (sched,
290 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
291 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
295 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
296 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
300 setup_peer (&p1, "test_transport_api_tcp_nat_peer1.conf");
301 setup_peer (&p2, "test_transport_api_tcp_nat_peer2.conf");
305 setup_peer (&p1, "test_transport_api_udp_nat_peer1.conf");
306 setup_peer (&p2, "test_transport_api_udp_nat_peer2.conf");
310 setup_peer (&p1, "test_transport_api_http_peer1.conf");
311 setup_peer (&p2, "test_transport_api_http_peer2.conf");
315 setup_peer (&p1, "test_transport_api_https_peer1.conf");
316 setup_peer (&p2, "test_transport_api_https_peer2.conf");
318 GNUNET_assert(p1.th != NULL);
319 GNUNET_assert(p2.th != NULL);
321 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
327 static char *const argv[] = { "test-transport-api",
329 "test_transport_api_data.conf",
335 static struct GNUNET_GETOPT_CommandLineOption options[] = {
336 GNUNET_GETOPT_OPTION_END
340 setTransportOptions("test_transport_api_data.conf");
343 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
344 argv, "test-transport-api", "nohelp",
353 get_path_from_PATH ()
364 path = GNUNET_strdup (p); /* because we write on it */
365 buf = GNUNET_malloc (strlen (path) + 20);
368 while (NULL != (end = strchr (pos, ':')))
371 sprintf (buf, "%s/%s", pos, "gnunet-nat-server");
372 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
379 sprintf (buf, "%s/%s", pos, "gnunet-nat-server");
381 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
389 check_gnunet_nat_server()
394 p = get_path_from_PATH ();
397 if (0 != STAT (p, &statbuf))
400 return GNUNET_SYSERR;
403 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
404 (statbuf.st_uid == 0) )
410 main (int argc, char *argv[])
414 return GNUNET_SYSERR;
416 if (strstr(argv[0], "tcp_nat") != NULL)
418 is_tcp_nat = GNUNET_YES;
419 if (check_gnunet_nat_server() != GNUNET_OK)
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422 "`%s' not properly installed, cannot run NAT test!\n",
423 "gnunet-nat-server");
427 else if (strstr(argv[0], "tcp") != NULL)
431 else if (strstr(argv[0], "udp_nat") != NULL)
433 is_udp_nat = GNUNET_YES;
434 if (check_gnunet_nat_server() != GNUNET_OK)
436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
437 "`%s' not properly installed, cannot run NAT test!\n",
438 "gnunet-nat-server");
442 else if (strstr(argv[0], "udp") != NULL)
446 else if (strstr(argv[0], "https") != NULL)
448 is_https = GNUNET_YES;
449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"HTTPS!!!!");
451 else if (strstr(argv[0], "http") != NULL)
453 is_http = GNUNET_YES;
456 GNUNET_log_setup ("test-transport-api",
464 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
465 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
469 /* end of test_transport_api.c */