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 static char *data_filename;
108 struct GNUNET_TESTING_Daemon *d1;
110 struct GNUNET_TESTING_Daemon *d2;
112 struct GNUNET_DHT_Handle **hs;
114 struct GNUNET_DHT_GetHandle *get_h;
117 * Check whether peers successfully shut down.
120 shutdown_callback (void *cls, const char *emsg)
125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
139 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
145 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
147 GNUNET_SCHEDULER_cancel (disconnect_task);
148 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
151 if (data_file != NULL)
152 GNUNET_DISK_file_close (data_file);
153 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
154 GNUNET_CONFIGURATION_destroy (testing_cfg);
159 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "test: disconnecting peers\n");
165 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
166 GNUNET_SCHEDULER_cancel (put_task);
168 GNUNET_DHT_get_stop(get_h);
169 for (i = 0; i < num_peers; i++)
171 GNUNET_DHT_disconnect(hs[i]);
173 GNUNET_SCHEDULER_cancel (shutdown_handle);
174 shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
178 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
179 const GNUNET_HashCode * key,
180 const struct GNUNET_PeerIdentity *get_path,
181 unsigned int get_path_length,
182 const struct GNUNET_PeerIdentity *put_path,
183 unsigned int put_path_length,
184 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189 "test: ************* FOUND!!! ***********\n");
190 if (sizeof(GNUNET_HashCode) == size)
192 const GNUNET_HashCode *h = data;
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
194 "test: Contents: %s\n",
195 GNUNET_h2s_full (h));
198 GNUNET_SCHEDULER_cancel(disconnect_task);
199 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
203 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 struct GNUNET_TESTING_Daemon *d;
206 struct GNUNET_TESTING_Daemon *o;
208 d = GNUNET_TESTING_daemon_get (pg, 2);
209 o = GNUNET_TESTING_daemon_get (pg, 0);
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "test: looking for [2] %s\n",
213 GNUNET_h2s_full (&d->id.hashPubKey));
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "test: from [0] %s\n",
216 GNUNET_h2s_full (&o->id.hashPubKey));
217 get_h = GNUNET_DHT_get_start (hs[0],
218 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
219 GNUNET_BLOCK_TYPE_TEST, /* type */
220 &d->id.hashPubKey, /*key to search */
221 4U, /* replication level */
222 GNUNET_DHT_RO_RECORD_ROUTE |
223 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
228 GNUNET_SCHEDULER_cancel (disconnect_task);
229 disconnect_task = GNUNET_SCHEDULER_add_delayed(
230 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
231 &disconnect_peers, NULL);
236 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
238 struct GNUNET_TESTING_Daemon *d;
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting id's in DHT\n");
242 for (i = 0; i < num_peers; i++)
244 d = GNUNET_TESTING_daemon_get (pg, i);
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "test: putting into DHT: %s\n",
247 GNUNET_h2s_full (&d->id.hashPubKey));
248 GNUNET_DHT_put(hs[i],
251 GNUNET_DHT_RO_RECORD_ROUTE |
252 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
253 GNUNET_BLOCK_TYPE_TEST,
254 sizeof(struct GNUNET_PeerIdentity),
255 (const char *) &d->id,
256 GNUNET_TIME_UNIT_FOREVER_ABS,
257 GNUNET_TIME_UNIT_FOREVER_REL,
262 put_task = GNUNET_SCHEDULER_add_delayed(
263 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
269 * peergroup_ready: start test when all peers are connected
271 * @param emsg error message
274 peergroup_ready (void *cls, const char *emsg)
276 struct GNUNET_TESTING_Daemon *d;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "test: Peergroup callback called with error, aborting test!\n");
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "test: Error from testing: `%s'\n", emsg);
288 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "************************************************************\n");
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 "test: Peer Group started successfully!\n");
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "test: Have %u connections\n",
301 if (data_file != NULL)
304 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
306 GNUNET_DISK_file_write (data_file, buf, buf_len);
309 peers_running = GNUNET_TESTING_daemons_running (pg);
311 GNUNET_assert (peers_running == num_peers);
312 hs = GNUNET_malloc (num_peers * sizeof(struct GNUNET_DHT_Handle *));
313 for (i = 0; i < num_peers; i++)
315 d = GNUNET_TESTING_daemon_get (pg, i);
316 hs[i] = GNUNET_DHT_connect(d->cfg, 32);
319 put_task = GNUNET_SCHEDULER_add_now(&put_id, NULL);
321 GNUNET_SCHEDULER_add_delayed(
322 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
325 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
326 GNUNET_TIME_UNIT_SECONDS,
335 * Function that will be called whenever two daemons are connected by
336 * the testing library.
339 * @param first peer id for first daemon
340 * @param second peer id for the second daemon
341 * @param distance distance between the connected peers
342 * @param first_cfg config for the first daemon
343 * @param second_cfg config for the second daemon
344 * @param first_daemon handle for the first daemon
345 * @param second_daemon handle for the second daemon
346 * @param emsg error message (NULL on success)
349 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
350 const struct GNUNET_PeerIdentity *second, uint32_t distance,
351 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
352 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
353 struct GNUNET_TESTING_Daemon *first_daemon,
354 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
362 /*f = */GNUNET_PEER_intern(first);
363 /*s = */GNUNET_PEER_intern(second);
364 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: New connection!\n");
365 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 // "test: %s (%u)\n",
367 // GNUNET_h2s(&first->hashPubKey),
369 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 // "test: %s (%u)\n",
371 // GNUNET_h2s(&second->hashPubKey),
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
377 "test: Problem with new connection (%s)\n",
385 * run: load configuration options and schedule test to run (start peergroup)
388 * @param cfgfile configuration file name (can be NULL)
389 * @param cfg configuration handle
392 run (void *cls, char *const *args, const char *cfgfile,
393 const struct GNUNET_CONFIGURATION_Handle *cfg)
396 struct GNUNET_TESTING_Host *hosts;
399 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
401 GNUNET_log_setup ("test_dht_2dtorus",
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
411 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
412 "use_progressbars", "YES");
416 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
417 "num_peers", &num_peers))
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 "Option TESTING:NUM_PEERS is required!\n");
425 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
426 "topology_output_file",
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 "Option test_dht_2d:topology_output_file is required!\n");
435 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_2dtorus",
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 "Option test_dht_2d:data_output_file is required!\n");
445 GNUNET_DISK_file_open (data_filename,
446 GNUNET_DISK_OPEN_READWRITE |
447 GNUNET_DISK_OPEN_CREATE,
448 GNUNET_DISK_PERM_USER_READ |
449 GNUNET_DISK_PERM_USER_WRITE);
450 if (data_file == NULL)
452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
454 GNUNET_free (data_filename);
458 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_2dtorus",
459 "output_file", &temp_str))
462 GNUNET_DISK_file_open (temp_str,
463 GNUNET_DISK_OPEN_READWRITE |
464 GNUNET_DISK_OPEN_CREATE,
465 GNUNET_DISK_PERM_USER_READ |
466 GNUNET_DISK_PERM_USER_WRITE);
467 if (output_file == NULL)
468 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
471 GNUNET_free_non_null (temp_str);
473 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
475 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
476 &connect_cb, &peergroup_ready, NULL,
478 GNUNET_assert (pg != NULL);
480 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
481 &shutdown_task, NULL);
487 * test_dht_2d command line options
489 static struct GNUNET_GETOPT_CommandLineOption options[] = {
490 {'V', "verbose", NULL,
491 gettext_noop ("be verbose (print progress information)"),
492 0, &GNUNET_GETOPT_set_one, &verbose},
493 GNUNET_GETOPT_OPTION_END
501 main (int xargc, char *xargv[])
503 char *const argv[] = { "test-dht-2dtorus",
505 "test_dht_2dtorus.conf",
512 GNUNET_PROGRAM_run (sizeof(argv)/sizeof(char*) - 1, argv, "test_dht_2dtorus",
513 gettext_noop ("Test dht in a small 2D torus."), options,
516 GNUNET_DISK_directory_remove ("/tmp/test_dht_2dtorus");
520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
525 /* end of test_dht_2dtorus.c */