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 struct GNUNET_SCHEDULER_Handle *sched;
67 static unsigned long long num_peers;
69 static unsigned int total_gets;
71 static unsigned int gets_succeeded;
73 static unsigned int total_connections;
75 static unsigned int failed_connections;
77 GNUNET_SCHEDULER_TaskIdentifier die_task;
81 static struct GNUNET_PeerIdentity peer1id;
83 static struct GNUNET_PeerIdentity peer2id;
85 static struct GNUNET_DHT_Handle *peer1dht;
87 static struct GNUNET_DHT_Handle *peer2dht;
90 * Check whether peers successfully shut down.
92 void shutdown_callback (void *cls,
103 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
105 GNUNET_assert (pg != NULL);
106 GNUNET_assert (peer1dht != NULL);
107 GNUNET_assert (peer2dht != NULL);
108 GNUNET_DHT_disconnect(peer1dht);
109 GNUNET_DHT_disconnect(peer2dht);
110 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
115 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
117 if (peer1dht != NULL)
118 GNUNET_DHT_disconnect(peer1dht);
120 if (peer2dht != NULL)
121 GNUNET_DHT_disconnect(peer2dht);
124 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
126 if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
127 GNUNET_SCHEDULER_cancel(sched, curr_get_ctx.retry_task);
131 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
133 if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
134 GNUNET_SCHEDULER_cancel(sched, curr_get_ctx.retry_task);
136 if (curr_get_ctx.get_handle != NULL)
138 GNUNET_DHT_get_stop(curr_get_ctx.get_handle);
141 GNUNET_SCHEDULER_add_now (sched, &end_badly_cont, NULL);
145 /* Forward declaration */
147 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
150 * Iterator called on each result obtained for a DHT
151 * operation that expects a reply
154 * @param exp when will this value expire
155 * @param key key of the result
156 * @param type type of the result
157 * @param size number of bytes in data
158 * @param data pointer to the result data
160 void get_result_iterator (void *cls,
161 struct GNUNET_TIME_Absolute exp,
162 const GNUNET_HashCode * key,
163 const struct GNUNET_PeerIdentity * const *get_path,
164 const struct GNUNET_PeerIdentity * const *put_path,
165 enum GNUNET_BLOCK_Type type,
169 struct PeerGetContext *get_context = cls;
171 if (0 != memcmp(&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode)))
173 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Key returned is not the same key as was searched for!\n");
174 GNUNET_SCHEDULER_cancel(sched, die_task);
175 GNUNET_SCHEDULER_add_now(sched, &end_badly, "key mismatch in get response!\n");
179 if (get_context->retry_task != GNUNET_SCHEDULER_NO_TASK)
181 GNUNET_SCHEDULER_cancel(sched, get_context->retry_task);
182 get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
185 if (get_context->peer == &peer2id)
187 get_context->peer = &peer1id;
188 get_context->dht_handle = peer2dht;
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received first correct GET request response!\n");
190 GNUNET_DHT_get_stop(get_context->get_handle);
191 GNUNET_SCHEDULER_add_now (sched, &do_get, get_context);
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received second correct GET request response!\n");
196 GNUNET_SCHEDULER_cancel(sched, die_task);
197 GNUNET_DHT_get_stop(get_context->get_handle);
198 GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
204 stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
207 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
209 struct PeerGetContext *get_context = cls;
211 if (get_context->get_attempts < MAX_GET_ATTEMPTS)
212 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Get attempt %u failed, retrying request!\n", get_context->get_attempts);
215 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Too many attempts failed, ending test!\n", get_context->get_attempts);
216 GNUNET_SCHEDULER_cancel(sched, die_task);
217 GNUNET_SCHEDULER_add_now(sched, &end_badly, "key mismatch in get response!\n");
220 get_context->get_attempts++;
221 get_context->retry_task = GNUNET_SCHEDULER_add_delayed(sched,
222 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
223 &stop_retry_get, get_context);
224 get_context->get_handle = GNUNET_DHT_get_start(get_context->dht_handle,
225 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
226 0 /* fixme: use real type */, &get_context->peer->hashPubKey,
230 &get_result_iterator, get_context);
234 stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
236 struct PeerGetContext *get_context = cls;
237 get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
238 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Get attempt %u failed, canceling request!\n", get_context->get_attempts);
239 GNUNET_DHT_get_stop(get_context->get_handle);
240 get_context->get_handle = NULL;
241 GNUNET_SCHEDULER_add_now(sched, &get_stop_finished, get_context);
245 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
247 struct PeerGetContext *get_context = cls;
249 get_context->retry_task = GNUNET_SCHEDULER_add_delayed(sched,
250 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10),
251 &stop_retry_get, get_context);
253 get_context->get_handle = GNUNET_DHT_get_start(get_context->dht_handle,
254 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
255 GNUNET_BLOCK_TYPE_DHT_HELLO,
256 &get_context->peer->hashPubKey,
260 &get_result_iterator, get_context);
265 topology_callback (void *cls,
266 const struct GNUNET_PeerIdentity *first,
267 const struct GNUNET_PeerIdentity *second,
269 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
270 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
271 struct GNUNET_TESTING_Daemon *first_daemon,
272 struct GNUNET_TESTING_Daemon *second_daemon,
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
280 first_daemon->shortname,
281 second_daemon->shortname,
288 failed_connections++;
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
290 first_daemon->shortname,
291 second_daemon->shortname, emsg);
295 if (total_connections == expected_connections)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
302 GNUNET_SCHEDULER_cancel (sched, die_task);
303 die_task = GNUNET_SCHEDULER_add_delayed (sched, TIMEOUT,
304 &end_badly, "from test gets");
306 curr_get_ctx.dht_handle = peer1dht;
307 curr_get_ctx.peer = &peer2id;
308 //GNUNET_SCHEDULER_add_now (sched, &do_get, &curr_get_ctx);
309 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_get, &curr_get_ctx);
311 else if (total_connections + failed_connections == expected_connections)
313 GNUNET_SCHEDULER_cancel (sched, die_task);
314 die_task = GNUNET_SCHEDULER_add_now (sched,
315 &end_badly, "from topology_callback (too many failed connections)");
320 connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
322 expected_connections = -1;
323 if ((pg != NULL) && (peers_left == 0))
324 expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0, NULL, NULL);
326 GNUNET_SCHEDULER_cancel (sched, die_task);
327 if (expected_connections == GNUNET_SYSERR)
328 die_task = GNUNET_SCHEDULER_add_now (sched,
329 &end_badly, "from connect topology (bad return)");
332 die_task = GNUNET_SCHEDULER_add_delayed (sched,
334 &end_badly, "from connect topology (timeout)");
338 peers_started_callback (void *cls,
339 const struct GNUNET_PeerIdentity *id,
340 const struct GNUNET_CONFIGURATION_Handle *cfg,
341 struct GNUNET_TESTING_Daemon *d, const char *emsg)
345 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n",
349 GNUNET_assert (id != NULL);
350 if (peers_left == num_peers)
352 memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity));
353 peer1dht = GNUNET_DHT_connect(sched, cfg, 100);
354 if (peer1dht == NULL)
356 GNUNET_SCHEDULER_cancel (sched, die_task);
357 GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
362 memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity));
363 peer2dht = GNUNET_DHT_connect(sched, cfg, 100);
364 if (peer2dht == NULL)
366 GNUNET_SCHEDULER_cancel (sched, die_task);
367 GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "All %d daemons started, now connecting peers!\n",
381 GNUNET_SCHEDULER_cancel (sched, die_task);
382 /* Set up task in case topology creation doesn't finish
383 * within a reasonable amount of time */
384 die_task = GNUNET_SCHEDULER_add_delayed (sched,
386 &end_badly, "from peers_started_callback");
388 GNUNET_SCHEDULER_add_now(sched, &connect_topology, NULL);
395 struct GNUNET_SCHEDULER_Handle *s,
397 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
401 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
408 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
410 num_peers = DEFAULT_NUM_PEERS;
412 peers_left = num_peers;
413 total_gets = num_peers;
415 /* Set up a task to end testing if peer start fails */
416 die_task = GNUNET_SCHEDULER_add_delayed (sched,
418 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
420 pg = GNUNET_TESTING_daemons_start (sched, cfg,
421 num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
422 &topology_callback, NULL, NULL);
430 char *const argv[] = {"test-dht-twopeer",
432 "test_dht_twopeer_data.conf",
438 struct GNUNET_GETOPT_CommandLineOption options[] = {
439 GNUNET_GETOPT_OPTION_END
441 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
442 argv, "test-dht-twopeer", "nohelp",
444 if (ret != GNUNET_OK)
446 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
452 main (int argc, char *argv[])
456 GNUNET_log_setup ("test-dht-twopeer",
465 * Need to remove base directory, subdirectories taken care
466 * of by the testing framework.
468 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
470 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
475 /* end of test_dht_twopeer.c */