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_YES
33 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
35 #define DEFAULT_NUM_PEERS 2
41 struct GNUNET_PeerIdentity *peer;
43 struct GNUNET_DHT_Handle *dht_handle;
45 struct GNUNET_DHT_GetHandle *get_handle;
49 static char *test_directory;
51 static struct PeerGetContext curr_get_ctx;
53 static unsigned int expected_connections;
55 static unsigned long long peers_left;
57 static struct GNUNET_TESTING_PeerGroup *pg;
59 static struct GNUNET_SCHEDULER_Handle *sched;
61 static unsigned long long num_peers;
63 static unsigned int total_gets;
65 static unsigned int gets_succeeded;
67 static unsigned int total_connections;
69 static unsigned int failed_connections;
71 GNUNET_SCHEDULER_TaskIdentifier die_task;
75 static struct GNUNET_PeerIdentity peer1id;
77 static struct GNUNET_PeerIdentity peer2id;
79 static struct GNUNET_DHT_Handle *peer1dht;
81 static struct GNUNET_DHT_Handle *peer2dht;
84 * Check whether peers successfully shut down.
86 void shutdown_callback (void *cls,
97 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
99 GNUNET_assert (pg != NULL);
100 GNUNET_assert (peer1dht != NULL);
101 GNUNET_assert (peer2dht != NULL);
102 GNUNET_DHT_disconnect(peer1dht);
103 GNUNET_DHT_disconnect(peer2dht);
104 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
109 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
111 if (peer1dht != NULL)
112 GNUNET_DHT_disconnect(peer1dht);
114 if (peer2dht != NULL)
115 GNUNET_DHT_disconnect(peer2dht);
118 GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
122 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
124 if (curr_get_ctx.get_handle != NULL)
126 GNUNET_DHT_get_stop(curr_get_ctx.get_handle, &end_badly_cont, NULL);
132 /* Forward declaration */
134 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
137 * Iterator called on each result obtained for a DHT
138 * operation that expects a reply
141 * @param exp when will this value expire
142 * @param key key of the result
143 * @param type type of the result
144 * @param size number of bytes in data
145 * @param data pointer to the result data
147 void get_result_iterator (void *cls,
148 struct GNUNET_TIME_Absolute exp,
149 const GNUNET_HashCode * key,
154 struct PeerGetContext *get_context = cls;
155 if (0 != memcmp(&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode)))
157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Key returned is not the same key as was searched for!\n");
158 GNUNET_SCHEDULER_cancel(sched, die_task);
159 GNUNET_SCHEDULER_add_now(sched, &end_badly, "key mismatch in get response!\n");
163 if (get_context->peer == &peer2id)
165 get_context->peer = &peer1id;
166 get_context->dht_handle = peer2dht;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received first correct GET request response!\n");
168 GNUNET_DHT_get_stop(get_context->get_handle, &do_get, get_context);
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received second correct GET request response!\n");
173 GNUNET_SCHEDULER_cancel(sched, die_task);
174 GNUNET_DHT_get_stop(get_context->get_handle, &finish_testing, NULL);
181 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
183 struct PeerGetContext *get_context = cls;
185 get_context->get_handle = GNUNET_DHT_get_start(get_context->dht_handle, GNUNET_TIME_relative_get_forever(), 0, &get_context->peer->hashPubKey, &get_result_iterator, get_context, NULL, NULL);
190 topology_callback (void *cls,
191 const struct GNUNET_PeerIdentity *first,
192 const struct GNUNET_PeerIdentity *second,
194 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
195 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
196 struct GNUNET_TESTING_Daemon *first_daemon,
197 struct GNUNET_TESTING_Daemon *second_daemon,
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
205 first_daemon->shortname,
206 second_daemon->shortname,
213 failed_connections++;
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
215 first_daemon->shortname,
216 second_daemon->shortname, emsg);
220 if (total_connections == expected_connections)
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Created %d total connections, which is our target number! Starting next phase of testing.\n",
227 GNUNET_SCHEDULER_cancel (sched, die_task);
228 die_task = GNUNET_SCHEDULER_add_delayed (sched, TIMEOUT,
229 &end_badly, "from test gets");
231 curr_get_ctx.dht_handle = peer1dht;
232 curr_get_ctx.peer = &peer2id;
233 //GNUNET_SCHEDULER_add_now (sched, &do_get, &curr_get_ctx);
234 GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_get, &curr_get_ctx);
236 else if (total_connections + failed_connections == expected_connections)
238 GNUNET_SCHEDULER_cancel (sched, die_task);
239 die_task = GNUNET_SCHEDULER_add_now (sched,
240 &end_badly, "from topology_callback (too many failed connections)");
245 connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
247 expected_connections = -1;
248 if ((pg != NULL) && (peers_left == 0))
249 expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0);
251 GNUNET_SCHEDULER_cancel (sched, die_task);
252 if (expected_connections == GNUNET_SYSERR)
253 die_task = GNUNET_SCHEDULER_add_now (sched,
254 &end_badly, "from connect topology (bad return)");
257 die_task = GNUNET_SCHEDULER_add_delayed (sched,
259 &end_badly, "from connect topology (timeout)");
263 peers_started_callback (void *cls,
264 const struct GNUNET_PeerIdentity *id,
265 const struct GNUNET_CONFIGURATION_Handle *cfg,
266 struct GNUNET_TESTING_Daemon *d, const char *emsg)
270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n",
274 GNUNET_assert (id != NULL);
275 if (peers_left == num_peers)
277 memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity));
278 peer1dht = GNUNET_DHT_connect(sched, cfg, 100);
279 if (peer1dht == NULL)
281 GNUNET_SCHEDULER_cancel (sched, die_task);
282 GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
287 memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity));
288 peer2dht = GNUNET_DHT_connect(sched, cfg, 100);
289 if (peer2dht == NULL)
291 GNUNET_SCHEDULER_cancel (sched, die_task);
292 GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "All %d daemons started, now connecting peers!\n",
306 GNUNET_SCHEDULER_cancel (sched, die_task);
307 /* Set up task in case topology creation doesn't finish
308 * within a reasonable amount of time */
309 die_task = GNUNET_SCHEDULER_add_delayed (sched,
311 &end_badly, "from peers_started_callback");
313 GNUNET_SCHEDULER_add_now(sched, &connect_topology, NULL);
320 struct GNUNET_SCHEDULER_Handle *s,
322 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
326 if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
333 GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
335 num_peers = DEFAULT_NUM_PEERS;
337 peers_left = num_peers;
338 total_gets = num_peers;
340 /* Set up a task to end testing if peer start fails */
341 die_task = GNUNET_SCHEDULER_add_delayed (sched,
343 &end_badly, "didn't start all daemons in reasonable amount of time!!!");
345 pg = GNUNET_TESTING_daemons_start (sched, cfg,
346 num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
347 &topology_callback, NULL, NULL);
355 char *const argv[] = {"test-dht-twopeer",
357 "test_dht_twopeer_data.conf",
363 struct GNUNET_GETOPT_CommandLineOption options[] = {
364 GNUNET_GETOPT_OPTION_END
366 ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
367 argv, "test-dht-twopeer", "nohelp",
369 if (ret != GNUNET_OK)
371 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
377 main (int argc, char *argv[])
381 GNUNET_log_setup ("test-dht-twopeer",
390 * Need to remove base directory, subdirectories taken care
391 * of by the testing framework.
393 if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
395 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
400 /* end of test_dht_twopeer.c */