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;
64 const struct GNUNET_HELLO_Message *hello;
70 static struct PeerContext p1;
72 static struct PeerContext p2;
74 static struct PeerContext p3;
76 static struct GNUNET_SCHEDULER_Handle *sched;
80 GNUNET_SCHEDULER_TaskIdentifier die_task;
83 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
85 #define OKPP do { ok++; } while (0)
93 GNUNET_SCHEDULER_cancel (sched, die_task);
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 1!\n");
95 GNUNET_TRANSPORT_disconnect (p1.th);
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 2!\n");
97 GNUNET_TRANSPORT_disconnect (p2.th);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 3!\n");
99 GNUNET_TRANSPORT_disconnect (p3.th);
101 die_task = GNUNET_SCHEDULER_NO_TASK;
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
107 stop_arm (struct PeerContext *p)
110 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
115 "-c", p->cfg_file, "-e", NULL);
117 GNUNET_OS_process_wait (p->arm_pid);
119 GNUNET_CONFIGURATION_destroy (p->cfg);
124 restart_transport (struct PeerContext *p)
127 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
132 "-c", p->cfg_file, "-k", "transport", NULL);
134 fprintf(stderr, "stop arm command returned %d\n", p->arm_pid);
135 GNUNET_OS_process_wait (p->arm_pid);
139 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
144 "-c", p->cfg_file, "-i", "transport", NULL);
146 GNUNET_OS_process_wait (p->arm_pid);
156 fprintf(stderr, "Ending on an unhappy note.\n");
159 GNUNET_TRANSPORT_disconnect (p1.th);
160 GNUNET_TRANSPORT_disconnect (p2.th);
161 GNUNET_TRANSPORT_disconnect (p3.th);
168 notify_receive (void *cls,
169 const struct GNUNET_PeerIdentity *peer,
170 const struct GNUNET_MessageHeader *message,
171 struct GNUNET_TIME_Relative latency,
175 if (ntohs(message->type) != MTYPE)
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p) distance %d!\n",
179 ntohs(message->type), cls, distance);
181 GNUNET_assert (MTYPE == ntohs (message->type));
182 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
183 ntohs (message->size));
189 notify_ready (void *cls, size_t size, void *buf)
191 struct GNUNET_MessageHeader *hdr;
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "Transmitting message to peer (%p) - %u!\n", cls, size);
195 GNUNET_assert (size >= 256);
200 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
201 hdr->type = htons (MTYPE);
204 return sizeof (struct GNUNET_MessageHeader);
209 notify_connect (void *cls,
210 const struct GNUNET_PeerIdentity *peer,
211 struct GNUNET_TIME_Relative latency,
224 if (memcmp(peer, &p1.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
226 else if (memcmp(peer, &p2.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
228 else if (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
231 if ((cls == &p1) && (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235 "Peer 1 notified about connection to peer 3, distance %d!\n", GNUNET_i2s (peer), cls, distance);
237 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
239 256, 0, TIMEOUT, ¬ify_ready,
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Peer `%d' connected to peer `%d' distance %d!\n", peer_num, connect_num, distance);
248 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Peer `%4s' disconnected (%p)!\n",
252 GNUNET_i2s (peer), cls);
257 setup_peer (struct PeerContext *p, const char *cfgname)
259 p->cfg = GNUNET_CONFIGURATION_create ();
260 p->cfg_file = strdup(cfgname);
262 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
267 "-c", cfgname, "-s", NULL);
269 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
271 /*p->th = GNUNET_TRANSPORT_connect (sched, p->cfg,
274 ¬ify_connect, ¬ify_disconnect);*/
275 /*GNUNET_assert (p->th != NULL);*/
280 exchange_hello_last (void *cls,
281 const struct GNUNET_MessageHeader *message)
283 struct PeerContext *me = cls;
285 GNUNET_TRANSPORT_get_hello_cancel (p3.th, &exchange_hello_last, me);
287 GNUNET_assert (message != NULL);
288 GNUNET_assert (GNUNET_OK ==
289 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Finished exchanging HELLOs, now waiting for transmission!\n");
302 exchange_hello_next (void *cls,
303 const struct GNUNET_MessageHeader *message)
305 struct PeerContext *me = cls;
307 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_next, me);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
309 "Exchanging HELLO with peer (%p)!\n", cls);
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 (p3.th, message);
321 GNUNET_TRANSPORT_get_hello (p3.th, &exchange_hello_last, &p3);
328 exchange_hello (void *cls,
329 const struct GNUNET_MessageHeader *message)
331 struct PeerContext *me = cls;
333 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, me);
334 p2.th = GNUNET_TRANSPORT_connect (sched, p2.cfg,
337 ¬ify_connect, ¬ify_disconnect);
339 GNUNET_assert(p2.th != NULL);
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Exchanging HELLO with peer (%p)!\n", cls);
344 GNUNET_assert (message != NULL);
345 GNUNET_assert (GNUNET_OK ==
346 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
352 GNUNET_TRANSPORT_offer_hello (p2.th, message);
353 GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_next, &p2);
357 blacklist_setup_third (void *cls,
358 const struct GNUNET_MessageHeader *message)
360 struct PeerContext *me = cls;
361 char *blacklist_filename;
362 struct GNUNET_DISK_FileHandle *file;
363 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
367 GNUNET_TRANSPORT_get_hello_cancel (p3.th, &blacklist_setup_third, &p3);
369 GNUNET_assert (message != NULL);
370 GNUNET_assert (GNUNET_OK ==
371 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
374 GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-1/blacklist");
375 if (blacklist_filename != NULL)
377 file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
378 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
379 GNUNET_free(blacklist_filename);
383 GNUNET_SCHEDULER_cancel(sched, die_task);
384 GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
387 GNUNET_CRYPTO_hash_to_enc(&me->id.hashPubKey, &peer_enc);
388 size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
389 GNUNET_DISK_file_write(file, buf, size);
390 GNUNET_free_non_null(buf);
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", cls, p1.cfg_file);
396 restart_transport(&p1);
398 p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
401 ¬ify_connect, ¬ify_disconnect);
403 GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
407 blacklist_setup_first (void *cls,
408 const struct GNUNET_MessageHeader *message)
410 struct PeerContext *me = cls;
411 char *blacklist_filename;
412 struct GNUNET_DISK_FileHandle *file;
413 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
417 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &blacklist_setup_first, me);
420 GNUNET_assert (message != NULL);
421 GNUNET_assert (GNUNET_OK ==
422 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
425 GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-3/blacklist");
426 if (blacklist_filename != NULL)
428 file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
429 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
430 GNUNET_free(blacklist_filename);
434 GNUNET_SCHEDULER_cancel(sched, die_task);
435 GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
438 GNUNET_CRYPTO_hash_to_enc(&me->id.hashPubKey, &peer_enc);
439 size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
440 GNUNET_DISK_file_write(file, buf, size);
441 GNUNET_free_non_null(buf);
444 GNUNET_TRANSPORT_disconnect(p1.th);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", cls, p3.cfg_file);
448 restart_transport(&p3);
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "reconnecting to transport (%p)!\n", cls);
452 p3.th = GNUNET_TRANSPORT_connect (sched, p3.cfg,
455 ¬ify_connect, ¬ify_disconnect);
457 GNUNET_TRANSPORT_get_hello (p3.th, &blacklist_setup_third, &p3);
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "reconnecting to transport (%p) failed.!\n", cls);
461 //GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
467 struct GNUNET_SCHEDULER_Handle *s,
469 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
471 GNUNET_assert (ok == 1);
475 die_task = GNUNET_SCHEDULER_add_delayed (sched,
476 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &end_badly, NULL);
478 setup_peer (&p1, "test_transport_api_dv_peer1.conf");
479 setup_peer (&p2, "test_transport_api_dv_peer2.conf");
480 setup_peer (&p3, "test_transport_api_dv_peer3.conf");
482 p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
485 ¬ify_connect, ¬ify_disconnect);
486 GNUNET_assert(p1.th != NULL);
487 /*GNUNET_assert(p2.th != NULL);
488 GNUNET_assert(p3.th != NULL);*/
490 GNUNET_TRANSPORT_get_hello (p1.th, &blacklist_setup_first, &p1);
497 char *const argv[] = { "test-transport-api",
499 "test_transport_api_data.conf",
506 struct GNUNET_GETOPT_CommandLineOption options[] = {
507 GNUNET_GETOPT_OPTION_END
511 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
512 argv, "test-transport-api", "nohelp",
522 main (int argc, char *argv[])
526 return GNUNET_SYSERR;
529 GNUNET_log_setup ("test-transport-api-dv",
537 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
538 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
539 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-3");
543 /* end of test_transport_api_dv.c */