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_twopeer.c
22 * @brief base 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 #define MAX_GET_ATTEMPTS 10
35 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
37 #define DEFAULT_NUM_PEERS 2
43 struct GNUNET_PeerIdentity *peer;
45 struct GNUNET_DHT_Handle *dht_handle;
47 struct GNUNET_DHT_GetHandle *get_handle;
49 unsigned int get_attempts;
51 GNUNET_SCHEDULER_TaskIdentifier retry_task;
55 static char *test_directory;
57 static struct PeerGetContext curr_get_ctx;
59 static unsigned int expected_connections;
61 static unsigned long long peers_left;
63 static struct GNUNET_TESTING_PeerGroup *pg;
65 static unsigned long long num_peers;
67 static unsigned int total_gets;
69 static unsigned int gets_succeeded;
71 static unsigned int total_connections;
73 static unsigned int failed_connections;
75 static GNUNET_SCHEDULER_TaskIdentifier die_task;
79 static struct GNUNET_PeerIdentity peer1id;
81 static struct GNUNET_PeerIdentity peer2id;
83 static struct GNUNET_DHT_Handle *peer1dht;
85 static struct GNUNET_DHT_Handle *peer2dht;
88 * Check whether peers successfully shut down.
91 shutdown_callback (void *cls, const char *emsg)
101 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 GNUNET_assert (pg != NULL);
104 GNUNET_assert (peer1dht != NULL);
105 GNUNET_assert (peer2dht != NULL);
106 GNUNET_DHT_disconnect (peer1dht);
107 GNUNET_DHT_disconnect (peer2dht);
108 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
114 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 if (peer1dht != NULL)
117 GNUNET_DHT_disconnect (peer1dht);
119 if (peer2dht != NULL)
120 GNUNET_DHT_disconnect (peer2dht);
124 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
128 if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
129 GNUNET_SCHEDULER_cancel (curr_get_ctx.retry_task);
134 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 const char *emsg = cls;
141 if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
142 GNUNET_SCHEDULER_cancel (curr_get_ctx.retry_task);
144 if (curr_get_ctx.get_handle != NULL)
146 GNUNET_DHT_get_stop (curr_get_ctx.get_handle);
149 GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
154 /* Forward declaration */
156 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
159 * Iterator called on each result obtained for a DHT
160 * operation that expects a reply
163 * @param exp when will this value expire
164 * @param key key of the result
165 * @param type type of the result
166 * @param size number of bytes in data
167 * @param data pointer to the result data
170 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
171 const GNUNET_HashCode * key,
172 const struct GNUNET_PeerIdentity *const *get_path,
173 const struct GNUNET_PeerIdentity *const *put_path,
174 enum GNUNET_BLOCK_Type type, size_t size, const void *data)
176 struct PeerGetContext *get_context = cls;
179 memcmp (&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode)))
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Key returned is not the same key as was searched for!\n");
183 GNUNET_SCHEDULER_cancel (die_task);
185 GNUNET_SCHEDULER_add_now (&end_badly,
186 "key mismatch in get response!\n");
190 if (get_context->retry_task != GNUNET_SCHEDULER_NO_TASK)
192 GNUNET_SCHEDULER_cancel (get_context->retry_task);
193 get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
196 if (get_context->peer == &peer2id)
198 get_context->peer = &peer1id;
199 get_context->dht_handle = peer2dht;
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Received first correct GET request response!\n");
202 GNUNET_DHT_get_stop (get_context->get_handle);
203 GNUNET_SCHEDULER_add_now (&do_get, get_context);
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Received second correct GET request response!\n");
209 GNUNET_SCHEDULER_cancel (die_task);
210 GNUNET_DHT_get_stop (get_context->get_handle);
211 die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
217 stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
220 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 struct PeerGetContext *get_context = cls;
224 if (get_context->get_attempts < MAX_GET_ATTEMPTS)
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Get attempt %u failed, retrying request!\n",
227 get_context->get_attempts);
230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
231 "Too many attempts failed, ending test!\n",
232 get_context->get_attempts);
233 GNUNET_SCHEDULER_cancel (die_task);
235 GNUNET_SCHEDULER_add_now (&end_badly,
236 "GET attempt failed, ending test!\n");
239 get_context->get_attempts++;
240 get_context->retry_task =
241 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
242 (GNUNET_TIME_UNIT_SECONDS, 10),
243 &stop_retry_get, get_context);
244 get_context->get_handle =
245 GNUNET_DHT_get_start (get_context->dht_handle,
246 GNUNET_TIME_relative_multiply
247 (GNUNET_TIME_UNIT_SECONDS, 5),
248 GNUNET_BLOCK_TYPE_DHT_HELLO,
249 &get_context->peer->hashPubKey,
250 DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
251 0, NULL, 0, &get_result_iterator, get_context);
255 stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
257 struct PeerGetContext *get_context = cls;
259 get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Get attempt %u failed, canceling request!\n",
262 get_context->get_attempts);
263 GNUNET_DHT_get_stop (get_context->get_handle);
264 get_context->get_handle = NULL;
265 GNUNET_SCHEDULER_add_now (&get_stop_finished, get_context);
269 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
271 struct PeerGetContext *get_context = cls;
273 get_context->retry_task =
274 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
275 (GNUNET_TIME_UNIT_SECONDS, 10),
276 &stop_retry_get, get_context);
278 get_context->get_handle =
279 GNUNET_DHT_get_start (get_context->dht_handle,
280 GNUNET_TIME_relative_multiply
281 (GNUNET_TIME_UNIT_SECONDS, 5),
282 GNUNET_BLOCK_TYPE_DHT_HELLO,
283 &get_context->peer->hashPubKey,
284 DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
285 0, NULL, 0, &get_result_iterator, get_context);
290 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
291 const struct GNUNET_PeerIdentity *second, uint32_t distance,
292 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
293 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
294 struct GNUNET_TESTING_Daemon *first_daemon,
295 struct GNUNET_TESTING_Daemon *second_daemon,
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "connected peer %s to peer %s, distance %u\n",
304 first_daemon->shortname, second_daemon->shortname, distance);
310 failed_connections++;
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Failed to connect peer %s to peer %s with error :\n%s\n",
313 first_daemon->shortname, second_daemon->shortname, emsg);
317 if (total_connections == expected_connections)
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
324 GNUNET_SCHEDULER_cancel (die_task);
326 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
327 &end_badly, "Timeout trying to GET");
329 curr_get_ctx.dht_handle = peer1dht;
330 curr_get_ctx.peer = &peer2id;
331 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
332 (GNUNET_TIME_UNIT_SECONDS, 2), &do_get,
335 else if (total_connections + failed_connections == expected_connections)
337 GNUNET_SCHEDULER_cancel (die_task);
339 GNUNET_SCHEDULER_add_now (&end_badly,
340 "from topology_callback (too many failed connections)");
346 connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 expected_connections = -1;
349 if ((pg != NULL) && (peers_left == 0))
350 expected_connections =
351 GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE,
352 GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
353 0.0, TIMEOUT, 12, NULL, NULL);
355 GNUNET_SCHEDULER_cancel (die_task);
356 if (expected_connections == GNUNET_SYSERR)
358 GNUNET_SCHEDULER_add_now (&end_badly,
359 "from connect topology (bad return)");
362 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
363 "from connect topology (timeout)");
368 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
369 const struct GNUNET_CONFIGURATION_Handle *cfg,
370 struct GNUNET_TESTING_Daemon *d, const char *emsg)
375 "Failed to start daemon: `%s'\n",
379 GNUNET_assert (id != NULL);
380 if (peers_left == num_peers)
382 memcpy (&peer1id, id, sizeof (struct GNUNET_PeerIdentity));
383 peer1dht = GNUNET_DHT_connect (cfg, 100);
384 if (peer1dht == NULL)
386 GNUNET_SCHEDULER_cancel (die_task);
388 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
393 memcpy (&peer2id, id, sizeof (struct GNUNET_PeerIdentity));
394 peer2dht = GNUNET_DHT_connect (cfg, 100);
395 if (peer2dht == NULL)
397 GNUNET_SCHEDULER_cancel (die_task);
399 GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "All %d daemons started, now connecting peers!\n", num_peers);
412 GNUNET_SCHEDULER_cancel (die_task);
413 /* Set up task in case topology creation doesn't finish
414 * within a reasonable amount of time */
416 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
417 "from peers_started_callback");
419 GNUNET_SCHEDULER_add_now (&connect_topology, NULL);
426 run (void *cls, char *const *args, const char *cfgfile,
427 const struct GNUNET_CONFIGURATION_Handle *cfg)
431 GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
439 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
441 num_peers = DEFAULT_NUM_PEERS;
443 peers_left = num_peers;
444 total_gets = num_peers;
446 /* Set up a task to end testing if peer start fails */
448 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
449 "didn't start all daemons in reasonable amount of time!!!");
451 pg = GNUNET_TESTING_daemons_start (cfg, num_peers, 10, num_peers, TIMEOUT,
452 NULL, NULL, &peers_started_callback, NULL,
453 &topology_callback, NULL, NULL);
462 char *const argv[] = { "test-dht-twopeer",
464 "test_dht_twopeer_data.conf",
470 struct GNUNET_GETOPT_CommandLineOption options[] = {
471 GNUNET_GETOPT_OPTION_END
474 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
475 "test-dht-twopeer", "nohelp", options, &run, &ok);
476 if (ret != GNUNET_OK)
478 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
479 "`test-dht-twopeer': Failed with error code %d\n", ret);
485 main (int argc, char *argv[])
489 GNUNET_log_setup ("test-dht-twopeer",
498 * Need to remove base directory, subdirectories taken care
499 * of by the testing framework.
501 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504 "Failed to remove testing directory %s\n", test_directory);
509 /* end of test_dht_twopeer.c */