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! */
121 static struct TestMessageContext *test_messages;
126 GNUNET_assert (pg != NULL);
127 struct TestMessageContext *pos;
128 struct TestMessageContext *free_pos;
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Called finish testing, stopping daemons.\n");
138 if (pos->peer1handle != NULL)
140 GNUNET_CORE_disconnect(pos->peer1handle);
141 pos->peer1handle = NULL;
143 if (pos->peer2handle != NULL)
145 GNUNET_CORE_disconnect(pos->peer2handle);
146 pos->peer2handle = NULL;
150 GNUNET_free(free_pos);
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
154 "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_failed, transmit_ready_called);
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Calling daemons_stop\n");
161 GNUNET_TESTING_daemons_stop (pg);
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "daemons_stop finished\n");
166 if (dotOutFile != NULL)
168 fprintf(dotOutFile, "}");
177 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
179 struct TestMessageContext *pos = cls;
181 /* Disconnect from the respective cores */
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Disconnecting from peer 1 `%4s'\n", GNUNET_i2s (&pos->peer1->id));
186 if (pos->peer1handle != NULL)
187 GNUNET_CORE_disconnect(pos->peer1handle);
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Disconnecting from peer 2 `%4s'\n", GNUNET_i2s (&pos->peer2->id));
192 if (pos->peer2handle != NULL)
193 GNUNET_CORE_disconnect(pos->peer2handle);
194 /* Set handles to NULL so test case can be ended properly */
195 pos->peer1handle = NULL;
196 pos->peer2handle = NULL;
197 /* Decrement total connections so new can be established */
198 total_server_connections -= 2;
202 process_mtype (void *cls,
203 const struct GNUNET_PeerIdentity *peer,
204 const struct GNUNET_MessageHeader *message,
205 struct GNUNET_TIME_Relative latency,
208 struct TestMessageContext *pos = cls;
209 struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
210 if (pos->uid != ntohl(msg->uid))
213 total_messages_received++;
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Received message from `%4s', type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
221 if (total_messages_received == expected_messages)
223 GNUNET_SCHEDULER_cancel (sched, die_task);
224 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
228 GNUNET_SCHEDULER_add_now(sched, &disconnect_cores, pos);
235 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
238 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
239 "End badly was called (%s)... stopping daemons.\n", msg);
240 struct TestMessageContext *pos;
241 struct TestMessageContext *free_pos;
246 if (pos->peer1handle != NULL)
248 GNUNET_CORE_disconnect(pos->peer1handle);
249 pos->peer1handle = NULL;
251 if (pos->peer2handle != NULL)
253 GNUNET_CORE_disconnect(pos->peer2handle);
254 pos->peer2handle = NULL;
258 GNUNET_free(free_pos);
263 GNUNET_TESTING_daemons_stop (pg);
264 ok = 7331; /* Opposite of leet */
267 ok = 401; /* Never got peers started */
269 if (dotOutFile != NULL)
271 fprintf(dotOutFile, "}");
279 transmit_ready (void *cls, size_t size, void *buf)
281 struct GNUNET_TestMessage *m;
282 struct TestMessageContext *pos = cls;
284 GNUNET_assert (buf != NULL);
285 m = (struct GNUNET_TestMessage *) buf;
286 m->header.type = htons (MTYPE);
287 m->header.size = htons (sizeof (struct GNUNET_TestMessage));
288 m->uid = htonl(pos->uid);
289 transmit_ready_called++;
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "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);
294 return sizeof (struct GNUNET_TestMessage);
298 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
302 static struct GNUNET_CORE_MessageHandler handlers[] = {
303 {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
308 init_notify_peer2 (void *cls,
309 struct GNUNET_CORE_Handle *server,
310 const struct GNUNET_PeerIdentity *my_identity,
311 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
313 struct TestMessageContext *pos = cls;
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Core connection to `%4s' established, scheduling message send\n",
318 GNUNET_i2s (my_identity));
320 total_server_connections++;
322 if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
326 sizeof (struct GNUNET_TestMessage),
327 &transmit_ready, pos))
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
331 GNUNET_i2s (&pos->peer2->id));
332 transmit_ready_failed++;
336 transmit_ready_scheduled++;
342 init_notify_peer1 (void *cls,
343 struct GNUNET_CORE_Handle *server,
344 const struct GNUNET_PeerIdentity *my_identity,
345 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
347 struct TestMessageContext *pos = cls;
348 total_server_connections++;
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Core connection to `%4s' established, setting up handles\n",
353 GNUNET_i2s (my_identity));
357 * Connect to the receiving peer
359 pos->peer2handle = GNUNET_CORE_connect (sched,
367 GNUNET_YES, NULL, GNUNET_YES, handlers);
373 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
375 struct TestMessageContext *pos = cls;
377 if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) || (cls == NULL))
380 if (die_task == GNUNET_SCHEDULER_NO_TASK)
382 die_task = GNUNET_SCHEDULER_add_delayed (sched,
384 &end_badly, "from create topology (timeout)");
387 if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
389 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
390 &send_test_messages, pos);
391 return; /* Otherwise we'll double schedule messages here! */
395 * Connect to the sending peer
397 pos->peer1handle = GNUNET_CORE_connect (sched,
405 GNUNET_NO, NULL, GNUNET_NO, no_handlers);
407 GNUNET_assert(pos->peer1handle != NULL);
409 if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
411 GNUNET_SCHEDULER_add_now (sched,
412 &send_test_messages, pos->next);
416 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
417 &send_test_messages, pos->next);
423 topology_callback (void *cls,
424 const struct GNUNET_PeerIdentity *first,
425 const struct GNUNET_PeerIdentity *second,
426 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
427 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
428 struct GNUNET_TESTING_Daemon *first_daemon,
429 struct GNUNET_TESTING_Daemon *second_daemon,
432 struct TestMessageContext *temp_context;
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
438 first_daemon->shortname,
439 second_daemon->shortname);
441 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
442 temp_context->peer1 = first_daemon;
443 temp_context->peer2 = second_daemon;
444 temp_context->next = test_messages;
445 temp_context->uid = total_connections;
446 test_messages = temp_context;
449 if (dotOutFile != NULL)
450 fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
455 failed_connections++;
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
457 first_daemon->shortname,
458 second_daemon->shortname, emsg);
462 if (total_connections == expected_connections)
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Created %d total connections, which is our target number! Calling send messages.\n",
470 GNUNET_SCHEDULER_cancel (sched, die_task);
471 die_task = GNUNET_SCHEDULER_NO_TASK;
472 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
474 else if (total_connections + failed_connections == expected_connections)
476 if (failed_connections < (unsigned int)(fail_percentage * total_connections))
478 GNUNET_SCHEDULER_cancel (sched, die_task);
479 die_task = GNUNET_SCHEDULER_NO_TASK;
480 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
484 GNUNET_SCHEDULER_cancel (sched, die_task);
485 die_task = GNUNET_SCHEDULER_add_now (sched,
486 &end_badly, "from topology_callback (too many failed connections)");
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
494 total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
503 expected_connections = -1;
504 if ((pg != NULL) && (peers_left == 0))
506 /* create_topology will read the topology information from
507 the config already contained in the peer group, so should
508 we have create_topology called from start peers? I think
509 maybe this way is best so that the client can know both
510 when peers are started, and when they are connected.
512 expected_connections = GNUNET_TESTING_create_topology (pg);
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
515 "Have %d expected connections\n", expected_connections);
519 GNUNET_SCHEDULER_cancel (sched, die_task);
520 if (expected_connections == GNUNET_SYSERR)
522 die_task = GNUNET_SCHEDULER_add_now (sched,
523 &end_badly, "from create topology (bad return)");
525 die_task = GNUNET_SCHEDULER_add_delayed (sched,
527 &end_badly, "from create topology (timeout)");
533 const struct GNUNET_PeerIdentity *id,
534 const struct GNUNET_CONFIGURATION_Handle *cfg,
535 struct GNUNET_TESTING_Daemon *d, const char *emsg)
537 GNUNET_assert (id != NULL);
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
540 (num_peers - peers_left) + 1, num_peers);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "All %d daemons started, now creating topology!\n",
550 GNUNET_SCHEDULER_cancel (sched, die_task);
551 /* Set up task in case topology creation doesn't finish
552 * within a reasonable amount of time */
553 die_task = GNUNET_SCHEDULER_add_delayed (sched,
554 GNUNET_TIME_relative_multiply
555 (GNUNET_TIME_UNIT_MINUTES, 5),
556 &end_badly, "from my_cb");
565 struct GNUNET_SCHEDULER_Handle *s,
567 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
572 dotOutFile = fopen (dotOutFileName, "w");
573 if (dotOutFile != NULL)
575 fprintf (dotOutFile, "strict graph G {\n");
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Starting daemons based on config file %s\n", cfgfile);
583 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
585 num_peers = DEFAULT_NUM_PEERS;
589 peers_left = num_peers;
591 /* Set up a task to end testing if peer start fails */
592 die_task = GNUNET_SCHEDULER_add_delayed (sched,
593 GNUNET_TIME_relative_multiply
594 (GNUNET_TIME_UNIT_MINUTES, 5),
595 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
597 pg = GNUNET_TESTING_daemons_start (sched, cfg,
598 peers_left, &my_cb, NULL,
599 &topology_callback, NULL, NULL);
607 char *config_file_name;
608 GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
609 GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
610 char *const argv[] = {binary_name,
618 struct GNUNET_GETOPT_CommandLineOption options[] = {
619 GNUNET_GETOPT_OPTION_END
621 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
622 argv, binary_name, "nohelp",
624 GNUNET_free(binary_name);
625 GNUNET_free(config_file_name);
630 main (int argc, char *argv[])
633 char *binary_start_pos;
634 char *our_binary_name;
636 binary_start_pos = rindex(argv[0], '/');
637 topology_string = strstr (binary_start_pos,
639 GNUNET_assert (topology_string != NULL);
641 topology_string = strstr (topology_string, "_");
642 GNUNET_assert (topology_string != NULL);
645 GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
646 GNUNET_log_setup (our_binary_name,
656 * Need to remove base directory, subdirectories taken care
657 * of by the testing framework.
659 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
660 GNUNET_free(our_binary_name);
664 /* end of test_testing_group.c */