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 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 struct GNUNET_TESTING_Daemon *d1;
112 struct GNUNET_TESTING_Daemon *d2;
114 struct GNUNET_DHT_Handle **hs;
116 struct GNUNET_DHT_MonitorHandle **mhs;
118 struct GNUNET_DHT_GetHandle *get_h_far;
120 const char *id_origin = "FC74";
121 const char *id_far = "2UVH";
123 struct GNUNET_TESTING_Daemon *d_far;
124 struct GNUNET_TESTING_Daemon *o;
126 unsigned int monitor_counter;
131 * Check whether peers successfully shut down.
134 shutdown_callback (void *cls, const char *emsg)
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: Shutdown of peers failed: %s\n",
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "test: All peers successfully shut down!\n");
147 GNUNET_CONFIGURATION_destroy (testing_cfg);
152 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
155 if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
157 GNUNET_SCHEDULER_cancel (disconnect_task);
158 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
160 if (data_file != NULL)
161 GNUNET_DISK_file_close (data_file);
162 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
167 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
172 disconnect_task = GNUNET_SCHEDULER_NO_TASK;
173 GNUNET_SCHEDULER_cancel (put_task);
174 if (NULL != get_h_far)
175 GNUNET_DHT_get_stop (get_h_far);
176 for (i = 0; i < num_peers; i++)
178 GNUNET_DHT_monitor_stop(mhs[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 struct 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, enum GNUNET_BLOCK_Type type,
192 size_t size, const void *data)
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197 "test: ************* FOUND!!! ***********\n");
198 if (sizeof (struct GNUNET_HashCode) == size)
200 const struct GNUNET_HashCode *h = data;
202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Contents: %s\n",
203 GNUNET_h2s_full (h));
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: PATH: (get %u, put %u)\n",
207 get_path_length, put_path_length);
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: LOCAL\n");
209 for (i = get_path_length - 1; i >= 0; i--)
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
212 GNUNET_i2s (&get_path[i]));
214 for (i = put_path_length - 1; i >= 0; i--)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: %s\n",
217 GNUNET_i2s (&put_path[i]));
219 if (monitor_counter >= get_path_length + put_path_length)
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "expected at least %u hops, got %u\n",
223 get_path_length + put_path_length, monitor_counter);
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "expected at least %u hops, got %u\n",
227 get_path_length + put_path_length, monitor_counter);
228 GNUNET_SCHEDULER_cancel (disconnect_task);
229 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
233 * Start test: start GET request from the first node in the line looking for
234 * the ID of the last node in the line.
236 * @param cls Closure (not used).
237 * @param tc Task context.
240 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
247 in_test = GNUNET_YES;
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: looking for %s\n",
250 GNUNET_h2s_full (&d_far->id.hashPubKey));
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: from %s\n",
252 GNUNET_h2s_full (&o->id.hashPubKey));
253 get_h_far = GNUNET_DHT_get_start (hs[0],
254 GNUNET_BLOCK_TYPE_TEST, /* type */
255 &d_far->id.hashPubKey, /*key to search */
256 4U, /* replication level */
257 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, NULL, /* xquery */
259 &dht_get_id_handler, NULL);
260 GNUNET_SCHEDULER_cancel (disconnect_task);
262 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
267 * Periodic function used to put the ID of the far peer in the DHT.
269 * @param cls Closure (not used).
270 * @param tc Task context.
273 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 struct GNUNET_TESTING_Daemon *d;
277 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
279 put_task = GNUNET_SCHEDULER_NO_TASK;
283 d = GNUNET_TESTING_daemon_get (pg, 4);
284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting into DHT: %s\n",
285 GNUNET_h2s_full (&d->id.hashPubKey));
286 GNUNET_DHT_put (hs[4], &d->id.hashPubKey, 10U,
287 GNUNET_DHT_RO_RECORD_ROUTE |
288 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
289 GNUNET_BLOCK_TYPE_TEST, sizeof (struct GNUNET_PeerIdentity),
290 (const char *) &d->id, GNUNET_TIME_UNIT_FOREVER_ABS,
291 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
293 put_task = GNUNET_SCHEDULER_add_delayed (PUT_FREQUENCY, &put_id, NULL);
297 * Callback called on each GET request going through the DHT.
298 * Prints the info about the intercepted packet and increments a counter.
300 * @param cls Closure.
301 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
302 * @param type The type of data in the request.
303 * @param hop_count Hop count so far.
304 * @param path_length number of entries in path (or 0 if not recorded).
305 * @param path peers on the GET path (or NULL if not recorded).
306 * @param desired_replication_level Desired replication level.
307 * @param key Key of the requested data.
310 monitor_get_cb (void *cls,
311 enum GNUNET_DHT_RouteOption options,
312 enum GNUNET_BLOCK_Type type,
314 uint32_t desired_replication_level,
315 unsigned int path_length,
316 const struct GNUNET_PeerIdentity *path,
317 const struct GNUNET_HashCode * key)
322 i = (unsigned int) (long) cls;
323 s_key = GNUNET_h2s(key);
324 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
325 "%u got a GET message for key %s\n",
328 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
334 * Callback called on each PUT request going through the DHT.
335 * Prints the info about the intercepted packet and increments a counter.
337 * @param cls Closure.
338 * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
339 * @param type The type of data in the request.
340 * @param hop_count Hop count so far.
341 * @param path_length number of entries in path (or 0 if not recorded).
342 * @param path peers on the PUT path (or NULL if not recorded).
343 * @param desired_replication_level Desired replication level.
344 * @param exp Expiration time of the data.
345 * @param key Key under which data is to be stored.
346 * @param data Pointer to the data carried.
347 * @param size Number of bytes in data.
350 monitor_put_cb (void *cls,
351 enum GNUNET_DHT_RouteOption options,
352 enum GNUNET_BLOCK_Type type,
354 uint32_t desired_replication_level,
355 unsigned int path_length,
356 const struct GNUNET_PeerIdentity *path,
357 struct GNUNET_TIME_Absolute exp,
358 const struct GNUNET_HashCode * key,
365 i = (unsigned int) (long) cls;
366 s_key = GNUNET_h2s(key);
368 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
369 "%u got a PUT message for key %s with %u bytes\n",
372 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
378 * Callback called on each GET reply going through the DHT.
379 * Prints the info about the intercepted packet and increments a counter.
381 * @param cls Closure.
382 * @param type The type of data in the result.
383 * @param get_path Peers on GET path (or NULL if not recorded).
384 * @param get_path_length number of entries in get_path.
385 * @param put_path peers on the PUT path (or NULL if not recorded).
386 * @param put_path_length number of entries in get_path.
387 * @param exp Expiration time of the data.
388 * @param key Key of the data.
389 * @param data Pointer to the result data.
390 * @param size Number of bytes in data.
393 monitor_res_cb (void *cls,
394 enum GNUNET_BLOCK_Type type,
395 const struct GNUNET_PeerIdentity *get_path,
396 unsigned int get_path_length,
397 const struct GNUNET_PeerIdentity *put_path,
398 unsigned int put_path_length,
399 struct GNUNET_TIME_Absolute exp,
400 const struct GNUNET_HashCode * key,
407 i = (unsigned int) (long) cls;
408 s_key = GNUNET_h2s(key);
409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410 "%u got a REPLY message for key %s with %u bytes\n",
413 if (strncmp (s_key, id_far, 4) == 0 && in_test == GNUNET_YES)
419 * peergroup_ready: start test when all peers are connected
422 * @param emsg error message
425 peergroup_ready (void *cls, const char *emsg)
427 struct GNUNET_TESTING_Daemon *d;
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "test: Peergroup callback called with error, aborting test!\n");
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
439 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "test: Peer Group started successfully with %u connections\n",
445 if (data_file != NULL)
448 buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
450 GNUNET_DISK_file_write (data_file, buf, buf_len);
453 peers_running = GNUNET_TESTING_daemons_running (pg);
455 GNUNET_assert (peers_running == num_peers);
456 hs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_Handle *));
457 mhs = GNUNET_malloc (num_peers * sizeof (struct GNUNET_DHT_MonitorHandle *));
459 o = GNUNET_TESTING_daemon_get (pg, 0);
460 d_far = GNUNET_TESTING_daemon_get (pg, 4);
462 for (i = 0; i < num_peers; i++)
464 d = GNUNET_TESTING_daemon_get (pg, i);
465 hs[i] = GNUNET_DHT_connect (d->cfg, 32);
466 mhs[i] = GNUNET_DHT_monitor_start(hs[i],
467 GNUNET_BLOCK_TYPE_ANY,
475 if ((NULL == o) || (NULL == d_far))
477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
478 "test: Error getting daemons from pg\n");
479 GNUNET_SCHEDULER_cancel (disconnect_task);
480 disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
484 put_task = GNUNET_SCHEDULER_add_now (&put_id, NULL);
486 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
487 (GNUNET_TIME_UNIT_SECONDS, 2), &do_test,
490 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &disconnect_peers, NULL);
496 * Function that will be called whenever two daemons are connected by
497 * the testing library.
500 * @param first peer id for first daemon
501 * @param second peer id for the second daemon
502 * @param distance distance between the connected peers
503 * @param first_cfg config for the first daemon
504 * @param second_cfg config for the second daemon
505 * @param first_daemon handle for the first daemon
506 * @param second_daemon handle for the second daemon
507 * @param emsg error message (NULL on success)
510 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
511 const struct GNUNET_PeerIdentity *second, uint32_t distance,
512 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
513 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
514 struct GNUNET_TESTING_Daemon *first_daemon,
515 struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
521 GNUNET_PEER_intern (first);
522 GNUNET_PEER_intern (second);
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "test: Problem with new connection (%s)\n", emsg);
534 * run: load configuration options and schedule test to run (start peergroup)
537 * @param cfgfile configuration file name (can be NULL)
538 * @param cfg configuration handle
541 run (void *cls, char *const *args, const char *cfgfile,
542 const struct GNUNET_CONFIGURATION_Handle *cfg)
545 struct GNUNET_TESTING_Host *hosts;
549 testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
551 GNUNET_log_setup ("test_dht_monitor",
555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
556 GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
557 "use_progressbars", "YES");
559 GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
560 "num_peers", &num_peers))
562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
563 "Option TESTING:NUM_PEERS is required!\n");
567 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing_old",
568 "topology_output_file",
571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
572 "Option test_dht_monitor:topology_output_file is required!\n");
577 GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_topo",
582 GNUNET_DISK_file_open (data_filename,
583 GNUNET_DISK_OPEN_READWRITE |
584 GNUNET_DISK_OPEN_CREATE,
585 GNUNET_DISK_PERM_USER_READ |
586 GNUNET_DISK_PERM_USER_WRITE);
587 if (data_file == NULL)
589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
591 GNUNET_free (data_filename);
596 GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_topo",
597 "output_file", &temp_str))
600 GNUNET_DISK_file_open (temp_str,
601 GNUNET_DISK_OPEN_READWRITE |
602 GNUNET_DISK_OPEN_CREATE,
603 GNUNET_DISK_PERM_USER_READ |
604 GNUNET_DISK_PERM_USER_WRITE);
605 if (output_file == NULL)
606 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
609 GNUNET_free_non_null (temp_str);
611 hosts = GNUNET_TESTING_hosts_load (testing_cfg);
613 pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
614 &connect_cb, &peergroup_ready, NULL,
616 GNUNET_assert (pg != NULL);
618 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
619 &shutdown_task, NULL);
625 * test_dht_monitor command line options
627 static struct GNUNET_GETOPT_CommandLineOption options[] = {
628 {'V', "verbose", NULL,
629 gettext_noop ("be verbose (print progress information)"),
630 0, &GNUNET_GETOPT_set_one, &verbose},
631 GNUNET_GETOPT_OPTION_END
639 main (int xargc, char *xargv[])
641 char *const argv[] = { "test-dht-monitor",
643 "test_dht_line.conf",
648 GNUNET_PROGRAM_run (sizeof (argv) / sizeof (char *) - 1, argv,
650 gettext_noop ("Test dht monitoring in a line."),
651 options, &run, NULL);
653 GNUNET_DISK_directory_remove ("/tmp/test_dht_monitor");
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
662 /* end of test_dht_monitor.c */