2 This file is part of GNUnet.
3 (C) 2009 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 2, 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 testcase for transport_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
34 #define VERBOSE GNUNET_NO
36 #define VERBOSE_ARM GNUNET_NO
38 #define START_ARM GNUNET_YES
40 #define VERBOSE_TRANSPORT GNUNET_NO
43 * How long until we give up on transmitting the message?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_TRANSPORT_Handle *th;
53 struct GNUNET_PeerIdentity id;
59 static struct PeerContext p1;
61 static struct PeerContext p2;
63 static struct GNUNET_SCHEDULER_Handle *sched;
71 GNUNET_SCHEDULER_TaskIdentifier die_task;
74 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
76 #define OKPP do { ok++; } while (0)
84 GNUNET_assert (ok == 8);
85 GNUNET_SCHEDULER_cancel (sched, die_task);
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
87 GNUNET_TRANSPORT_disconnect (p1.th);
88 GNUNET_TRANSPORT_disconnect (p2.th);
90 die_task = GNUNET_SCHEDULER_NO_TASK;
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
96 stop_arm (struct PeerContext *p)
99 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
100 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
101 GNUNET_OS_process_wait (p->arm_pid);
103 GNUNET_CONFIGURATION_destroy (p->cfg);
110 fprintf(stderr, "Ending on an unhappy note.\n");
112 GNUNET_TRANSPORT_disconnect (p1.th);
113 GNUNET_TRANSPORT_disconnect (p2.th);
120 notify_receive (void *cls,
121 const struct GNUNET_PeerIdentity *peer,
122 const struct GNUNET_MessageHeader *message,
123 struct GNUNET_TIME_Relative latency,
126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
130 ntohs(message->type), cls);
132 GNUNET_assert (ok == 7);
135 GNUNET_assert (MTYPE == ntohs (message->type));
136 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
137 ntohs (message->size));
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
145 notify_connect (void *cls,
146 const struct GNUNET_PeerIdentity *peer,
147 struct GNUNET_TIME_Relative latency,
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
152 GNUNET_assert ((ok >= 1) && (ok <= 6));
158 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
165 setup_peer (struct PeerContext *p, const char *cfgname)
167 p->cfg = GNUNET_CONFIGURATION_create ();
169 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
170 "gnunet-service-arm",
174 "-c", cfgname, NULL);
176 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
178 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
181 ¬ify_connect, ¬ify_disconnect);
182 GNUNET_assert (p->th != NULL);
187 notify_ready (void *cls, size_t size, void *buf)
189 struct GNUNET_MessageHeader *hdr;
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Transmitting message to peer (%p) - %u!\n", cls, size);
193 GNUNET_assert (size >= 256);
194 GNUNET_assert ((ok >= 5) && (ok <= 6));
199 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
200 hdr->type = htons (MTYPE);
203 return sizeof (struct GNUNET_MessageHeader);
208 exchange_hello_last (void *cls,
209 const struct GNUNET_MessageHeader *message)
211 struct PeerContext *me = cls;
213 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Exchanging HELLO with peer (%p)!\n", cls);
217 GNUNET_assert (ok >= 3);
219 GNUNET_assert (message != NULL);
220 GNUNET_assert (GNUNET_OK ==
221 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
224 GNUNET_TRANSPORT_offer_hello (p1.th, message);
226 /*sleep(1);*/ /* Make sure we are not falling prey to the "favorable timing" bug... */
228 /* both HELLOs exchanged, get ready to test transmission! */
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "Finished exchanging HELLOs, now waiting for transmission!\n");
232 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
234 256, 0, TIMEOUT, ¬ify_ready,
239 exchange_hello (void *cls,
240 const struct GNUNET_MessageHeader *message)
242 struct PeerContext *me = cls;
244 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Exchanging HELLO with peer (%p)!\n", cls);
247 GNUNET_assert (ok >= 2);
249 GNUNET_assert (message != NULL);
250 GNUNET_assert (GNUNET_OK ==
251 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
257 GNUNET_TRANSPORT_offer_hello (p2.th, message);
259 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
264 setTransportOptions(char * filename)
266 struct GNUNET_CONFIGURATION_Handle * tempcfg;
268 tempcfg = GNUNET_CONFIGURATION_create();
269 GNUNET_CONFIGURATION_load (tempcfg, filename);
271 unsigned long long curr_port;
272 GNUNET_CONFIGURATION_get_value_number(tempcfg, "transport", "port", &curr_port);
276 GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "udp");
277 GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-udp", "PORT", curr_port + 3);
281 GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "plugins", "tcp");
282 GNUNET_CONFIGURATION_set_value_number(tempcfg, "transport-tcp", "port", curr_port + 3);
285 #if VERBOSE_TRANSPORT
286 GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "YES");
287 GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "xterm -e xterm -T transport -e gdb --args");
289 GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "DEBUG", "NO");
290 GNUNET_CONFIGURATION_set_value_string(tempcfg, "transport", "PREFIX", "");
293 GNUNET_CONFIGURATION_write(tempcfg, filename);
295 GNUNET_CONFIGURATION_destroy(tempcfg);
302 struct GNUNET_SCHEDULER_Handle *s,
304 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
306 GNUNET_assert (ok == 1);
310 die_task = GNUNET_SCHEDULER_add_delayed (sched,
311 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
315 setup_peer (&p1, "test_transport_api_udp_peer1.conf");
316 setup_peer (&p2, "test_transport_api_udp_peer2.conf");
320 setup_peer (&p1, "test_transport_api_tcp_peer1.conf");
321 setup_peer (&p2, "test_transport_api_tcp_peer2.conf");
324 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
331 char *const argv[] = { "test-transport-api",
333 "test_transport_api_data.conf",
341 setTransportOptions("test_transport_api_data.conf");
344 struct GNUNET_GETOPT_CommandLineOption options[] = {
345 GNUNET_GETOPT_OPTION_END
349 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
350 argv, "test-transport-api", "nohelp",
358 main (int argc, char *argv[])
362 if (strstr(argv[0], "tcp") != NULL)
366 else if (strstr(argv[0], "udp") != NULL)
372 GNUNET_log_setup ("test-transport-api",
380 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
381 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
385 /* end of test_transport_api.c */