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 REMOVE_DIR GNUNET_YES
36 * DIFFERENT TESTS TO RUN
42 * How long until we give up on connecting the peers?
44 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
46 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
48 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
61 * Total number of peers in the test.
63 static unsigned long long num_peers;
66 * Global configuration file
68 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
71 * Total number of currently running peers.
73 static unsigned long long peers_running;
76 * Total number of connections in the whole network.
78 static unsigned int total_connections;
81 * The currently running peer group.
83 static struct GNUNET_TESTING_PeerGroup *pg;
86 * File to report results to.
88 static struct GNUNET_DISK_FileHandle *output_file;
91 * File to log connection info, statistics to.
93 static struct GNUNET_DISK_FileHandle *data_file;
96 * Task called to disconnect peers.
98 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
101 * Task To perform tests
103 static GNUNET_SCHEDULER_TaskIdentifier test_task;
106 * Task to do DHT_puts
108 static GNUNET_SCHEDULER_TaskIdentifier put_task;
111 * Task called to shutdown test.
113 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
115 static char *topology_file;
117 struct GNUNET_TESTING_Daemon *d1;
119 struct GNUNET_TESTING_Daemon *d2;
121 struct GNUNET_DHT_Handle **hs;
123 struct GNUNET_DHT_GetHandle *get_h;
125 struct GNUNET_DHT_GetHandle *get_h_2;
127 struct GNUNET_DHT_GetHandle *get_h_far;
134 * Which topology are we to run
136 static int test_topology;
139 * Check whether peers successfully shut down.
142 shutdown_callback (void *cls, const char *emsg)
146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Shutdown of peers failed!\n");
151 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All peers successfully shut down!\n");
153 GNUNET_CONFIGURATION_destroy (testing_cfg);
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Ending test.\n");
162 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
164 GNUNET_SCHEDULER_cancel (disconnect_task);
165 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
168 if (data_file != NULL)
169 GNUNET_DISK_file_close (data_file);
170 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
175 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
179 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "disconnecting peers\n");
180 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
181 GNUNET_SCHEDULER_cancel (put_task);
183 GNUNET_DHT_get_stop (get_h);
185 GNUNET_DHT_get_stop (get_h_2);
186 if (NULL != get_h_far)
187 GNUNET_DHT_get_stop (get_h_far);
188 for (i = 0; i < num_peers; i++)
190 GNUNET_DHT_disconnect (hs[i]);
192 GNUNET_SCHEDULER_cancel (shutdown_handle);
193 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
197 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
198 const GNUNET_HashCode * key,
199 const struct GNUNET_PeerIdentity *get_path,
200 unsigned int get_path_length,
201 const struct GNUNET_PeerIdentity *put_path,
202 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
203 size_t size, const void *data)
207 if (sizeof (GNUNET_HashCode) == size)
209 const GNUNET_HashCode *h = data;
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Contents: %s\n",
212 GNUNET_h2s_full (h));
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH: (get %u, put %u)\n",
220 get_path_length, put_path_length);
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " LOCAL\n");
222 for (i = get_path_length - 1; i >= 0; i--)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
225 GNUNET_i2s (&get_path[i]));
227 for (i = put_path_length - 1; i >= 0; i--)
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %s\n",
230 GNUNET_i2s (&put_path[i]));
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "FOUND 1!\n");
240 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "FOUND 2!\n");
244 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "FOUND FAR!\n");
249 if (TORUS == test_topology &&
250 (found_1 == 0 || found_2 == 0 || found_far == 0))
253 GNUNET_SCHEDULER_cancel (disconnect_task);
254 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
258 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
260 struct GNUNET_TESTING_Daemon *d;
261 struct GNUNET_TESTING_Daemon *d2;
262 struct GNUNET_TESTING_Daemon *d_far;
263 struct GNUNET_TESTING_Daemon *o;
264 struct GNUNET_TESTING_Daemon *aux;
266 const char *id_origin = "FC74";
267 const char *id_near = "9P6V";
268 const char *id_near2 = "2GDS";
269 const char *id_far = "KPST";
272 d = d2 = d_far = o = NULL;
273 found_1 = found_2 = found_far = 0;
274 if (LINE == test_topology)
276 o = GNUNET_TESTING_daemon_get (pg, 0);
277 d = GNUNET_TESTING_daemon_get (pg, 4);
279 else if (TORUS == test_topology)
281 for (i = 0; i < num_peers; i++)
283 aux = GNUNET_TESTING_daemon_get (pg, i);
284 id_aux = GNUNET_i2s (&aux->id);
285 if (strcmp (id_aux, id_origin) == 0)
287 if (strcmp (id_aux, id_far) == 0)
289 if (strcmp (id_aux, id_near) == 0)
291 if (strcmp (id_aux, id_near2) == 0)
294 if ((NULL == o) || (NULL == d) || (NULL == d2) || (NULL == d_far))
296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
297 "Peers not found (hostkey file changed?)\n");
298 GNUNET_SCHEDULER_cancel (disconnect_task);
299 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\nfrom %s\n",
308 GNUNET_h2s_full (&o->id.hashPubKey));
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking for %s\n",
310 GNUNET_h2s_full (&d->id.hashPubKey));
311 get_h = GNUNET_DHT_get_start (hs[0],
312 GNUNET_BLOCK_TYPE_TEST, /* type */
313 &d->id.hashPubKey, /*key to search */
314 4U, /* replication level */
315 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
317 &dht_get_id_handler, (void *)1);
318 if (TORUS == test_topology)
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking for %s\n",
321 GNUNET_h2s_full (&d2->id.hashPubKey));
322 get_h_2 = GNUNET_DHT_get_start (hs[0],
323 GNUNET_BLOCK_TYPE_TEST, /* type */
324 &d2->id.hashPubKey, /*key to search */
325 4U, /* replication level */
326 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
328 &dht_get_id_handler, (void *)2);
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking for %s\n",
330 GNUNET_h2s_full (&d_far->id.hashPubKey));
331 get_h_far = GNUNET_DHT_get_start (hs[0],
332 GNUNET_BLOCK_TYPE_TEST, /* type */
333 &d_far->id.hashPubKey, /*key to search */
334 4U, /* replication level */
335 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
337 &dht_get_id_handler, (void *)3);
339 GNUNET_SCHEDULER_cancel (disconnect_task);
341 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
345 * Task to put the id of each peer into teh DHT.
347 * @param cls Closure (unused)
348 * @param tc Task context
352 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
354 struct GNUNET_TESTING_Daemon *d;
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "putting id's in DHT\n");
358 for (i = 0; i < num_peers; i++)
360 d = GNUNET_TESTING_daemon_get (pg, i);
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " putting into DHT: %s\n",
362 GNUNET_h2s_full (&d->id.hashPubKey));
363 GNUNET_DHT_put (hs[i], &d->id.hashPubKey, 10U,
364 GNUNET_DHT_RO_RECORD_ROUTE |
365 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
366 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
367 (const char *) &d->id, GNUNET_TIME_UNIT_FOREVER_ABS,
368 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
371 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, &put_id, NULL);
372 if (GNUNET_SCHEDULER_NO_TASK == test_task)
373 test_task = GNUNET_SCHEDULER_add_now (&do_test, NULL);
378 * peergroup_ready: start test when all peers are connected
381 * @param emsg error message
385 peergroup_ready (void *cls, const char *emsg)
387 struct GNUNET_TESTING_Daemon *d;
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Peergroup callback called with error, aborting test!\n");
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Error from testing: `%s'\n",
399 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
403 "Peer Group started successfully with %u connections\n",
405 if (data_file != NULL)
408 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
410 GNUNET_DISK_file_write (data_file, buf, buf_len);
413 peers_running = GNUNET_TESTING_daemons_running (pg);
415 GNUNET_assert (peers_running == num_peers);
416 hs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
417 for (i = 0; i < num_peers; i++)
419 d = GNUNET_TESTING_daemon_get (pg, i);
420 hs[i] = GNUNET_DHT_connect (d->cfg, 32);
423 test_task = GNUNET_SCHEDULER_NO_TASK;
424 put_task = GNUNET_SCHEDULER_add_now (&put_id, NULL);
426 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
432 * Function that will be called whenever two daemons are connected by
433 * the testing library.
436 * @param first peer id for first daemon
437 * @param second peer id for the second daemon
438 * @param distance distance between the connected peers
439 * @param first_cfg config for the first daemon
440 * @param second_cfg config for the second daemon
441 * @param first_daemon handle for the first daemon
442 * @param second_daemon handle for the second daemon
443 * @param emsg error message (NULL on success)
446 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
447 const struct GNUNET_PeerIdentity *second, uint32_t distance,
448 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
449 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
450 struct GNUNET_TESTING_Daemon *first_daemon,
451 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
457 GNUNET_PEER_intern (first);
458 GNUNET_PEER_intern (second);
462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
463 "Problem with new connection (%s)\n", emsg);
470 * run: load configuration options and schedule test to run (start peergroup)
473 * @param cfgfile configuration file name (can be NULL)
474 * @param cfg configuration handle
477 run (void *cls, char *const *args, const char *cfgfile,
478 const struct GNUNET_CONFIGURATION_Handle *cfg)
481 struct GNUNET_TESTING_Host *hosts;
485 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
487 GNUNET_log_setup ("test_dht_topo",
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemons.\n");
491 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
492 "use_progressbars", "YES");
494 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
495 "num_peers", &num_peers))
497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498 "Option TESTING:NUM_PEERS is required!\n");
503 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
504 "topology_output_file",
507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
508 "Option test_dht_topo:topology_output_file is required!\n");
513 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_topo",
518 GNUNET_DISK_file_open (data_filename,
519 GNUNET_DISK_OPEN_READWRITE |
520 GNUNET_DISK_OPEN_CREATE,
521 GNUNET_DISK_PERM_USER_READ |
522 GNUNET_DISK_PERM_USER_WRITE);
523 if (data_file == NULL)
525 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
527 GNUNET_free (data_filename);
532 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_topo",
533 "output_file", &temp_str))
536 GNUNET_DISK_file_open (temp_str,
537 GNUNET_DISK_OPEN_READWRITE |
538 GNUNET_DISK_OPEN_CREATE,
539 GNUNET_DISK_PERM_USER_READ |
540 GNUNET_DISK_PERM_USER_WRITE);
541 if (output_file == NULL)
542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
545 GNUNET_free_non_null (temp_str);
547 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
549 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
550 &connect_cb, &peergroup_ready, NULL,
552 GNUNET_assert (pg != NULL);
554 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
555 &shutdown_task, NULL);
561 * test_dht_2d command line options
563 static struct GNUNET_GETOPT_CommandLineOption options[] = {
564 {'V', "verbose", NULL,
565 gettext_noop ("be verbose (print progress information)"),
566 0, &GNUNET_GETOPT_set_one, &verbose},
567 GNUNET_GETOPT_OPTION_END
575 main (int xargc, char *xargv[])
577 char *const argv_torus[] = { "test-dht-2dtorus",
579 "test_dht_2dtorus.conf",
582 char *const argv_line[] = { "test-dht-line",
584 "test_dht_line.conf",
590 if (strstr (xargv[0], "test_dht_2dtorus") != NULL)
593 argc = sizeof (argv_torus) / sizeof (char *);
594 test_topology = TORUS;
596 else if (strstr (xargv[0], "test_dht_line") != NULL)
599 argc = sizeof (argv_line) / sizeof (char *);
600 test_topology = LINE;
607 GNUNET_PROGRAM_run (argc - 1, argv,
609 gettext_noop ("Test dht in different topologies."),
613 GNUNET_DISK_directory_remove ("/tmp/test_dht_topo");
617 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ID 1 not found!\n");
619 if (TORUS == test_topology)
623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ID 2 not found!\n");
627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "ID far not found!\n");
633 /* end of test_dht_topo.c */