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_NO
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)
58 static int num_wanted = 2;
60 static int num_received = 0;
64 struct GNUNET_CONFIGURATION_Handle *cfg;
65 struct GNUNET_TRANSPORT_Handle *th;
66 struct GNUNET_PeerIdentity id;
68 struct GNUNET_HELLO_Message *hello;
74 static struct PeerContext p1;
76 static struct PeerContext p2;
78 static struct PeerContext p3;
80 static struct PeerContext p4;
82 static struct GNUNET_SCHEDULER_Handle *sched;
86 GNUNET_SCHEDULER_TaskIdentifier die_task;
89 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
91 #define OKPP do { ok++; } while (0)
99 GNUNET_SCHEDULER_cancel (sched, die_task);
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 1!\n");
104 GNUNET_TRANSPORT_disconnect (p1.th);
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 2!\n");
111 GNUNET_TRANSPORT_disconnect (p2.th);
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 3!\n");
118 GNUNET_TRANSPORT_disconnect (p3.th);
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from transport 4!\n");
125 GNUNET_TRANSPORT_disconnect (p4.th);
129 die_task = GNUNET_SCHEDULER_NO_TASK;
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transports disconnected, returning success!\n");
136 stop_arm (struct PeerContext *p)
139 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
144 "-c", p->cfg_file, "-e", "-q", NULL);
146 GNUNET_OS_process_wait (p->arm_pid);
148 GNUNET_CONFIGURATION_destroy (p->cfg);
153 restart_transport (struct PeerContext *p)
156 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
161 "-c", p->cfg_file, "-k", "transport", "-q", NULL);
163 GNUNET_OS_process_wait (p->arm_pid);
167 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
172 "-c", p->cfg_file, "-i", "transport", "-q", NULL);
174 GNUNET_OS_process_wait (p->arm_pid);
184 fprintf(stderr, "Ending on an unhappy note.\n");
189 GNUNET_TRANSPORT_disconnect (p1.th);
195 GNUNET_TRANSPORT_disconnect (p2.th);
201 GNUNET_TRANSPORT_disconnect (p3.th);
207 GNUNET_TRANSPORT_disconnect (p4.th);
216 notify_receive (void *cls,
217 const struct GNUNET_PeerIdentity *peer,
218 const struct GNUNET_MessageHeader *message,
219 struct GNUNET_TIME_Relative latency,
222 if (ntohs(message->type) != MTYPE)
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message of type %d from peer (%p) distance %d latency %u!\n",
226 ntohs(message->type), cls, distance, latency.value);
228 GNUNET_assert (MTYPE == ntohs (message->type));
229 GNUNET_assert (sizeof (struct GNUNET_MessageHeader) ==
230 ntohs (message->size));
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %d of %d messages.\n", num_received, num_wanted);
234 if (num_wanted == num_received)
242 notify_ready (void *cls, size_t size, void *buf)
244 struct GNUNET_MessageHeader *hdr;
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Transmitting message to peer (%p) - %u!\n", cls, size);
248 GNUNET_assert (size >= 256);
253 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
254 hdr->type = htons (MTYPE);
257 return sizeof (struct GNUNET_MessageHeader);
262 notify_connect (void *cls,
263 const struct GNUNET_PeerIdentity *peer,
264 struct GNUNET_TIME_Relative latency,
269 struct PeerContext *from_peer = cls;
281 if (memcmp(peer, &p1.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
283 else if (memcmp(peer, &p2.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
285 else if (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
287 else if (memcmp(peer, &p4.id, sizeof(struct GNUNET_PeerIdentity)) == 0)
292 if ((cls == &p1) && (memcmp(peer, &p3.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
294 GNUNET_TRANSPORT_notify_transmit_ready (p1.th,
296 256, 0, TIMEOUT, ¬ify_ready,
300 if ((cls == &p4) && (memcmp(peer, &p1.id, sizeof(struct GNUNET_PeerIdentity)) == 0))
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Peer 4 notified about connection to peer 1, distance %u!\n", distance);
306 GNUNET_TRANSPORT_notify_transmit_ready (p4.th,
308 256, 0, TIMEOUT, ¬ify_ready,
312 GNUNET_asprintf(&from_peer_str, "%s", GNUNET_i2s(&from_peer->id));
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Peer `%d' %4s connected to peer `%d' %4s distance %d!\n", peer_num, from_peer_str, connect_num, GNUNET_i2s(peer), distance);
319 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Peer `%4s' disconnected (%p)!\n",
323 GNUNET_i2s (peer), cls);
328 setup_peer (struct PeerContext *p, const char *cfgname)
330 p->cfg = GNUNET_CONFIGURATION_create ();
331 p->cfg_file = strdup(cfgname);
333 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
338 "-c", cfgname, "-s", "-q", NULL);
340 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
344 static void blacklist_peer(struct GNUNET_DISK_FileHandle *file, struct PeerContext *peer)
346 struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
350 GNUNET_CRYPTO_hash_to_enc(&peer->id.hashPubKey, &peer_enc);
351 size = GNUNET_asprintf(&buf, "%s:%s\n", "tcp", (char *)&peer_enc);
352 GNUNET_DISK_file_write(file, buf, size);
353 GNUNET_free_non_null(buf);
357 setup_blacklists (void *cls,
358 const struct GNUNET_SCHEDULER_TaskContext *tc)
360 char *blacklist_filename;
361 struct GNUNET_DISK_FileHandle *file;
364 for (i = 1; i <= 4; i++)
366 GNUNET_asprintf(&blacklist_filename, "/tmp/test-gnunetd-transport-peer-%d/blacklist", i);
367 if (blacklist_filename != NULL)
369 file = GNUNET_DISK_file_open(blacklist_filename, GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE | GNUNET_DISK_OPEN_CREATE,
370 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
371 GNUNET_free(blacklist_filename);
375 GNUNET_SCHEDULER_cancel(sched, die_task);
376 GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
382 blacklist_peer(file, &p3);
383 blacklist_peer(file, &p4);
386 blacklist_peer(file, &p4);
389 blacklist_peer(file, &p1);
392 blacklist_peer(file, &p1);
393 blacklist_peer(file, &p2);
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Disconnecting transports...\n");
404 GNUNET_TRANSPORT_disconnect (p1.th);
410 GNUNET_TRANSPORT_disconnect (p2.th);
416 GNUNET_TRANSPORT_disconnect (p3.th);
422 GNUNET_TRANSPORT_disconnect (p4.th);
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", p1.arm_pid, p1.cfg_file);
430 restart_transport(&p1);
432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", p2.arm_pid, p2.cfg_file);
434 restart_transport(&p2);
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", p3.arm_pid, p3.cfg_file);
438 restart_transport(&p3);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Restarting transport service (%p) with gnunet-arm -c %s -L DEBUG -k transport!\n", p4.arm_pid, p4.cfg_file);
442 restart_transport(&p4);
444 p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
447 ¬ify_connect, ¬ify_disconnect);
449 p2.th = GNUNET_TRANSPORT_connect (sched, p2.cfg,
452 ¬ify_connect, ¬ify_disconnect);
454 p3.th = GNUNET_TRANSPORT_connect (sched, p3.cfg,
457 ¬ify_connect, ¬ify_disconnect);
459 p4.th = GNUNET_TRANSPORT_connect (sched, p4.cfg,
462 ¬ify_connect, ¬ify_disconnect);
463 GNUNET_assert(p1.th != NULL);
464 GNUNET_assert(p2.th != NULL);
465 GNUNET_assert(p3.th != NULL);
466 GNUNET_assert(p4.th != NULL);
468 GNUNET_TRANSPORT_offer_hello (p1.th, GNUNET_HELLO_get_header(p2.hello));
469 GNUNET_TRANSPORT_offer_hello (p2.th, GNUNET_HELLO_get_header(p3.hello));
470 GNUNET_TRANSPORT_offer_hello (p3.th, GNUNET_HELLO_get_header(p4.hello));
476 get_hello_fourth (void *cls,
477 const struct GNUNET_MessageHeader *message)
479 struct PeerContext *me = cls;
481 GNUNET_TRANSPORT_get_hello_cancel (me->th, &get_hello_fourth, me);
483 GNUNET_assert (message != NULL);
484 GNUNET_assert (GNUNET_OK ==
485 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
491 me->hello = GNUNET_malloc(GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
492 memcpy(me->hello, message, GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "All HELLO's received, setting up blacklists!\n");
497 GNUNET_SCHEDULER_add_now(sched, &setup_blacklists, NULL);
502 get_hello_third (void *cls,
503 const struct GNUNET_MessageHeader *message)
505 struct PeerContext *me = cls;
507 GNUNET_TRANSPORT_get_hello_cancel (me->th, &get_hello_third, me);
509 GNUNET_assert (message != NULL);
510 GNUNET_assert (GNUNET_OK ==
511 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
517 me->hello = GNUNET_malloc(GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
518 memcpy(me->hello, message, GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
520 GNUNET_TRANSPORT_get_hello (p4.th, &get_hello_fourth, &p4);
525 get_hello_second (void *cls,
526 const struct GNUNET_MessageHeader *message)
528 struct PeerContext *me = cls;
530 GNUNET_TRANSPORT_get_hello_cancel (me->th, &get_hello_second, me);
532 GNUNET_assert (message != NULL);
533 GNUNET_assert (GNUNET_OK ==
534 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
540 me->hello = GNUNET_malloc(GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
541 memcpy(me->hello, message, GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
543 GNUNET_TRANSPORT_get_hello (p3.th, &get_hello_third, &p3);
548 get_hello_first (void *cls,
549 const struct GNUNET_MessageHeader *message)
551 struct PeerContext *me = cls;
553 GNUNET_TRANSPORT_get_hello_cancel (me->th, &get_hello_first, me);
555 GNUNET_assert (message != NULL);
556 GNUNET_assert (GNUNET_OK ==
557 GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Received HELLO size %d\n", GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
563 me->hello = GNUNET_malloc(GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
564 memcpy(me->hello, message, GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message));
566 GNUNET_TRANSPORT_get_hello (p2.th, &get_hello_second, &p2);
571 struct GNUNET_SCHEDULER_Handle *s,
573 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
575 GNUNET_assert (ok == 1);
579 die_task = GNUNET_SCHEDULER_add_delayed (sched,
580 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &end_badly, NULL);
582 setup_peer (&p1, "test_transport_api_dv_peer1.conf");
583 setup_peer (&p2, "test_transport_api_dv_peer2.conf");
584 setup_peer (&p3, "test_transport_api_dv_peer3.conf");
585 setup_peer (&p4, "test_transport_api_dv_peer4.conf");
587 p1.th = GNUNET_TRANSPORT_connect (sched, p1.cfg,
590 ¬ify_connect, ¬ify_disconnect);
592 p2.th = GNUNET_TRANSPORT_connect (sched, p2.cfg,
595 ¬ify_connect, ¬ify_disconnect);
597 p3.th = GNUNET_TRANSPORT_connect (sched, p3.cfg,
600 ¬ify_connect, ¬ify_disconnect);
602 p4.th = GNUNET_TRANSPORT_connect (sched, p4.cfg,
605 ¬ify_connect, ¬ify_disconnect);
606 GNUNET_assert(p1.th != NULL);
607 GNUNET_assert(p2.th != NULL);
608 GNUNET_assert(p3.th != NULL);
609 GNUNET_assert(p4.th != NULL);
611 GNUNET_TRANSPORT_get_hello (p1.th, &get_hello_first, &p1);
618 char *const argv[] = { "test-transport-api",
620 "test_transport_api_data.conf",
627 struct GNUNET_GETOPT_CommandLineOption options[] = {
628 GNUNET_GETOPT_OPTION_END
632 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
633 argv, "test-transport-api", "nohelp",
644 main (int argc, char *argv[])
648 return GNUNET_SYSERR;
651 GNUNET_log_setup ("test-transport-api-dv",
659 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-1");
660 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-2");
661 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-3");
662 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-transport-peer-4");
666 /* end of test_transport_api_dv.c */