2 This file is part of GNUnet.
3 (C) 2009 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_multipeer.c
22 * @brief testcase for testing DHT service with
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
31 #define VERBOSE GNUNET_NO
33 /* Timeout for entire testcase */
34 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
36 /* Timeout for waiting for replies to get requests */
37 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
39 /* Timeout for waiting for gets to complete */
40 #define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
42 /* Timeout for waiting for puts to complete */
43 #define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
45 /* If number of peers not in config file, use this number */
46 #define DEFAULT_NUM_PEERS 10
48 #define TEST_DATA_SIZE 8
50 #define MAX_OUTSTANDING_PUTS 10
52 #define MAX_OUTSTANDING_GETS 10
54 #define PATH_TRACKING GNUNET_YES
61 * This is a linked list
63 struct TestPutContext *next;
66 * Handle to the first peers DHT service (via the API)
68 struct GNUNET_DHT_Handle *dht_handle;
71 * Handle to the PUT peer daemon
73 struct GNUNET_TESTING_Daemon *daemon;
76 * Identifier for this PUT
81 * Task for disconnecting DHT handles
83 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
88 /* This is a linked list */
89 struct TestGetContext *next;
92 * Handle to the first peers DHT service (via the API)
94 struct GNUNET_DHT_Handle *dht_handle;
97 * Handle for the DHT get request
99 struct GNUNET_DHT_GetHandle *get_handle;
102 * Handle to the GET peer daemon
104 struct GNUNET_TESTING_Daemon *daemon;
107 * Identifier for this GET
112 * Task for disconnecting DHT handles (and stopping GET)
114 GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
117 * Whether or not this request has been fulfilled already.
125 * List of GETS to perform
127 struct TestGetContext *all_gets;
130 * List of PUTS to perform
132 struct TestPutContext *all_puts;
135 * Handle to the set of all peers run for this test.
137 static struct GNUNET_TESTING_PeerGroup *pg;
140 * Total number of peers to run, set based on config file.
142 static unsigned long long num_peers;
145 * How many puts do we currently have in flight?
147 static unsigned long long outstanding_puts;
150 * How many puts are done?
152 static unsigned long long puts_completed;
155 * How many puts do we currently have in flight?
157 static unsigned long long outstanding_gets;
160 * How many gets are done?
162 static unsigned long long gets_completed;
165 * How many gets failed?
167 static unsigned long long gets_failed;
170 * Directory to remove on shutdown.
172 static char *test_directory;
175 * Option to use when routing.
177 static enum GNUNET_DHT_RouteOption route_option;
180 * Task handle to use to schedule test failure / success.
182 static GNUNET_SCHEDULER_TaskIdentifier die_task;
184 /* Global return value (0 for success, anything else for failure) */
188 * Check whether peers successfully shut down.
191 shutdown_callback (void *cls, const char *emsg)
196 "Failed to shutdown testing topology: %s\n",
204 * Task to release DHT handles for PUT
207 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 struct TestPutContext *test_put = cls;
211 test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
212 GNUNET_DHT_disconnect (test_put->dht_handle);
213 test_put->dht_handle = NULL;
217 * Function scheduled to be run on the successful completion of this
221 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 GNUNET_assert (pg != NULL);
224 struct TestPutContext *test_put = all_puts;
225 struct TestGetContext *test_get = all_gets;
227 while (test_put != NULL)
229 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
230 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
231 if (test_put->dht_handle != NULL)
232 GNUNET_DHT_disconnect (test_put->dht_handle);
233 test_put = test_put->next;
236 while (test_get != NULL)
238 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
239 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
240 if (test_get->get_handle != NULL)
241 GNUNET_DHT_get_stop (test_get->get_handle);
242 if (test_get->dht_handle != NULL)
243 GNUNET_DHT_disconnect (test_get->dht_handle);
244 test_get = test_get->next;
247 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
253 * Check if the get_handle is being used, if so stop the request. Either
254 * way, schedule the end_badly_cont function which actually shuts down the
258 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
260 const char *emsg = cls;
263 "Failing test with error: `%s'!\n",
266 struct TestPutContext *test_put = all_puts;
267 struct TestGetContext *test_get = all_gets;
269 while (test_put != NULL)
271 if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
272 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
273 if (test_put->dht_handle != NULL)
274 GNUNET_DHT_disconnect (test_put->dht_handle);
275 test_put = test_put->next;
278 while (test_get != NULL)
280 if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
281 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
282 if (test_get->get_handle != NULL)
283 GNUNET_DHT_get_stop (test_get->get_handle);
284 if (test_get->dht_handle != NULL)
285 GNUNET_DHT_disconnect (test_get->dht_handle);
286 test_get = test_get->next;
289 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
295 * Task to release get handle.
298 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
300 struct TestGetContext *test_get = cls;
301 GNUNET_HashCode search_key; /* Key stored under */
302 char original_data[TEST_DATA_SIZE]; /* Made up data to store */
304 test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
305 memset (original_data, test_get->uid, sizeof (original_data));
306 GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
308 if (test_get->succeeded != GNUNET_YES)
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Get from peer %s for key %s failed!\n",
313 test_get->daemon->shortname, GNUNET_h2s (&search_key));
315 GNUNET_assert (test_get->get_handle != NULL);
316 GNUNET_DHT_get_stop (test_get->get_handle);
317 test_get->get_handle = NULL;
319 outstanding_gets--; /* GET is really finished */
320 GNUNET_DHT_disconnect (test_get->dht_handle);
321 test_get->dht_handle = NULL;
324 "%llu gets succeeded, %llu gets failed!\n",
325 gets_completed, gets_failed);
326 if ((gets_failed > 0) && (outstanding_gets == 0)) /* Had some failures */
328 GNUNET_SCHEDULER_cancel (die_task);
330 GNUNET_SCHEDULER_add_now (&end_badly, "not all gets succeeded");
334 if ( (gets_completed == num_peers * num_peers) &&
335 (outstanding_gets == 0) ) /* All gets successful */
337 GNUNET_SCHEDULER_cancel (die_task);
338 //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &get_topology, NULL);
339 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
344 * Iterator called if the GET request initiated returns a response.
347 * @param exp when will this value expire
348 * @param key key of the result
349 * @param type type of the result
350 * @param size number of bytes in data
351 * @param data pointer to the result data
354 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
355 const GNUNET_HashCode * key,
356 const struct GNUNET_PeerIdentity *get_path,
357 unsigned int get_path_length,
358 const struct GNUNET_PeerIdentity *put_path,
359 unsigned int put_path_length,
360 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
362 struct TestGetContext *test_get = cls;
363 GNUNET_HashCode search_key; /* Key stored under */
364 char original_data[TEST_DATA_SIZE]; /* Made up data to store */
367 memset (original_data, test_get->uid, sizeof (original_data));
368 GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
370 if (test_get->succeeded == GNUNET_YES)
371 return; /* Get has already been successful, probably ending now */
374 if (put_path != NULL)
376 fprintf (stderr, "PUT (%u) Path: ",
378 for (i = 0; i<put_path_length; i++)
379 fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
380 fprintf (stderr, "\n");
382 if (get_path != NULL)
384 fprintf (stderr, "GET (%u) Path: ",
386 for (i = 0; i < get_path_length; i++)
387 fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
388 fprintf (stderr, "->%s\n",
389 GNUNET_i2s (&test_get->daemon->id));
393 if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
394 (0 != memcmp (original_data, data, sizeof (original_data))))
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Key or data is not the same as was inserted!\n");
402 test_get->succeeded = GNUNET_YES;
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
406 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
407 test_get->disconnect_task = GNUNET_SCHEDULER_add_now (&get_stop_task, test_get);
412 * Set up some data, and call API PUT function
415 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
417 struct TestGetContext *test_get = cls;
418 GNUNET_HashCode key; /* Made up key to store data under */
419 char data[TEST_DATA_SIZE]; /* Made up data to store */
421 if (test_get == NULL)
422 return; /* End of the list */
423 memset (data, test_get->uid, sizeof (data));
424 GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
426 if (outstanding_gets > MAX_OUTSTANDING_GETS)
428 GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
432 test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
433 /* Insert the data at the first peer */
434 GNUNET_assert (test_get->dht_handle != NULL);
436 test_get->get_handle =
437 GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
438 GNUNET_BLOCK_TYPE_TEST, &key,
439 1, route_option, NULL, 0,
440 &get_result_iterator, test_get);
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
443 test_get->uid, test_get->daemon->shortname);
445 test_get->disconnect_task =
446 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
447 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
451 * Called when the PUT request has been transmitted to the DHT service.
452 * Schedule the GET request for some time in the future.
455 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
457 struct TestPutContext *test_put = cls;
462 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
463 test_put->disconnect_task =
464 GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
465 if (puts_completed == num_peers)
467 GNUNET_assert (outstanding_puts == 0);
468 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
469 (GNUNET_TIME_UNIT_SECONDS, 10), &do_get,
476 * Set up some data, and call API PUT function
479 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
481 struct TestPutContext *test_put = cls;
482 GNUNET_HashCode key; /* Made up key to store data under */
483 char data[TEST_DATA_SIZE]; /* Made up data to store */
485 if (test_put == NULL)
486 return; /* End of list */
488 memset (data, test_put->uid, sizeof (data));
489 GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
491 if (outstanding_puts > MAX_OUTSTANDING_PUTS)
493 GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
499 test_put->uid, test_put->daemon->shortname);
501 test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
503 GNUNET_assert (test_put->dht_handle != NULL);
505 fprintf (stderr, "PUT %u at `%s'\n",
507 GNUNET_i2s (&test_put->daemon->id));
508 GNUNET_DHT_put (test_put->dht_handle, &key, 1,
509 route_option, GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
510 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
511 &put_finished, test_put);
512 test_put->disconnect_task =
513 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
514 &put_disconnect_task, test_put);
515 GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
521 * This function is called once testing has finished setting up the topology.
524 * @param emsg variable is NULL on success (peers connected), and non-NULL on
525 * failure (peers failed to connect).
528 run_dht_test (void *cls, const char *emsg)
530 unsigned long long i;
531 unsigned long long j;
532 struct TestPutContext *test_put;
533 struct TestGetContext *test_get;
538 "Failed to setup topology: %s\n",
541 GNUNET_SCHEDULER_add_now (&end_badly,
542 "topology setup failed");
547 route_option = GNUNET_DHT_RO_RECORD_ROUTE;
549 route_option = GNUNET_DHT_RO_NONE;
552 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
553 "from setup puts/gets");
555 "Issuing %llu PUTs (one per peer)\n",
557 for (i = 0; i < num_peers; i++)
559 test_put = GNUNET_malloc (sizeof (struct TestPutContext));
561 test_put->daemon = GNUNET_TESTING_daemon_get (pg, i);
562 test_put->next = all_puts;
565 GNUNET_SCHEDULER_add_now (&do_put, all_puts);
568 "Issuing %llu GETs\n",
569 num_peers * num_peers);
570 for (i = 0; i < num_peers; i++)
571 for (j = 0; j < num_peers; j++)
573 test_get = GNUNET_malloc (sizeof (struct TestGetContext));
575 test_get->daemon = GNUNET_TESTING_daemon_get (pg, j);
576 test_get->next = all_gets;
583 run (void *cls, char *const *args, const char *cfgfile,
584 const struct GNUNET_CONFIGURATION_Handle *cfg)
586 /* Get path from configuration file */
588 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
596 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
598 num_peers = DEFAULT_NUM_PEERS;
599 pg = GNUNET_TESTING_peergroup_start (cfg,
619 /* Arguments for GNUNET_PROGRAM_run */
620 char *const argv[] = { "test-dht-multipeer", /* Name to give running binary */
622 "test_dht_multipeer_data.conf", /* Config file to use */
628 struct GNUNET_GETOPT_CommandLineOption options[] = {
629 GNUNET_GETOPT_OPTION_END
631 /* Run the run function as a new program */
633 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
634 "test-dht-multipeer", "nohelp", options, &run, &ok);
635 if (ret != GNUNET_OK)
637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638 "`test-dht-multipeer': Failed with error code %d\n", ret);
645 main (int argc, char *argv[])
650 GNUNET_log_setup ("test-dht-multipeer",
659 * Need to remove base directory, subdirectories taken care
660 * of by the testing framework.
662 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
664 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665 "Failed to remove testing directory %s\n", test_directory);
670 /* end of test_dht_multipeer.c */