2 This file is part of GNUnet.
3 (C) 2011, 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_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 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)
42 #define GET_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
44 #define PUT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
54 * Total number of peers in the test.
56 static unsigned long long num_peers;
59 * Global configuration file
61 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
64 * Total number of currently running peers.
66 static unsigned long long peers_running;
69 * Total number of connections in the whole network.
71 static unsigned int total_connections;
74 * The currently running peer group.
76 static struct GNUNET_TESTING_PeerGroup *pg;
79 * File to report results to.
81 static struct GNUNET_DISK_FileHandle *output_file;
84 * File to log connection info, statistics to.
86 static struct GNUNET_DISK_FileHandle *data_file;
89 * Task called to disconnect peers.
91 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
94 * Task To perform tests
96 static GNUNET_SCHEDULER_TaskIdentifier test_task;
101 static GNUNET_SCHEDULER_TaskIdentifier put_task;
104 * Task called to shutdown test.
106 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
108 static char *topology_file;
110 static struct GNUNET_TESTING_Daemon *d1;
112 static struct GNUNET_TESTING_Daemon *d2;
114 static struct GNUNET_DHT_Handle **hs;
116 static struct GNUNET_DHT_MonitorHandle **mhs;
118 static struct GNUNET_DHT_GetHandle *get_h_far;
120 static const char *id_origin = "FC74";
122 static const char *id_far = "2UVH";
124 static struct GNUNET_TESTING_Daemon *d_far;
126 static struct GNUNET_TESTING_Daemon *o;
128 static unsigned int monitor_counter;
130 static unsigned int monitor_expect = UINT_MAX;
136 * Check whether peers successfully shut down.
139 shutdown_callback (void *cls, const char *emsg)
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Shutdown of peers failed: %s\n",
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "test: All peers successfully shut down!\n");
152 GNUNET_CONFIGURATION_destroy (testing_cfg);
157 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
160 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
162 GNUNET_SCHEDULER_cancel (disconnect_task);
163 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
165 if (data_file != NULL)
166 GNUNET_DISK_file_close (data_file);
167 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
172 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
177 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
178 GNUNET_SCHEDULER_cancel (put_task);
179 if (NULL != get_h_far)
180 GNUNET_DHT_get_stop (get_h_far);
181 for (i = 0; i < num_peers; i++)
183 GNUNET_DHT_monitor_stop(mhs[i]);
184 GNUNET_DHT_disconnect (hs[i]);
186 GNUNET_SCHEDULER_cancel (shutdown_handle);
187 shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
192 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
193 const struct GNUNET_HashCode * key,
194 const struct GNUNET_PeerIdentity *get_path,
195 unsigned int get_path_length,
196 const struct GNUNET_PeerIdentity *put_path,
197 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
198 size_t size, const void *data)
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203 "test: ************* FOUND!!! ***********\n");
204 if (sizeof (struct GNUNET_HashCode) == size)
206 const struct GNUNET_HashCode *h = data;
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Contents: %s\n",
209 GNUNET_h2s_full (h));
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: PATH: (get %u, put %u)\n",
213 get_path_length, put_path_length);
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: LOCAL\n");
215 for (i = get_path_length - 1; i >= 0; i--)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
218 GNUNET_i2s (&get_path[i]));
220 for (i = put_path_length - 1; i >= 0; i--)
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
223 GNUNET_i2s (&put_path[i]));
225 monitor_expect = get_path_length + put_path_length;
226 if (monitor_counter >= monitor_expect)
229 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "expected at least %u hops, got %u\n",
230 get_path_length + put_path_length, monitor_counter);
231 GNUNET_SCHEDULER_cancel (disconnect_task);
232 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
238 * Start test: start GET request from the first node in the line looking for
239 * the ID of the last node in the line.
241 * @param cls Closure (not used).
242 * @param tc Task context.
245 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
252 in_test = GNUNET_YES;
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: looking for %s\n",
255 GNUNET_h2s_full (&d_far->id.hashPubKey));
256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: from %s\n",
257 GNUNET_h2s_full (&o->id.hashPubKey));
258 get_h_far = GNUNET_DHT_get_start (hs[0],
259 GNUNET_BLOCK_TYPE_TEST, /* type */
260 &d_far->id.hashPubKey, /*key to search */
261 4U, /* replication level */
262 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
264 &dht_get_id_handler, NULL);
265 GNUNET_SCHEDULER_cancel (disconnect_task);
267 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
272 * Periodic function used to put the ID of the far peer in the DHT.
274 * @param cls Closure (not used).
275 * @param tc Task context.
278 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
280 struct GNUNET_TESTING_Daemon *d;
282 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
284 put_task = GNUNET_SCHEDULER_NO_TASK;
288 d = GNUNET_TESTING_daemon_get (pg, 4);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting into DHT: %s\n",
290 GNUNET_h2s_full (&d->id.hashPubKey));
291 GNUNET_DHT_put (hs[4], &d->id.hashPubKey, 10U,
292 GNUNET_DHT_RO_RECORD_ROUTE |
293 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
294 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
295 (const char *) &d->id, GNUNET_TIME_UNIT_FOREVER_ABS,
296 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
298 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, &put_id, NULL);
303 * Callback called on each GET request going through the DHT.
304 * Prints the info about the intercepted packet and increments a counter.
306 * @param cls Closure.
307 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
308 * @param type The type of data in the request.
309 * @param hop_count Hop count so far.
310 * @param path_length number of entries in path (or 0 if not recorded).
311 * @param path peers on the GET path (or NULL if not recorded).
312 * @param desired_replication_level Desired replication level.
313 * @param key Key of the requested data.
316 monitor_get_cb (void *cls,
317 enum GNUNET_DHT_RouteOption options,
318 enum GNUNET_BLOCK_Type type,
320 uint32_t desired_replication_level,
321 unsigned int path_length,
322 const struct GNUNET_PeerIdentity *path,
323 const struct GNUNET_HashCode * key)
328 i = (unsigned int) (long) cls;
329 s_key = GNUNET_h2s(key);
330 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
331 "%u got a GET message for key %s\n",
334 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
337 if (monitor_counter >= monitor_expect)
340 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "expected at least %u hops, got %u\n",
341 monitor_expect, monitor_counter);
342 GNUNET_SCHEDULER_cancel (disconnect_task);
343 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
350 * Callback called on each PUT request going through the DHT.
351 * Prints the info about the intercepted packet and increments a counter.
353 * @param cls Closure.
354 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
355 * @param type The type of data in the request.
356 * @param hop_count Hop count so far.
357 * @param path_length number of entries in path (or 0 if not recorded).
358 * @param path peers on the PUT path (or NULL if not recorded).
359 * @param desired_replication_level Desired replication level.
360 * @param exp Expiration time of the data.
361 * @param key Key under which data is to be stored.
362 * @param data Pointer to the data carried.
363 * @param size Number of bytes in data.
366 monitor_put_cb (void *cls,
367 enum GNUNET_DHT_RouteOption options,
368 enum GNUNET_BLOCK_Type type,
370 uint32_t desired_replication_level,
371 unsigned int path_length,
372 const struct GNUNET_PeerIdentity *path,
373 struct GNUNET_TIME_Absolute exp,
374 const struct GNUNET_HashCode * key,
381 i = (unsigned int) (long) cls;
382 s_key = GNUNET_h2s(key);
384 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
385 "%u got a PUT message for key %s with %u bytes\n",
388 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
394 * Callback called on each GET reply going through the DHT.
395 * Prints the info about the intercepted packet and increments a counter.
397 * @param cls Closure.
398 * @param type The type of data in the result.
399 * @param get_path Peers on GET path (or NULL if not recorded).
400 * @param get_path_length number of entries in get_path.
401 * @param put_path peers on the PUT path (or NULL if not recorded).
402 * @param put_path_length number of entries in get_path.
403 * @param exp Expiration time of the data.
404 * @param key Key of the data.
405 * @param data Pointer to the result data.
406 * @param size Number of bytes in data.
409 monitor_res_cb (void *cls,
410 enum GNUNET_BLOCK_Type type,
411 const struct GNUNET_PeerIdentity *get_path,
412 unsigned int get_path_length,
413 const struct GNUNET_PeerIdentity *put_path,
414 unsigned int put_path_length,
415 struct GNUNET_TIME_Absolute exp,
416 const struct GNUNET_HashCode * key,
423 i = (unsigned int) (long) cls;
424 s_key = GNUNET_h2s(key);
425 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
426 "%u got a REPLY message for key %s with %u bytes\n",
429 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
435 * peergroup_ready: start test when all peers are connected
438 * @param emsg error message
441 peergroup_ready (void *cls, const char *emsg)
443 struct GNUNET_TESTING_Daemon *d;
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "test: Peergroup callback called with error, aborting test!\n");
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
455 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "test: Peer Group started successfully with %u connections\n",
461 if (data_file != NULL)
464 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
466 GNUNET_DISK_file_write (data_file, buf, buf_len);
469 peers_running = GNUNET_TESTING_daemons_running (pg);
471 GNUNET_assert (peers_running == num_peers);
472 hs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
473 mhs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_MonitorHandle *));
475 o = GNUNET_TESTING_daemon_get (pg, 0);
476 d_far = GNUNET_TESTING_daemon_get (pg, 4);
478 for (i = 0; i < num_peers; i++)
480 d = GNUNET_TESTING_daemon_get (pg, i);
481 hs[i] = GNUNET_DHT_connect (d->cfg, 32);
482 mhs[i] = GNUNET_DHT_monitor_start(hs[i],
483 GNUNET_BLOCK_TYPE_ANY,
491 if ((NULL == o) || (NULL == d_far))
493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
494 "test: Error getting daemons from pg\n");
495 GNUNET_SCHEDULER_cancel (disconnect_task);
496 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
500 put_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
501 (GNUNET_TIME_UNIT_SECONDS, 3),
504 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
505 (GNUNET_TIME_UNIT_SECONDS, 6), &do_test,
508 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
514 * Function that will be called whenever two daemons are connected by
515 * the testing library.
518 * @param first peer id for first daemon
519 * @param second peer id for the second daemon
520 * @param distance distance between the connected peers
521 * @param first_cfg config for the first daemon
522 * @param second_cfg config for the second daemon
523 * @param first_daemon handle for the first daemon
524 * @param second_daemon handle for the second daemon
525 * @param emsg error message (NULL on success)
528 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
529 const struct GNUNET_PeerIdentity *second, uint32_t distance,
530 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
531 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
532 struct GNUNET_TESTING_Daemon *first_daemon,
533 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
539 GNUNET_PEER_intern (first);
540 GNUNET_PEER_intern (second);
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "test: Problem with new connection (%s)\n", emsg);
551 * run: load configuration options and schedule test to run (start peergroup)
554 * @param cfgfile configuration file name (can be NULL)
555 * @param cfg configuration handle
558 run (void *cls, char *const *args, const char *cfgfile,
559 const struct GNUNET_CONFIGURATION_Handle *cfg)
562 struct GNUNET_TESTING_Host *hosts;
566 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
568 GNUNET_log_setup ("test_dht_monitor",
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
573 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
574 "use_progressbars", "YES");
576 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
577 "num_peers", &num_peers))
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 "Option TESTING:NUM_PEERS is required!\n");
584 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing_old",
585 "topology_output_file",
588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
589 "Option test_dht_monitor:topology_output_file is required!\n");
594 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_topo",
599 GNUNET_DISK_file_open (data_filename,
600 GNUNET_DISK_OPEN_READWRITE |
601 GNUNET_DISK_OPEN_CREATE,
602 GNUNET_DISK_PERM_USER_READ |
603 GNUNET_DISK_PERM_USER_WRITE);
604 if (data_file == NULL)
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
608 GNUNET_free (data_filename);
613 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_topo",
614 "output_file", &temp_str))
617 GNUNET_DISK_file_open (temp_str,
618 GNUNET_DISK_OPEN_READWRITE |
619 GNUNET_DISK_OPEN_CREATE,
620 GNUNET_DISK_PERM_USER_READ |
621 GNUNET_DISK_PERM_USER_WRITE);
622 if (output_file == NULL)
623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
626 GNUNET_free_non_null (temp_str);
628 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
630 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
631 &connect_cb, &peergroup_ready, NULL,
633 GNUNET_assert (pg != NULL);
635 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
636 &shutdown_task, NULL);
641 * test_dht_monitor command line options
643 static struct GNUNET_GETOPT_CommandLineOption options[] = {
644 {'V', "verbose", NULL,
645 gettext_noop ("be verbose (print progress information)"),
646 0, &GNUNET_GETOPT_set_one, &verbose},
647 GNUNET_GETOPT_OPTION_END
655 main (int xargc, char *xargv[])
657 char *const argv[] = { "test-dht-monitor",
659 "test_dht_line.conf",
664 GNUNET_PROGRAM_run (sizeof (argv) / sizeof (char *) - 1, argv,
666 gettext_noop ("Test dht monitoring in a line."),
667 options, &run, NULL);
669 GNUNET_DISK_directory_remove ("/tmp/test_dht_monitor");
673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
678 /* end of test_dht_monitor.c */