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 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;
254 struct GNUNET_PeerIdentity *peer = cls;
256 GNUNET_assert (buf != NULL);
257 m = (struct GNUNET_MessageHeader *) buf;
258 m->type = htons (MTYPE);
259 m->size = htons (sizeof (struct GNUNET_MessageHeader));
261 transmit_ready_called++;
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
264 "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);
266 GNUNET_SCHEDULER_add_now(sched, &schedule_transmission, NULL);
267 return sizeof (struct GNUNET_MessageHeader);
271 static struct GNUNET_CORE_MessageHandler handlers[] = {
272 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
279 send_test_messages ()
281 struct TestMessageContext *pos;
282 struct Connection *conn_pos;
283 die_task = GNUNET_SCHEDULER_add_delayed (sched,
285 &end_badly, "from send test messages");
292 conn_pos = global_connections;
294 while (conn_pos != NULL)
296 if (conn_pos->peer == pos->peer1)
298 pos->peer1handle = conn_pos->server;
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
300 "Peer matched conn_count %d\n",
305 conn_pos = conn_pos->next;
307 GNUNET_assert(pos->peer1handle != NULL);
310 if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
314 sizeof (struct GNUNET_MessageHeader),
315 &transmit_ready, &pos->peer1->id))
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
319 GNUNET_i2s (&pos->peer2->id));
323 transmit_ready_scheduled++;
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Prepared %d messages\n",
333 global_pos = test_messages;
335 GNUNET_SCHEDULER_add_now(sched, &schedule_transmission, NULL);
341 init_notify (void *cls,
342 struct GNUNET_CORE_Handle *server,
343 const struct GNUNET_PeerIdentity *my_identity,
344 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
346 struct Connection *connection = cls;
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Core connection to `%4s' established, setting up handles\n",
351 GNUNET_i2s (my_identity));
353 connection->server = server;
354 total_server_connections++;
356 if (total_server_connections == num_peers)
358 GNUNET_SCHEDULER_cancel(sched, die_task);
359 GNUNET_SCHEDULER_add_now(sched, &send_test_messages, NULL);
362 if (context->first_step_done == GNUNET_NO)
364 context->peer1handle = server;
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting core to peer 2\n");
368 context->first_step_done = GNUNET_YES;
370 GNUNET_CORE_connect (sched,
380 NULL, GNUNET_YES, handlers);
385 context->peer2handle = server;
387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388 "Asking core (1) for transmission to peer `%4s'\n",
389 GNUNET_i2s (&context->peer2->id));
391 transmit_ready_scheduled++;
392 if (NULL == GNUNET_CORE_notify_transmit_ready (context->peer1->server,
396 sizeof (struct GNUNET_MessageHeader),
397 &transmit_ready, NULL))
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
401 GNUNET_i2s (&context->peer2->id));
412 struct Connection *new_connection;
414 struct GNUNET_TESTING_Daemon *temp_daemon;
415 die_task = GNUNET_SCHEDULER_add_delayed (sched,
417 &end_badly, "from setup_handlers");
421 * Set up a single handler for each peer
423 for (i = 0; i < num_peers; i++)
425 new_connection = GNUNET_malloc(sizeof(struct Connection));
426 temp_daemon = GNUNET_TESTING_daemon_get(pg, i);
427 new_connection->peer = temp_daemon;
428 new_connection->server = NULL;
429 new_connection->next = global_connections;
430 global_connections = new_connection;
432 GNUNET_CORE_connect (sched,
441 GNUNET_YES, NULL, GNUNET_YES, handlers);
448 topology_callback (void *cls,
449 const struct GNUNET_PeerIdentity *first,
450 const struct GNUNET_PeerIdentity *second,
451 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
452 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
453 struct GNUNET_TESTING_Daemon *first_daemon,
454 struct GNUNET_TESTING_Daemon *second_daemon,
457 struct TestMessageContext *temp_context;
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
463 first_daemon->shortname,
464 second_daemon->shortname);
466 temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
467 temp_context->first_step_done = 0;
468 temp_context->peer1handle = NULL;
469 temp_context->peer1 = first_daemon;
470 temp_context->peer2 = second_daemon;
471 temp_context->peer2handle = NULL;
472 temp_context->next = test_messages;
473 test_messages = temp_context;
476 if (dotOutFile != NULL)
477 fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error %s\n",
484 first_daemon->shortname,
485 second_daemon->shortname, emsg);
489 if (total_connections == expected_connections)
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
493 "Created %d total connections, which is our target number! Calling send messages.\n",
497 GNUNET_SCHEDULER_cancel (sched, die_task);
498 die_task = GNUNET_SCHEDULER_add_now (sched, &setup_handlers, NULL);
503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504 "Have %d total connections, Need %d\n",
505 total_connections, 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);
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,
536 die_task = GNUNET_SCHEDULER_add_delayed (sched,
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 %d out of %d\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),
576 struct GNUNET_SCHEDULER_Handle *s,
578 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
583 dotOutFile = fopen (dotOutFileName, "w");
584 if (dotOutFile != NULL)
586 fprintf (dotOutFile, "strict graph G {\n");
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Starting daemons based on config file %s\n", cfgfile);
594 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
596 num_peers = DEFAULT_NUM_PEERS;
600 peers_left = num_peers;
602 /* Set up a task to end testing if peer start fails */
603 die_task = GNUNET_SCHEDULER_add_delayed (sched,
604 GNUNET_TIME_relative_multiply
605 (GNUNET_TIME_UNIT_MINUTES, 5),
608 pg = GNUNET_TESTING_daemons_start (sched, cfg,
609 peers_left, &my_cb, NULL,
610 &topology_callback, NULL, NULL);
618 char *config_file_name;
619 GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
620 GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
621 char *const argv[] = {binary_name,
629 struct GNUNET_GETOPT_CommandLineOption options[] = {
630 GNUNET_GETOPT_OPTION_END
632 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
633 argv, binary_name, "nohelp",
639 main (int argc, char *argv[])
642 char *binary_start_pos;
643 char *our_binary_name;
644 binary_start_pos = rindex(argv[0], '/');
645 if (strstr(binary_start_pos, "test_testing_topology_") == NULL)
647 return GNUNET_SYSERR;
650 topology_string = &binary_start_pos[23];
652 GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
653 GNUNET_log_setup (our_binary_name,
662 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
666 /* end of test_testing_group.c */