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_EXTRA_LOGGING
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;
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)
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "test: All peers successfully shut down!\n");
162 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
168 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
170 GNUNET_SCHEDULER_cancel (disconnect_task);
171 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
174 if (data_file != NULL)
175 GNUNET_DISK_file_close (data_file);
176 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
177 GNUNET_CONFIGURATION_destroy (testing_cfg);
182 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
187 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
188 GNUNET_SCHEDULER_cancel (put_task);
190 GNUNET_DHT_get_stop (get_h);
192 GNUNET_DHT_get_stop (get_h_2);
193 if (NULL != get_h_far)
194 GNUNET_DHT_get_stop (get_h_far);
195 for (i = 0; i < num_peers; i++)
197 GNUNET_DHT_disconnect (hs[i]);
199 GNUNET_SCHEDULER_cancel (shutdown_handle);
200 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
204 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
205 const GNUNET_HashCode * key,
206 const struct GNUNET_PeerIdentity *get_path,
207 unsigned int get_path_length,
208 const struct GNUNET_PeerIdentity *put_path,
209 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
210 size_t size, const void *data)
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
215 "test: ************* FOUND!!! ***********\n");
216 if (sizeof (GNUNET_HashCode) == size)
218 const GNUNET_HashCode *h = data;
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Contents: %s\n",
221 GNUNET_h2s_full (h));
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: PATH: (get %u, put %u)\n",
225 get_path_length, put_path_length);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: LOCAL\n");
227 for (i = get_path_length - 1; i >= 0; i--)
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
230 GNUNET_i2s (&get_path[i]));
232 for (i = put_path_length - 1; i >= 0; i--)
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
235 GNUNET_i2s (&put_path[i]));
238 if (TORUS == test_topology && found < 3)
241 GNUNET_SCHEDULER_cancel (disconnect_task);
242 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
246 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
248 struct GNUNET_TESTING_Daemon *d;
249 struct GNUNET_TESTING_Daemon *d2;
250 struct GNUNET_TESTING_Daemon *d_far;
251 struct GNUNET_TESTING_Daemon *o;
252 struct GNUNET_TESTING_Daemon *aux;
254 const char *id_origin = "FC74";
255 const char *id_near = "9P6V";
256 const char *id_near2 = "2GDS";
257 const char *id_far = "KPST";
260 d = d2 = d_far = o = NULL;
261 if (LINE == test_topology)
263 o = GNUNET_TESTING_daemon_get (pg, 0);
264 d = GNUNET_TESTING_daemon_get (pg, 4);
266 else if (TORUS == test_topology)
268 for (i = 0; i < num_peers; i++)
270 aux = GNUNET_TESTING_daemon_get (pg, i);
271 id_aux = GNUNET_i2s (&aux->id);
272 if (strcmp (id_aux, id_origin) == 0)
274 if (strcmp (id_aux, id_far) == 0)
276 if (strcmp (id_aux, id_near) == 0)
278 if (strcmp (id_aux, id_near2) == 0)
281 if ((NULL == o) || (NULL == d) || (NULL == d2) || (NULL == d_far))
283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
284 "test: Peers not found (hostkey file changed?)\n");
285 GNUNET_SCHEDULER_cancel (disconnect_task);
286 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\ntest: from %s\n",
295 GNUNET_h2s_full (&o->id.hashPubKey));
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: looking for %s\n",
297 GNUNET_h2s_full (&d->id.hashPubKey));
299 get_h = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
300 GNUNET_BLOCK_TYPE_TEST, /* type */
301 &d->id.hashPubKey, /*key to search */
302 4U, /* replication level */
303 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
305 &dht_get_id_handler, NULL);
306 if (TORUS == test_topology)
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: looking for %s\n",
309 GNUNET_h2s_full (&d2->id.hashPubKey));
310 get_h_2 = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
311 GNUNET_BLOCK_TYPE_TEST, /* type */
312 &d2->id.hashPubKey, /*key to search */
313 4U, /* replication level */
314 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
316 &dht_get_id_handler, NULL);
317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: looking for %s\n",
318 GNUNET_h2s_full (&d_far->id.hashPubKey));
319 get_h_far = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
320 GNUNET_BLOCK_TYPE_TEST, /* type */
321 &d_far->id.hashPubKey, /*key to search */
322 4U, /* replication level */
323 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
325 &dht_get_id_handler, NULL);
327 GNUNET_SCHEDULER_cancel (disconnect_task);
329 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
333 * Task to put the id of each peer into teh DHT.
335 * @param cls Closure (unused)
336 * @param tc Task context
340 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
342 struct GNUNET_TESTING_Daemon *d;
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting id's in DHT\n");
346 for (i = 0; i < num_peers; i++)
348 d = GNUNET_TESTING_daemon_get (pg, i);
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting into DHT: %s\n",
350 GNUNET_h2s_full (&d->id.hashPubKey));
351 GNUNET_DHT_put (hs[i], &d->id.hashPubKey, 10U,
352 GNUNET_DHT_RO_RECORD_ROUTE |
353 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
354 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
355 (const char *) &d->id, GNUNET_TIME_UNIT_FOREVER_ABS,
356 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
359 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, &put_id, NULL);
360 if (GNUNET_SCHEDULER_NO_TASK == test_task)
361 test_task = GNUNET_SCHEDULER_add_now (&do_test, NULL);
366 * peergroup_ready: start test when all peers are connected
369 * @param emsg error message
373 peergroup_ready (void *cls, const char *emsg)
375 struct GNUNET_TESTING_Daemon *d;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "test: Peergroup callback called with error, aborting test!\n");
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
387 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "************************************************************\n");
393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
394 "test: Peer Group started successfully!\n");
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Have %u connections\n",
399 if (data_file != NULL)
402 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
404 GNUNET_DISK_file_write (data_file, buf, buf_len);
407 peers_running = GNUNET_TESTING_daemons_running (pg);
409 GNUNET_assert (peers_running == num_peers);
410 hs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
411 for (i = 0; i < num_peers; i++)
413 d = GNUNET_TESTING_daemon_get (pg, i);
414 hs[i] = GNUNET_DHT_connect (d->cfg, 32);
417 test_task = GNUNET_SCHEDULER_NO_TASK;
418 put_task = GNUNET_SCHEDULER_add_now (&put_id, NULL);
420 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
426 * Function that will be called whenever two daemons are connected by
427 * the testing library.
430 * @param first peer id for first daemon
431 * @param second peer id for the second daemon
432 * @param distance distance between the connected peers
433 * @param first_cfg config for the first daemon
434 * @param second_cfg config for the second daemon
435 * @param first_daemon handle for the first daemon
436 * @param second_daemon handle for the second daemon
437 * @param emsg error message (NULL on success)
440 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
441 const struct GNUNET_PeerIdentity *second, uint32_t distance,
442 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
443 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
444 struct GNUNET_TESTING_Daemon *first_daemon,
445 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
451 GNUNET_PEER_intern (first);
452 GNUNET_PEER_intern (second);
456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
457 "test: Problem with new connection (%s)\n", emsg);
464 * run: load configuration options and schedule test to run (start peergroup)
467 * @param cfgfile configuration file name (can be NULL)
468 * @param cfg configuration handle
471 run (void *cls, char *const *args, const char *cfgfile,
472 const struct GNUNET_CONFIGURATION_Handle *cfg)
475 struct GNUNET_TESTING_Host *hosts;
479 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
481 GNUNET_log_setup ("test_dht_topo",
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
491 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
492 "use_progressbars", "YES");
496 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
497 "num_peers", &num_peers))
499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500 "Option TESTING:NUM_PEERS is required!\n");
505 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
506 "topology_output_file",
509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
510 "Option test_dht_topo:topology_output_file is required!\n");
515 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_topo",
520 GNUNET_DISK_file_open (data_filename,
521 GNUNET_DISK_OPEN_READWRITE |
522 GNUNET_DISK_OPEN_CREATE,
523 GNUNET_DISK_PERM_USER_READ |
524 GNUNET_DISK_PERM_USER_WRITE);
525 if (data_file == NULL)
527 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
529 GNUNET_free (data_filename);
534 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_topo",
535 "output_file", &temp_str))
538 GNUNET_DISK_file_open (temp_str,
539 GNUNET_DISK_OPEN_READWRITE |
540 GNUNET_DISK_OPEN_CREATE,
541 GNUNET_DISK_PERM_USER_READ |
542 GNUNET_DISK_PERM_USER_WRITE);
543 if (output_file == NULL)
544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
547 GNUNET_free_non_null (temp_str);
549 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
551 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
552 &connect_cb, &peergroup_ready, NULL,
554 GNUNET_assert (pg != NULL);
556 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
557 &shutdown_task, NULL);
563 * test_dht_2d command line options
565 static struct GNUNET_GETOPT_CommandLineOption options[] = {
566 {'V', "verbose", NULL,
567 gettext_noop ("be verbose (print progress information)"),
568 0, &GNUNET_GETOPT_set_one, &verbose},
569 GNUNET_GETOPT_OPTION_END
577 main (int xargc, char *xargv[])
579 char *const argv_torus[] = { "test-dht-2dtorus",
581 "test_dht_2dtorus.conf",
587 char *const argv_line[] = { "test-dht-line",
589 "test_dht_line.conf",
598 if (strstr (xargv[0], "test_dht_2dtorus") != NULL)
601 argc = sizeof (argv_torus) / sizeof (char *);
602 test_topology = TORUS;
604 else if (strstr (xargv[0], "test_dht_line") != NULL)
607 argc = sizeof (argv_line) / sizeof (char *);
608 test_topology = LINE;
615 GNUNET_PROGRAM_run (argc - 1, argv,
617 gettext_noop ("Test dht in different topologies."),
621 GNUNET_DISK_directory_remove ("/tmp/test_dht_topo");
625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
630 /* end of test_dht_topo.c */