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 give up on connecting the peers?
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
35 #define DEFAULT_NUM_PEERS 4;
39 static unsigned long long num_peers;
41 static unsigned int total_connections;
43 static unsigned int total_server_connections;
45 static unsigned int total_messages_received;
47 static unsigned int expected_messages;
49 static unsigned int expected_connections;
51 static int peers_left;
53 static struct GNUNET_TESTING_PeerGroup *pg;
55 static struct GNUNET_SCHEDULER_Handle *sched;
57 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
59 GNUNET_SCHEDULER_TaskIdentifier die_task;
61 static char *dotOutFileName = "topology.dot";
63 static FILE *dotOutFile;
65 static char *topology_string;
67 static int transmit_ready_scheduled;
69 static int transmit_ready_called;
71 struct TestMessageContext *global_pos;
75 struct TestMessageContext
77 /* This is a linked list */
78 struct TestMessageContext *next;
80 /* Handle to the sending peer core */
81 struct GNUNET_CORE_Handle *peer1handle;
83 /* Handle to the receiving peer core */
84 struct GNUNET_CORE_Handle *peer2handle;
86 /* Handle to the sending peer daemon */
87 struct GNUNET_TESTING_Daemon *peer1;
89 /* Handle to the receiving peer daemon */
90 struct GNUNET_TESTING_Daemon *peer2;
92 /* Maintain some state */
99 struct Connection *next;
100 struct GNUNET_TESTING_Daemon *peer;
101 struct GNUNET_CORE_Handle *server;
104 static struct Connection *global_connections;
106 static struct TestMessageContext *test_messages;
111 GNUNET_assert (pg != NULL);
112 struct Connection *pos;
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115 "Called finish testing, stopping daemons.\n");
119 pos = global_connections;
122 if (pos->server != NULL)
124 GNUNET_CORE_disconnect(pos->server);
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "transmit_ready's scheduled %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_called);
135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136 "Calling daemons_stop\n");
138 GNUNET_TESTING_daemons_stop (pg);
140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
141 "daemons_stop finished\n");
143 if (dotOutFile != NULL)
145 fprintf(dotOutFile, "}");
153 process_mtype (void *cls,
154 const struct GNUNET_PeerIdentity *peer,
155 const struct GNUNET_MessageHeader *message,
156 struct GNUNET_TIME_Relative latency,
159 total_messages_received++;
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "Received message from `%4s', type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
167 if (total_messages_received == expected_messages)
169 GNUNET_SCHEDULER_cancel (sched, die_task);
170 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
176 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "End badly was called... stopping daemons.\n");
182 struct Connection *pos;
184 pos = global_connections;
187 if (pos->server != NULL)
189 GNUNET_CORE_disconnect(pos->server);
197 GNUNET_TESTING_daemons_stop (pg);
198 ok = 7331; /* Opposite of leet */
201 ok = 401; /* Never got peers started */
203 if (dotOutFile != NULL)
205 fprintf(dotOutFile, "}");
212 * Forward declaration.
215 transmit_ready (void *cls, size_t size, void *buf);
218 schedule_transmission (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
221 if (global_pos != NULL)
223 if (NULL == GNUNET_CORE_notify_transmit_ready (global_pos->peer1handle,
226 &global_pos->peer2->id,
227 sizeof (struct GNUNET_MessageHeader),
228 &transmit_ready, &global_pos->peer1->id))
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
232 GNUNET_i2s (&global_pos->peer2->id));
236 transmit_ready_scheduled++;
238 global_pos = global_pos->next;
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Transmit ready scheduled on %d messages\n",
244 transmit_ready_scheduled);
250 transmit_ready (void *cls, size_t size, void *buf)
252 struct GNUNET_MessageHeader *m;
253 struct GNUNET_PeerIdentity *peer = cls;
254 GNUNET_assert (buf != NULL);
255 m = (struct GNUNET_MessageHeader *) buf;
256 m->type = htons (MTYPE);
257 m->size = htons (sizeof (struct GNUNET_MessageHeader));
259 transmit_ready_called++;
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n", GNUNET_i2s(peer), transmit_ready_scheduled, transmit_ready_called);
264 GNUNET_SCHEDULER_add_now(sched, &schedule_transmission, NULL);
265 return sizeof (struct GNUNET_MessageHeader);
269 static struct GNUNET_CORE_MessageHandler handlers[] = {
270 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
277 send_test_messages ()
279 struct TestMessageContext *pos;
280 struct Connection *conn_pos;
281 die_task = GNUNET_SCHEDULER_add_delayed (sched,
283 &end_badly, "from send test messages");
290 conn_pos = global_connections;
292 while (conn_pos != NULL)
294 if (conn_pos->peer == pos->peer1)
296 pos->peer1handle = conn_pos->server;
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "Peer matched conn_count %d\n",
303 conn_pos = conn_pos->next;
305 GNUNET_assert(pos->peer1handle != NULL);
308 if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
312 sizeof (struct GNUNET_MessageHeader),
313 &transmit_ready, &pos->peer1->id))
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
317 GNUNET_i2s (&pos->peer2->id));
321 transmit_ready_scheduled++;
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Prepared %d messages\n",
331 global_pos = test_messages;
333 GNUNET_SCHEDULER_add_now(sched, &schedule_transmission, NULL);
339 init_notify (void *cls,
340 struct GNUNET_CORE_Handle *server,
341 const struct GNUNET_PeerIdentity *my_identity,
342 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
344 struct Connection *connection = cls;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Core connection to `%4s' established, setting up handles\n",
349 GNUNET_i2s (my_identity));
351 connection->server = server;
352 total_server_connections++;
354 if (total_server_connections == num_peers)
356 GNUNET_SCHEDULER_cancel(sched, die_task);
357 GNUNET_SCHEDULER_add_now(sched, &send_test_messages, NULL);
360 if (context->first_step_done == GNUNET_NO)
362 context->peer1handle = server;
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting core to peer 2\n");
366 context->first_step_done = GNUNET_YES;
368 GNUNET_CORE_connect (sched,
378 NULL, GNUNET_YES, handlers);
383 context->peer2handle = server;
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "Asking core (1) for transmission to peer `%4s'\n",
387 GNUNET_i2s (&context->peer2->id));
389 transmit_ready_scheduled++;
390 if (NULL == GNUNET_CORE_notify_transmit_ready (context->peer1->server,
394 sizeof (struct GNUNET_MessageHeader),
395 &transmit_ready, NULL))
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
399 GNUNET_i2s (&context->peer2->id));
410 struct Connection *new_connection;
412 struct GNUNET_TESTING_Daemon *temp_daemon;
413 die_task = GNUNET_SCHEDULER_add_delayed (sched,
415 &end_badly, "from setup_handlers");
419 * Set up a single handler for each peer
421 for (i = 0; i < num_peers; i++)
423 new_connection = GNUNET_malloc(sizeof(struct Connection));
424 temp_daemon = GNUNET_TESTING_daemon_get(pg, i);
425 new_connection->peer = temp_daemon;
426 new_connection->server = NULL;
427 new_connection->next = global_connections;
428 global_connections = new_connection;
430 GNUNET_CORE_connect (sched,
439 GNUNET_YES, NULL, GNUNET_YES, handlers);
446 topology_callback (void *cls,
447 const struct GNUNET_PeerIdentity *first,
448 const struct GNUNET_PeerIdentity *second,
449 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
450 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
451 struct GNUNET_TESTING_Daemon *first_daemon,
452 struct GNUNET_TESTING_Daemon *second_daemon,
455 struct TestMessageContext *temp_context;
460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
461 first_daemon->shortname,
462 second_daemon->shortname);
464 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
465 temp_context->first_step_done = 0;
466 temp_context->peer1handle = NULL;
467 temp_context->peer1 = first_daemon;
468 temp_context->peer2 = second_daemon;
469 temp_context->peer2handle = NULL;
470 temp_context->next = test_messages;
471 test_messages = temp_context;
474 if (dotOutFile != NULL)
475 fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error %s\n",
482 first_daemon->shortname,
483 second_daemon->shortname, emsg);
487 if (total_connections == expected_connections)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Created %d total connections, which is our target number! Calling send messages.\n",
495 GNUNET_SCHEDULER_cancel (sched, die_task);
496 die_task = GNUNET_SCHEDULER_add_now (sched, &setup_handlers, NULL);
501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
502 "Have %d total connections, Need %d\n",
503 total_connections, 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,
534 die_task = GNUNET_SCHEDULER_add_delayed (sched,
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 %d out of %d\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),
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),
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",
637 main (int argc, char *argv[])
640 char *binary_start_pos;
641 char *our_binary_name;
642 binary_start_pos = rindex(argv[0], '/');
643 if (strstr(binary_start_pos, "test_testing_topology_") == NULL)
645 return GNUNET_SYSERR;
648 topology_string = &binary_start_pos[23];
650 GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
651 GNUNET_log_setup (our_binary_name,
660 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
664 /* end of test_testing_group.c */