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 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 dv/test_transport_api_dv.c
22 * @brief base testcase for testing distance vector transport
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
28 #define VERBOSE GNUNET_NO
30 #define TEST_ALL GNUNET_NO
33 * How long until we fail the whole testcase?
35 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
38 * How long until we give up on starting the peers?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 500)
42 #define DEFAULT_NUM_PEERS 4
44 #define DEFAULT_ADDITIONAL_MESSAGES 2
46 #define MAX_OUTSTANDING_CONNECTIONS 100
48 static float fail_percentage = 0.00;
52 static unsigned long long num_additional_messages;
54 static unsigned long long num_peers;
56 static unsigned int total_connections;
58 static unsigned int failed_connections;
60 static unsigned int total_server_connections;
62 static unsigned int total_messages_received;
64 static unsigned int total_other_expected_messages;
66 static unsigned int temp_total_other_messages;
68 static unsigned int total_other_messages;
70 static unsigned int expected_messages;
72 static unsigned int expected_connections;
74 static unsigned long long peers_left;
76 static struct GNUNET_TESTING_PeerGroup *pg;
78 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
80 GNUNET_SCHEDULER_TaskIdentifier die_task;
82 static char *dotOutFileName = "topology.dot";
84 static FILE *dotOutFile;
86 static char *blacklist_transports;
88 static int transmit_ready_scheduled;
90 static int transmit_ready_failed;
92 static int transmit_ready_called;
94 static enum GNUNET_TESTING_Topology topology;
96 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
98 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
100 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
102 static double connect_topology_option_modifier = 0.0;
104 static char *test_directory;
106 struct GNUNET_CONTAINER_MultiHashMap *peer_daemon_hash;
110 struct GNUNET_TestMessage
113 * Header of the message
115 struct GNUNET_MessageHeader header;
118 * Unique identifier for this message.
125 /* This is a linked list */
126 struct PeerContext *next;
129 * Handle to the daemon
131 struct GNUNET_TESTING_Daemon *daemon;
133 /* Handle to the peer core */
134 struct GNUNET_CORE_Handle *peer_handle;
137 static struct PeerContext *all_peers;
139 struct TestMessageContext
141 /* This is a linked list */
142 struct TestMessageContext *next;
144 /* Handle to the sending peer core */
145 struct GNUNET_CORE_Handle *peer1handle;
147 /* Handle to the receiving peer core */
148 struct GNUNET_CORE_Handle *peer2handle;
150 /* Handle to the sending peer daemon */
151 struct GNUNET_TESTING_Daemon *peer1;
153 /* Handle to the receiving peer daemon */
154 struct GNUNET_TESTING_Daemon *peer2;
156 /* Identifier for this message, so we don't disconnect other peers! */
159 /* Task for disconnecting cores, allow task to be cancelled on shutdown */
160 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
163 static struct TestMessageContext *test_messages;
165 static struct TestMessageContext *other_test_messages;
168 * Check whether peers successfully shut down.
170 void shutdown_callback (void *cls,
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Shutdown of peers failed!\n");
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "All peers successfully shut down!\n");
194 GNUNET_assert (pg != NULL);
195 struct PeerContext *peer_pos;
196 struct PeerContext *free_peer_pos;
197 struct TestMessageContext *pos;
198 struct TestMessageContext *free_pos;
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Called finish testing, stopping daemons.\n");
204 peer_pos = all_peers;
205 while (peer_pos != NULL)
207 if (peer_pos->peer_handle != NULL)
208 GNUNET_CORE_disconnect(peer_pos->peer_handle);
209 free_peer_pos = peer_pos;
210 peer_pos = peer_pos->next;
211 GNUNET_free(free_peer_pos);
218 if (pos->peer1handle != NULL)
220 GNUNET_CORE_disconnect(pos->peer1handle);
221 pos->peer1handle = NULL;
223 if (pos->peer2handle != NULL)
225 GNUNET_CORE_disconnect(pos->peer2handle);
226 pos->peer2handle = NULL;
230 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
232 GNUNET_SCHEDULER_cancel(free_pos->disconnect_task);
234 GNUNET_free(free_pos);
237 pos = other_test_messages;
240 if (pos->peer1handle != NULL)
242 GNUNET_CORE_disconnect(pos->peer1handle);
243 pos->peer1handle = NULL;
245 if (pos->peer2handle != NULL)
247 GNUNET_CORE_disconnect(pos->peer2handle);
248 pos->peer2handle = NULL;
252 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel(free_pos->disconnect_task);
256 GNUNET_free(free_pos);
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
260 "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_failed, transmit_ready_called);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Calling daemons_stop\n");
267 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270 "daemons_stop finished\n");
272 if (dotOutFile != NULL)
274 fprintf(dotOutFile, "}");
283 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
285 struct TestMessageContext *pos = cls;
287 /* Disconnect from the respective cores */
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Disconnecting from peer 1 `%4s'\n", GNUNET_i2s (&pos->peer1->id));
292 if (pos->peer1handle != NULL)
293 GNUNET_CORE_disconnect(pos->peer1handle);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Disconnecting from peer 2 `%4s'\n", GNUNET_i2s (&pos->peer2->id));
298 if (pos->peer2handle != NULL)
299 GNUNET_CORE_disconnect(pos->peer2handle);
300 /* Set handles to NULL so test case can be ended properly */
301 pos->peer1handle = NULL;
302 pos->peer2handle = NULL;
303 pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
304 /* Decrement total connections so new can be established */
305 total_server_connections -= 2;
309 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
313 "End badly was called (%s)... stopping daemons.\n", msg);
314 struct TestMessageContext *pos;
315 struct TestMessageContext *free_pos;
316 struct PeerContext * peer_pos;
317 struct PeerContext * free_peer_pos;
319 peer_pos = all_peers;
320 while (peer_pos != NULL)
322 if (peer_pos->peer_handle != NULL)
323 GNUNET_CORE_disconnect(peer_pos->peer_handle);
324 free_peer_pos = peer_pos;
325 peer_pos = peer_pos->next;
326 GNUNET_free(free_peer_pos);
333 if (pos->peer1handle != NULL)
335 GNUNET_CORE_disconnect(pos->peer1handle);
336 pos->peer1handle = NULL;
338 if (pos->peer2handle != NULL)
340 GNUNET_CORE_disconnect(pos->peer2handle);
341 pos->peer2handle = NULL;
345 GNUNET_free(free_pos);
348 pos = other_test_messages;
351 if (pos->peer1handle != NULL)
353 GNUNET_CORE_disconnect(pos->peer1handle);
354 pos->peer1handle = NULL;
356 if (pos->peer2handle != NULL)
358 GNUNET_CORE_disconnect(pos->peer2handle);
359 pos->peer2handle = NULL;
363 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
365 GNUNET_SCHEDULER_cancel(free_pos->disconnect_task);
367 GNUNET_free(free_pos);
372 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
373 ok = 7331; /* Opposite of leet */
376 ok = 401; /* Never got peers started */
378 if (dotOutFile != NULL)
380 fprintf(dotOutFile, "}");
386 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
389 * Get distance information from 'atsi'.
391 * @param atsi performance data
392 * @return connected transport distance
395 get_atsi_distance (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
397 while ( (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
398 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE) )
400 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
403 /* FIXME: we do not have distance data? Assume direct neighbor. */
406 return ntohl (atsi->value);
410 process_mtype (void *cls,
411 const struct GNUNET_PeerIdentity *peer,
412 const struct GNUNET_MessageHeader *message,
413 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
415 struct TestMessageContext *pos = cls;
416 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
420 if (pos->uid != ntohl(msg->uid))
424 distance = get_atsi_distance(atsi);
426 GNUNET_assert(0 == memcmp(peer, &pos->peer1->id, sizeof(struct GNUNET_PeerIdentity)));
427 if (total_other_expected_messages == 0)
429 total_messages_received++;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Received message from `%4s', type %d, uid %u, distance %u.\n", GNUNET_i2s (peer), ntohs(message->type), ntohl(msg->uid), distance);
433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
434 "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
439 total_other_messages++;
441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
442 "Received message from `%4s', type %d, uid %u, distance %u.\n", GNUNET_i2s (peer), ntohs(message->type), ntohl(msg->uid), distance);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444 "Total messages received %d, expected %d.\n", total_other_messages, total_other_expected_messages);
448 if ((total_messages_received == expected_messages) && (total_other_messages == 0))
450 GNUNET_SCHEDULER_cancel (die_task);
451 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
452 &end_badly, "waiting for DV peers to connect!");
454 if ((num_peers == 3) && (total_other_expected_messages == 2))
456 GNUNET_SCHEDULER_add_now (&send_other_messages, NULL);
460 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20), &send_other_messages, NULL);
463 else if ((total_other_expected_messages > 0) && (total_other_messages == total_other_expected_messages))
465 GNUNET_SCHEDULER_cancel (die_task);
466 GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
470 pos->disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_cores, pos);
477 transmit_ready (void *cls, size_t size, void *buf)
479 struct GNUNET_TestMessage *m;
480 struct TestMessageContext *pos = cls;
482 GNUNET_assert (buf != NULL);
483 m = (struct GNUNET_TestMessage *) buf;
484 m->header.type = htons (MTYPE);
485 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
486 m->uid = htonl(pos->uid);
487 transmit_ready_called++;
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n", GNUNET_i2s(&pos->peer1->id), transmit_ready_scheduled, transmit_ready_called);
492 return sizeof (struct GNUNET_TestMessage);
496 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
500 static struct GNUNET_CORE_MessageHandler handlers[] = {
501 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
506 * Notify of all peer1's peers, once peer 2 is found, schedule connect
507 * to peer two for message send.
510 * @param peer peer identity this notification is about
511 * @param atsi performance data for the connection
513 static void connect_notify_peer2 (void *cls,
515 GNUNET_PeerIdentity *peer,
516 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
518 struct TestMessageContext *pos = cls;
520 if (0 == memcmp(&pos->peer1->id, peer, sizeof(struct GNUNET_PeerIdentity)))
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Core connection from `%s' to `%4s' verfied, sending message!\n",
525 GNUNET_i2s(&pos->peer2->id), GNUNET_h2s (&peer->hashPubKey));
527 if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
532 sizeof (struct GNUNET_TestMessage),
533 &transmit_ready, pos))
535 /* This probably shouldn't happen, but it does (timing issue?) */
536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
537 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
538 GNUNET_i2s (&pos->peer2->id));
539 transmit_ready_failed++;
540 total_other_expected_messages--;
544 transmit_ready_scheduled++;
550 init_notify_peer2 (void *cls,
551 struct GNUNET_CORE_Handle *server,
552 const struct GNUNET_PeerIdentity *my_identity,
553 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Core connection to `%4s' established, awaiting connections.\n",
558 GNUNET_i2s (my_identity));
560 total_server_connections++;
564 * Notify of all peer1's peers, once peer 2 is found, schedule connect
565 * to peer two for message send.
568 * @param peer peer identity this notification is about
569 * @param atsi performance data for the connection
571 static void connect_notify_peer1 (void *cls,
573 GNUNET_PeerIdentity *peer,
574 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
576 struct TestMessageContext *pos = cls;
578 if (0 == memcmp(&pos->peer2->id, peer, sizeof(struct GNUNET_PeerIdentity)))
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Core connection from `%s' to `%4s' verified.\n",
583 GNUNET_i2s(&pos->peer1->id), GNUNET_h2s (&peer->hashPubKey));
586 * Connect to the receiving peer
588 pos->peer2handle = GNUNET_CORE_connect (pos->peer2->cfg,
592 &connect_notify_peer2,
595 GNUNET_YES, NULL, GNUNET_YES, handlers);
600 init_notify_peer1 (void *cls,
601 struct GNUNET_CORE_Handle *server,
602 const struct GNUNET_PeerIdentity *my_identity,
603 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
605 total_server_connections++;
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
608 "Core connection to `%4s' established, awaiting connections...\n",
609 GNUNET_i2s (my_identity));
615 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
617 struct TestMessageContext *pos = cls;
619 if (( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) || (cls == NULL))
622 if (die_task == GNUNET_SCHEDULER_NO_TASK)
624 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
625 &end_badly, "from create topology (timeout)");
628 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
630 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
631 &send_test_messages, pos);
632 return; /* Otherwise we'll double schedule messages here! */
635 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Attempting to send test message from %s to %s\n", pos->peer1->shortname, pos->peer2->shortname);
638 * Connect to the sending peer
640 pos->peer1handle = GNUNET_CORE_connect (pos->peer1->cfg,
644 &connect_notify_peer1,
648 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
650 GNUNET_assert(pos->peer1handle != NULL);
652 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
654 GNUNET_SCHEDULER_add_now (&send_test_messages, pos->next);
658 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
659 &send_test_messages, pos->next);
664 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
666 struct TestMessageContext *pos;
667 struct TestMessageContext *free_pos;
668 struct PeerContext *peer_pos;
670 struct PeerContext *inner_peer_pos;
671 struct TestMessageContext *temp_context;
673 peer_pos = all_peers;
674 while (peer_pos != NULL)
676 if (peer_pos->peer_handle != NULL)
678 GNUNET_CORE_disconnect(peer_pos->peer_handle);
679 peer_pos->peer_handle = NULL;
682 inner_peer_pos = all_peers;
683 while (inner_peer_pos != NULL)
685 if (inner_peer_pos != peer_pos)
687 temp_total_other_messages++;
688 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
689 temp_context->peer1 = peer_pos->daemon;
690 temp_context->peer2 = inner_peer_pos->daemon;
691 temp_context->next = other_test_messages;
692 temp_context->uid = total_connections + temp_total_other_messages;
693 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
694 other_test_messages = temp_context;
696 inner_peer_pos = inner_peer_pos->next;
699 peer_pos = peer_pos->next;
706 if (pos->peer1handle != NULL)
708 GNUNET_CORE_disconnect(pos->peer1handle);
709 pos->peer1handle = NULL;
711 if (pos->peer2handle != NULL)
713 GNUNET_CORE_disconnect(pos->peer2handle);
714 pos->peer2handle = NULL;
718 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
720 GNUNET_SCHEDULER_cancel(free_pos->disconnect_task);
722 GNUNET_free(free_pos);
724 test_messages = NULL;
726 total_other_expected_messages = temp_total_other_messages;
727 if (total_other_expected_messages == 0)
729 GNUNET_SCHEDULER_add_now (&end_badly, "send_other_messages had 0 messages to send, no DV connections made!");
732 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Preparing to send %d other test messages\n", total_other_expected_messages);
735 GNUNET_SCHEDULER_add_now (&send_test_messages, other_test_messages);
736 GNUNET_SCHEDULER_cancel(die_task);
737 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 250), &end_badly, "from send_other_messages");
741 topology_callback (void *cls,
742 const struct GNUNET_PeerIdentity *first,
743 const struct GNUNET_PeerIdentity *second,
745 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
746 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
747 struct GNUNET_TESTING_Daemon *first_daemon,
748 struct GNUNET_TESTING_Daemon *second_daemon,
751 struct TestMessageContext *temp_context;
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
757 first_daemon->shortname,
758 second_daemon->shortname,
761 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
762 temp_context->peer1 = first_daemon;
763 temp_context->peer2 = second_daemon;
764 temp_context->next = test_messages;
765 temp_context->uid = total_connections;
766 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
767 test_messages = temp_context;
773 failed_connections++;
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
775 first_daemon->shortname,
776 second_daemon->shortname, emsg);
780 if (total_connections == expected_connections)
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Created %u total connections, which is our target number! Calling send messages.\n",
788 GNUNET_SCHEDULER_cancel (die_task);
789 die_task = GNUNET_SCHEDULER_NO_TASK;
790 GNUNET_SCHEDULER_add_now (&send_test_messages, test_messages);
792 else if (total_connections + failed_connections == expected_connections)
794 if (failed_connections < (unsigned int)(fail_percentage * total_connections))
796 GNUNET_SCHEDULER_cancel (die_task);
797 die_task = GNUNET_SCHEDULER_NO_TASK;
798 GNUNET_SCHEDULER_add_now (&send_test_messages, test_messages);
802 GNUNET_SCHEDULER_cancel (die_task);
803 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from topology_callback (too many failed connections)");
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
811 total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
819 expected_connections = -1;
820 if ((pg != NULL) && (peers_left == 0))
822 expected_connections = GNUNET_TESTING_connect_topology (pg,
824 connect_topology_option,
825 connect_topology_option_modifier,
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Have %d expected connections\n", expected_connections);
835 GNUNET_SCHEDULER_cancel (die_task);
836 if (expected_connections == GNUNET_SYSERR)
838 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from connect topology (bad return)");
841 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
842 &end_badly, "from connect topology (timeout)");
848 peers_left = num_peers; /* Reset counter */
849 if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Topology set up, now starting peers!\n");
855 GNUNET_TESTING_daemons_continue_startup(pg);
859 GNUNET_SCHEDULER_cancel (die_task);
860 die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from create topology (bad return)");
862 GNUNET_SCHEDULER_cancel (die_task);
863 die_task = GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT,
864 &end_badly, "from continue startup (timeout)");
868 * Method called whenever a given peer connects.
871 * @param peer peer identity this notification is about
872 * @param latency reported latency of the connection with 'other'
873 * @param distance reported distance (DV) to 'other'
875 static void all_connect_handler (void *cls,
877 GNUNET_PeerIdentity * peer,
878 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
880 struct GNUNET_TESTING_Daemon *d = cls;
881 struct GNUNET_TESTING_Daemon *second_daemon;
882 char *second_shortname = strdup(GNUNET_i2s(peer));
884 struct TestMessageContext *temp_context;
888 if (0 == memcmp(&d->id, peer, sizeof(struct GNUNET_PeerIdentity)))
891 distance = get_atsi_distance(atsi);
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
900 second_daemon = GNUNET_CONTAINER_multihashmap_get(peer_daemon_hash, &peer->hashPubKey);
902 if (second_daemon == NULL)
904 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't find second peer!\n");
905 GNUNET_free(second_shortname);
911 temp_total_other_messages++;
912 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
913 temp_context->peer1 = d;
914 temp_context->peer2 = second_daemon;
915 temp_context->next = other_test_messages;
916 temp_context->uid = total_connections + temp_total_other_messages;
917 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
918 other_test_messages = temp_context;
922 if (dotOutFile != NULL)
925 fprintf(dotOutFile, "\tn%s -- n%s;\n", d->shortname, second_shortname);
926 else if (distance == 2)
927 fprintf(dotOutFile, "\tn%s -- n%s [color=blue];\n", d->shortname, second_shortname);
928 else if (distance == 3)
929 fprintf(dotOutFile, "\tn%s -- n%s [color=red];\n", d->shortname, second_shortname);
930 else if (distance == 4)
931 fprintf(dotOutFile, "\tn%s -- n%s [color=green];\n", d->shortname, second_shortname);
933 fprintf(dotOutFile, "\tn%s -- n%s [color=brown];\n", d->shortname, second_shortname);
935 GNUNET_free(second_shortname);
937 if (temp_total_other_messages == num_additional_messages)
939 GNUNET_SCHEDULER_add_now (&send_other_messages, NULL);
944 peers_started_callback (void *cls,
945 const struct GNUNET_PeerIdentity *id,
946 const struct GNUNET_CONFIGURATION_Handle *cfg,
947 struct GNUNET_TESTING_Daemon *d, const char *emsg)
949 struct PeerContext *new_peer;
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
956 GNUNET_assert (id != NULL);
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
959 (num_peers - peers_left) + 1, num_peers);
961 GNUNET_assert(GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_put(peer_daemon_hash, &id->hashPubKey, d, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
963 new_peer = GNUNET_malloc(sizeof(struct PeerContext));
964 new_peer->peer_handle = GNUNET_CORE_connect(cfg,
967 &all_connect_handler,
969 GNUNET_NO, NULL, GNUNET_NO,
971 new_peer->daemon = d;
972 new_peer->next = all_peers;
973 all_peers = new_peer;
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "All %d daemons started, now creating topology!\n",
983 GNUNET_SCHEDULER_cancel (die_task);
984 /* Set up task in case topology creation doesn't finish
985 * within a reasonable amount of time */
986 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
987 (GNUNET_TIME_UNIT_MINUTES, 5),
988 &end_badly, "from peers_started_callback");
996 * Callback indicating that the hostkey was created for a peer.
999 * @param id the peer identity
1000 * @param d the daemon handle (pretty useless at this point, remove?)
1001 * @param emsg non-null on failure
1003 void hostkey_callback (void *cls,
1004 const struct GNUNET_PeerIdentity *id,
1005 struct GNUNET_TESTING_Daemon *d,
1010 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Hostkey created for peer `%s'\n",
1019 if (peers_left == 0)
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "All %d hostkeys created, now creating topology!\n",
1026 GNUNET_SCHEDULER_cancel (die_task);
1027 /* Set up task in case topology creation doesn't finish
1028 * within a reasonable amount of time */
1029 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1030 (GNUNET_TIME_UNIT_MINUTES, 5),
1031 &end_badly, "from hostkey_callback");
1032 GNUNET_SCHEDULER_add_now(&create_topology, NULL);
1040 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
1042 char * topology_str;
1043 char * connect_topology_str;
1044 char * blacklist_topology_str;
1045 char * connect_topology_option_str;
1046 char * connect_topology_option_modifier_string;
1049 dotOutFile = fopen (dotOutFileName, "w");
1050 if (dotOutFile != NULL)
1052 fprintf (dotOutFile, "strict graph G {\n");
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "Starting daemons based on config file %s\n", cfgfile);
1060 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
1067 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
1068 &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
1070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1071 "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
1072 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1076 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
1077 &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connection_topology, connect_topology_str)))
1079 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1080 "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1082 GNUNET_free_non_null(connect_topology_str);
1084 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
1085 &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1088 "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
1089 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1091 GNUNET_free_non_null(connect_topology_option_str);
1093 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
1094 &connect_topology_option_modifier_string))
1096 if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
1098 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1099 _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1100 connect_topology_option_modifier_string,
1101 "connect_topology_option_modifier",
1104 GNUNET_free (connect_topology_option_modifier_string);
1107 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
1108 &blacklist_transports))
1109 blacklist_transports = NULL;
1112 GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
1113 &blacklist_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
1115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1116 "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1118 GNUNET_free_non_null(topology_str);
1119 GNUNET_free_non_null(blacklist_topology_str);
1120 if (GNUNET_SYSERR ==
1121 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1123 num_peers = DEFAULT_NUM_PEERS;
1125 if (GNUNET_SYSERR ==
1126 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "additional_messages",
1127 &num_additional_messages))
1128 num_additional_messages = DEFAULT_ADDITIONAL_MESSAGES;
1132 GNUNET_assert(num_peers > 0 && num_peers < (unsigned int)-1);
1133 peers_left = num_peers;
1135 /* Set up a task to end testing if peer start fails */
1136 die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1137 (GNUNET_TIME_UNIT_MINUTES, 5),
1138 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
1140 peer_daemon_hash = GNUNET_CONTAINER_multihashmap_create(peers_left);
1141 pg = GNUNET_TESTING_daemons_start (cfg,
1142 peers_left, /* Total number of peers */
1143 peers_left, /* Number of outstanding connections */
1144 peers_left, /* Number of parallel ssh connections, or peers being started at once */
1148 &peers_started_callback,
1160 char *const argv[] = {"test-transport-dv",
1162 "test_transport_dv_data.conf",
1168 struct GNUNET_GETOPT_CommandLineOption options[] = {
1169 GNUNET_GETOPT_OPTION_END
1171 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
1172 argv, "test-transport-dv", "nohelp",
1173 options, &run, &ok);
1174 if (ret != GNUNET_OK)
1176 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-transport-dv': Failed with error code %d\n", ret);
1182 main (int argc, char *argv[])
1186 GNUNET_log_setup ("test-transport-dv",
1195 * Need to remove base directory, subdirectories taken care
1196 * of by the testing framework.
1198 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
1200 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
1205 /* end of test_transport_api_dv.c */