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"
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 static 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 =
101 GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
103 static double connect_topology_option_modifier = 0.0;
105 static char *test_directory;
107 struct GNUNET_CONTAINER_MultiHashMap *peer_daemon_hash;
111 GNUNET_NETWORK_STRUCT_BEGIN
113 struct GNUNET_TestMessage
116 * Header of the message
118 struct GNUNET_MessageHeader header;
121 * Unique identifier for this message.
125 GNUNET_NETWORK_STRUCT_END
129 /* This is a linked list */
130 struct PeerContext *next;
133 * Handle to the daemon
135 struct GNUNET_TESTING_Daemon *daemon;
137 /* Handle to the peer core */
138 struct GNUNET_CORE_Handle *peer_handle;
141 static struct PeerContext *all_peers;
143 struct TestMessageContext
145 /* This is a linked list */
146 struct TestMessageContext *next;
148 /* Handle to the sending peer core */
149 struct GNUNET_CORE_Handle *peer1handle;
151 /* Handle to the receiving peer core */
152 struct GNUNET_CORE_Handle *peer2handle;
154 /* Handle to the sending peer daemon */
155 struct GNUNET_TESTING_Daemon *peer1;
157 /* Handle to the receiving peer daemon */
158 struct GNUNET_TESTING_Daemon *peer2;
160 /* Identifier for this message, so we don't disconnect other peers! */
163 /* Task for disconnecting cores, allow task to be cancelled on shutdown */
164 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
167 static struct TestMessageContext *test_messages;
169 static struct TestMessageContext *other_test_messages;
172 * Check whether peers successfully shut down.
175 shutdown_callback (void *cls, const char *emsg)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
194 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
196 GNUNET_assert (pg != NULL);
197 struct PeerContext *peer_pos;
198 struct PeerContext *free_peer_pos;
199 struct TestMessageContext *pos;
200 struct TestMessageContext *free_pos;
202 die_task = GNUNET_SCHEDULER_NO_TASK;
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Called finish testing, stopping daemons.\n");
207 peer_pos = all_peers;
208 while (peer_pos != NULL)
210 if (peer_pos->peer_handle != NULL)
211 GNUNET_CORE_disconnect (peer_pos->peer_handle);
212 free_peer_pos = peer_pos;
213 peer_pos = peer_pos->next;
214 GNUNET_free (free_peer_pos);
221 if (pos->peer1handle != NULL)
223 GNUNET_CORE_disconnect (pos->peer1handle);
224 pos->peer1handle = NULL;
226 if (pos->peer2handle != NULL)
228 GNUNET_CORE_disconnect (pos->peer2handle);
229 pos->peer2handle = NULL;
233 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
235 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
237 GNUNET_free (free_pos);
240 pos = other_test_messages;
243 if (pos->peer1handle != NULL)
245 GNUNET_CORE_disconnect (pos->peer1handle);
246 pos->peer1handle = NULL;
248 if (pos->peer2handle != NULL)
250 GNUNET_CORE_disconnect (pos->peer2handle);
251 pos->peer2handle = NULL;
255 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
257 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
259 GNUNET_free (free_pos);
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n",
264 transmit_ready_scheduled, transmit_ready_failed,
265 transmit_ready_called);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
271 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "daemons_stop finished\n");
275 if (dotOutFile != NULL)
277 FPRINTF (dotOutFile, "%s", "}");
286 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
288 struct TestMessageContext *pos = cls;
290 /* Disconnect from the respective cores */
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from peer 1 `%4s'\n",
293 GNUNET_i2s (&pos->peer1->id));
295 if (pos->peer1handle != NULL)
296 GNUNET_CORE_disconnect (pos->peer1handle);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from peer 2 `%4s'\n",
299 GNUNET_i2s (&pos->peer2->id));
301 if (pos->peer2handle != NULL)
302 GNUNET_CORE_disconnect (pos->peer2handle);
303 /* Set handles to NULL so test case can be ended properly */
304 pos->peer1handle = NULL;
305 pos->peer2handle = NULL;
306 pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
307 /* Decrement total connections so new can be established */
308 total_server_connections -= 2;
312 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 struct TestMessageContext *pos;
316 struct TestMessageContext *free_pos;
317 struct PeerContext *peer_pos;
318 struct PeerContext *free_peer_pos;
320 die_task = GNUNET_SCHEDULER_NO_TASK;
321 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
322 "End badly was called (%s)... stopping daemons.\n", msg);
324 peer_pos = all_peers;
325 while (peer_pos != NULL)
327 if (peer_pos->peer_handle != NULL)
328 GNUNET_CORE_disconnect (peer_pos->peer_handle);
329 free_peer_pos = peer_pos;
330 peer_pos = peer_pos->next;
331 GNUNET_free (free_peer_pos);
338 if (pos->peer1handle != NULL)
340 GNUNET_CORE_disconnect (pos->peer1handle);
341 pos->peer1handle = NULL;
343 if (pos->peer2handle != NULL)
345 GNUNET_CORE_disconnect (pos->peer2handle);
346 pos->peer2handle = NULL;
350 GNUNET_free (free_pos);
353 pos = other_test_messages;
356 if (pos->peer1handle != NULL)
358 GNUNET_CORE_disconnect (pos->peer1handle);
359 pos->peer1handle = NULL;
361 if (pos->peer2handle != NULL)
363 GNUNET_CORE_disconnect (pos->peer2handle);
364 pos->peer2handle = NULL;
368 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
370 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
372 GNUNET_free (free_pos);
377 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
378 ok = 7331; /* Opposite of leet */
381 ok = 401; /* Never got peers started */
383 if (dotOutFile != NULL)
385 FPRINTF (dotOutFile, "%s", "}");
391 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
394 * Get distance information from 'atsi'.
396 * @param atsi performance data
397 * @return connected transport distance
400 get_atsi_distance (const struct GNUNET_ATS_Information *atsi,
401 unsigned int atsi_count)
405 for (i = 0; i < atsi_count; i++)
407 if (ntohl (atsi->type) == GNUNET_ATS_QUALITY_NET_DISTANCE)
408 return ntohl (atsi->value);
412 /* FIXME: we do not have distance data? Assume direct neighbor. */
418 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
419 const struct GNUNET_MessageHeader *message,
420 const struct GNUNET_ATS_Information *atsi,
421 unsigned int atsi_count)
423 struct TestMessageContext *pos = cls;
424 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *) message;
429 if (pos->uid != ntohl (msg->uid))
433 distance = get_atsi_distance (atsi, atsi_count);
436 memcmp (peer, &pos->peer1->id,
437 sizeof (struct GNUNET_PeerIdentity)));
438 if (total_other_expected_messages == 0)
440 total_messages_received++;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "Received message from `%4s', type %d, uid %u, distance %u.\n",
444 GNUNET_i2s (peer), ntohs (message->type), ntohl (msg->uid),
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Total messages received %d, expected %d.\n",
448 total_messages_received, expected_messages);
453 total_other_messages++;
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
456 "Received message from `%4s', type %d, uid %u, distance %u.\n",
457 GNUNET_i2s (peer), ntohs (message->type), ntohl (msg->uid),
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
460 "Total OTHER messages received %d, expected %d.\n",
461 total_other_messages, total_other_expected_messages);
465 if ((total_messages_received == expected_messages) &&
466 (total_other_messages == 0))
468 GNUNET_SCHEDULER_cancel (die_task);
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Scheduling timeout from DV connections.\n");
474 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
475 "waiting for DV peers to connect!");
477 else if ((total_other_expected_messages > 0) &&
478 (total_other_messages == total_other_expected_messages))
480 GNUNET_SCHEDULER_cancel (die_task);
481 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
485 pos->disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cores, pos);
492 transmit_ready (void *cls, size_t size, void *buf)
494 struct GNUNET_TestMessage *m;
495 struct TestMessageContext *pos = cls;
497 GNUNET_assert (buf != NULL);
498 m = (struct GNUNET_TestMessage *) buf;
499 m->header.type = htons (MTYPE);
500 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
501 m->uid = htonl (pos->uid);
502 transmit_ready_called++;
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505 "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n",
506 GNUNET_i2s (&pos->peer1->id), transmit_ready_scheduled,
507 transmit_ready_called);
509 return sizeof (struct GNUNET_TestMessage);
513 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
517 static struct GNUNET_CORE_MessageHandler handlers[] = {
518 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
523 * Notify of all peer1's peers, once peer 2 is found, schedule connect
524 * to peer two for message send.
527 * @param peer peer identity this notification is about
528 * @param atsi performance data for the connection
529 * @param atsi_count number of ATS information included
532 connect_notify_peer2 (void *cls, const struct GNUNET_PeerIdentity *peer,
533 const struct GNUNET_ATS_Information *atsi,
534 unsigned int atsi_count)
536 struct TestMessageContext *pos = cls;
538 if (0 == memcmp (&pos->peer1->id, peer, sizeof (struct GNUNET_PeerIdentity)))
541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
542 "Core connection from `%s' to `%4s' verfied, sending message!\n",
543 GNUNET_i2s (&pos->peer2->id), GNUNET_h2s (&peer->hashPubKey));
546 GNUNET_CORE_notify_transmit_ready (pos->peer1handle, GNUNET_YES, 0,
547 TIMEOUT, &pos->peer2->id,
548 sizeof (struct GNUNET_TestMessage),
549 &transmit_ready, pos))
551 /* This probably shouldn't happen, but it does (timing issue?) */
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
554 GNUNET_i2s (&pos->peer2->id));
555 transmit_ready_failed++;
556 total_other_expected_messages--;
560 transmit_ready_scheduled++;
566 init_notify_peer2 (void *cls, struct GNUNET_CORE_Handle *server,
567 const struct GNUNET_PeerIdentity *my_identity)
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "Core connection to `%4s' established, awaiting connections.\n",
572 GNUNET_i2s (my_identity));
574 total_server_connections++;
578 * Notify of all peer1's peers, once peer 2 is found, schedule connect
579 * to peer two for message send.
582 * @param peer peer identity this notification is about
583 * @param atsi performance data for the connection
584 * @param atsi_count number of atsi datums
587 connect_notify_peer1 (void *cls, const struct GNUNET_PeerIdentity *peer,
588 const struct GNUNET_ATS_Information *atsi,
589 unsigned int atsi_count)
591 struct TestMessageContext *pos = cls;
593 if (0 == memcmp (&pos->peer2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597 "Core connection from `%s' to `%4s' verified.\n",
598 GNUNET_i2s (&pos->peer1->id), GNUNET_h2s (&peer->hashPubKey));
601 * Connect to the receiving peer
604 GNUNET_CORE_connect (pos->peer2->cfg, 1, pos, &init_notify_peer2,
605 &connect_notify_peer2, NULL, NULL, GNUNET_YES,
606 NULL, GNUNET_YES, handlers);
611 init_notify_peer1 (void *cls, struct GNUNET_CORE_Handle *server,
612 const struct GNUNET_PeerIdentity *my_identity)
614 total_server_connections++;
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Core connection to `%4s' established, awaiting connections...\n",
618 GNUNET_i2s (my_identity));
624 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
626 struct TestMessageContext *pos = cls;
628 if (((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) || (cls == NULL))
631 if (die_task == GNUNET_SCHEDULER_NO_TASK)
634 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
635 "from create topology (timeout)");
638 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
640 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
641 (GNUNET_TIME_UNIT_SECONDS, 1),
642 &send_test_messages, pos);
643 return; /* Otherwise we'll double schedule messages here! */
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647 "Attempting to send test message from %s to %s\n",
648 pos->peer1->shortname, pos->peer2->shortname);
651 * Connect to the sending peer
654 GNUNET_CORE_connect (pos->peer1->cfg, 1, pos, &init_notify_peer1,
655 &connect_notify_peer1, NULL, NULL, GNUNET_NO, NULL,
656 GNUNET_NO, no_handlers);
658 GNUNET_assert (pos->peer1handle != NULL);
660 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
662 GNUNET_SCHEDULER_add_now (&send_test_messages, pos->next);
666 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
667 (GNUNET_TIME_UNIT_SECONDS, 1),
668 &send_test_messages, pos->next);
673 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
675 struct TestMessageContext *pos;
676 struct TestMessageContext *free_pos;
677 struct PeerContext *peer_pos;
680 struct PeerContext *inner_peer_pos;
681 struct TestMessageContext *temp_context;
683 peer_pos = all_peers;
684 while (peer_pos != NULL)
686 if (peer_pos->peer_handle != NULL)
688 GNUNET_CORE_disconnect (peer_pos->peer_handle);
689 peer_pos->peer_handle = NULL;
692 inner_peer_pos = all_peers;
693 while (inner_peer_pos != NULL)
695 if (inner_peer_pos != peer_pos)
697 temp_total_other_messages++;
698 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
699 temp_context->peer1 = peer_pos->daemon;
700 temp_context->peer2 = inner_peer_pos->daemon;
701 temp_context->next = other_test_messages;
702 temp_context->uid = total_connections + temp_total_other_messages;
703 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
704 other_test_messages = temp_context;
706 inner_peer_pos = inner_peer_pos->next;
709 peer_pos = peer_pos->next;
716 if (pos->peer1handle != NULL)
718 GNUNET_CORE_disconnect (pos->peer1handle);
719 pos->peer1handle = NULL;
721 if (pos->peer2handle != NULL)
723 GNUNET_CORE_disconnect (pos->peer2handle);
724 pos->peer2handle = NULL;
728 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
730 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
732 GNUNET_free (free_pos);
734 test_messages = NULL;
736 total_other_expected_messages = temp_total_other_messages;
737 if (total_other_expected_messages == 0)
739 GNUNET_SCHEDULER_add_now (&end_badly,
740 "send_other_messages had 0 messages to send, no DV connections made!");
743 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
744 "Preparing to send %d other test messages\n",
745 total_other_expected_messages);
748 GNUNET_SCHEDULER_add_now (&send_test_messages, other_test_messages);
749 if (GNUNET_SCHEDULER_NO_TASK != die_task)
750 GNUNET_SCHEDULER_cancel (die_task);
752 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
753 (GNUNET_TIME_UNIT_SECONDS, 250), &end_badly,
754 "from send_other_messages");
758 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
759 const struct GNUNET_PeerIdentity *second, uint32_t distance,
760 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
761 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
762 struct GNUNET_TESTING_Daemon *first_daemon,
763 struct GNUNET_TESTING_Daemon *second_daemon,
766 struct TestMessageContext *temp_context;
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "connected peer %s to peer %s, distance %u\n",
774 first_daemon->shortname, second_daemon->shortname, distance);
776 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
777 temp_context->peer1 = first_daemon;
778 temp_context->peer2 = second_daemon;
779 temp_context->next = test_messages;
780 temp_context->uid = total_connections;
781 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
782 test_messages = temp_context;
788 failed_connections++;
789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790 "Failed to connect peer %s to peer %s with error :\n%s\n",
791 first_daemon->shortname, second_daemon->shortname, emsg);
795 if (total_connections == expected_connections)
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Created %u total connections, which is our target number! Calling send messages.\n",
802 if (GNUNET_SCHEDULER_NO_TASK != die_task)
803 GNUNET_SCHEDULER_cancel (die_task);
804 die_task = GNUNET_SCHEDULER_NO_TASK;
805 GNUNET_SCHEDULER_add_now (&send_test_messages, test_messages);
807 else if (total_connections + failed_connections == expected_connections)
809 if (failed_connections <
810 (unsigned int) (fail_percentage * total_connections))
812 GNUNET_SCHEDULER_cancel (die_task);
813 die_task = GNUNET_SCHEDULER_NO_TASK;
814 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_test_messages,
819 if (die_task != GNUNET_SCHEDULER_NO_TASK)
820 GNUNET_SCHEDULER_cancel (die_task);
822 GNUNET_SCHEDULER_add_now (&end_badly,
823 "from topology_callback (too many failed connections)");
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
830 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
831 total_connections, failed_connections, expected_connections,
832 expected_connections -
833 (unsigned int) (fail_percentage * expected_connections));
840 * Method called whenever a given peer connects.
843 * @param peer peer identity this notification is about
844 * @param atsi performance data about this peer's connection
845 * @param atsi_count number of atsi datums
849 all_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
850 const struct GNUNET_ATS_Information *atsi,
851 unsigned int atsi_count)
853 struct GNUNET_TESTING_Daemon *d = cls;
854 struct GNUNET_TESTING_Daemon *second_daemon;
855 char *second_shortname;
858 struct TestMessageContext *temp_context;
862 if (0 == memcmp (&d->id, peer, sizeof (struct GNUNET_PeerIdentity)))
864 second_shortname = GNUNET_strdup (GNUNET_i2s (peer));
865 distance = get_atsi_distance (atsi, atsi_count);
868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
869 "connected peer %s to peer %s, distance %u\n", d->shortname,
870 second_shortname, distance);
874 GNUNET_CONTAINER_multihashmap_get (peer_daemon_hash, &peer->hashPubKey);
876 if (second_daemon == NULL)
878 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't find second peer!\n");
879 GNUNET_free (second_shortname);
885 temp_total_other_messages++;
886 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
887 temp_context->peer1 = d;
888 temp_context->peer2 = second_daemon;
889 temp_context->next = other_test_messages;
890 temp_context->uid = total_connections + temp_total_other_messages;
891 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
892 other_test_messages = temp_context;
896 if (dotOutFile != NULL)
899 FPRINTF (dotOutFile, "\tn%s -- n%s;\n", d->shortname, second_shortname);
900 else if (distance == 2)
901 FPRINTF (dotOutFile, "\tn%s -- n%s [color=blue];\n", d->shortname,
903 else if (distance == 3)
904 FPRINTF (dotOutFile, "\tn%s -- n%s [color=red];\n", d->shortname,
906 else if (distance == 4)
907 FPRINTF (dotOutFile, "\tn%s -- n%s [color=green];\n", d->shortname,
910 FPRINTF (dotOutFile, "\tn%s -- n%s [color=brown];\n", d->shortname,
913 GNUNET_free (second_shortname);
915 if (temp_total_other_messages == num_additional_messages)
917 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_other_messages,
923 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
924 const struct GNUNET_CONFIGURATION_Handle *cfg,
925 struct GNUNET_TESTING_Daemon *d, const char *emsg)
927 struct PeerContext *new_peer;
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Failed to start daemon with error: `%s'\n", emsg);
935 GNUNET_assert (id != NULL);
937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
938 (num_peers - peers_left) + 1, num_peers);
940 GNUNET_assert (GNUNET_SYSERR !=
941 GNUNET_CONTAINER_multihashmap_put (peer_daemon_hash,
943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
945 new_peer = GNUNET_malloc (sizeof (struct PeerContext));
946 new_peer->peer_handle =
947 GNUNET_CORE_connect (cfg, 1, d, NULL, &all_connect_handler, NULL, NULL,
948 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
949 new_peer->daemon = d;
950 new_peer->next = all_peers;
951 all_peers = new_peer;
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "All %d daemons started, now creating topology!\n", num_peers);
960 GNUNET_SCHEDULER_cancel (die_task);
961 die_task = GNUNET_SCHEDULER_NO_TASK;
962 expected_connections = -1;
963 if ((pg != NULL) && (peers_left == 0))
965 expected_connections =
966 GNUNET_TESTING_connect_topology (pg, connection_topology,
967 connect_topology_option,
968 connect_topology_option_modifier,
969 TIMEOUT, 12, NULL, NULL);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
972 expected_connections);
976 if (expected_connections == GNUNET_SYSERR)
979 GNUNET_SCHEDULER_add_now (&end_badly,
980 "from connect topology (bad return)");
984 /* Set up task in case topology creation doesn't finish
985 * within a reasonable amount of time */
987 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
988 "from connect topology (timeout)");
995 * Callback indicating that the hostkey was created for a peer.
998 * @param id the peer identity
999 * @param d the daemon handle (pretty useless at this point, remove?)
1000 * @param emsg non-null on failure
1003 hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
1004 struct GNUNET_TESTING_Daemon *d, const char *emsg)
1008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1009 "Hostkey callback received error: %s\n", emsg);
1013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostkey created for peer `%s'\n",
1017 if (peers_left == 0)
1020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1021 "All %d hostkeys created, now creating topology!\n", num_peers);
1023 if (GNUNET_SCHEDULER_NO_TASK != die_task)
1025 GNUNET_SCHEDULER_cancel (die_task);
1026 die_task = GNUNET_SCHEDULER_NO_TASK;
1028 /* create topology */
1029 peers_left = num_peers; /* Reset counter */
1030 if (GNUNET_TESTING_create_topology
1031 (pg, topology, blacklist_topology,
1032 blacklist_transports) != GNUNET_SYSERR)
1035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1036 "Topology set up, now starting peers!\n");
1038 GNUNET_TESTING_daemons_continue_startup (pg);
1039 /* Set up task in case topology creation doesn't finish
1040 * within a reasonable amount of time */
1042 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
1043 "from continue startup (timeout)");
1048 GNUNET_SCHEDULER_add_now (&end_badly,
1049 "from create topology (bad return)");
1056 run (void *cls, char *const *args, const char *cfgfile,
1057 const struct GNUNET_CONFIGURATION_Handle *cfg)
1060 char *connect_topology_str;
1061 char *blacklist_topology_str;
1062 char *connect_topology_option_str;
1063 char *connect_topology_option_modifier_string;
1067 dotOutFile = fopen (dotOutFileName, "w");
1068 if (dotOutFile != NULL)
1070 FPRINTF (dotOutFile, "%s", "strict graph G {\n");
1074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1075 "Starting daemons based on config file %s\n", cfgfile);
1079 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
1087 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
1089 (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
1091 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1092 "Invalid topology `%s' given for section %s option %s\n",
1093 topology_str, "TESTING", "TOPOLOGY");
1094 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1098 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1100 &connect_topology_str)) &&
1102 GNUNET_TESTING_topology_get (&connection_topology,
1103 connect_topology_str)))
1105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1106 "Invalid connect topology `%s' given for section %s option %s\n",
1107 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1109 GNUNET_free_non_null (connect_topology_str);
1111 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1112 "connect_topology_option",
1113 &connect_topology_option_str)) &&
1115 GNUNET_TESTING_topology_option_get (&connect_topology_option,
1116 connect_topology_option_str)))
1118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1119 "Invalid connect topology option `%s' given for section %s option %s\n",
1120 connect_topology_option_str, "TESTING",
1121 "CONNECT_TOPOLOGY_OPTION");
1122 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1124 GNUNET_free_non_null (connect_topology_option_str);
1126 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1127 "connect_topology_option_modifier",
1128 &connect_topology_option_modifier_string))
1131 (connect_topology_option_modifier_string, "%lf",
1132 &connect_topology_option_modifier) != 1)
1134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1136 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1137 connect_topology_option_modifier_string,
1138 "connect_topology_option_modifier", "TESTING");
1140 GNUNET_free (connect_topology_option_modifier_string);
1144 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1145 "blacklist_transports",
1146 &blacklist_transports))
1147 blacklist_transports = NULL;
1150 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1151 "blacklist_topology",
1152 &blacklist_topology_str)) &&
1154 GNUNET_TESTING_topology_get (&blacklist_topology,
1155 blacklist_topology_str)))
1157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1158 "Invalid topology `%s' given for section %s option %s\n",
1159 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1161 GNUNET_free_non_null (topology_str);
1162 GNUNET_free_non_null (blacklist_topology_str);
1163 if (GNUNET_SYSERR ==
1164 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1166 num_peers = DEFAULT_NUM_PEERS;
1168 if (GNUNET_SYSERR ==
1169 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1170 "additional_messages",
1171 &num_additional_messages))
1172 num_additional_messages = DEFAULT_ADDITIONAL_MESSAGES;
1176 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
1177 peers_left = num_peers;
1179 /* Set up a task to end testing if peer start fails */
1181 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1182 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
1183 "didn't start all daemons in reasonable amount of time!!!");
1185 peer_daemon_hash = GNUNET_CONTAINER_multihashmap_create (peers_left);
1186 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
1187 peers_left, /* Number of outstanding connections */
1188 peers_left, /* Number of parallel ssh connections, or peers being started at once */
1189 TIMEOUT, &hostkey_callback, NULL,
1190 &peers_started_callback, NULL,
1191 &topology_callback, NULL, NULL);
1200 char *const argv[] = { "test-transport-dv",
1202 "test_transport_dv_data.conf",
1208 struct GNUNET_GETOPT_CommandLineOption options[] = {
1209 GNUNET_GETOPT_OPTION_END
1212 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
1213 "test-transport-dv", "nohelp", options, &run, &ok);
1214 if (ret != GNUNET_OK)
1216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1217 "`test-transport-dv': Failed with error code %d\n", ret);
1223 main (int argc, char *argv[])
1227 GNUNET_log_setup ("test-transport-dv",
1236 * Need to remove base directory, subdirectories taken care
1237 * of by the testing framework.
1239 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
1241 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1242 "Failed to remove testing directory %s\n", test_directory);
1247 /* end of test_transport_api_dv.c */