2 This file is part of GNUnet.
3 (C) 2012 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 3, 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 dht/test_dht_topo.c
23 * @brief Test for the dht service: store and retrieve in various topologies.
24 * Each peer stores it own ID in the DHT and then a different peer tries to
25 * retrieve that key from it. The GET starts after a first round of PUTS has
26 * been made. Periodically, each peer stores its ID into the DHT. If after
27 * a timeout no result has been returned, the test fails.
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_dht_service.h"
33 #define VERBOSE GNUNET_NO
35 #define REMOVE_DIR GNUNET_YES
38 * DIFFERENT TESTS TO RUN
44 * How long until we give up on connecting the peers?
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
48 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
50 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
63 * Total number of peers in the test.
65 static unsigned long long num_peers;
68 * Global configuration file
70 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
73 * Total number of currently running peers.
75 static unsigned long long peers_running;
78 * Total number of connections in the whole network.
80 static unsigned int total_connections;
83 * The currently running peer group.
85 static struct GNUNET_TESTING_PeerGroup *pg;
88 * File to report results to.
90 static struct GNUNET_DISK_FileHandle *output_file;
93 * File to log connection info, statistics to.
95 static struct GNUNET_DISK_FileHandle *data_file;
98 * Task called to disconnect peers.
100 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
103 * Task To perform tests
105 static GNUNET_SCHEDULER_TaskIdentifier test_task;
108 * Task to do DHT_puts
110 static GNUNET_SCHEDULER_TaskIdentifier put_task;
113 * Task called to shutdown test.
115 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
117 static char *topology_file;
119 struct GNUNET_TESTING_Daemon *d1;
121 struct GNUNET_TESTING_Daemon *d2;
123 struct GNUNET_DHT_Handle **hs;
125 struct GNUNET_DHT_GetHandle *get_h;
127 struct GNUNET_DHT_GetHandle *get_h_2;
129 struct GNUNET_DHT_GetHandle *get_h_far;
136 * Which topology are we to run
138 static int test_topology;
141 * Check whether peers successfully shut down.
144 shutdown_callback (void *cls, const char *emsg)
148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Shutdown of peers failed!\n");
153 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All peers successfully shut down!\n");
155 GNUNET_CONFIGURATION_destroy (testing_cfg);
160 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
164 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
166 GNUNET_SCHEDULER_cancel (disconnect_task);
167 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
170 if (data_file != NULL)
171 GNUNET_DISK_file_close (data_file);
172 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
177 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "disconnecting peers\n");
182 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
183 GNUNET_SCHEDULER_cancel (put_task);
185 GNUNET_DHT_get_stop (get_h);
187 GNUNET_DHT_get_stop (get_h_2);
188 if (NULL != get_h_far)
189 GNUNET_DHT_get_stop (get_h_far);
190 for (i = 0; i < num_peers; i++)
192 GNUNET_DHT_disconnect (hs[i]);
194 GNUNET_SCHEDULER_cancel (shutdown_handle);
195 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
199 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
200 const GNUNET_HashCode * key,
201 const struct GNUNET_PeerIdentity *get_path,
202 unsigned int get_path_length,
203 const struct GNUNET_PeerIdentity *put_path,
204 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
205 size_t size, const void *data)
209 if (sizeof (GNUNET_HashCode) == size)
211 const GNUNET_HashCode *h = data;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Contents: %s\n",
214 GNUNET_h2s_full (h));
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH: (get %u, put %u)\n",
222 get_path_length, put_path_length);
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " LOCAL\n");
224 for (i = get_path_length - 1; i >= 0; i--)
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
227 GNUNET_i2s (&get_path[i]));
229 for (i = put_path_length - 1; i >= 0; i--)
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
232 GNUNET_i2s (&put_path[i]));
238 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "FOUND 1!\n");
242 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "FOUND 2!\n");
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "FOUND FAR!\n");
251 if (TORUS == test_topology &&
252 (found_1 == 0 || found_2 == 0 || found_far == 0))
255 GNUNET_SCHEDULER_cancel (disconnect_task);
256 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
260 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 struct GNUNET_TESTING_Daemon *d;
263 struct GNUNET_TESTING_Daemon *d2;
264 struct GNUNET_TESTING_Daemon *d_far;
265 struct GNUNET_TESTING_Daemon *o;
266 struct GNUNET_TESTING_Daemon *aux;
268 const char *id_origin = "FC74";
269 const char *id_near = "9P6V";
270 const char *id_near2 = "2GDS";
271 const char *id_far = "KPST";
274 d = d2 = d_far = o = NULL;
275 found_1 = found_2 = found_far = 0;
276 if (LINE == test_topology)
278 o = GNUNET_TESTING_daemon_get (pg, 0);
279 d = GNUNET_TESTING_daemon_get (pg, 4);
281 else if (TORUS == test_topology)
283 for (i = 0; i < num_peers; i++)
285 aux = GNUNET_TESTING_daemon_get (pg, i);
286 id_aux = GNUNET_i2s (&aux->id);
287 if (strcmp (id_aux, id_origin) == 0)
289 if (strcmp (id_aux, id_far) == 0)
291 if (strcmp (id_aux, id_near) == 0)
293 if (strcmp (id_aux, id_near2) == 0)
296 if ((NULL == o) || (NULL == d) || (NULL == d2) || (NULL == d_far))
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 "Peers not found (hostkey file changed?)\n");
300 GNUNET_SCHEDULER_cancel (disconnect_task);
301 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\nfrom %s\n",
310 GNUNET_h2s_full (&o->id.hashPubKey));
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking for %s\n",
312 GNUNET_h2s_full (&d->id.hashPubKey));
313 get_h = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
314 GNUNET_BLOCK_TYPE_TEST, /* type */
315 &d->id.hashPubKey, /*key to search */
316 4U, /* replication level */
317 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
319 &dht_get_id_handler, (void *)1);
320 if (TORUS == test_topology)
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking for %s\n",
323 GNUNET_h2s_full (&d2->id.hashPubKey));
324 get_h_2 = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
325 GNUNET_BLOCK_TYPE_TEST, /* type */
326 &d2->id.hashPubKey, /*key to search */
327 4U, /* replication level */
328 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
330 &dht_get_id_handler, (void *)2);
331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking for %s\n",
332 GNUNET_h2s_full (&d_far->id.hashPubKey));
333 get_h_far = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
334 GNUNET_BLOCK_TYPE_TEST, /* type */
335 &d_far->id.hashPubKey, /*key to search */
336 4U, /* replication level */
337 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
339 &dht_get_id_handler, (void *)3);
341 GNUNET_SCHEDULER_cancel (disconnect_task);
343 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
347 * Task to put the id of each peer into teh DHT.
349 * @param cls Closure (unused)
350 * @param tc Task context
354 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct GNUNET_TESTING_Daemon *d;
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "putting id's in DHT\n");
360 for (i = 0; i < num_peers; i++)
362 d = GNUNET_TESTING_daemon_get (pg, i);
363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " putting into DHT: %s\n",
364 GNUNET_h2s_full (&d->id.hashPubKey));
365 GNUNET_DHT_put (hs[i], &d->id.hashPubKey, 10U,
366 GNUNET_DHT_RO_RECORD_ROUTE |
367 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
368 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
369 (const char *) &d->id, GNUNET_TIME_UNIT_FOREVER_ABS,
370 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
373 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, &put_id, NULL);
374 if (GNUNET_SCHEDULER_NO_TASK == test_task)
375 test_task = GNUNET_SCHEDULER_add_now (&do_test, NULL);
380 * peergroup_ready: start test when all peers are connected
383 * @param emsg error message
387 peergroup_ready (void *cls, const char *emsg)
389 struct GNUNET_TESTING_Daemon *d;
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Peergroup callback called with error, aborting test!\n");
398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n",
401 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "************************************************************\n");
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
408 "Peer Group started successfully!\n");
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have %u connections\n",
413 if (data_file != NULL)
416 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
418 GNUNET_DISK_file_write (data_file, buf, buf_len);
421 peers_running = GNUNET_TESTING_daemons_running (pg);
423 GNUNET_assert (peers_running == num_peers);
424 hs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
425 for (i = 0; i < num_peers; i++)
427 d = GNUNET_TESTING_daemon_get (pg, i);
428 hs[i] = GNUNET_DHT_connect (d->cfg, 32);
431 test_task = GNUNET_SCHEDULER_NO_TASK;
432 put_task = GNUNET_SCHEDULER_add_now (&put_id, NULL);
434 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
440 * Function that will be called whenever two daemons are connected by
441 * the testing library.
444 * @param first peer id for first daemon
445 * @param second peer id for the second daemon
446 * @param distance distance between the connected peers
447 * @param first_cfg config for the first daemon
448 * @param second_cfg config for the second daemon
449 * @param first_daemon handle for the first daemon
450 * @param second_daemon handle for the second daemon
451 * @param emsg error message (NULL on success)
454 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
455 const struct GNUNET_PeerIdentity *second, uint32_t distance,
456 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
457 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
458 struct GNUNET_TESTING_Daemon *first_daemon,
459 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
465 GNUNET_PEER_intern (first);
466 GNUNET_PEER_intern (second);
470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
471 "Problem with new connection (%s)\n", emsg);
478 * run: load configuration options and schedule test to run (start peergroup)
481 * @param cfgfile configuration file name (can be NULL)
482 * @param cfg configuration handle
485 run (void *cls, char *const *args, const char *cfgfile,
486 const struct GNUNET_CONFIGURATION_Handle *cfg)
489 struct GNUNET_TESTING_Host *hosts;
493 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
495 GNUNET_log_setup ("test_dht_topo",
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
505 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
506 "use_progressbars", "YES");
510 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
511 "num_peers", &num_peers))
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 "Option TESTING:NUM_PEERS is required!\n");
519 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
520 "topology_output_file",
523 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
524 "Option test_dht_topo:topology_output_file is required!\n");
529 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_topo",
534 GNUNET_DISK_file_open (data_filename,
535 GNUNET_DISK_OPEN_READWRITE |
536 GNUNET_DISK_OPEN_CREATE,
537 GNUNET_DISK_PERM_USER_READ |
538 GNUNET_DISK_PERM_USER_WRITE);
539 if (data_file == NULL)
541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
543 GNUNET_free (data_filename);
548 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_topo",
549 "output_file", &temp_str))
552 GNUNET_DISK_file_open (temp_str,
553 GNUNET_DISK_OPEN_READWRITE |
554 GNUNET_DISK_OPEN_CREATE,
555 GNUNET_DISK_PERM_USER_READ |
556 GNUNET_DISK_PERM_USER_WRITE);
557 if (output_file == NULL)
558 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
561 GNUNET_free_non_null (temp_str);
563 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
565 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
566 &connect_cb, &peergroup_ready, NULL,
568 GNUNET_assert (pg != NULL);
570 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
571 &shutdown_task, NULL);
577 * test_dht_2d command line options
579 static struct GNUNET_GETOPT_CommandLineOption options[] = {
580 {'V', "verbose", NULL,
581 gettext_noop ("be verbose (print progress information)"),
582 0, &GNUNET_GETOPT_set_one, &verbose},
583 GNUNET_GETOPT_OPTION_END
591 main (int xargc, char *xargv[])
593 char *const argv_torus[] = { "test-dht-2dtorus",
595 "test_dht_2dtorus.conf",
601 char *const argv_line[] = { "test-dht-line",
603 "test_dht_line.conf",
612 if (strstr (xargv[0], "test_dht_2dtorus") != NULL)
615 argc = sizeof (argv_torus) / sizeof (char *);
616 test_topology = TORUS;
618 else if (strstr (xargv[0], "test_dht_line") != NULL)
621 argc = sizeof (argv_line) / sizeof (char *);
622 test_topology = LINE;
629 GNUNET_PROGRAM_run (argc - 1, argv,
631 gettext_noop ("Test dht in different topologies."),
635 GNUNET_DISK_directory_remove ("/tmp/test_dht_topo");
639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ID 1 not found!\n");
641 if (TORUS == test_topology)
645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ID 2 not found!\n");
649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ID far not found!\n");
655 /* end of test_dht_topo.c */