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_monitor.c
23 * @brief Test for the dht service: store, retrieve and monitor in a line.
24 * TODO: update this description
25 * Each peer stores it own ID in the DHT and then a different peer tries to
26 * retrieve that key from it. The GET starts after a first round of PUTS has
27 * been made. Periodically, each peer stores its ID into the DHT. If after
28 * a timeout no result has been returned, the test fails.
31 #include "gnunet_testing_lib.h"
32 #include "gnunet_dht_service.h"
34 #define VERBOSE GNUNET_YES
36 #define REMOVE_DIR GNUNET_YES
40 * How long until we give up on connecting the peers?
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
44 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
46 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
56 * Total number of peers in the test.
58 static unsigned long long num_peers;
61 * Global configuration file
63 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
66 * Total number of currently running peers.
68 static unsigned long long peers_running;
71 * Total number of connections in the whole network.
73 static unsigned int total_connections;
76 * The currently running peer group.
78 static struct GNUNET_TESTING_PeerGroup *pg;
81 * File to report results to.
83 static struct GNUNET_DISK_FileHandle *output_file;
86 * File to log connection info, statistics to.
88 static struct GNUNET_DISK_FileHandle *data_file;
91 * Task called to disconnect peers.
93 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
96 * Task To perform tests
98 static GNUNET_SCHEDULER_TaskIdentifier test_task;
101 * Task to do DHT_puts
103 static GNUNET_SCHEDULER_TaskIdentifier put_task;
106 * Task called to shutdown test.
108 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
110 static char *topology_file;
112 struct GNUNET_TESTING_Daemon *d1;
114 struct GNUNET_TESTING_Daemon *d2;
116 struct GNUNET_DHT_Handle **hs;
118 struct GNUNET_DHT_MonitorHandle **mhs;
120 struct GNUNET_DHT_GetHandle *get_h_far;
122 const char *id_origin = "FC74";
123 const char *id_far = "2UVH";
125 struct GNUNET_TESTING_Daemon *d_far;
126 struct GNUNET_TESTING_Daemon *o;
128 unsigned int monitor_counter;
133 * Check whether peers successfully shut down.
136 shutdown_callback (void *cls, const char *emsg)
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "test: All peers successfully shut down!\n");
152 GNUNET_CONFIGURATION_destroy (testing_cfg);
157 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
163 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
165 GNUNET_SCHEDULER_cancel (disconnect_task);
166 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
169 if (data_file != NULL)
170 GNUNET_DISK_file_close (data_file);
171 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
176 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
181 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
182 GNUNET_SCHEDULER_cancel (put_task);
183 if (NULL != get_h_far)
184 GNUNET_DHT_get_stop (get_h_far);
185 for (i = 0; i < num_peers; i++)
187 GNUNET_DHT_disconnect (hs[i]);
189 GNUNET_SCHEDULER_cancel (shutdown_handle);
190 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
194 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
195 const GNUNET_HashCode * key,
196 const struct GNUNET_PeerIdentity *get_path,
197 unsigned int get_path_length,
198 const struct GNUNET_PeerIdentity *put_path,
199 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
200 size_t size, const void *data)
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "test: ************* FOUND!!! ***********\n");
206 if (sizeof (GNUNET_HashCode) == size)
208 const GNUNET_HashCode *h = data;
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Contents: %s\n",
211 GNUNET_h2s_full (h));
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: PATH: (get %u, put %u)\n",
215 get_path_length, put_path_length);
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: LOCAL\n");
217 for (i = get_path_length - 1; i >= 0; i--)
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
220 GNUNET_i2s (&get_path[i]));
222 for (i = put_path_length - 1; i >= 0; i--)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
225 GNUNET_i2s (&put_path[i]));
227 if (monitor_counter >= get_path_length + put_path_length)
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "expected at least %u hops, got %u\n",
231 get_path_length + put_path_length, monitor_counter);
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "expected at least %u hops, got %u\n",
235 get_path_length + put_path_length, monitor_counter);
236 GNUNET_SCHEDULER_cancel (disconnect_task);
237 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
241 * Start test: start GET request from the first node in the line looking for
242 * the ID of the last node in the line.
244 * @param cls Closure (not used).
245 * @param tc Task context.
248 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
255 in_test = GNUNET_YES;
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: looking for %s\n",
258 GNUNET_h2s_full (&d_far->id.hashPubKey));
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: from %s\n",
260 GNUNET_h2s_full (&o->id.hashPubKey));
261 get_h_far = GNUNET_DHT_get_start (hs[0], GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
262 GNUNET_BLOCK_TYPE_TEST, /* type */
263 &d_far->id.hashPubKey, /*key to search */
264 4U, /* replication level */
265 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
267 &dht_get_id_handler, NULL);
268 GNUNET_SCHEDULER_cancel (disconnect_task);
270 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
275 * Periodic function used to put the ID of the far peer in the DHT.
277 * @param cls Closure (not used).
278 * @param tc Task context.
281 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
283 struct GNUNET_TESTING_Daemon *d;
285 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
287 put_task = GNUNET_SCHEDULER_NO_TASK;
291 d = GNUNET_TESTING_daemon_get (pg, 4);
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting into DHT: %s\n",
293 GNUNET_h2s_full (&d->id.hashPubKey));
294 GNUNET_DHT_put (hs[4], &d->id.hashPubKey, 10U,
295 GNUNET_DHT_RO_RECORD_ROUTE |
296 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
297 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
298 (const char *) &d->id, GNUNET_TIME_UNIT_FOREVER_ABS,
299 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
301 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, &put_id, NULL);
305 * Callback called on each GET request going through the DHT.
306 * Prints the info about the intercepted packet and increments a counter.
308 * @param cls Closure.
309 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
310 * @param type The type of data in the request.
311 * @param hop_count Hop count so far.
312 * @param path_length number of entries in path (or 0 if not recorded).
313 * @param path peers on the GET path (or NULL if not recorded).
314 * @param desired_replication_level Desired replication level.
315 * @param key Key of the requested data.
318 monitor_get_cb (void *cls,
319 enum GNUNET_DHT_RouteOption options,
320 enum GNUNET_BLOCK_Type type,
322 uint32_t desired_replication_level,
323 unsigned int path_length,
324 const struct GNUNET_PeerIdentity *path,
325 const GNUNET_HashCode * key)
330 i = (unsigned int) (long) cls;
331 s_key = GNUNET_h2s(key);
332 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
333 "%u got a GET message for key %s\n",
336 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
342 * Callback called on each PUT request going through the DHT.
343 * Prints the info about the intercepted packet and increments a counter.
345 * @param cls Closure.
346 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
347 * @param type The type of data in the request.
348 * @param hop_count Hop count so far.
349 * @param path_length number of entries in path (or 0 if not recorded).
350 * @param path peers on the PUT path (or NULL if not recorded).
351 * @param desired_replication_level Desired replication level.
352 * @param exp Expiration time of the data.
353 * @param key Key under which data is to be stored.
354 * @param data Pointer to the data carried.
355 * @param size Number of bytes in data.
358 monitor_put_cb (void *cls,
359 enum GNUNET_DHT_RouteOption options,
360 enum GNUNET_BLOCK_Type type,
362 uint32_t desired_replication_level,
363 unsigned int path_length,
364 const struct GNUNET_PeerIdentity *path,
365 struct GNUNET_TIME_Absolute exp,
366 const GNUNET_HashCode * key,
373 i = (unsigned int) (long) cls;
374 s_key = GNUNET_h2s(key);
376 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
377 "%u got a PUT message for key %s with %u bytes\n",
380 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
386 * Callback called on each GET reply going through the DHT.
387 * Prints the info about the intercepted packet and increments a counter.
389 * @param cls Closure.
390 * @param type The type of data in the result.
391 * @param get_path Peers on GET path (or NULL if not recorded).
392 * @param get_path_length number of entries in get_path.
393 * @param put_path peers on the PUT path (or NULL if not recorded).
394 * @param put_path_length number of entries in get_path.
395 * @param exp Expiration time of the data.
396 * @param key Key of the data.
397 * @param data Pointer to the result data.
398 * @param size Number of bytes in data.
401 monitor_res_cb (void *cls,
402 enum GNUNET_BLOCK_Type type,
403 const struct GNUNET_PeerIdentity *get_path,
404 unsigned int get_path_length,
405 const struct GNUNET_PeerIdentity *put_path,
406 unsigned int put_path_length,
407 struct GNUNET_TIME_Absolute exp,
408 const GNUNET_HashCode * key,
415 i = (unsigned int) (long) cls;
416 s_key = GNUNET_h2s(key);
417 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
418 "%u got a REPLY message for key %s with %u bytes\n",
421 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
427 * peergroup_ready: start test when all peers are connected
430 * @param emsg error message
433 peergroup_ready (void *cls, const char *emsg)
435 struct GNUNET_TESTING_Daemon *d;
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "test: Peergroup callback called with error, aborting test!\n");
444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
447 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "************************************************************\n");
453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454 "test: Peer Group started successfully!\n");
455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Have %u connections\n",
459 if (data_file != NULL)
462 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
464 GNUNET_DISK_file_write (data_file, buf, buf_len);
467 peers_running = GNUNET_TESTING_daemons_running (pg);
469 GNUNET_assert (peers_running == num_peers);
470 hs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
471 mhs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_MonitorHandle *));
473 o = GNUNET_TESTING_daemon_get (pg, 0);
474 d_far = GNUNET_TESTING_daemon_get (pg, 4);
476 for (i = 0; i < num_peers; i++)
478 d = GNUNET_TESTING_daemon_get (pg, i);
479 hs[i] = GNUNET_DHT_connect (d->cfg, 32);
480 mhs[i] = GNUNET_DHT_monitor_start(hs[i],
481 GNUNET_BLOCK_TYPE_ANY,
489 if ((NULL == o) || (NULL == d_far))
491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
492 "test: Error getting daemons from pg\n");
493 GNUNET_SCHEDULER_cancel (disconnect_task);
494 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
498 put_task = GNUNET_SCHEDULER_add_now (&put_id, NULL);
500 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
501 (GNUNET_TIME_UNIT_SECONDS, 2), &do_test,
504 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
510 * Function that will be called whenever two daemons are connected by
511 * the testing library.
514 * @param first peer id for first daemon
515 * @param second peer id for the second daemon
516 * @param distance distance between the connected peers
517 * @param first_cfg config for the first daemon
518 * @param second_cfg config for the second daemon
519 * @param first_daemon handle for the first daemon
520 * @param second_daemon handle for the second daemon
521 * @param emsg error message (NULL on success)
524 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
525 const struct GNUNET_PeerIdentity *second, uint32_t distance,
526 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
527 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
528 struct GNUNET_TESTING_Daemon *first_daemon,
529 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
535 GNUNET_PEER_intern (first);
536 GNUNET_PEER_intern (second);
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "test: Problem with new connection (%s)\n", emsg);
548 * run: load configuration options and schedule test to run (start peergroup)
551 * @param cfgfile configuration file name (can be NULL)
552 * @param cfg configuration handle
555 run (void *cls, char *const *args, const char *cfgfile,
556 const struct GNUNET_CONFIGURATION_Handle *cfg)
559 struct GNUNET_TESTING_Host *hosts;
563 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
565 GNUNET_log_setup ("test_dht_monitor",
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
575 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
576 "use_progressbars", "YES");
580 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
581 "num_peers", &num_peers))
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 "Option TESTING:NUM_PEERS is required!\n");
589 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
590 "topology_output_file",
593 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
594 "Option test_dht_monitor:topology_output_file is required!\n");
599 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_topo",
604 GNUNET_DISK_file_open (data_filename,
605 GNUNET_DISK_OPEN_READWRITE |
606 GNUNET_DISK_OPEN_CREATE,
607 GNUNET_DISK_PERM_USER_READ |
608 GNUNET_DISK_PERM_USER_WRITE);
609 if (data_file == NULL)
611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
613 GNUNET_free (data_filename);
618 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_topo",
619 "output_file", &temp_str))
622 GNUNET_DISK_file_open (temp_str,
623 GNUNET_DISK_OPEN_READWRITE |
624 GNUNET_DISK_OPEN_CREATE,
625 GNUNET_DISK_PERM_USER_READ |
626 GNUNET_DISK_PERM_USER_WRITE);
627 if (output_file == NULL)
628 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
631 GNUNET_free_non_null (temp_str);
633 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
635 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
636 &connect_cb, &peergroup_ready, NULL,
638 GNUNET_assert (pg != NULL);
640 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
641 &shutdown_task, NULL);
647 * test_dht_monitor command line options
649 static struct GNUNET_GETOPT_CommandLineOption options[] = {
650 {'V', "verbose", NULL,
651 gettext_noop ("be verbose (print progress information)"),
652 0, &GNUNET_GETOPT_set_one, &verbose},
653 GNUNET_GETOPT_OPTION_END
661 main (int xargc, char *xargv[])
663 char *const argv[] = { "test-dht-monitor",
665 "test_dht_line.conf",
673 GNUNET_PROGRAM_run (sizeof (argv) / sizeof (char *) - 1, argv,
675 gettext_noop ("Test dht monitoring in a line."),
676 options, &run, NULL);
678 GNUNET_DISK_directory_remove ("/tmp/test_dht_monitor");
682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
687 /* end of test_dht_monitor.c */