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 2, 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 testing/test_testing_topology.c
22 * @brief base testcase for testing all the topologies provided
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
28 #define VERBOSE GNUNET_NO
31 * How long until we fail the whole testcase?
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
36 * How long until we give up on connecting the peers?
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
40 #define DEFAULT_NUM_PEERS 4
42 #define MAX_OUTSTANDING_CONNECTIONS 300
44 static float fail_percentage = 0.05;
48 static unsigned long long num_peers;
50 static unsigned int total_connections;
52 static unsigned int failed_connections;
54 static unsigned int total_server_connections;
56 static unsigned int total_messages_received;
58 static unsigned int expected_messages;
60 static unsigned int expected_connections;
62 static unsigned long long peers_left;
64 static struct GNUNET_TESTING_PeerGroup *pg;
66 static struct GNUNET_SCHEDULER_Handle *sched;
68 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
70 GNUNET_SCHEDULER_TaskIdentifier die_task;
72 static char *dotOutFileName = "topology.dot";
74 static FILE *dotOutFile;
76 static char *topology_string;
78 static int transmit_ready_scheduled;
80 static int transmit_ready_failed;
82 static int transmit_ready_called;
84 static enum GNUNET_TESTING_Topology topology;
88 struct GNUNET_TestMessage
91 * Header of the message
93 struct GNUNET_MessageHeader header;
96 * Unique identifier for this message.
101 struct TestMessageContext
103 /* This is a linked list */
104 struct TestMessageContext *next;
106 /* Handle to the sending peer core */
107 struct GNUNET_CORE_Handle *peer1handle;
109 /* Handle to the receiving peer core */
110 struct GNUNET_CORE_Handle *peer2handle;
112 /* Handle to the sending peer daemon */
113 struct GNUNET_TESTING_Daemon *peer1;
115 /* Handle to the receiving peer daemon */
116 struct GNUNET_TESTING_Daemon *peer2;
118 /* Identifier for this message, so we don't disconnect other peers! */
121 /* Task for disconnecting cores, allow task to be cancelled on shutdown */
122 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
126 static struct TestMessageContext *test_messages;
131 GNUNET_assert (pg != NULL);
132 struct TestMessageContext *pos;
133 struct TestMessageContext *free_pos;
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "Called finish testing, stopping daemons.\n");
143 if (pos->peer1handle != NULL)
145 GNUNET_CORE_disconnect(pos->peer1handle);
146 pos->peer1handle = NULL;
148 if (pos->peer2handle != NULL)
150 GNUNET_CORE_disconnect(pos->peer2handle);
151 pos->peer2handle = NULL;
155 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
157 GNUNET_SCHEDULER_cancel(sched, free_pos->disconnect_task);
159 GNUNET_free(free_pos);
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_failed, transmit_ready_called);
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Calling daemons_stop\n");
170 GNUNET_TESTING_daemons_stop (pg);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "daemons_stop finished\n");
175 if (dotOutFile != NULL)
177 fprintf(dotOutFile, "}");
186 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
188 struct TestMessageContext *pos = cls;
190 /* Disconnect from the respective cores */
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Disconnecting from peer 1 `%4s'\n", GNUNET_i2s (&pos->peer1->id));
195 if (pos->peer1handle != NULL)
196 GNUNET_CORE_disconnect(pos->peer1handle);
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199 "Disconnecting from peer 2 `%4s'\n", GNUNET_i2s (&pos->peer2->id));
201 if (pos->peer2handle != NULL)
202 GNUNET_CORE_disconnect(pos->peer2handle);
203 /* Set handles to NULL so test case can be ended properly */
204 pos->peer1handle = NULL;
205 pos->peer2handle = NULL;
206 pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
207 /* Decrement total connections so new can be established */
208 total_server_connections -= 2;
212 process_mtype (void *cls,
213 const struct GNUNET_PeerIdentity *peer,
214 const struct GNUNET_MessageHeader *message,
215 struct GNUNET_TIME_Relative latency,
218 struct TestMessageContext *pos = cls;
219 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
220 if (pos->uid != ntohl(msg->uid))
223 total_messages_received++;
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Received message from `%4s', type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
231 if (total_messages_received == expected_messages)
233 GNUNET_SCHEDULER_cancel (sched, die_task);
234 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
238 pos->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &disconnect_cores, pos);
245 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
249 "End badly was called (%s)... stopping daemons.\n", msg);
250 struct TestMessageContext *pos;
251 struct TestMessageContext *free_pos;
256 if (pos->peer1handle != NULL)
258 GNUNET_CORE_disconnect(pos->peer1handle);
259 pos->peer1handle = NULL;
261 if (pos->peer2handle != NULL)
263 GNUNET_CORE_disconnect(pos->peer2handle);
264 pos->peer2handle = NULL;
268 GNUNET_free(free_pos);
273 GNUNET_TESTING_daemons_stop (pg);
274 ok = 7331; /* Opposite of leet */
277 ok = 401; /* Never got peers started */
279 if (dotOutFile != NULL)
281 fprintf(dotOutFile, "}");
289 transmit_ready (void *cls, size_t size, void *buf)
291 struct GNUNET_TestMessage *m;
292 struct TestMessageContext *pos = cls;
294 GNUNET_assert (buf != NULL);
295 m = (struct GNUNET_TestMessage *) buf;
296 m->header.type = htons (MTYPE);
297 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
298 m->uid = htonl(pos->uid);
299 transmit_ready_called++;
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n", GNUNET_i2s(&pos->peer1->id), transmit_ready_scheduled, transmit_ready_called);
304 return sizeof (struct GNUNET_TestMessage);
308 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
312 static struct GNUNET_CORE_MessageHandler handlers[] = {
313 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
318 init_notify_peer2 (void *cls,
319 struct GNUNET_CORE_Handle *server,
320 const struct GNUNET_PeerIdentity *my_identity,
321 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
323 struct TestMessageContext *pos = cls;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Core connection to `%4s' established, scheduling message send\n",
328 GNUNET_i2s (my_identity));
330 total_server_connections++;
332 if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
336 sizeof (struct GNUNET_TestMessage),
337 &transmit_ready, pos))
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
341 GNUNET_i2s (&pos->peer2->id));
342 transmit_ready_failed++;
346 transmit_ready_scheduled++;
352 init_notify_peer1 (void *cls,
353 struct GNUNET_CORE_Handle *server,
354 const struct GNUNET_PeerIdentity *my_identity,
355 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
357 struct TestMessageContext *pos = cls;
358 total_server_connections++;
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Core connection to `%4s' established, setting up handles\n",
363 GNUNET_i2s (my_identity));
367 * Connect to the receiving peer
369 pos->peer2handle = GNUNET_CORE_connect (sched,
377 GNUNET_YES, NULL, GNUNET_YES, handlers);
383 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
385 struct TestMessageContext *pos = cls;
387 if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) || (cls == NULL))
390 if (die_task == GNUNET_SCHEDULER_NO_TASK)
392 die_task = GNUNET_SCHEDULER_add_delayed (sched,
394 &end_badly, "from create topology (timeout)");
397 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
399 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
400 &send_test_messages, pos);
401 return; /* Otherwise we'll double schedule messages here! */
405 * Connect to the sending peer
407 pos->peer1handle = GNUNET_CORE_connect (sched,
415 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
417 GNUNET_assert(pos->peer1handle != NULL);
419 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
421 GNUNET_SCHEDULER_add_now (sched,
422 &send_test_messages, pos->next);
426 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
427 &send_test_messages, pos->next);
433 topology_callback (void *cls,
434 const struct GNUNET_PeerIdentity *first,
435 const struct GNUNET_PeerIdentity *second,
436 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
437 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
438 struct GNUNET_TESTING_Daemon *first_daemon,
439 struct GNUNET_TESTING_Daemon *second_daemon,
442 struct TestMessageContext *temp_context;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
448 first_daemon->shortname,
449 second_daemon->shortname);
451 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
452 temp_context->peer1 = first_daemon;
453 temp_context->peer2 = second_daemon;
454 temp_context->next = test_messages;
455 temp_context->uid = total_connections;
456 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
457 test_messages = temp_context;
460 if (dotOutFile != NULL)
461 fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
466 failed_connections++;
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
468 first_daemon->shortname,
469 second_daemon->shortname, emsg);
473 if (total_connections == expected_connections)
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "Created %d total connections, which is our target number! Calling send messages.\n",
481 GNUNET_SCHEDULER_cancel (sched, die_task);
482 die_task = GNUNET_SCHEDULER_NO_TASK;
483 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
485 else if (total_connections + failed_connections == expected_connections)
487 if (failed_connections < (unsigned int)(fail_percentage * total_connections))
489 GNUNET_SCHEDULER_cancel (sched, die_task);
490 die_task = GNUNET_SCHEDULER_NO_TASK;
491 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
495 GNUNET_SCHEDULER_cancel (sched, die_task);
496 die_task = GNUNET_SCHEDULER_add_now (sched,
497 &end_badly, "from topology_callback (too many failed connections)");
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
505 total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
514 expected_connections = -1;
515 if ((pg != NULL) && (peers_left == 0))
517 /* create_topology will read the topology information from
518 the config already contained in the peer group, so should
519 we have create_topology called from start peers? I think
520 maybe this way is best so that the client can know both
521 when peers are started, and when they are connected.
523 expected_connections = GNUNET_TESTING_create_topology (pg, topology);
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Have %d expected connections\n", expected_connections);
530 GNUNET_SCHEDULER_cancel (sched, die_task);
531 if (expected_connections == GNUNET_SYSERR)
533 die_task = GNUNET_SCHEDULER_add_now (sched,
534 &end_badly, "from create topology (bad return)");
536 die_task = GNUNET_SCHEDULER_add_delayed (sched,
538 &end_badly, "from create topology (timeout)");
544 const struct GNUNET_PeerIdentity *id,
545 const struct GNUNET_CONFIGURATION_Handle *cfg,
546 struct GNUNET_TESTING_Daemon *d, const char *emsg)
548 GNUNET_assert (id != NULL);
550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
551 (num_peers - peers_left) + 1, num_peers);
557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558 "All %d daemons started, now creating topology!\n",
561 GNUNET_SCHEDULER_cancel (sched, die_task);
562 /* Set up task in case topology creation doesn't finish
563 * within a reasonable amount of time */
564 die_task = GNUNET_SCHEDULER_add_delayed (sched,
565 GNUNET_TIME_relative_multiply
566 (GNUNET_TIME_UNIT_MINUTES, 5),
567 &end_badly, "from my_cb");
576 struct GNUNET_SCHEDULER_Handle *s,
578 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
580 unsigned long long topology_num;
584 dotOutFile = fopen (dotOutFileName, "w");
585 if (dotOutFile != NULL)
587 fprintf (dotOutFile, "strict graph G {\n");
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "Starting daemons based on config file %s\n", cfgfile);
596 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
598 topology = topology_num;
601 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
603 num_peers = DEFAULT_NUM_PEERS;
607 peers_left = num_peers;
609 /* Set up a task to end testing if peer start fails */
610 die_task = GNUNET_SCHEDULER_add_delayed (sched,
611 GNUNET_TIME_relative_multiply
612 (GNUNET_TIME_UNIT_MINUTES, 5),
613 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
615 pg = GNUNET_TESTING_daemons_start (sched, cfg,
616 peers_left, &my_cb, NULL,
617 &topology_callback, NULL, NULL);
625 char *config_file_name;
626 GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
627 GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
628 char *const argv[] = {binary_name,
636 struct GNUNET_GETOPT_CommandLineOption options[] = {
637 GNUNET_GETOPT_OPTION_END
639 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
640 argv, binary_name, "nohelp",
642 GNUNET_free(binary_name);
643 GNUNET_free(config_file_name);
648 main (int argc, char *argv[])
651 char *binary_start_pos;
652 char *our_binary_name;
654 binary_start_pos = rindex(argv[0], '/');
655 topology_string = strstr (binary_start_pos,
657 GNUNET_assert (topology_string != NULL);
659 topology_string = strstr (topology_string, "_");
660 GNUNET_assert (topology_string != NULL);
663 GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
664 GNUNET_log_setup (our_binary_name,
674 * Need to remove base directory, subdirectories taken care
675 * of by the testing framework.
677 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
678 GNUNET_free(our_binary_name);
682 /* end of test_testing_group.c */