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, 10)
51 * How long until we give up on transmitting the message?
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
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 GNUNET_SCHEDULER_TaskIdentifier die_task;
78 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
80 #define OKPP do { ok++; } while (0)
88 GNUNET_assert (ok == 6);
89 GNUNET_SCHEDULER_cancel (die_task);
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
91 GNUNET_TRANSPORT_disconnect (p1.th);
92 GNUNET_TRANSPORT_disconnect (p2.th);
94 die_task = GNUNET_SCHEDULER_NO_TASK;
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
100 stop_arm (struct PeerContext *p)
103 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
104 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
105 GNUNET_OS_process_wait (p->arm_proc);
106 GNUNET_OS_process_close (p->arm_proc);
109 GNUNET_CONFIGURATION_destroy (p->cfg);
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
118 GNUNET_TRANSPORT_disconnect (p1.th);
119 GNUNET_TRANSPORT_disconnect (p2.th);
124 notify_receive (void *cls,
125 const struct GNUNET_PeerIdentity *peer,
126 const struct GNUNET_MessageHeader *message,
127 const struct GNUNET_TRANSPORT_ATS_Information *ats,
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok is (%d)!\n",
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p)!\n",
134 ntohs(message->type), cls);
136 GNUNET_assert (ok == 5);
139 GNUNET_assert (MTYPE == ntohs (message->type));
140 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
141 ntohs (message->size));
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message from peer (%p)!\n",
150 notify_ready (void *cls, size_t size, void *buf)
152 struct GNUNET_MessageHeader *hdr;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Transmitting message to peer (%p) - %u!\n", cls, sizeof (struct GNUNET_MessageHeader));
156 GNUNET_assert (size >= 256);
157 GNUNET_assert (ok == 4);
163 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
164 hdr->type = htons (MTYPE);
167 return sizeof (struct GNUNET_MessageHeader);
171 notify_ready_connect (void *cls, size_t size, void *buf)
178 notify_connect (void *cls,
179 const struct GNUNET_PeerIdentity *peer,
180 const struct GNUNET_TRANSPORT_ATS_Information *ats,
185 GNUNET_SCHEDULER_cancel (die_task);
186 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT_TRANSMIT,
189 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
191 256, 0, TIMEOUT, ¬ify_ready,
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
200 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "Peer `%4s' disconnected (%p)!\n",
204 GNUNET_i2s (peer), cls);
209 setup_peer (struct PeerContext *p, const char *cfgname)
211 p->cfg = GNUNET_CONFIGURATION_create ();
213 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
214 "gnunet-service-arm",
218 "-c", cfgname, NULL);
220 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
222 p->th = GNUNET_TRANSPORT_connect (p->cfg,
225 ¬ify_connect, ¬ify_disconnect);
226 GNUNET_assert (p->th != NULL);
231 exchange_hello_last (void *cls,
232 const struct GNUNET_MessageHeader *message)
234 struct PeerContext *me = cls;
236 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, me);
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Exchanging HELLO with peer (%p)!\n", cls);
239 GNUNET_assert (ok >= 3);
241 GNUNET_assert (message != NULL);
242 GNUNET_assert (GNUNET_OK ==
243 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
246 GNUNET_assert(NULL != GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
248 sizeof (struct GNUNET_MessageHeader), 0,
250 ¬ify_ready_connect,
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Finished exchanging HELLOs, now waiting for transmission!\n");
258 exchange_hello (void *cls,
259 const struct GNUNET_MessageHeader *message)
261 struct PeerContext *me = cls;
263 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Exchanging HELLO with peer (%p)!\n", cls);
266 GNUNET_assert (ok >= 2);
268 GNUNET_assert (message != NULL);
269 GNUNET_assert (GNUNET_OK ==
270 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
276 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
277 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
284 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
286 GNUNET_assert (ok == 1);
288 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
291 setup_peer (&p1, "test_transport_ats_peer1.conf");
292 setup_peer (&p2, "test_transport_ats_peer2.conf");
293 GNUNET_assert(p1.th != NULL);
294 GNUNET_assert(p2.th != NULL);
296 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
302 static char *const argv[] = { "test-transport-api",
304 "test_transport_api_data.conf",
310 static struct GNUNET_GETOPT_CommandLineOption options[] = {
311 GNUNET_GETOPT_OPTION_END
315 setTransportOptions("test_transport_api_data.conf");
318 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
319 argv, "test-transport-api", "nohelp",
329 main (int argc, char *argv[])
333 return GNUNET_SYSERR;
336 GNUNET_log_setup ("test-transport-api",
345 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
346 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
351 /* end of test_transport_api.c */