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_dv.c
22 * @brief base test case for dv transport (separated from other transport
23 * testcases for two reasons. 1) dv-service relies on core and other
24 * transport plugins, dv plugin relies on dv-service, so dv-plugin needs
25 * to live here, and 2) a dv plugin testcase is different from other
26 * tranport plugin testcases because we need at least three peer to test
29 * This test case tests DV functionality. Specifically it starts three
30 * peers connected in a line (1 <-> 2 <-> 3). Then a message is transmitted
31 * from peer 1 to peer 3. Assuming that DV is working, peer 2 should have
32 * gossiped about peer 3 to 1, and should then forward a message from one
36 #include "gnunet_common.h"
37 #include "gnunet_hello_lib.h"
38 #include "gnunet_getopt_lib.h"
39 #include "gnunet_os_lib.h"
40 #include "gnunet_program_lib.h"
41 #include "gnunet_scheduler_lib.h"
42 #include "gnunet_transport_service.h"
43 #include "../transport/transport.h"
45 #define VERBOSE GNUNET_YES
47 #define VERBOSE_ARM GNUNET_YES
49 #define START_ARM GNUNET_YES
52 * How long until we give up on transmitting the message?
54 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
60 struct GNUNET_CONFIGURATION_Handle *cfg;
61 struct GNUNET_TRANSPORT_Handle *th;
62 struct GNUNET_PeerIdentity id;
68 static struct PeerContext p1;
70 static struct PeerContext p2;
72 static struct PeerContext p3;
74 static struct GNUNET_SCHEDULER_Handle *sched;
78 GNUNET_SCHEDULER_TaskIdentifier die_task;
81 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
83 #define OKPP do { ok++; } while (0)
91 GNUNET_assert (ok == 6);
92 GNUNET_SCHEDULER_cancel (sched, die_task);
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transports!\n");
94 GNUNET_TRANSPORT_disconnect (p1.th);
95 GNUNET_TRANSPORT_disconnect (p2.th);
96 GNUNET_TRANSPORT_disconnect (p3.th);
98 die_task = GNUNET_SCHEDULER_NO_TASK;
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
104 stop_arm (struct PeerContext *p)
107 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
108 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
109 GNUNET_OS_process_wait (p->arm_pid);
111 GNUNET_CONFIGURATION_destroy (p->cfg);
120 fprintf(stderr, "Ending on an unhappy note.\n");
123 GNUNET_TRANSPORT_disconnect (p1.th);
124 GNUNET_TRANSPORT_disconnect (p2.th);
125 GNUNET_TRANSPORT_disconnect (p3.th);
132 notify_receive (void *cls,
133 const struct GNUNET_PeerIdentity *peer,
134 const struct GNUNET_MessageHeader *message,
135 struct GNUNET_TIME_Relative latency,
139 if (ntohs(message->type) != MTYPE)
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,
184 if ((cls == &p1) && (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Peer 1 notified about connection to peer 3!\n", GNUNET_i2s (peer), cls);
188 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
190 256, 0, TIMEOUT, ¬ify_ready,
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
199 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Peer `%4s' disconnected (%p)!\n",
203 GNUNET_i2s (peer), cls);
208 setup_peer (struct PeerContext *p, const char *cfgname)
210 p->cfg = GNUNET_CONFIGURATION_create ();
212 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
213 "gnunet-service-arm",
217 "-c", cfgname, NULL);
219 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
221 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
224 ¬ify_connect, ¬ify_disconnect);
225 GNUNET_assert (p->th != NULL);
230 exchange_hello_next (void *cls,
231 const struct GNUNET_MessageHeader *message)
233 struct PeerContext *me = cls;
235 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_next, me);
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "Exchanging HELLO with peer (%p)!\n", cls);
238 GNUNET_assert (ok >= 3);
240 GNUNET_assert (message != NULL);
241 GNUNET_assert (GNUNET_OK ==
242 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
248 GNUNET_TRANSPORT_offer_hello (p3.th, message);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Finished exchanging HELLOs, now waiting for transmission!\n");
257 exchange_hello (void *cls,
258 const struct GNUNET_MessageHeader *message)
260 struct PeerContext *me = cls;
262 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "Exchanging HELLO with peer (%p)!\n", cls);
265 GNUNET_assert (ok >= 2);
267 GNUNET_assert (message != NULL);
268 GNUNET_assert (GNUNET_OK ==
269 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
275 GNUNET_TRANSPORT_offer_hello (p2.th, message);
276 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_next, &p2);
281 struct GNUNET_SCHEDULER_Handle *s,
283 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
285 GNUNET_assert (ok == 1);
289 die_task = GNUNET_SCHEDULER_add_delayed (sched,
290 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
292 setup_peer (&p1, "test_transport_api_dv_peer1.conf");
293 setup_peer (&p2, "test_transport_api_dv_peer2.conf");
294 setup_peer (&p3, "test_transport_api_dv_peer3.conf");
296 GNUNET_assert(p1.th != NULL);
297 GNUNET_assert(p2.th != NULL);
298 GNUNET_assert(p3.th != NULL);
300 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
307 char *const argv[] = { "test-transport-api",
309 "test_transport_api_data.conf",
316 struct GNUNET_GETOPT_CommandLineOption options[] = {
317 GNUNET_GETOPT_OPTION_END
321 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
322 argv, "test-transport-api", "nohelp",
332 main (int argc, char *argv[])
336 return GNUNET_SYSERR;
339 GNUNET_log_setup ("test-transport-api-dv",
347 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
348 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
349 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-3");
353 /* end of test_transport_api_dv.c */