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_YES
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;
86 struct GNUNET_TestMessage
89 * Header of the message
91 struct GNUNET_MessageHeader header;
94 * Unique identifier for this message.
99 struct TestMessageContext
101 /* This is a linked list */
102 struct TestMessageContext *next;
104 /* Handle to the sending peer core */
105 struct GNUNET_CORE_Handle *peer1handle;
107 /* Handle to the receiving peer core */
108 struct GNUNET_CORE_Handle *peer2handle;
110 /* Handle to the sending peer daemon */
111 struct GNUNET_TESTING_Daemon *peer1;
113 /* Handle to the receiving peer daemon */
114 struct GNUNET_TESTING_Daemon *peer2;
116 /* Identifier for this message, so we don't disconnect other peers! */
119 /* Task for disconnecting cores, allow task to be cancelled on shutdown */
120 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
124 static struct TestMessageContext *test_messages;
129 GNUNET_assert (pg != NULL);
130 struct TestMessageContext *pos;
131 struct TestMessageContext *free_pos;
133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
134 "Called finish testing, stopping daemons.\n");
141 if (pos->peer1handle != NULL)
143 GNUNET_CORE_disconnect(pos->peer1handle);
144 pos->peer1handle = NULL;
146 if (pos->peer2handle != NULL)
148 GNUNET_CORE_disconnect(pos->peer2handle);
149 pos->peer2handle = NULL;
153 if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
155 GNUNET_SCHEDULER_cancel(sched, free_pos->disconnect_task);
157 GNUNET_free(free_pos);
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_failed, transmit_ready_called);
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Calling daemons_stop\n");
168 GNUNET_TESTING_daemons_stop (pg);
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "daemons_stop finished\n");
173 if (dotOutFile != NULL)
175 fprintf(dotOutFile, "}");
184 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
186 struct TestMessageContext *pos = cls;
188 /* Disconnect from the respective cores */
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Disconnecting from peer 1 `%4s'\n", GNUNET_i2s (&pos->peer1->id));
193 if (pos->peer1handle != NULL)
194 GNUNET_CORE_disconnect(pos->peer1handle);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "Disconnecting from peer 2 `%4s'\n", GNUNET_i2s (&pos->peer2->id));
199 if (pos->peer2handle != NULL)
200 GNUNET_CORE_disconnect(pos->peer2handle);
201 /* Set handles to NULL so test case can be ended properly */
202 pos->peer1handle = NULL;
203 pos->peer2handle = NULL;
204 pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
205 /* Decrement total connections so new can be established */
206 total_server_connections -= 2;
210 process_mtype (void *cls,
211 const struct GNUNET_PeerIdentity *peer,
212 const struct GNUNET_MessageHeader *message,
213 struct GNUNET_TIME_Relative latency,
216 struct TestMessageContext *pos = cls;
217 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
218 if (pos->uid != ntohl(msg->uid))
221 total_messages_received++;
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Received message from `%4s', type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
229 if (total_messages_received == expected_messages)
231 GNUNET_SCHEDULER_cancel (sched, die_task);
232 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
236 pos->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &disconnect_cores, pos);
243 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
247 "End badly was called (%s)... stopping daemons.\n", msg);
248 struct TestMessageContext *pos;
249 struct TestMessageContext *free_pos;
254 if (pos->peer1handle != NULL)
256 GNUNET_CORE_disconnect(pos->peer1handle);
257 pos->peer1handle = NULL;
259 if (pos->peer2handle != NULL)
261 GNUNET_CORE_disconnect(pos->peer2handle);
262 pos->peer2handle = NULL;
266 GNUNET_free(free_pos);
271 GNUNET_TESTING_daemons_stop (pg);
272 ok = 7331; /* Opposite of leet */
275 ok = 401; /* Never got peers started */
277 if (dotOutFile != NULL)
279 fprintf(dotOutFile, "}");
287 transmit_ready (void *cls, size_t size, void *buf)
289 struct GNUNET_TestMessage *m;
290 struct TestMessageContext *pos = cls;
292 GNUNET_assert (buf != NULL);
293 m = (struct GNUNET_TestMessage *) buf;
294 m->header.type = htons (MTYPE);
295 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
296 m->uid = htonl(pos->uid);
297 transmit_ready_called++;
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "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);
302 return sizeof (struct GNUNET_TestMessage);
306 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
310 static struct GNUNET_CORE_MessageHandler handlers[] = {
311 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
316 init_notify_peer2 (void *cls,
317 struct GNUNET_CORE_Handle *server,
318 const struct GNUNET_PeerIdentity *my_identity,
319 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
321 struct TestMessageContext *pos = cls;
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Core connection to `%4s' established, scheduling message send\n",
326 GNUNET_i2s (my_identity));
328 total_server_connections++;
330 if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
334 sizeof (struct GNUNET_TestMessage),
335 &transmit_ready, pos))
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
339 GNUNET_i2s (&pos->peer2->id));
340 transmit_ready_failed++;
344 transmit_ready_scheduled++;
350 init_notify_peer1 (void *cls,
351 struct GNUNET_CORE_Handle *server,
352 const struct GNUNET_PeerIdentity *my_identity,
353 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
355 struct TestMessageContext *pos = cls;
356 total_server_connections++;
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Core connection to `%4s' established, setting up handles\n",
361 GNUNET_i2s (my_identity));
365 * Connect to the receiving peer
367 pos->peer2handle = GNUNET_CORE_connect (sched,
375 GNUNET_YES, NULL, GNUNET_YES, handlers);
381 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
383 struct TestMessageContext *pos = cls;
385 if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) || (cls == NULL))
388 if (die_task == GNUNET_SCHEDULER_NO_TASK)
390 die_task = GNUNET_SCHEDULER_add_delayed (sched,
392 &end_badly, "from create topology (timeout)");
395 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
397 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
398 &send_test_messages, pos);
399 return; /* Otherwise we'll double schedule messages here! */
403 * Connect to the sending peer
405 pos->peer1handle = GNUNET_CORE_connect (sched,
413 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
415 GNUNET_assert(pos->peer1handle != NULL);
417 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
419 GNUNET_SCHEDULER_add_now (sched,
420 &send_test_messages, pos->next);
424 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
425 &send_test_messages, pos->next);
431 topology_callback (void *cls,
432 const struct GNUNET_PeerIdentity *first,
433 const struct GNUNET_PeerIdentity *second,
434 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
435 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
436 struct GNUNET_TESTING_Daemon *first_daemon,
437 struct GNUNET_TESTING_Daemon *second_daemon,
440 struct TestMessageContext *temp_context;
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
446 first_daemon->shortname,
447 second_daemon->shortname);
449 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
450 temp_context->peer1 = first_daemon;
451 temp_context->peer2 = second_daemon;
452 temp_context->next = test_messages;
453 temp_context->uid = total_connections;
454 temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
455 test_messages = temp_context;
458 if (dotOutFile != NULL)
459 fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
464 failed_connections++;
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
466 first_daemon->shortname,
467 second_daemon->shortname, emsg);
471 if (total_connections == expected_connections)
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Created %d total connections, which is our target number! Calling send messages.\n",
479 GNUNET_SCHEDULER_cancel (sched, die_task);
480 die_task = GNUNET_SCHEDULER_NO_TASK;
481 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
483 else if (total_connections + failed_connections == expected_connections)
485 if (failed_connections < (unsigned int)(fail_percentage * total_connections))
487 GNUNET_SCHEDULER_cancel (sched, die_task);
488 die_task = GNUNET_SCHEDULER_NO_TASK;
489 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
493 GNUNET_SCHEDULER_cancel (sched, die_task);
494 die_task = GNUNET_SCHEDULER_add_now (sched,
495 &end_badly, "from topology_callback (too many failed connections)");
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
503 total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
512 expected_connections = -1;
513 if ((pg != NULL) && (peers_left == 0))
515 /* create_topology will read the topology information from
516 the config already contained in the peer group, so should
517 we have create_topology called from start peers? I think
518 maybe this way is best so that the client can know both
519 when peers are started, and when they are connected.
521 expected_connections = GNUNET_TESTING_create_topology (pg);
523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
524 "Have %d expected connections\n", expected_connections);
528 GNUNET_SCHEDULER_cancel (sched, die_task);
529 if (expected_connections == GNUNET_SYSERR)
531 die_task = GNUNET_SCHEDULER_add_now (sched,
532 &end_badly, "from create topology (bad return)");
534 die_task = GNUNET_SCHEDULER_add_delayed (sched,
536 &end_badly, "from create topology (timeout)");
542 const struct GNUNET_PeerIdentity *id,
543 const struct GNUNET_CONFIGURATION_Handle *cfg,
544 struct GNUNET_TESTING_Daemon *d, const char *emsg)
546 GNUNET_assert (id != NULL);
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
549 (num_peers - peers_left) + 1, num_peers);
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
556 "All %d daemons started, now creating topology!\n",
559 GNUNET_SCHEDULER_cancel (sched, die_task);
560 /* Set up task in case topology creation doesn't finish
561 * within a reasonable amount of time */
562 die_task = GNUNET_SCHEDULER_add_delayed (sched,
563 GNUNET_TIME_relative_multiply
564 (GNUNET_TIME_UNIT_MINUTES, 5),
565 &end_badly, "from my_cb");
574 struct GNUNET_SCHEDULER_Handle *s,
576 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
581 dotOutFile = fopen (dotOutFileName, "w");
582 if (dotOutFile != NULL)
584 fprintf (dotOutFile, "strict graph G {\n");
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "Starting daemons based on config file %s\n", cfgfile);
592 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
594 num_peers = DEFAULT_NUM_PEERS;
598 peers_left = num_peers;
600 /* Set up a task to end testing if peer start fails */
601 die_task = GNUNET_SCHEDULER_add_delayed (sched,
602 GNUNET_TIME_relative_multiply
603 (GNUNET_TIME_UNIT_MINUTES, 5),
604 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
606 pg = GNUNET_TESTING_daemons_start (sched, cfg,
607 peers_left, &my_cb, NULL,
608 &topology_callback, NULL, NULL);
616 char *config_file_name;
617 GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
618 GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
619 char *const argv[] = {binary_name,
627 struct GNUNET_GETOPT_CommandLineOption options[] = {
628 GNUNET_GETOPT_OPTION_END
630 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
631 argv, binary_name, "nohelp",
633 GNUNET_free(binary_name);
634 GNUNET_free(config_file_name);
639 main (int argc, char *argv[])
642 char *binary_start_pos;
643 char *our_binary_name;
645 binary_start_pos = rindex(argv[0], '/');
646 topology_string = strstr (binary_start_pos,
648 GNUNET_assert (topology_string != NULL);
650 topology_string = strstr (topology_string, "_");
651 GNUNET_assert (topology_string != NULL);
654 GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
655 GNUNET_log_setup (our_binary_name,
665 * Need to remove base directory, subdirectories taken care
666 * of by the testing framework.
668 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
669 GNUNET_free(our_binary_name);
673 /* end of test_testing_group.c */