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)},
488 * Notify of all peer1's peers, once peer 2 is found, schedule connect
489 * to peer two for message send.
492 * @param peer peer identity this notification is about
493 * @param atsi performance data for the connection
494 * @param atsi_count number of ATS information included
497 connect_notify_peer2 (void *cls, const struct GNUNET_PeerIdentity *peer,
498 const struct GNUNET_ATS_Information *atsi,
499 unsigned int atsi_count)
501 struct TestMessageContext *pos = cls;
503 if (0 == memcmp (&pos->peer1->id, peer, sizeof (struct GNUNET_PeerIdentity)))
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "Core connection from `%s' to `%4s' verfied, sending message!\n",
507 GNUNET_i2s (&pos->peer2->id), GNUNET_h2s (&peer->hashPubKey));
509 GNUNET_CORE_notify_transmit_ready (pos->peer1handle, GNUNET_YES, 0,
510 TIMEOUT, &pos->peer2->id,
511 sizeof (struct GNUNET_TestMessage),
512 &transmit_ready, pos))
514 /* This probably shouldn't happen, but it does (timing issue?) */
515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
516 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
517 GNUNET_i2s (&pos->peer2->id));
518 transmit_ready_failed++;
519 total_other_expected_messages--;
523 transmit_ready_scheduled++;
529 init_notify_peer2 (void *cls, struct GNUNET_CORE_Handle *server,
530 const struct GNUNET_PeerIdentity *my_identity)
532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533 "Core connection to `%4s' established, awaiting connections.\n",
534 GNUNET_i2s (my_identity));
535 total_server_connections++;
539 * Notify of all peer1's peers, once peer 2 is found, schedule connect
540 * to peer two for message send.
543 * @param peer peer identity this notification is about
544 * @param atsi performance data for the connection
545 * @param atsi_count number of atsi datums
548 connect_notify_peer1 (void *cls, const struct GNUNET_PeerIdentity *peer,
549 const struct GNUNET_ATS_Information *atsi,
550 unsigned int atsi_count)
552 struct TestMessageContext *pos = cls;
554 if (0 == memcmp (&pos->peer2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "Core connection from `%s' to `%4s' verified.\n",
558 GNUNET_i2s (&pos->peer1->id), GNUNET_h2s (&peer->hashPubKey));
560 * Connect to the receiving peer
563 GNUNET_CORE_connect (pos->peer2->cfg, pos, &init_notify_peer2,
564 &connect_notify_peer2, NULL, NULL, GNUNET_YES,
565 NULL, GNUNET_YES, handlers);
570 init_notify_peer1 (void *cls, struct GNUNET_CORE_Handle *server,
571 const struct GNUNET_PeerIdentity *my_identity)
573 total_server_connections++;
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Core connection to `%4s' established, awaiting connections...\n",
576 GNUNET_i2s (my_identity));
581 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
583 struct TestMessageContext *pos = cls;
585 if (((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) || (cls == NULL))
588 if (die_task == GNUNET_SCHEDULER_NO_TASK)
591 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
592 "from create topology (timeout)");
595 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
597 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
598 (GNUNET_TIME_UNIT_SECONDS, 1),
599 &send_test_messages, pos);
600 return; /* Otherwise we'll double schedule messages here! */
602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
603 "Attempting to send test message from %s to %s\n",
604 pos->peer1->shortname, pos->peer2->shortname);
606 * Connect to the sending peer
609 GNUNET_CORE_connect (pos->peer1->cfg, pos, &init_notify_peer1,
610 &connect_notify_peer1, NULL, NULL, GNUNET_NO, NULL,
611 GNUNET_NO, no_handlers);
613 GNUNET_assert (pos->peer1handle != NULL);
615 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
617 GNUNET_SCHEDULER_add_now (&send_test_messages, pos->next);
621 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
622 (GNUNET_TIME_UNIT_SECONDS, 1),
623 &send_test_messages, pos->next);
628 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
630 struct TestMessageContext *pos;
631 struct TestMessageContext *free_pos;
632 struct PeerContext *peer_pos;
635 struct PeerContext *inner_peer_pos;
636 struct TestMessageContext *temp_context;
638 peer_pos = all_peers;
639 while (peer_pos != NULL)
641 if (peer_pos->peer_handle != NULL)
643 GNUNET_CORE_disconnect (peer_pos->peer_handle);
644 peer_pos->peer_handle = NULL;
647 inner_peer_pos = all_peers;
648 while (inner_peer_pos != NULL)
650 if (inner_peer_pos != peer_pos)
652 temp_total_other_messages++;
653 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
654 temp_context->peer1 = peer_pos->daemon;
655 temp_context->peer2 = inner_peer_pos->daemon;
656 temp_context->next = other_test_messages;
657 temp_context->uid = total_connections + temp_total_other_messages;
658 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
659 other_test_messages = temp_context;
661 inner_peer_pos = inner_peer_pos->next;
664 peer_pos = peer_pos->next;
671 if (pos->peer1handle != NULL)
673 GNUNET_CORE_disconnect (pos->peer1handle);
674 pos->peer1handle = NULL;
676 if (pos->peer2handle != NULL)
678 GNUNET_CORE_disconnect (pos->peer2handle);
679 pos->peer2handle = NULL;
683 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
685 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
687 GNUNET_free (free_pos);
689 test_messages = NULL;
691 total_other_expected_messages = temp_total_other_messages;
692 if (total_other_expected_messages == 0)
694 GNUNET_SCHEDULER_add_now (&end_badly,
695 "send_other_messages had 0 messages to send, no DV connections made!");
697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
698 "Preparing to send %d other test messages\n",
699 total_other_expected_messages);
700 GNUNET_SCHEDULER_add_now (&send_test_messages, other_test_messages);
701 if (GNUNET_SCHEDULER_NO_TASK != die_task)
702 GNUNET_SCHEDULER_cancel (die_task);
704 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
705 (GNUNET_TIME_UNIT_SECONDS, 250), &end_badly,
706 "from send_other_messages");
710 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
711 const struct GNUNET_PeerIdentity *second, uint32_t distance,
712 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
713 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
714 struct GNUNET_TESTING_Daemon *first_daemon,
715 struct GNUNET_TESTING_Daemon *second_daemon,
718 struct TestMessageContext *temp_context;
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "connected peer %s to peer %s, distance %u\n",
725 first_daemon->shortname, second_daemon->shortname, distance);
726 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
727 temp_context->peer1 = first_daemon;
728 temp_context->peer2 = second_daemon;
729 temp_context->next = test_messages;
730 temp_context->uid = total_connections;
731 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
732 test_messages = temp_context;
737 failed_connections++;
738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
739 "Failed to connect peer %s to peer %s with error :\n%s\n",
740 first_daemon->shortname, second_daemon->shortname, emsg);
743 if (total_connections == expected_connections)
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
746 "Created %u total connections, which is our target number! Calling send messages.\n",
748 if (GNUNET_SCHEDULER_NO_TASK != die_task)
749 GNUNET_SCHEDULER_cancel (die_task);
750 die_task = GNUNET_SCHEDULER_NO_TASK;
751 GNUNET_SCHEDULER_add_now (&send_test_messages, test_messages);
753 else if (total_connections + failed_connections == expected_connections)
755 if (failed_connections <
756 (unsigned int) (fail_percentage * total_connections))
758 GNUNET_SCHEDULER_cancel (die_task);
759 die_task = GNUNET_SCHEDULER_NO_TASK;
760 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_test_messages,
765 if (die_task != GNUNET_SCHEDULER_NO_TASK)
766 GNUNET_SCHEDULER_cancel (die_task);
768 GNUNET_SCHEDULER_add_now (&end_badly,
769 "from topology_callback (too many failed connections)");
774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
775 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
776 total_connections, failed_connections, expected_connections,
777 expected_connections -
778 (unsigned int) (fail_percentage * expected_connections));
784 * Method called whenever a given peer connects.
787 * @param peer peer identity this notification is about
788 * @param atsi performance data about this peer's connection
789 * @param atsi_count number of atsi datums
793 all_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
794 const struct GNUNET_ATS_Information *atsi,
795 unsigned int atsi_count)
797 struct GNUNET_TESTING_Daemon *d = cls;
798 struct GNUNET_TESTING_Daemon *second_daemon;
799 char *second_shortname;
802 struct TestMessageContext *temp_context;
806 if (0 == memcmp (&d->id, peer, sizeof (struct GNUNET_PeerIdentity)))
808 second_shortname = GNUNET_strdup (GNUNET_i2s (peer));
809 distance = get_atsi_distance (atsi, atsi_count);
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "connected peer %s to peer %s, distance %u\n", d->shortname,
813 second_shortname, distance);
815 GNUNET_CONTAINER_multihashmap_get (peer_daemon_hash, &peer->hashPubKey);
817 if (second_daemon == NULL)
819 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't find second peer!\n");
820 GNUNET_free (second_shortname);
826 temp_total_other_messages++;
827 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
828 temp_context->peer1 = d;
829 temp_context->peer2 = second_daemon;
830 temp_context->next = other_test_messages;
831 temp_context->uid = total_connections + temp_total_other_messages;
832 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
833 other_test_messages = temp_context;
837 if (dotOutFile != NULL)
840 FPRINTF (dotOutFile, "\tn%s -- n%s;\n", d->shortname, second_shortname);
841 else if (distance == 2)
842 FPRINTF (dotOutFile, "\tn%s -- n%s [color=blue];\n", d->shortname,
844 else if (distance == 3)
845 FPRINTF (dotOutFile, "\tn%s -- n%s [color=red];\n", d->shortname,
847 else if (distance == 4)
848 FPRINTF (dotOutFile, "\tn%s -- n%s [color=green];\n", d->shortname,
851 FPRINTF (dotOutFile, "\tn%s -- n%s [color=brown];\n", d->shortname,
854 GNUNET_free (second_shortname);
856 if (temp_total_other_messages == num_additional_messages)
858 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_other_messages,
864 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
865 const struct GNUNET_CONFIGURATION_Handle *cfg,
866 struct GNUNET_TESTING_Daemon *d, const char *emsg)
868 struct PeerContext *new_peer;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Failed to start daemon with error: `%s'\n", emsg);
876 GNUNET_assert (id != NULL);
877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
878 (num_peers - peers_left) + 1, num_peers);
879 GNUNET_assert (GNUNET_SYSERR !=
880 GNUNET_CONTAINER_multihashmap_put (peer_daemon_hash,
882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
884 new_peer = GNUNET_malloc (sizeof (struct PeerContext));
885 new_peer->peer_handle =
886 GNUNET_CORE_connect (cfg, d, NULL, &all_connect_handler, NULL, NULL,
887 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
888 new_peer->daemon = d;
889 new_peer->next = all_peers;
890 all_peers = new_peer;
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "All %d daemons started, now creating topology!\n", num_peers);
897 GNUNET_SCHEDULER_cancel (die_task);
898 die_task = GNUNET_SCHEDULER_NO_TASK;
899 expected_connections = -1;
900 if ((pg != NULL) && (peers_left == 0))
902 expected_connections =
903 GNUNET_TESTING_connect_topology (pg, connection_topology,
904 connect_topology_option,
905 connect_topology_option_modifier,
906 TIMEOUT, 12, NULL, NULL);
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
908 expected_connections);
911 if (expected_connections == GNUNET_SYSERR)
914 GNUNET_SCHEDULER_add_now (&end_badly,
915 "from connect topology (bad return)");
919 /* Set up task in case topology creation doesn't finish
920 * within a reasonable amount of time */
922 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
923 "from connect topology (timeout)");
930 * Callback indicating that the hostkey was created for a peer.
933 * @param id the peer identity
934 * @param d the daemon handle (pretty useless at this point, remove?)
935 * @param emsg non-null on failure
938 hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
939 struct GNUNET_TESTING_Daemon *d, const char *emsg)
943 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
944 "Hostkey callback received error: %s\n", emsg);
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostkey created for peer `%s'\n",
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "All %d hostkeys created, now creating topology!\n", num_peers);
954 if (GNUNET_SCHEDULER_NO_TASK != die_task)
956 GNUNET_SCHEDULER_cancel (die_task);
957 die_task = GNUNET_SCHEDULER_NO_TASK;
959 /* create topology */
960 peers_left = num_peers; /* Reset counter */
961 if (GNUNET_TESTING_create_topology
962 (pg, topology, blacklist_topology,
963 blacklist_transports) != GNUNET_SYSERR)
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 "Topology set up, now starting peers!\n");
967 GNUNET_TESTING_daemons_continue_startup (pg);
968 /* Set up task in case topology creation doesn't finish
969 * within a reasonable amount of time */
971 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
972 "from continue startup (timeout)");
977 GNUNET_SCHEDULER_add_now (&end_badly,
978 "from create topology (bad return)");
985 run (void *cls, char *const *args, const char *cfgfile,
986 const struct GNUNET_CONFIGURATION_Handle *cfg)
989 char *connect_topology_str;
990 char *blacklist_topology_str;
991 char *connect_topology_option_str;
992 char *connect_topology_option_modifier_string;
996 dotOutFile = fopen (dotOutFileName, "w");
997 if (dotOutFile != NULL)
999 FPRINTF (dotOutFile, "%s", "strict graph G {\n");
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1003 "Starting daemons based on config file %s\n", cfgfile);
1005 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
1013 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
1015 (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
1017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1018 "Invalid topology `%s' given for section %s option %s\n",
1019 topology_str, "TESTING", "TOPOLOGY");
1020 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1024 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1026 &connect_topology_str)) &&
1028 GNUNET_TESTING_topology_get (&connection_topology,
1029 connect_topology_str)))
1031 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1032 "Invalid connect topology `%s' given for section %s option %s\n",
1033 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1035 GNUNET_free_non_null (connect_topology_str);
1037 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1038 "connect_topology_option",
1039 &connect_topology_option_str)) &&
1041 GNUNET_TESTING_topology_option_get (&connect_topology_option,
1042 connect_topology_option_str)))
1044 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1045 "Invalid connect topology option `%s' given for section %s option %s\n",
1046 connect_topology_option_str, "TESTING",
1047 "CONNECT_TOPOLOGY_OPTION");
1048 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1050 GNUNET_free_non_null (connect_topology_option_str);
1052 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1053 "connect_topology_option_modifier",
1054 &connect_topology_option_modifier_string))
1057 (connect_topology_option_modifier_string, "%lf",
1058 &connect_topology_option_modifier) != 1)
1060 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
1061 "testing", "connect_topology_option_modifier",
1064 GNUNET_free (connect_topology_option_modifier_string);
1068 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1069 "blacklist_transports",
1070 &blacklist_transports))
1071 blacklist_transports = NULL;
1074 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1075 "blacklist_topology",
1076 &blacklist_topology_str)) &&
1078 GNUNET_TESTING_topology_get (&blacklist_topology,
1079 blacklist_topology_str)))
1081 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1082 "Invalid topology `%s' given for section %s option %s\n",
1083 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1085 GNUNET_free_non_null (topology_str);
1086 GNUNET_free_non_null (blacklist_topology_str);
1087 if (GNUNET_SYSERR ==
1088 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1090 num_peers = DEFAULT_NUM_PEERS;
1092 if (GNUNET_SYSERR ==
1093 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1094 "additional_messages",
1095 &num_additional_messages))
1096 num_additional_messages = DEFAULT_ADDITIONAL_MESSAGES;
1100 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
1101 peers_left = num_peers;
1103 /* Set up a task to end testing if peer start fails */
1105 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1106 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
1107 "didn't start all daemons in reasonable amount of time!!!");
1109 peer_daemon_hash = GNUNET_CONTAINER_multihashmap_create (peers_left, GNUNET_NO);
1110 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
1111 peers_left, /* Number of outstanding connections */
1112 peers_left, /* Number of parallel ssh connections, or peers being started at once */
1113 TIMEOUT, &hostkey_callback, NULL,
1114 &peers_started_callback, NULL,
1115 &topology_callback, NULL, NULL);
1124 char *const argv[] = { "test-transport-dv",
1126 "test_transport_dv_data.conf",
1129 struct GNUNET_GETOPT_CommandLineOption options[] = {
1130 GNUNET_GETOPT_OPTION_END
1133 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
1134 "test-transport-dv", "nohelp", options, &run, &ok);
1135 if (ret != GNUNET_OK)
1137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1138 "`test-transport-dv': Failed with error code %d\n", ret);
1144 main (int argc, char *argv[])
1148 GNUNET_log_setup ("test-transport-dv",
1153 * Need to remove base directory, subdirectories taken care
1154 * of by the testing framework.
1156 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
1158 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1159 "Failed to remove testing directory %s\n", test_directory);
1164 /* end of test_transport_api_dv.c */