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 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 struct GNUNET_TestMessage
114 * Header of the message
116 struct GNUNET_MessageHeader header;
119 * Unique identifier for this message.
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)
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown of peers failed!\n");
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
191 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
193 GNUNET_assert (pg != NULL);
194 struct PeerContext *peer_pos;
195 struct PeerContext *free_peer_pos;
196 struct TestMessageContext *pos;
197 struct TestMessageContext *free_pos;
199 die_task = GNUNET_SCHEDULER_NO_TASK;
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",
261 transmit_ready_scheduled, transmit_ready_failed,
262 transmit_ready_called);
266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling daemons_stop\n");
268 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "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, "Disconnecting from peer 1 `%4s'\n",
290 GNUNET_i2s (&pos->peer1->id));
292 if (pos->peer1handle != NULL)
293 GNUNET_CORE_disconnect (pos->peer1handle);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from peer 2 `%4s'\n",
296 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 struct TestMessageContext *pos;
313 struct TestMessageContext *free_pos;
314 struct PeerContext *peer_pos;
315 struct PeerContext *free_peer_pos;
317 die_task = GNUNET_SCHEDULER_NO_TASK;
318 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
319 "End badly was called (%s)... stopping daemons.\n", msg);
321 peer_pos = all_peers;
322 while (peer_pos != NULL)
324 if (peer_pos->peer_handle != NULL)
325 GNUNET_CORE_disconnect (peer_pos->peer_handle);
326 free_peer_pos = peer_pos;
327 peer_pos = peer_pos->next;
328 GNUNET_free (free_peer_pos);
335 if (pos->peer1handle != NULL)
337 GNUNET_CORE_disconnect (pos->peer1handle);
338 pos->peer1handle = NULL;
340 if (pos->peer2handle != NULL)
342 GNUNET_CORE_disconnect (pos->peer2handle);
343 pos->peer2handle = NULL;
347 GNUNET_free (free_pos);
350 pos = other_test_messages;
353 if (pos->peer1handle != NULL)
355 GNUNET_CORE_disconnect (pos->peer1handle);
356 pos->peer1handle = NULL;
358 if (pos->peer2handle != NULL)
360 GNUNET_CORE_disconnect (pos->peer2handle);
361 pos->peer2handle = NULL;
365 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
367 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
369 GNUNET_free (free_pos);
374 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
375 ok = 7331; /* Opposite of leet */
378 ok = 401; /* Never got peers started */
380 if (dotOutFile != NULL)
382 fprintf (dotOutFile, "}");
388 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
391 * Get distance information from 'atsi'.
393 * @param atsi performance data
394 * @return connected transport distance
397 get_atsi_distance (const struct GNUNET_TRANSPORT_ATS_Information *atsi)
399 while ((ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR) &&
400 (ntohl (atsi->type) != GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE))
402 if (ntohl (atsi->type) == GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR)
405 /* FIXME: we do not have distance data? Assume direct neighbor. */
408 return ntohl (atsi->value);
412 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
413 const struct GNUNET_MessageHeader *message,
414 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
416 struct TestMessageContext *pos = cls;
417 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *) message;
422 if (pos->uid != ntohl (msg->uid))
426 distance = get_atsi_distance (atsi);
429 memcmp (peer, &pos->peer1->id,
430 sizeof (struct GNUNET_PeerIdentity)));
431 if (total_other_expected_messages == 0)
433 total_messages_received++;
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436 "Received message from `%4s', type %d, uid %u, distance %u.\n",
437 GNUNET_i2s (peer), ntohs (message->type), ntohl (msg->uid),
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Total messages received %d, expected %d.\n",
441 total_messages_received, expected_messages);
446 total_other_messages++;
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
449 "Received message from `%4s', type %d, uid %u, distance %u.\n",
450 GNUNET_i2s (peer), ntohs (message->type), ntohl (msg->uid),
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Total messages received %d, expected %d.\n",
454 total_other_messages, total_other_expected_messages);
458 if ((total_messages_received == expected_messages) &&
459 (total_other_messages == 0))
461 GNUNET_SCHEDULER_cancel (die_task);
463 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
464 "waiting for DV peers to connect!");
466 * if ((num_peers == 3) && (total_other_expected_messages == 2))
468 * GNUNET_SCHEDULER_add_now (&send_other_messages, NULL);
472 * GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20), &send_other_messages, NULL);
475 else if ((total_other_expected_messages > 0) &&
476 (total_other_messages == total_other_expected_messages))
478 GNUNET_SCHEDULER_cancel (die_task);
479 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
483 pos->disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cores, pos);
490 transmit_ready (void *cls, size_t size, void *buf)
492 struct GNUNET_TestMessage *m;
493 struct TestMessageContext *pos = cls;
495 GNUNET_assert (buf != NULL);
496 m = (struct GNUNET_TestMessage *) buf;
497 m->header.type = htons (MTYPE);
498 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
499 m->uid = htonl (pos->uid);
500 transmit_ready_called++;
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n",
504 GNUNET_i2s (&pos->peer1->id), transmit_ready_scheduled,
505 transmit_ready_called);
507 return sizeof (struct GNUNET_TestMessage);
511 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
515 static struct GNUNET_CORE_MessageHandler handlers[] = {
516 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
521 * Notify of all peer1's peers, once peer 2 is found, schedule connect
522 * to peer two for message send.
525 * @param peer peer identity this notification is about
526 * @param atsi performance data for the connection
529 connect_notify_peer2 (void *cls, const struct GNUNET_PeerIdentity *peer,
530 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
532 struct TestMessageContext *pos = cls;
534 if (0 == memcmp (&pos->peer1->id, peer, sizeof (struct GNUNET_PeerIdentity)))
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "Core connection from `%s' to `%4s' verfied, sending message!\n",
539 GNUNET_i2s (&pos->peer2->id), GNUNET_h2s (&peer->hashPubKey));
542 GNUNET_CORE_notify_transmit_ready (pos->peer1handle, GNUNET_YES, 0,
543 TIMEOUT, &pos->peer2->id,
544 sizeof (struct GNUNET_TestMessage),
545 &transmit_ready, pos))
547 /* This probably shouldn't happen, but it does (timing issue?) */
548 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
549 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
550 GNUNET_i2s (&pos->peer2->id));
551 transmit_ready_failed++;
552 total_other_expected_messages--;
556 transmit_ready_scheduled++;
562 init_notify_peer2 (void *cls, struct GNUNET_CORE_Handle *server,
563 const struct GNUNET_PeerIdentity *my_identity,
564 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Core connection to `%4s' established, awaiting connections.\n",
570 GNUNET_i2s (my_identity));
572 total_server_connections++;
576 * Notify of all peer1's peers, once peer 2 is found, schedule connect
577 * to peer two for message send.
580 * @param peer peer identity this notification is about
581 * @param atsi performance data for the connection
584 connect_notify_peer1 (void *cls, const struct GNUNET_PeerIdentity *peer,
585 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
587 struct TestMessageContext *pos = cls;
589 if (0 == memcmp (&pos->peer2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Core connection from `%s' to `%4s' verified.\n",
594 GNUNET_i2s (&pos->peer1->id), GNUNET_h2s (&peer->hashPubKey));
597 * Connect to the receiving peer
600 GNUNET_CORE_connect (pos->peer2->cfg, 1, pos, &init_notify_peer2,
601 &connect_notify_peer2, NULL, NULL, NULL,
602 GNUNET_YES, NULL, GNUNET_YES, handlers);
607 init_notify_peer1 (void *cls, struct GNUNET_CORE_Handle *server,
608 const struct GNUNET_PeerIdentity *my_identity,
609 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
612 total_server_connections++;
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Core connection to `%4s' established, awaiting connections...\n",
616 GNUNET_i2s (my_identity));
622 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
624 struct TestMessageContext *pos = cls;
626 if (((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) || (cls == NULL))
629 if (die_task == GNUNET_SCHEDULER_NO_TASK)
632 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
633 "from create topology (timeout)");
636 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
638 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
639 (GNUNET_TIME_UNIT_SECONDS, 1),
640 &send_test_messages, pos);
641 return; /* Otherwise we'll double schedule messages here! */
644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
645 "Attempting to send test message from %s to %s\n",
646 pos->peer1->shortname, pos->peer2->shortname);
649 * Connect to the sending peer
652 GNUNET_CORE_connect (pos->peer1->cfg, 1, pos, &init_notify_peer1,
653 &connect_notify_peer1, NULL, NULL, NULL, GNUNET_NO,
654 NULL, GNUNET_NO, no_handlers);
656 GNUNET_assert (pos->peer1handle != NULL);
658 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
660 GNUNET_SCHEDULER_add_now (&send_test_messages, pos->next);
664 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
665 (GNUNET_TIME_UNIT_SECONDS, 1),
666 &send_test_messages, pos->next);
671 send_other_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
673 struct TestMessageContext *pos;
674 struct TestMessageContext *free_pos;
675 struct PeerContext *peer_pos;
678 struct PeerContext *inner_peer_pos;
679 struct TestMessageContext *temp_context;
681 peer_pos = all_peers;
682 while (peer_pos != NULL)
684 if (peer_pos->peer_handle != NULL)
686 GNUNET_CORE_disconnect (peer_pos->peer_handle);
687 peer_pos->peer_handle = NULL;
690 inner_peer_pos = all_peers;
691 while (inner_peer_pos != NULL)
693 if (inner_peer_pos != peer_pos)
695 temp_total_other_messages++;
696 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
697 temp_context->peer1 = peer_pos->daemon;
698 temp_context->peer2 = inner_peer_pos->daemon;
699 temp_context->next = other_test_messages;
700 temp_context->uid = total_connections + temp_total_other_messages;
701 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
702 other_test_messages = temp_context;
704 inner_peer_pos = inner_peer_pos->next;
707 peer_pos = peer_pos->next;
714 if (pos->peer1handle != NULL)
716 GNUNET_CORE_disconnect (pos->peer1handle);
717 pos->peer1handle = NULL;
719 if (pos->peer2handle != NULL)
721 GNUNET_CORE_disconnect (pos->peer2handle);
722 pos->peer2handle = NULL;
726 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
728 GNUNET_SCHEDULER_cancel (free_pos->disconnect_task);
730 GNUNET_free (free_pos);
732 test_messages = NULL;
734 total_other_expected_messages = temp_total_other_messages;
735 if (total_other_expected_messages == 0)
737 GNUNET_SCHEDULER_add_now (&end_badly,
738 "send_other_messages had 0 messages to send, no DV connections made!");
741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
742 "Preparing to send %d other test messages\n",
743 total_other_expected_messages);
746 GNUNET_SCHEDULER_add_now (&send_test_messages, other_test_messages);
747 if (GNUNET_SCHEDULER_NO_TASK != die_task)
748 GNUNET_SCHEDULER_cancel (die_task);
750 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
751 (GNUNET_TIME_UNIT_SECONDS, 250), &end_badly,
752 "from send_other_messages");
756 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
757 const struct GNUNET_PeerIdentity *second, uint32_t distance,
758 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
759 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
760 struct GNUNET_TESTING_Daemon *first_daemon,
761 struct GNUNET_TESTING_Daemon *second_daemon,
764 struct TestMessageContext *temp_context;
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "connected peer %s to peer %s, distance %u\n",
772 first_daemon->shortname, second_daemon->shortname, distance);
774 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
775 temp_context->peer1 = first_daemon;
776 temp_context->peer2 = second_daemon;
777 temp_context->next = test_messages;
778 temp_context->uid = total_connections;
779 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
780 test_messages = temp_context;
786 failed_connections++;
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Failed to connect peer %s to peer %s with error :\n%s\n",
789 first_daemon->shortname, second_daemon->shortname, emsg);
793 if (total_connections == expected_connections)
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797 "Created %u total connections, which is our target number! Calling send messages.\n",
800 if (GNUNET_SCHEDULER_NO_TASK != die_task)
801 GNUNET_SCHEDULER_cancel (die_task);
802 die_task = GNUNET_SCHEDULER_NO_TASK;
803 GNUNET_SCHEDULER_add_now (&send_test_messages, test_messages);
805 else if (total_connections + failed_connections == expected_connections)
807 if (failed_connections <
808 (unsigned int) (fail_percentage * total_connections))
810 GNUNET_SCHEDULER_cancel (die_task);
811 die_task = GNUNET_SCHEDULER_NO_TASK;
812 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_test_messages,
817 if (die_task != GNUNET_SCHEDULER_NO_TASK)
818 GNUNET_SCHEDULER_cancel (die_task);
820 GNUNET_SCHEDULER_add_now (&end_badly,
821 "from topology_callback (too many failed connections)");
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
828 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
829 total_connections, failed_connections, expected_connections,
830 expected_connections -
831 (unsigned int) (fail_percentage * expected_connections));
838 * Method called whenever a given peer connects.
841 * @param peer peer identity this notification is about
842 * @param latency reported latency of the connection with 'other'
843 * @param distance reported distance (DV) to 'other'
846 all_connect_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
847 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
849 struct GNUNET_TESTING_Daemon *d = cls;
850 struct GNUNET_TESTING_Daemon *second_daemon;
851 char *second_shortname;
854 struct TestMessageContext *temp_context;
858 if (0 == memcmp (&d->id, peer, sizeof (struct GNUNET_PeerIdentity)))
860 second_shortname = GNUNET_strdup (GNUNET_i2s (peer));
861 distance = get_atsi_distance (atsi);
864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865 "connected peer %s to peer %s, distance %u\n", d->shortname,
866 second_shortname, distance);
870 GNUNET_CONTAINER_multihashmap_get (peer_daemon_hash, &peer->hashPubKey);
872 if (second_daemon == NULL)
874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Couldn't find second peer!\n");
875 GNUNET_free (second_shortname);
881 temp_total_other_messages++;
882 temp_context = GNUNET_malloc (sizeof (struct TestMessageContext));
883 temp_context->peer1 = d;
884 temp_context->peer2 = second_daemon;
885 temp_context->next = other_test_messages;
886 temp_context->uid = total_connections + temp_total_other_messages;
887 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
888 other_test_messages = temp_context;
892 if (dotOutFile != NULL)
895 fprintf (dotOutFile, "\tn%s -- n%s;\n", d->shortname, second_shortname);
896 else if (distance == 2)
897 fprintf (dotOutFile, "\tn%s -- n%s [color=blue];\n", d->shortname,
899 else if (distance == 3)
900 fprintf (dotOutFile, "\tn%s -- n%s [color=red];\n", d->shortname,
902 else if (distance == 4)
903 fprintf (dotOutFile, "\tn%s -- n%s [color=green];\n", d->shortname,
906 fprintf (dotOutFile, "\tn%s -- n%s [color=brown];\n", d->shortname,
909 GNUNET_free (second_shortname);
911 if (temp_total_other_messages == num_additional_messages)
913 /* FIXME: ret value!? */ GNUNET_SCHEDULER_add_now (&send_other_messages,
919 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
920 const struct GNUNET_CONFIGURATION_Handle *cfg,
921 struct GNUNET_TESTING_Daemon *d, const char *emsg)
923 struct PeerContext *new_peer;
927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
928 "Failed to start daemon with error: `%s'\n", emsg);
931 GNUNET_assert (id != NULL);
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
934 (num_peers - peers_left) + 1, num_peers);
936 GNUNET_assert (GNUNET_SYSERR !=
937 GNUNET_CONTAINER_multihashmap_put (peer_daemon_hash,
939 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
941 new_peer = GNUNET_malloc (sizeof (struct PeerContext));
942 new_peer->peer_handle =
943 GNUNET_CORE_connect (cfg, 1, d, NULL, &all_connect_handler, NULL, NULL,
944 NULL, GNUNET_NO, NULL, GNUNET_NO, no_handlers);
945 new_peer->daemon = d;
946 new_peer->next = all_peers;
947 all_peers = new_peer;
953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954 "All %d daemons started, now creating topology!\n", num_peers);
956 GNUNET_SCHEDULER_cancel (die_task);
957 die_task = GNUNET_SCHEDULER_NO_TASK;
958 expected_connections = -1;
959 if ((pg != NULL) && (peers_left == 0))
961 expected_connections =
962 GNUNET_TESTING_connect_topology (pg, connection_topology,
963 connect_topology_option,
964 connect_topology_option_modifier,
965 TIMEOUT, 12, NULL, NULL);
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %d expected connections\n",
968 expected_connections);
972 if (expected_connections == GNUNET_SYSERR)
975 GNUNET_SCHEDULER_add_now (&end_badly,
976 "from connect topology (bad return)");
980 /* Set up task in case topology creation doesn't finish
981 * within a reasonable amount of time */
983 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
984 "from connect topology (timeout)");
991 * Callback indicating that the hostkey was created for a peer.
994 * @param id the peer identity
995 * @param d the daemon handle (pretty useless at this point, remove?)
996 * @param emsg non-null on failure
999 hostkey_callback (void *cls, const struct GNUNET_PeerIdentity *id,
1000 struct GNUNET_TESTING_Daemon *d, const char *emsg)
1004 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1005 "Hostkey callback received error: %s\n", emsg);
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Hostkey created for peer `%s'\n",
1013 if (peers_left == 0)
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1017 "All %d hostkeys created, now creating topology!\n", num_peers);
1019 if (GNUNET_SCHEDULER_NO_TASK != die_task)
1021 GNUNET_SCHEDULER_cancel (die_task);
1022 die_task = GNUNET_SCHEDULER_NO_TASK;
1024 /* create topology */
1025 peers_left = num_peers; /* Reset counter */
1026 if (GNUNET_TESTING_create_topology
1027 (pg, topology, blacklist_topology,
1028 blacklist_transports) != GNUNET_SYSERR)
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Topology set up, now starting peers!\n");
1034 GNUNET_TESTING_daemons_continue_startup (pg);
1035 /* Set up task in case topology creation doesn't finish
1036 * within a reasonable amount of time */
1038 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &end_badly,
1039 "from continue startup (timeout)");
1044 GNUNET_SCHEDULER_add_now (&end_badly,
1045 "from create topology (bad return)");
1052 run (void *cls, char *const *args, const char *cfgfile,
1053 const struct GNUNET_CONFIGURATION_Handle *cfg)
1056 char *connect_topology_str;
1057 char *blacklist_topology_str;
1058 char *connect_topology_option_str;
1059 char *connect_topology_option_modifier_string;
1063 dotOutFile = fopen (dotOutFileName, "w");
1064 if (dotOutFile != NULL)
1066 fprintf (dotOutFile, "strict graph G {\n");
1070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071 "Starting daemons based on config file %s\n", cfgfile);
1075 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
1083 GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
1085 (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
1087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1088 "Invalid topology `%s' given for section %s option %s\n",
1089 topology_str, "TESTING", "TOPOLOGY");
1090 topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
1094 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1096 &connect_topology_str)) &&
1098 GNUNET_TESTING_topology_get (&connection_topology,
1099 connect_topology_str)))
1101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1102 "Invalid connect topology `%s' given for section %s option %s\n",
1103 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
1105 GNUNET_free_non_null (connect_topology_str);
1107 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1108 "connect_topology_option",
1109 &connect_topology_option_str)) &&
1111 GNUNET_TESTING_topology_option_get (&connect_topology_option,
1112 connect_topology_option_str)))
1114 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1115 "Invalid connect topology option `%s' given for section %s option %s\n",
1116 connect_topology_option_str, "TESTING",
1117 "CONNECT_TOPOLOGY_OPTION");
1118 connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
1120 GNUNET_free_non_null (connect_topology_option_str);
1122 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1123 "connect_topology_option_modifier",
1124 &connect_topology_option_modifier_string))
1127 (connect_topology_option_modifier_string, "%lf",
1128 &connect_topology_option_modifier) != 1)
1130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1132 ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
1133 connect_topology_option_modifier_string,
1134 "connect_topology_option_modifier", "TESTING");
1136 GNUNET_free (connect_topology_option_modifier_string);
1140 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1141 "blacklist_transports",
1142 &blacklist_transports))
1143 blacklist_transports = NULL;
1146 GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
1147 "blacklist_topology",
1148 &blacklist_topology_str)) &&
1150 GNUNET_TESTING_topology_get (&blacklist_topology,
1151 blacklist_topology_str)))
1153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1154 "Invalid topology `%s' given for section %s option %s\n",
1155 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
1157 GNUNET_free_non_null (topology_str);
1158 GNUNET_free_non_null (blacklist_topology_str);
1159 if (GNUNET_SYSERR ==
1160 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
1162 num_peers = DEFAULT_NUM_PEERS;
1164 if (GNUNET_SYSERR ==
1165 GNUNET_CONFIGURATION_get_value_number (cfg, "testing",
1166 "additional_messages",
1167 &num_additional_messages))
1168 num_additional_messages = DEFAULT_ADDITIONAL_MESSAGES;
1172 GNUNET_assert (num_peers > 0 && num_peers < (unsigned int) -1);
1173 peers_left = num_peers;
1175 /* Set up a task to end testing if peer start fails */
1177 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1178 (GNUNET_TIME_UNIT_MINUTES, 5), &end_badly,
1179 "didn't start all daemons in reasonable amount of time!!!");
1181 peer_daemon_hash = GNUNET_CONTAINER_multihashmap_create (peers_left);
1182 pg = GNUNET_TESTING_daemons_start (cfg, peers_left, /* Total number of peers */
1183 peers_left, /* Number of outstanding connections */
1184 peers_left, /* Number of parallel ssh connections, or peers being started at once */
1185 TIMEOUT, &hostkey_callback, NULL,
1186 &peers_started_callback, NULL,
1187 &topology_callback, NULL, NULL);
1196 char *const argv[] = { "test-transport-dv",
1198 "test_transport_dv_data.conf",
1204 struct GNUNET_GETOPT_CommandLineOption options[] = {
1205 GNUNET_GETOPT_OPTION_END
1208 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
1209 "test-transport-dv", "nohelp", options, &run, &ok);
1210 if (ret != GNUNET_OK)
1212 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1213 "`test-transport-dv': Failed with error code %d\n", ret);
1219 main (int argc, char *argv[])
1223 GNUNET_log_setup ("test-transport-dv",
1232 * Need to remove base directory, subdirectories taken care
1233 * of by the testing framework.
1235 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
1237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1238 "Failed to remove testing directory %s\n", test_directory);
1243 /* end of test_transport_api_dv.c */