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_core_service.h"
27 #define TEST_ALL GNUNET_NO
30 * How long until we fail the whole testcase?
32 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
35 * How long until we give up on starting the peers?
37 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 500)
39 #define DEFAULT_NUM_PEERS 4
41 #define DEFAULT_ADDITIONAL_MESSAGES 2
43 #define MAX_OUTSTANDING_CONNECTIONS 100
45 static float fail_percentage = 0.00;
49 static unsigned long long num_additional_messages;
51 static unsigned long long num_peers;
53 static unsigned int total_connections;
55 static unsigned int failed_connections;
57 static unsigned int total_server_connections;
59 static unsigned int total_messages_received;
61 static unsigned int total_other_expected_messages;
63 static unsigned int temp_total_other_messages;
65 static unsigned int total_other_messages;
67 static unsigned int expected_messages;
69 static unsigned int expected_connections;
71 static unsigned long long peers_left;
73 static struct GNUNET_TESTING_PeerGroup *pg;
75 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
77 static GNUNET_SCHEDULER_TaskIdentifier die_task;
79 static char *dotOutFileName = "topology.dot";
81 static FILE *dotOutFile;
83 static char *blacklist_transports;
85 static int transmit_ready_scheduled;
87 static int transmit_ready_failed;
89 static int transmit_ready_called;
91 static enum GNUNET_TESTING_Topology topology;
93 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
95 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
97 static enum GNUNET_TESTING_TopologyOption connect_topology_option =
98 GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
100 static double connect_topology_option_modifier = 0.0;
102 static char *test_directory;
104 struct GNUNET_CONTAINER_MultiHashMap *peer_daemon_hash;
108 GNUNET_NETWORK_STRUCT_BEGIN
110 struct GNUNET_TestMessage
113 * Header of the message
115 struct GNUNET_MessageHeader header;
118 * Unique identifier for this message.
122 GNUNET_NETWORK_STRUCT_END
126 /* This is a linked list */
127 struct PeerContext *next;
130 * Handle to the daemon
132 struct GNUNET_TESTING_Daemon *daemon;
134 /* Handle to the peer core */
135 struct GNUNET_CORE_Handle *peer_handle;
138 static struct PeerContext *all_peers;
140 struct TestMessageContext
142 /* This is a linked list */
143 struct TestMessageContext *next;
145 /* Handle to the sending peer core */
146 struct GNUNET_CORE_Handle *peer1handle;
148 /* Handle to the receiving peer core */
149 struct GNUNET_CORE_Handle *peer2handle;
151 /* Handle to the sending peer daemon */
152 struct GNUNET_TESTING_Daemon *peer1;
154 /* Handle to the receiving peer daemon */
155 struct GNUNET_TESTING_Daemon *peer2;
157 /* Identifier for this message, so we don't disconnect other peers! */
160 /* Task for disconnecting cores, allow task to be cancelled on shutdown */
161 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
164 static struct TestMessageContext *test_messages;
166 static struct TestMessageContext *other_test_messages;
169 * Check whether peers successfully shut down.
172 shutdown_callback (void *cls, const char *emsg)
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
187 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 GNUNET_assert (pg != NULL);
190 struct PeerContext *peer_pos;
191 struct PeerContext *free_peer_pos;
192 struct TestMessageContext *pos;
193 struct TestMessageContext *free_pos;
195 die_task = GNUNET_SCHEDULER_NO_TASK;
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Called finish testing, stopping daemons.\n");
198 peer_pos = all_peers;
199 while (peer_pos != NULL)
201 if (peer_pos->peer_handle != NULL)
202 GNUNET_CORE_disconnect (peer_pos->peer_handle);
203 free_peer_pos = peer_pos;
204 peer_pos = peer_pos->next;
205 GNUNET_free (free_peer_pos);
212 if (pos->peer1handle != NULL)
214 GNUNET_CORE_disconnect (pos->peer1handle);
215 pos->peer1handle = NULL;
217 if (pos->peer2handle != NULL)
219 GNUNET_CORE_disconnect (pos->peer2handle);
220 pos->peer2handle = NULL;
224 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
226 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
228 GNUNET_free (free_pos);
231 pos = other_test_messages;
234 if (pos->peer1handle != NULL)
236 GNUNET_CORE_disconnect (pos->peer1handle);
237 pos->peer1handle = NULL;
239 if (pos->peer2handle != NULL)
241 GNUNET_CORE_disconnect (pos->peer2handle);
242 pos->peer2handle = NULL;
246 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
248 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
250 GNUNET_free (free_pos);
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n",
254 transmit_ready_scheduled, transmit_ready_failed,
255 transmit_ready_called);
256 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
257 if (dotOutFile != NULL)
259 FPRINTF (dotOutFile, "%s", "}");
268 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270 struct TestMessageContext *pos = cls;
272 /* Disconnect from the respective cores */
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from peer 1 `%4s'\n",
274 GNUNET_i2s (&pos->peer1->id));
275 if (pos->peer1handle != NULL)
276 GNUNET_CORE_disconnect (pos->peer1handle);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from peer 2 `%4s'\n",
278 GNUNET_i2s (&pos->peer2->id));
279 if (pos->peer2handle != NULL)
280 GNUNET_CORE_disconnect (pos->peer2handle);
281 /* Set handles to NULL so test case can be ended properly */
282 pos->peer1handle = NULL;
283 pos->peer2handle = NULL;
284 pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
285 /* Decrement total connections so new can be established */
286 total_server_connections -= 2;
290 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
293 struct TestMessageContext *pos;
294 struct TestMessageContext *free_pos;
295 struct PeerContext *peer_pos;
296 struct PeerContext *free_peer_pos;
298 die_task = GNUNET_SCHEDULER_NO_TASK;
299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
300 "End badly was called (%s)... stopping daemons.\n", msg);
302 peer_pos = all_peers;
303 while (peer_pos != NULL)
305 if (peer_pos->peer_handle != NULL)
306 GNUNET_CORE_disconnect (peer_pos->peer_handle);
307 free_peer_pos = peer_pos;
308 peer_pos = peer_pos->next;
309 GNUNET_free (free_peer_pos);
316 if (pos->peer1handle != NULL)
318 GNUNET_CORE_disconnect (pos->peer1handle);
319 pos->peer1handle = NULL;
321 if (pos->peer2handle != NULL)
323 GNUNET_CORE_disconnect (pos->peer2handle);
324 pos->peer2handle = NULL;
328 GNUNET_free (free_pos);
331 pos = other_test_messages;
334 if (pos->peer1handle != NULL)
336 GNUNET_CORE_disconnect (pos->peer1handle);
337 pos->peer1handle = NULL;
339 if (pos->peer2handle != NULL)
341 GNUNET_CORE_disconnect (pos->peer2handle);
342 pos->peer2handle = NULL;
346 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
348 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
350 GNUNET_free (free_pos);
355 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
356 ok = 7331; /* Opposite of leet */
359 ok = 401; /* Never got peers started */
361 if (dotOutFile != NULL)
363 FPRINTF (dotOutFile, "%s", "}");
369 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
372 * Get distance information from 'atsi'.
374 * @param atsi performance data
375 * @return connected transport distance
378 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
379 unsigned int atsi_count)
383 for (i = 0; i < atsi_count; i++)
385 if (ntohl (atsi->type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
386 return ntohl (atsi->value);
390 /* FIXME: we do not have distance data? Assume direct neighbor. */
396 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
397 const struct GNUNET_MessageHeader *message,
398 const struct GNUNET_ATS_Information *atsi,
399 unsigned int atsi_count)
401 struct TestMessageContext *pos = cls;
402 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *) message;
405 if (pos->uid != ntohl (msg->uid))
407 distance = get_atsi_distance (atsi, atsi_count);
409 memcmp (peer, &pos->peer1->id,
410 sizeof (struct GNUNET_PeerIdentity)));
411 if (total_other_expected_messages == 0)
413 total_messages_received++;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415 "Received message from `%4s', type %d, uid %u, distance %u.\n",
416 GNUNET_i2s (peer), ntohs (message->type), ntohl (msg->uid),
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Total messages received %d, expected %d.\n",
420 total_messages_received, expected_messages);
424 total_other_messages++;
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Received message from `%4s', type %d, uid %u, distance %u.\n",
427 GNUNET_i2s (peer), ntohs (message->type), ntohl (msg->uid),
429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430 "Total OTHER messages received %d, expected %d.\n",
431 total_other_messages, total_other_expected_messages);
434 if ((total_messages_received == expected_messages) &&
435 (total_other_messages == 0))
437 GNUNET_SCHEDULER_cancel (die_task);
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Scheduling timeout from DV connections.\n");
441 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
442 "waiting for DV peers to connect!");
444 else if ((total_other_expected_messages > 0) &&
445 (total_other_messages == total_other_expected_messages))
447 GNUNET_SCHEDULER_cancel (die_task);
448 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
452 pos->disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cores, pos);
459 transmit_ready (void *cls, size_t size, void *buf)
461 struct GNUNET_TestMessage *m;
462 struct TestMessageContext *pos = cls;
464 GNUNET_assert (buf != NULL);
465 m = (struct GNUNET_TestMessage *) buf;
466 m->header.type = htons (MTYPE);
467 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
468 m->uid = htonl (pos->uid);
469 transmit_ready_called++;
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n",
472 GNUNET_i2s (&pos->peer1->id), transmit_ready_scheduled,
473 transmit_ready_called);
474 return sizeof (struct GNUNET_TestMessage);
478 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
482 static struct GNUNET_CORE_MessageHandler handlers[] = {
483 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
489 * Notify of all peer1's peers, once peer 2 is found, schedule connect
490 * to peer two for message send.
493 * @param peer peer identity this notification is about
494 * @param atsi performance data for the connection
495 * @param atsi_count number of ATS information included
498 connect_notify_peer2 (void *cls, const struct GNUNET_PeerIdentity *peer,
499 const struct GNUNET_ATS_Information *atsi,
500 unsigned int atsi_count)
502 struct TestMessageContext *pos = cls;
504 if (0 == memcmp (&pos->peer1->id, peer, sizeof (struct GNUNET_PeerIdentity)))
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Core connection from `%s' to `%4s' verfied, sending message!\n",
508 GNUNET_i2s (&pos->peer2->id), GNUNET_h2s (&peer->hashPubKey));
510 GNUNET_CORE_notify_transmit_ready (pos->peer1handle, GNUNET_YES, 0,
511 TIMEOUT, &pos->peer2->id,
512 sizeof (struct GNUNET_TestMessage),
513 &transmit_ready, pos))
515 /* This probably shouldn't happen, but it does (timing issue?) */
516 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
517 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
518 GNUNET_i2s (&pos->peer2->id));
519 transmit_ready_failed++;
520 total_other_expected_messages--;
524 transmit_ready_scheduled++;
531 init_notify_peer2 (void *cls, struct GNUNET_CORE_Handle *server,
532 const struct GNUNET_PeerIdentity *my_identity)
534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
535 "Core connection to `%4s' established, awaiting connections.\n",
536 GNUNET_i2s (my_identity));
537 total_server_connections++;
542 * Notify of all peer1's peers, once peer 2 is found, schedule connect
543 * to peer two for message send.
546 * @param peer peer identity this notification is about
547 * @param atsi performance data for the connection
548 * @param atsi_count number of atsi datums
551 connect_notify_peer1 (void *cls, const struct GNUNET_PeerIdentity *peer,
552 const struct GNUNET_ATS_Information *atsi,
553 unsigned int atsi_count)
555 struct TestMessageContext *pos = cls;
557 if (0 == memcmp (&pos->peer2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Core connection from `%s' to `%4s' verified.\n",
561 GNUNET_i2s (&pos->peer1->id), GNUNET_h2s (&peer->hashPubKey));
563 * Connect to the receiving peer
566 GNUNET_CORE_connect (pos->peer2->cfg, pos, &init_notify_peer2,
567 &connect_notify_peer2, NULL, NULL, GNUNET_YES,
568 NULL, GNUNET_YES, handlers);
574 init_notify_peer1 (void *cls, struct GNUNET_CORE_Handle *server,
575 const struct GNUNET_PeerIdentity *my_identity)
577 total_server_connections++;
578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
579 "Core connection to `%4s' established, awaiting connections...\n",
580 GNUNET_i2s (my_identity));
585 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
587 struct TestMessageContext *pos = cls;
589 if (((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) || (cls == NULL))
592 if (die_task == GNUNET_SCHEDULER_NO_TASK)
595 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
596 "from create topology (timeout)");
599 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
601 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
602 (GNUNET_TIME_UNIT_SECONDS, 1),
603 &send_test_messages, pos);
604 return; /* Otherwise we'll double schedule messages here! */
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Attempting to send test message from %s to %s\n",
608 pos->peer1->shortname, pos->peer2->shortname);
610 * Connect to the sending peer
613 GNUNET_CORE_connect (pos->peer1->cfg, pos, &init_notify_peer1,
614 &connect_notify_peer1, NULL, NULL, GNUNET_NO, NULL,
615 GNUNET_NO, no_handlers);
617 GNUNET_assert (pos->peer1handle != NULL);
619 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
621 GNUNET_SCHEDULER_add_now (&send_test_messages, pos->next);
625 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
626 (GNUNET_TIME_UNIT_SECONDS, 1),
627 &send_test_messages, pos->next);
633 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
635 struct TestMessageContext *pos;
636 struct TestMessageContext *free_pos;
637 struct PeerContext *peer_pos;
640 struct PeerContext *inner_peer_pos;
641 struct TestMessageContext *temp_context;
643 peer_pos = all_peers;
644 while (peer_pos != NULL)
646 if (peer_pos->peer_handle != NULL)
648 GNUNET_CORE_disconnect (peer_pos->peer_handle);
649 peer_pos->peer_handle = NULL;
652 inner_peer_pos = all_peers;
653 while (inner_peer_pos != NULL)
655 if (inner_peer_pos != peer_pos)
657 temp_total_other_messages++;
658 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
659 temp_context->peer1 = peer_pos->daemon;
660 temp_context->peer2 = inner_peer_pos->daemon;
661 temp_context->next = other_test_messages;
662 temp_context->uid = total_connections + temp_total_other_messages;
663 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
664 other_test_messages = temp_context;
666 inner_peer_pos = inner_peer_pos->next;
669 peer_pos = peer_pos->next;
676 if (pos->peer1handle != NULL)
678 GNUNET_CORE_disconnect (pos->peer1handle);
679 pos->peer1handle = NULL;
681 if (pos->peer2handle != NULL)
683 GNUNET_CORE_disconnect (pos->peer2handle);
684 pos->peer2handle = NULL;
688 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
690 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
692 GNUNET_free (free_pos);
694 test_messages = NULL;
696 total_other_expected_messages = temp_total_other_messages;
697 if (total_other_expected_messages == 0)
699 GNUNET_SCHEDULER_add_now (&end_badly,
700 "send_other_messages had 0 messages to send, no DV connections made!");
702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
703 "Preparing to send %d other test messages\n",
704 total_other_expected_messages);
705 GNUNET_SCHEDULER_add_now (&send_test_messages, other_test_messages);
706 if (GNUNET_SCHEDULER_NO_TASK != die_task)
707 GNUNET_SCHEDULER_cancel (die_task);
709 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
710 (GNUNET_TIME_UNIT_SECONDS, 250), &end_badly,
711 "from send_other_messages");
716 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
717 const struct GNUNET_PeerIdentity *second, uint32_t distance,
718 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
719 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
720 struct GNUNET_TESTING_Daemon *first_daemon,
721 struct GNUNET_TESTING_Daemon *second_daemon,
724 struct TestMessageContext *temp_context;
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "connected peer %s to peer %s, distance %u\n",
731 first_daemon->shortname, second_daemon->shortname, distance);
732 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
733 temp_context->peer1 = first_daemon;
734 temp_context->peer2 = second_daemon;
735 temp_context->next = test_messages;
736 temp_context->uid = total_connections;
737 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
738 test_messages = temp_context;
743 failed_connections++;
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Failed to connect peer %s to peer %s with error :\n%s\n",
746 first_daemon->shortname, second_daemon->shortname, emsg);
749 if (total_connections == expected_connections)
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Created %u total connections, which is our target number! Calling send messages.\n",
754 if (GNUNET_SCHEDULER_NO_TASK != die_task)
755 GNUNET_SCHEDULER_cancel (die_task);
756 die_task = GNUNET_SCHEDULER_NO_TASK;
757 GNUNET_SCHEDULER_add_now (&send_test_messages, test_messages);
759 else if (total_connections + failed_connections == expected_connections)
761 if (failed_connections <
762 (unsigned int) (fail_percentage * total_connections))
764 GNUNET_SCHEDULER_cancel (die_task);
765 die_task = GNUNET_SCHEDULER_NO_TASK;
766 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_test_messages,
771 if (die_task != GNUNET_SCHEDULER_NO_TASK)
772 GNUNET_SCHEDULER_cancel (die_task);
774 GNUNET_SCHEDULER_add_now (&end_badly,
775 "from topology_callback (too many failed connections)");
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
782 total_connections, failed_connections, expected_connections,
783 expected_connections -
784 (unsigned int) (fail_percentage * expected_connections));
790 * Method called whenever a given peer connects.
793 * @param peer peer identity this notification is about
794 * @param atsi performance data about this peer's connection
795 * @param atsi_count number of atsi datums
799 all_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
800 const struct GNUNET_ATS_Information *atsi,
801 unsigned int atsi_count)
803 struct GNUNET_TESTING_Daemon *d = cls;
804 struct GNUNET_TESTING_Daemon *second_daemon;
805 char *second_shortname;
808 struct TestMessageContext *temp_context;
812 if (0 == memcmp (&d->id, peer, sizeof (struct GNUNET_PeerIdentity)))
814 second_shortname = GNUNET_strdup (GNUNET_i2s (peer));
815 distance = get_atsi_distance (atsi, atsi_count);
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "connected peer %s to peer %s, distance %u\n", d->shortname,
819 second_shortname, distance);
821 GNUNET_CONTAINER_multihashmap_get (peer_daemon_hash, &peer->hashPubKey);
823 if (second_daemon == NULL)
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't find second peer!\n");
826 GNUNET_free (second_shortname);
832 temp_total_other_messages++;
833 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
834 temp_context->peer1 = d;
835 temp_context->peer2 = second_daemon;
836 temp_context->next = other_test_messages;
837 temp_context->uid = total_connections + temp_total_other_messages;
838 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
839 other_test_messages = temp_context;
843 if (dotOutFile != NULL)
846 FPRINTF (dotOutFile, "\tn%s -- n%s;\n", d->shortname, second_shortname);
847 else if (distance == 2)
848 FPRINTF (dotOutFile, "\tn%s -- n%s [color=blue];\n", d->shortname,
850 else if (distance == 3)
851 FPRINTF (dotOutFile, "\tn%s -- n%s [color=red];\n", d->shortname,
853 else if (distance == 4)
854 FPRINTF (dotOutFile, "\tn%s -- n%s [color=green];\n", d->shortname,
857 FPRINTF (dotOutFile, "\tn%s -- n%s [color=brown];\n", d->shortname,
860 GNUNET_free (second_shortname);
862 if (temp_total_other_messages == num_additional_messages)
864 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_other_messages,
871 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
872 const struct GNUNET_CONFIGURATION_Handle *cfg,
873 struct GNUNET_TESTING_Daemon *d, const char *emsg)
875 struct PeerContext *new_peer;
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880 "Failed to start daemon with error: `%s'\n", emsg);
883 GNUNET_assert (id != NULL);
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
885 (num_peers - peers_left) + 1, num_peers);
886 GNUNET_assert (GNUNET_SYSERR !=
887 GNUNET_CONTAINER_multihashmap_put (peer_daemon_hash,
889 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
891 new_peer = GNUNET_malloc (sizeof (struct PeerContext));
892 new_peer->peer_handle =
893 GNUNET_CORE_connect (cfg, d, NULL, &all_connect_handler, NULL, NULL,
894 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
895 new_peer->daemon = d;
896 new_peer->next = all_peers;
897 all_peers = new_peer;
902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
903 "All %d daemons started, now creating topology!\n", num_peers);
904 GNUNET_SCHEDULER_cancel (die_task);
905 die_task = GNUNET_SCHEDULER_NO_TASK;
906 expected_connections = -1;
907 if ((pg != NULL) && (peers_left == 0))
909 expected_connections =
910 GNUNET_TESTING_connect_topology (pg, connection_topology,
911 connect_topology_option,
912 connect_topology_option_modifier,
913 TIMEOUT, 12, NULL, NULL);
914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
915 expected_connections);
918 if (expected_connections == GNUNET_SYSERR)
921 GNUNET_SCHEDULER_add_now (&end_badly,
922 "from connect topology (bad return)");
926 /* Set up task in case topology creation doesn't finish
927 * within a reasonable amount of time */
929 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
930 "from connect topology (timeout)");
938 * Callback indicating that the hostkey was created for a peer.
941 * @param id the peer identity
942 * @param d the daemon handle (pretty useless at this point, remove?)
943 * @param emsg non-null on failure
946 hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
947 struct GNUNET_TESTING_Daemon *d, const char *emsg)
951 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
952 "Hostkey callback received error: %s\n", emsg);
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostkey created for peer `%s'\n",
960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
961 "All %d hostkeys created, now creating topology!\n", num_peers);
962 if (GNUNET_SCHEDULER_NO_TASK != die_task)
964 GNUNET_SCHEDULER_cancel (die_task);
965 die_task = GNUNET_SCHEDULER_NO_TASK;
967 /* create topology */
968 peers_left = num_peers; /* Reset counter */
969 if (GNUNET_TESTING_create_topology
970 (pg, topology, blacklist_topology,
971 blacklist_transports) != GNUNET_SYSERR)
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Topology set up, now starting peers!\n");
975 GNUNET_TESTING_daemons_continue_startup (pg);
976 /* Set up task in case topology creation doesn't finish
977 * within a reasonable amount of time */
979 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
980 "from continue startup (timeout)");
985 GNUNET_SCHEDULER_add_now (&end_badly,
986 "from create topology (bad return)");
994 run (void *cls, char *const *args, const char *cfgfile,
995 const struct GNUNET_CONFIGURATION_Handle *cfg)
998 char *connect_topology_str;
999 char *blacklist_topology_str;
1000 char *connect_topology_option_str;
1001 char *connect_topology_option_modifier_string;
1005 dotOutFile = fopen (dotOutFileName, "w");
1006 if (dotOutFile != NULL)
1008 FPRINTF (dotOutFile, "%s", "strict graph G {\n");
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Starting daemons based on config file %s\n", cfgfile);
1014 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
1022 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
1024 (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
1026 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1027 "Invalid topology `%s' given for section %s option %s\n",
1028 topology_str, "TESTING", "TOPOLOGY");
1029 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1033 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1035 &connect_topology_str)) &&
1037 GNUNET_TESTING_topology_get (&connection_topology,
1038 connect_topology_str)))
1040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1041 "Invalid connect topology `%s' given for section %s option %s\n",
1042 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1044 GNUNET_free_non_null (connect_topology_str);
1046 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1047 "connect_topology_option",
1048 &connect_topology_option_str)) &&
1050 GNUNET_TESTING_topology_option_get (&connect_topology_option,
1051 connect_topology_option_str)))
1053 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1054 "Invalid connect topology option `%s' given for section %s option %s\n",
1055 connect_topology_option_str, "TESTING",
1056 "CONNECT_TOPOLOGY_OPTION");
1057 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1059 GNUNET_free_non_null (connect_topology_option_str);
1061 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1062 "connect_topology_option_modifier",
1063 &connect_topology_option_modifier_string))
1066 (connect_topology_option_modifier_string, "%lf",
1067 &connect_topology_option_modifier) != 1)
1069 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1070 "testing", "connect_topology_option_modifier",
1073 GNUNET_free (connect_topology_option_modifier_string);
1077 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1078 "blacklist_transports",
1079 &blacklist_transports))
1080 blacklist_transports = NULL;
1083 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1084 "blacklist_topology",
1085 &blacklist_topology_str)) &&
1087 GNUNET_TESTING_topology_get (&blacklist_topology,
1088 blacklist_topology_str)))
1090 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1091 "Invalid topology `%s' given for section %s option %s\n",
1092 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1094 GNUNET_free_non_null (topology_str);
1095 GNUNET_free_non_null (blacklist_topology_str);
1096 if (GNUNET_SYSERR ==
1097 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1099 num_peers = DEFAULT_NUM_PEERS;
1101 if (GNUNET_SYSERR ==
1102 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1103 "additional_messages",
1104 &num_additional_messages))
1105 num_additional_messages = DEFAULT_ADDITIONAL_MESSAGES;
1109 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
1110 peers_left = num_peers;
1112 /* Set up a task to end testing if peer start fails */
1114 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1115 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
1116 "didn't start all daemons in reasonable amount of time!!!");
1118 peer_daemon_hash = GNUNET_CONTAINER_multihashmap_create (peers_left, GNUNET_NO);
1119 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
1120 peers_left, /* Number of outstanding connections */
1121 peers_left, /* Number of parallel ssh connections, or peers being started at once */
1122 TIMEOUT, &hostkey_callback, NULL,
1123 &peers_started_callback, NULL,
1124 &topology_callback, NULL, NULL);
1130 main (int argc, char *argv[])
1134 char *const argv[] = { "test-transport-dv",
1136 "test_transport_dv_data.conf",
1139 struct GNUNET_GETOPT_CommandLineOption options[] = {
1140 GNUNET_GETOPT_OPTION_END
1142 GNUNET_log_setup ("test-transport-dv",
1146 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
1147 "test-transport-dv", "nohelp", options, &run, &ok);
1148 if (ret != GNUNET_OK)
1150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1151 "`test-transport-dv': Failed with error code %d\n", ret);
1154 * Need to remove base directory, subdirectories taken care
1155 * of by the testing framework.
1157 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
1159 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1160 "Failed to remove testing directory %s\n", test_directory);
1162 return (GNUNET_OK == ret) ? 0 : 1;
1165 /* end of test_transport_api_dv.c */