2 This file is part of GNUnet.
3 (C) 2011 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_2dtorus.c
23 * @brief Test for the dht service: store and retrieve in a 2d_torus.
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_YES
34 #define REMOVE_DIR GNUNET_YES
38 * How long until we give up on connecting the peers?
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
50 * Total number of peers in the test.
52 static unsigned long long num_peers;
55 * Global configuration file
57 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
60 * Total number of currently running peers.
62 static unsigned long long peers_running;
65 * Total number of connections in the whole network.
67 static unsigned int total_connections;
70 * The currently running peer group.
72 static struct GNUNET_TESTING_PeerGroup *pg;
75 * File to report results to.
77 static struct GNUNET_DISK_FileHandle *output_file;
80 * File to log connection info, statistics to.
82 static struct GNUNET_DISK_FileHandle *data_file;
85 * Task called to disconnect peers.
87 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
90 * Task To perform tests
92 static GNUNET_SCHEDULER_TaskIdentifier test_task;
97 static GNUNET_SCHEDULER_TaskIdentifier put_task;
100 * Task called to shutdown test.
102 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
104 static char *topology_file;
106 struct GNUNET_TESTING_Daemon *d1;
108 struct GNUNET_TESTING_Daemon *d2;
110 struct GNUNET_DHT_Handle **hs;
112 struct GNUNET_DHT_GetHandle *get_h;
114 struct GNUNET_DHT_GetHandle *get_h_2;
116 struct GNUNET_DHT_GetHandle *get_h_far;
121 * Check whether peers successfully shut down.
124 shutdown_callback (void *cls, const char *emsg)
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
143 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
149 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
151 GNUNET_SCHEDULER_cancel (disconnect_task);
152 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
155 if (data_file != NULL)
156 GNUNET_DISK_file_close (data_file);
157 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
158 GNUNET_CONFIGURATION_destroy (testing_cfg);
163 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "test: disconnecting peers\n");
169 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
170 GNUNET_SCHEDULER_cancel (put_task);
172 GNUNET_DHT_get_stop(get_h);
174 GNUNET_DHT_get_stop(get_h_2);
175 if (NULL != get_h_far)
176 GNUNET_DHT_get_stop(get_h_far);
177 for (i = 0; i < num_peers; i++)
179 GNUNET_DHT_disconnect(hs[i]);
181 GNUNET_SCHEDULER_cancel (shutdown_handle);
182 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
186 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
187 const GNUNET_HashCode * key,
188 const struct GNUNET_PeerIdentity *get_path,
189 unsigned int get_path_length,
190 const struct GNUNET_PeerIdentity *put_path,
191 unsigned int put_path_length,
192 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "test: ************* FOUND!!! ***********\n");
198 if (sizeof(GNUNET_HashCode) == size)
200 const GNUNET_HashCode *h = data;
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "test: Contents: %s\n",
203 GNUNET_h2s_full (h));
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "test: PATH: (get %u, put %u)\n",
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 for (i = get_path_length - 1; i >= 0; i--)
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 GNUNET_i2s (&get_path[i]));
218 for (i = put_path_length - 1; i >= 0; i--)
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 GNUNET_i2s (&put_path[i]));
228 GNUNET_SCHEDULER_cancel(disconnect_task);
229 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
233 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
235 struct GNUNET_TESTING_Daemon *d;
236 struct GNUNET_TESTING_Daemon *d2;
237 struct GNUNET_TESTING_Daemon *d_far;
238 struct GNUNET_TESTING_Daemon *o;
239 struct GNUNET_TESTING_Daemon *aux;
241 const char *id_origin = "FC74";
242 const char *id_near = "9P6V";
243 const char *id_near2 = "2GDS";
244 const char *id_far = "KPST";
247 d = d2 = d_far = o = NULL;
248 for (i = 0; i < num_peers; i++)
250 aux = GNUNET_TESTING_daemon_get (pg, i);
251 id_aux = GNUNET_i2s (&aux->id);
252 if (strcmp (id_aux, id_origin) == 0)
254 if (strcmp (id_aux, id_far) == 0)
256 if (strcmp (id_aux, id_near) == 0)
258 if (strcmp (id_aux, id_near2) == 0)
261 if ( (NULL == o) || (NULL == d) || (NULL == d2) || (NULL == d_far))
263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264 "test: Peers not found (hostkey file changed?)\n");
265 GNUNET_SCHEDULER_cancel (disconnect_task);
266 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "test: looking for %s\n",
272 GNUNET_h2s_full (&d->id.hashPubKey));
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "test: looking for %s\n",
275 GNUNET_h2s_full (&d2->id.hashPubKey));
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "test: looking for %s\n",
278 GNUNET_h2s_full (&d_far->id.hashPubKey));
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 GNUNET_h2s_full (&o->id.hashPubKey));
283 get_h = GNUNET_DHT_get_start (hs[0],
284 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
285 GNUNET_BLOCK_TYPE_TEST, /* type */
286 &d->id.hashPubKey, /*key to search */
287 4U, /* replication level */
288 GNUNET_DHT_RO_RECORD_ROUTE |
289 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
294 get_h_2 = GNUNET_DHT_get_start (hs[0],
295 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
296 GNUNET_BLOCK_TYPE_TEST, /* type */
297 &d2->id.hashPubKey, /*key to search */
298 4U, /* replication level */
299 GNUNET_DHT_RO_RECORD_ROUTE |
300 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
305 get_h_far = GNUNET_DHT_get_start (hs[0],
306 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
307 GNUNET_BLOCK_TYPE_TEST, /* type */
308 &d_far->id.hashPubKey, /*key to search */
309 4U, /* replication level */
310 GNUNET_DHT_RO_RECORD_ROUTE |
311 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
316 GNUNET_SCHEDULER_cancel (disconnect_task);
317 disconnect_task = GNUNET_SCHEDULER_add_delayed(
318 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
319 &disconnect_peers, NULL);
324 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 struct GNUNET_TESTING_Daemon *d;
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting id's in DHT\n");
330 for (i = 0; i < num_peers; i++)
332 d = GNUNET_TESTING_daemon_get (pg, i);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "test: putting into DHT: %s\n",
335 GNUNET_h2s_full (&d->id.hashPubKey));
336 GNUNET_DHT_put(hs[i],
339 GNUNET_DHT_RO_RECORD_ROUTE |
340 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
341 GNUNET_BLOCK_TYPE_TEST,
342 sizeof(struct GNUNET_PeerIdentity),
343 (const char *) &d->id,
344 GNUNET_TIME_UNIT_FOREVER_ABS,
345 GNUNET_TIME_UNIT_FOREVER_REL,
350 put_task = GNUNET_SCHEDULER_add_delayed(
351 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
357 * peergroup_ready: start test when all peers are connected
359 * @param emsg error message
362 peergroup_ready (void *cls, const char *emsg)
364 struct GNUNET_TESTING_Daemon *d;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "test: Peergroup callback called with error, aborting test!\n");
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374 "test: Error from testing: `%s'\n", emsg);
376 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "************************************************************\n");
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383 "test: Peer Group started successfully!\n");
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "test: Have %u connections\n",
389 if (data_file != NULL)
392 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
394 GNUNET_DISK_file_write (data_file, buf, buf_len);
397 peers_running = GNUNET_TESTING_daemons_running (pg);
399 GNUNET_assert (peers_running == num_peers);
400 hs = GNUNET_malloc (num_peers * sizeof(struct GNUNET_DHT_Handle *));
401 for (i = 0; i < num_peers; i++)
403 d = GNUNET_TESTING_daemon_get (pg, i);
404 hs[i] = GNUNET_DHT_connect(d->cfg, 32);
407 put_task = GNUNET_SCHEDULER_add_now(&put_id, NULL);
409 GNUNET_SCHEDULER_add_delayed(
410 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
413 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
414 GNUNET_TIME_UNIT_SECONDS,
423 * Function that will be called whenever two daemons are connected by
424 * the testing library.
427 * @param first peer id for first daemon
428 * @param second peer id for the second daemon
429 * @param distance distance between the connected peers
430 * @param first_cfg config for the first daemon
431 * @param second_cfg config for the second daemon
432 * @param first_daemon handle for the first daemon
433 * @param second_daemon handle for the second daemon
434 * @param emsg error message (NULL on success)
437 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
438 const struct GNUNET_PeerIdentity *second, uint32_t distance,
439 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
440 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
441 struct GNUNET_TESTING_Daemon *first_daemon,
442 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
448 GNUNET_PEER_intern(first);
449 GNUNET_PEER_intern(second);
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "test: Problem with new connection (%s)\n",
462 * run: load configuration options and schedule test to run (start peergroup)
465 * @param cfgfile configuration file name (can be NULL)
466 * @param cfg configuration handle
469 run (void *cls, char *const *args, const char *cfgfile,
470 const struct GNUNET_CONFIGURATION_Handle *cfg)
473 struct GNUNET_TESTING_Host *hosts;
477 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
479 GNUNET_log_setup ("test_dht_2dtorus",
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
489 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
490 "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_2d:topology_output_file is required!\n");
513 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_2dtorus",
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_2dtorus",
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_relative_get_forever (),
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[] = { "test-dht-2dtorus",
579 "test_dht_2dtorus.conf",
586 GNUNET_PROGRAM_run (sizeof(argv)/sizeof(char*) - 1, argv, "test_dht_2dtorus",
587 gettext_noop ("Test dht in a small 2D torus."), options,
590 GNUNET_DISK_directory_remove ("/tmp/test_dht_2dtorus");
594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
599 /* end of test_dht_2dtorus.c */