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_SECONDS, 300)
40 #define START_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
42 /* Timeout for waiting for gets to complete */
43 #define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50)
45 /* Timeout for waiting for puts to complete */
46 #define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 50)
48 /* If number of peers not in config file, use this number */
49 #define DEFAULT_NUM_PEERS 10
51 #define TEST_DATA_SIZE 8
53 #define MAX_OUTSTANDING_PUTS 100
55 #define MAX_OUTSTANDING_GETS 100
57 #define PATH_TRACKING GNUNET_NO
64 * This is a linked list
66 struct TestPutContext *next;
69 * This is a linked list
71 struct TestPutContext *prev;
74 * Handle to the first peers DHT service (via the API)
76 struct GNUNET_DHT_Handle *dht_handle;
79 * Handle to the PUT peer daemon
81 struct GNUNET_TESTING_Daemon *daemon;
84 * Identifier for this PUT
89 * Task handle for processing of the put.
91 GNUNET_SCHEDULER_TaskIdentifier task;
98 * This is a linked list
100 struct TestGetContext *next;
103 * This is a linked list
105 struct TestGetContext *prev;
108 * Handle to the first peers DHT service (via the API)
110 struct GNUNET_DHT_Handle *dht_handle;
113 * Handle for the DHT get request
115 struct GNUNET_DHT_GetHandle *get_handle;
118 * Handle to the GET peer daemon
120 struct GNUNET_TESTING_Daemon *daemon;
123 * Identifier for this GET
128 * Task for disconnecting DHT handles (and stopping GET)
130 GNUNET_SCHEDULER_TaskIdentifier task;
133 * Whether or not this request has been fulfilled already.
140 * List of GETS to perform
142 static struct TestGetContext *all_gets_head;
145 * List of GETS to perform
147 static struct TestGetContext *all_gets_tail;
150 * List of PUTS to perform
152 static struct TestPutContext *all_puts_head;
155 * List of PUTS to perform
157 static struct TestPutContext *all_puts_tail;
160 * Handle to the set of all peers run for this test.
162 static struct GNUNET_TESTING_PeerGroup *pg;
165 * Total number of peers to run, set based on config file.
167 static unsigned long long num_peers;
170 * How many puts do we currently have in flight?
172 static unsigned long long outstanding_puts;
175 * How many puts are done?
177 static unsigned long long puts_completed;
180 * How many puts do we currently have in flight?
182 static unsigned long long outstanding_gets;
185 * How many gets are done?
187 static unsigned long long gets_completed;
190 * How many gets failed?
192 static unsigned long long gets_failed;
195 * Directory to remove on shutdown.
197 static char *test_directory;
200 * Option to use when routing.
202 static enum GNUNET_DHT_RouteOption route_option;
205 * Task handle to use to schedule test failure / success.
207 static GNUNET_SCHEDULER_TaskIdentifier die_task;
210 * Global return value (0 for success, anything else for failure)
216 * Check whether peers successfully shut down.
219 shutdown_callback (void *cls, const char *emsg)
224 "Failed to shutdown testing topology: %s\n",
232 do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
234 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
240 * Master context for 'stat_run'.
244 struct GNUNET_STATISTICS_Handle *stat;
251 const char *subsystem;
253 unsigned long long total;
257 * Statistics we print out.
259 static struct StatValues stats[] = {
260 {"core", "# bytes decrypted", 0},
261 {"core", "# bytes encrypted", 0},
262 {"transport", "# bytes received via TCP", 0},
263 {"transport", "# bytes transmitted via TCP", 0},
264 {"dht", "# PUT messages queued for transmission"},
265 {"dht", "# P2P PUT requests received"},
266 {"dht", "# GET messages queued for transmission"},
267 {"dht", "# P2P GET requests received"},
268 {"dht", "# RESULT messages queued for transmission"},
269 {"dht", "# P2P RESULTS received"},
275 * Callback function to process statistic values.
278 * @param subsystem name of subsystem that created the statistic
279 * @param name the name of the datum
280 * @param value the current value
281 * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
282 * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
285 print_stat (void *cls, const char *subsystem, const char *name, uint64_t value,
288 struct StatMaster *sm = cls;
290 stats[sm->value].total += value;
291 fprintf (stderr, "Peer %2u: %12s/%50s = %12llu\n", sm->daemon, subsystem,
292 name, (unsigned long long) value);
298 * Function that gathers stats from all daemons.
301 stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
305 * Function called when GET operation on stats is done.
308 get_done (void *cls, int success)
310 struct StatMaster *sm = cls;
312 GNUNET_break (GNUNET_OK == success);
314 GNUNET_SCHEDULER_add_now (&stat_run, sm);
319 * Function that gathers stats from all daemons.
322 stat_run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 struct StatMaster *sm = cls;
327 die_task = GNUNET_SCHEDULER_NO_TASK;
328 if (stats[sm->value].name != NULL)
330 GNUNET_STATISTICS_get (sm->stat,
334 stats[sm->value].subsystem, stats[sm->value].name,
336 GNUNET_TIME_UNIT_FOREVER_REL, &get_done, &print_stat,
340 GNUNET_STATISTICS_destroy (sm->stat, GNUNET_NO);
343 if (sm->daemon == num_peers)
347 while (stats[i].name != NULL)
349 fprintf (stderr, "Total : %12s/%50s = %12llu\n", stats[i].subsystem,
350 stats[i].name, (unsigned long long) stats[i].total);
353 die_task = GNUNET_SCHEDULER_add_now (&do_stop, NULL);
357 GNUNET_STATISTICS_create ("<driver>",
358 GNUNET_TESTING_daemon_get (pg,
360 die_task = GNUNET_SCHEDULER_add_now (&stat_run, sm);
365 * Function scheduled to be run on the successful completion of this
369 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
371 struct TestPutContext *test_put;
372 struct TestGetContext *test_get;
373 struct StatMaster *sm;
375 die_task = GNUNET_SCHEDULER_NO_TASK;
376 while (NULL != (test_put = all_puts_head))
378 if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
379 GNUNET_SCHEDULER_cancel (test_put->task);
380 if (test_put->dht_handle != NULL)
381 GNUNET_DHT_disconnect (test_put->dht_handle);
382 GNUNET_CONTAINER_DLL_remove (all_puts_head,
385 GNUNET_free (test_put);
388 while (NULL != (test_get = all_gets_head))
390 if (test_get->task != GNUNET_SCHEDULER_NO_TASK)
391 GNUNET_SCHEDULER_cancel (test_get->task);
392 if (test_get->get_handle != NULL)
393 GNUNET_DHT_get_stop (test_get->get_handle);
394 if (test_get->dht_handle != NULL)
395 GNUNET_DHT_disconnect (test_get->dht_handle);
396 GNUNET_CONTAINER_DLL_remove (all_gets_head,
399 GNUNET_free (test_get);
401 sm = GNUNET_malloc (sizeof (struct StatMaster));
403 GNUNET_STATISTICS_create ("<driver>",
404 GNUNET_TESTING_daemon_get (pg,
406 die_task = GNUNET_SCHEDULER_add_now (&stat_run, sm);
411 * Check if the get_handle is being used, if so stop the request. Either
412 * way, schedule the end_badly_cont function which actually shuts down the
416 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
418 const char *emsg = cls;
419 struct TestPutContext *test_put;
420 struct TestGetContext *test_get;
422 die_task = GNUNET_SCHEDULER_NO_TASK;
424 "Failing test with error: `%s'!\n",
426 while (NULL != (test_put = all_puts_head))
428 if (test_put->task != GNUNET_SCHEDULER_NO_TASK)
429 GNUNET_SCHEDULER_cancel (test_put->task);
430 if (test_put->dht_handle != NULL)
431 GNUNET_DHT_disconnect (test_put->dht_handle);
432 GNUNET_CONTAINER_DLL_remove (all_puts_head,
435 GNUNET_free (test_put);
438 while (NULL != (test_get = all_gets_head))
440 if (test_get->task != GNUNET_SCHEDULER_NO_TASK)
441 GNUNET_SCHEDULER_cancel (test_get->task);
442 if (test_get->get_handle != NULL)
443 GNUNET_DHT_get_stop (test_get->get_handle);
444 if (test_get->dht_handle != NULL)
445 GNUNET_DHT_disconnect (test_get->dht_handle);
446 GNUNET_CONTAINER_DLL_remove (all_gets_head,
449 GNUNET_free (test_get);
452 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
458 * Task to release get handle.
461 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
463 struct TestGetContext *test_get = cls;
464 GNUNET_HashCode search_key; /* Key stored under */
465 char original_data[TEST_DATA_SIZE]; /* Made up data to store */
467 test_get->task = GNUNET_SCHEDULER_NO_TASK;
468 memset (original_data, test_get->uid, sizeof (original_data));
469 GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
470 if (test_get->succeeded != GNUNET_YES)
474 "Get from peer %s for key %s failed!\n",
475 GNUNET_i2s (&test_get->daemon->id),
476 GNUNET_h2s (&search_key));
478 GNUNET_assert (test_get->get_handle != NULL);
479 GNUNET_DHT_get_stop (test_get->get_handle);
480 test_get->get_handle = NULL;
482 outstanding_gets--; /* GET is really finished */
483 GNUNET_DHT_disconnect (test_get->dht_handle);
484 test_get->dht_handle = NULL;
486 GNUNET_CONTAINER_DLL_remove (all_gets_head,
489 GNUNET_free (test_get);
490 if ((gets_failed > 10) && (outstanding_gets == 0))
492 /* Had more than 10% failures */
494 "%llu gets succeeded, %llu gets failed!\n",
495 gets_completed, gets_failed);
496 GNUNET_SCHEDULER_cancel (die_task);
498 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, "not all gets succeeded");
501 if ( (gets_completed + gets_failed == num_peers * num_peers) &&
502 (outstanding_gets == 0) ) /* All gets successful */
505 "%llu gets succeeded, %llu gets failed!\n",
506 gets_completed, gets_failed);
507 GNUNET_SCHEDULER_cancel (die_task);
509 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
515 * Iterator called if the GET request initiated returns a response.
518 * @param exp when will this value expire
519 * @param key key of the result
520 * @param type type of the result
521 * @param size number of bytes in data
522 * @param data pointer to the result data
525 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
526 const GNUNET_HashCode * key,
527 const struct GNUNET_PeerIdentity *get_path,
528 unsigned int get_path_length,
529 const struct GNUNET_PeerIdentity *put_path,
530 unsigned int put_path_length,
531 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
533 struct TestGetContext *test_get = cls;
534 GNUNET_HashCode search_key; /* Key stored under */
535 char original_data[TEST_DATA_SIZE]; /* Made up data to store */
537 memset (original_data, test_get->uid, sizeof (original_data));
538 GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
539 if (test_get->succeeded == GNUNET_YES)
540 return; /* Get has already been successful, probably ending now */
543 if (put_path != NULL)
547 fprintf (stderr, "PUT (%u) Path: ",
549 for (i = 0; i<put_path_length; i++)
550 fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
551 fprintf (stderr, "\n");
553 if (get_path != NULL)
557 fprintf (stderr, "GET (%u) Path: ",
559 for (i = 0; i < get_path_length; i++)
560 fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
561 fprintf (stderr, "%s%s\n",
562 get_path_length > 0 ? "->":"",
563 GNUNET_i2s (&test_get->daemon->id));
567 if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
568 (0 != memcmp (original_data, data, sizeof (original_data))))
571 "Key or data is not the same as was inserted!\n");
575 test_get->succeeded = GNUNET_YES;
576 GNUNET_SCHEDULER_cancel (test_get->task);
577 test_get->task = GNUNET_SCHEDULER_add_now (&get_stop_task, test_get);
582 * Set up some data, and call API PUT function
585 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
587 struct TestGetContext *test_get = cls;
588 GNUNET_HashCode key; /* Made up key to store data under */
589 char data[TEST_DATA_SIZE]; /* Made up data to store */
591 if (outstanding_gets > MAX_OUTSTANDING_GETS)
593 test_get->task = GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
596 memset (data, test_get->uid, sizeof (data));
597 GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
598 test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
599 GNUNET_assert (test_get->dht_handle != NULL);
601 test_get->get_handle =
602 GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
603 GNUNET_BLOCK_TYPE_TEST, &key,
604 1, route_option, NULL, 0,
605 &get_result_iterator, test_get);
607 GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
612 * Task to release DHT handles for PUT
615 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
617 struct TestPutContext *test_put = cls;
619 test_put->task = GNUNET_SCHEDULER_NO_TASK;
620 GNUNET_DHT_disconnect (test_put->dht_handle);
621 test_put->dht_handle = NULL;
622 GNUNET_CONTAINER_DLL_remove (all_puts_head,
625 GNUNET_free (test_put);
630 * Schedule the GET requests
633 start_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
635 unsigned long long i;
636 unsigned long long j;
637 struct TestGetContext *test_get;
641 "Issuing %llu GETs\n",
642 num_peers * num_peers);
644 for (i = 0; i < num_peers; i++)
645 for (j = 0; j < num_peers; j++)
647 test_get = GNUNET_malloc (sizeof (struct TestGetContext));
648 test_get->uid = i + j*num_peers;
649 test_get->daemon = GNUNET_TESTING_daemon_get (pg, j);
650 GNUNET_CONTAINER_DLL_insert (all_gets_head,
653 test_get->task = GNUNET_SCHEDULER_add_now (&do_get,
660 * Called when the PUT request has been transmitted to the DHT service.
663 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
665 struct TestPutContext *test_put = cls;
669 GNUNET_SCHEDULER_cancel (test_put->task);
671 GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
672 if (puts_completed != num_peers * num_peers)
675 GNUNET_assert (outstanding_puts == 0);
676 GNUNET_SCHEDULER_add_delayed (START_DELAY,
683 * Set up some data, and call API PUT function
686 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
688 struct TestPutContext *test_put = cls;
689 GNUNET_HashCode key; /* Made up key to store data under */
690 char data[TEST_DATA_SIZE]; /* Made up data to store */
692 test_put->task = GNUNET_SCHEDULER_NO_TASK;
693 if (outstanding_puts > MAX_OUTSTANDING_PUTS)
695 test_put->task = GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
698 memset (data, test_put->uid, sizeof (data));
699 GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
700 test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
701 GNUNET_assert (test_put->dht_handle != NULL);
704 fprintf (stderr, "PUT %u at `%s'\n",
706 GNUNET_i2s (&test_put->daemon->id));
708 GNUNET_DHT_put (test_put->dht_handle, &key, 1,
709 route_option, GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
710 GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
711 &put_finished, test_put);
713 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
714 &put_disconnect_task, test_put);
719 run_dht_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
721 unsigned long long i;
722 struct TestPutContext *test_put;
725 route_option = GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
727 route_option = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
730 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
731 "from setup puts/gets");
733 "Issuing %llu PUTs (one per peer)\n",
735 for (i = 0; i < num_peers * num_peers; i++)
737 test_put = GNUNET_malloc (sizeof (struct TestPutContext));
739 test_put->daemon = GNUNET_TESTING_daemon_get (pg, i % num_peers);
740 test_put->task = GNUNET_SCHEDULER_add_now (&do_put, test_put);
741 GNUNET_CONTAINER_DLL_insert (all_puts_head,
749 * This function is called once testing has finished setting up the topology.
752 * @param emsg variable is NULL on success (peers connected), and non-NULL on
753 * failure (peers failed to connect).
756 startup_done (void *cls, const char *emsg)
761 "Failed to setup topology: %s\n",
764 GNUNET_SCHEDULER_add_now (&end_badly,
765 "topology setup failed");
769 GNUNET_SCHEDULER_add_delayed (START_DELAY, &run_dht_test,
770 "from setup puts/gets");
775 run (void *cls, char *const *args, const char *cfgfile,
776 const struct GNUNET_CONFIGURATION_Handle *cfg)
778 /* Get path from configuration file */
780 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
788 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
790 num_peers = DEFAULT_NUM_PEERS;
791 pg = GNUNET_TESTING_peergroup_start (cfg,
798 GNUNET_assert (NULL != pg);
807 /* Arguments for GNUNET_PROGRAM_run */
808 char *const argv[] = { "test-dht-multipeer", /* Name to give running binary */
810 "test_dht_multipeer_data.conf", /* Config file to use */
816 struct GNUNET_GETOPT_CommandLineOption options[] = {
817 GNUNET_GETOPT_OPTION_END
819 /* Run the run function as a new program */
821 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
822 "test-dht-multipeer", "nohelp", options, &run, &ok);
823 if (ret != GNUNET_OK)
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 "`test-dht-multipeer': Failed with error code %d\n", ret);
833 main (int argc, char *argv[])
838 GNUNET_log_setup ("test-dht-multipeer",
847 * Need to remove base directory, subdirectories taken care
848 * of by the testing framework.
850 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
852 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
853 "Failed to remove testing directory %s\n", test_directory);
858 /* end of test_dht_multipeer.c */