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%s\n",
389 get_path_length > 0 ? "->":"",
390 GNUNET_i2s (&test_get->daemon->id));
394 if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
395 (0 != memcmp (original_data, data, sizeof (original_data))))
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Key or data is not the same as was inserted!\n");
403 test_get->succeeded = GNUNET_YES;
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
407 GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
408 test_get->disconnect_task = GNUNET_SCHEDULER_add_now (&get_stop_task, test_get);
413 * Set up some data, and call API PUT function
416 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct TestGetContext *test_get = cls;
419 GNUNET_HashCode key; /* Made up key to store data under */
420 char data[TEST_DATA_SIZE]; /* Made up data to store */
422 if (test_get == NULL)
423 return; /* End of the list */
424 memset (data, test_get->uid, sizeof (data));
425 GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
427 if (outstanding_gets > MAX_OUTSTANDING_GETS)
429 GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
433 test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
434 /* Insert the data at the first peer */
435 GNUNET_assert (test_get->dht_handle != NULL);
437 test_get->get_handle =
438 GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
439 GNUNET_BLOCK_TYPE_TEST, &key,
440 1, route_option, NULL, 0,
441 &get_result_iterator, test_get);
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
444 test_get->uid, test_get->daemon->shortname);
446 test_get->disconnect_task =
447 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
448 GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
452 * Called when the PUT request has been transmitted to the DHT service.
453 * Schedule the GET request for some time in the future.
456 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
458 struct TestPutContext *test_put = cls;
463 GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
464 test_put->disconnect_task =
465 GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
466 if (puts_completed == num_peers)
468 GNUNET_assert (outstanding_puts == 0);
469 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
470 (GNUNET_TIME_UNIT_SECONDS, 10), &do_get,
477 * Set up some data, and call API PUT function
480 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
482 struct TestPutContext *test_put = cls;
483 GNUNET_HashCode key; /* Made up key to store data under */
484 char data[TEST_DATA_SIZE]; /* Made up data to store */
486 if (test_put == NULL)
487 return; /* End of list */
489 memset (data, test_put->uid, sizeof (data));
490 GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
492 if (outstanding_puts > MAX_OUTSTANDING_PUTS)
494 GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
500 test_put->uid, test_put->daemon->shortname);
502 test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
504 GNUNET_assert (test_put->dht_handle != NULL);
506 fprintf (stderr, "PUT %u at `%s'\n",
508 GNUNET_i2s (&test_put->daemon->id));
509 GNUNET_DHT_put (test_put->dht_handle, &key, 1,
510 route_option, GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
511 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
512 &put_finished, test_put);
513 test_put->disconnect_task =
514 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
515 &put_disconnect_task, test_put);
516 GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
522 * This function is called once testing has finished setting up the topology.
525 * @param emsg variable is NULL on success (peers connected), and non-NULL on
526 * failure (peers failed to connect).
529 run_dht_test (void *cls, const char *emsg)
531 unsigned long long i;
532 unsigned long long j;
533 struct TestPutContext *test_put;
534 struct TestGetContext *test_get;
539 "Failed to setup topology: %s\n",
542 GNUNET_SCHEDULER_add_now (&end_badly,
543 "topology setup failed");
548 route_option = GNUNET_DHT_RO_RECORD_ROUTE;
550 route_option = GNUNET_DHT_RO_NONE;
553 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
554 "from setup puts/gets");
556 "Issuing %llu PUTs (one per peer)\n",
558 for (i = 0; i < num_peers; i++)
560 test_put = GNUNET_malloc (sizeof (struct TestPutContext));
562 test_put->daemon = GNUNET_TESTING_daemon_get (pg, i);
563 test_put->next = all_puts;
566 GNUNET_SCHEDULER_add_now (&do_put, all_puts);
569 "Issuing %llu GETs\n",
570 num_peers * num_peers);
571 for (i = 0; i < num_peers; i++)
572 for (j = 0; j < num_peers; j++)
574 test_get = GNUNET_malloc (sizeof (struct TestGetContext));
576 test_get->daemon = GNUNET_TESTING_daemon_get (pg, j);
577 test_get->next = all_gets;
584 run (void *cls, char *const *args, const char *cfgfile,
585 const struct GNUNET_CONFIGURATION_Handle *cfg)
587 /* Get path from configuration file */
589 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
597 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
599 num_peers = DEFAULT_NUM_PEERS;
600 pg = GNUNET_TESTING_peergroup_start (cfg,
620 /* Arguments for GNUNET_PROGRAM_run */
621 char *const argv[] = { "test-dht-multipeer", /* Name to give running binary */
623 "test_dht_multipeer_data.conf", /* Config file to use */
629 struct GNUNET_GETOPT_CommandLineOption options[] = {
630 GNUNET_GETOPT_OPTION_END
632 /* Run the run function as a new program */
634 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
635 "test-dht-multipeer", "nohelp", options, &run, &ok);
636 if (ret != GNUNET_OK)
638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
639 "`test-dht-multipeer': Failed with error code %d\n", ret);
646 main (int argc, char *argv[])
651 GNUNET_log_setup ("test-dht-multipeer",
660 * Need to remove base directory, subdirectories taken care
661 * of by the testing framework.
663 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
666 "Failed to remove testing directory %s\n", test_directory);
671 /* end of test_dht_multipeer.c */