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 2, 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_api.c
22 * @brief base test case for dht api
24 * This test case tests DHT api to DUMMY DHT service communication.
28 #include "gnunet_common.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_dht_service.h"
36 #define VERBOSE GNUNET_NO
38 #define VERBOSE_ARM GNUNET_NO
40 #define START_ARM GNUNET_YES
43 * How long until we really give up on a particular testcase portion?
45 #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
48 * How long until we give up on any particular operation (and retry)?
50 #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
57 * When to really abort the operation.
59 struct GNUNET_TIME_Absolute real_timeout;
62 * What timeout to set for the current attempt (increases)
64 struct GNUNET_TIME_Relative next_timeout;
67 * The context of the peer we are dealing with.
69 struct PeerContext *peer_ctx;
72 * The task identifier of the retry task, so it can be cancelled.
74 GNUNET_SCHEDULER_TaskIdentifier retry_task;
80 struct GNUNET_CONFIGURATION_Handle *cfg;
81 struct GNUNET_DHT_Handle *dht_handle;
82 struct GNUNET_PeerIdentity id;
83 struct GNUNET_DHT_GetHandle *get_handle;
84 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
91 static struct PeerContext p1;
93 static struct GNUNET_SCHEDULER_Handle *sched;
97 GNUNET_SCHEDULER_TaskIdentifier die_task;
100 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
102 #define OKPP do { ok++; } while (0)
107 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 GNUNET_SCHEDULER_cancel (sched, die_task);
112 GNUNET_DHT_disconnect (p1.dht_handle);
114 die_task = GNUNET_SCHEDULER_NO_TASK;
116 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
119 "DHT disconnected, returning FAIL!\n");
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125 "DHT disconnected, returning success!\n");
131 stop_arm (struct PeerContext *p)
134 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
135 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
136 GNUNET_OS_process_wait (p->arm_pid);
138 GNUNET_CONFIGURATION_destroy (p->cfg);
147 fprintf (stderr, "Ending on an unhappy note.\n");
150 GNUNET_DHT_disconnect (p1.dht_handle);
157 * Signature of the main function of a task.
160 * @param tc context information (why was this task triggered now)
163 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct PeerContext *peer = cls;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
168 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
169 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
171 GNUNET_assert (peer->dht_handle != NULL);
173 GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &end, &p1);
175 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
181 * Iterator called on each result obtained from a find peer
184 * @param cls closure (NULL)
185 * @param peer the peer we learned about
186 * @param reply response
188 void test_find_peer_processor (void *cls,
189 const struct GNUNET_PeerIdentity *peer,
190 const struct GNUNET_MessageHeader *reply)
192 struct RetryContext *retry_ctx = cls;
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer));
197 if (retry_ctx->retry_task != GNUNET_SCHEDULER_NO_TASK)
198 GNUNET_SCHEDULER_cancel(sched, retry_ctx->retry_task);
200 GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
201 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
206 * Retry the find_peer task on timeout.
209 * @param tc context information (why was this task triggered now)
212 retry_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
214 struct RetryContext *retry_ctx = cls;
215 GNUNET_HashCode hash;
216 memset (&hash, 42, sizeof (GNUNET_HashCode));
218 if (GNUNET_TIME_absolute_get_remaining(retry_ctx->real_timeout).value > 0)
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "test_find_peer timed out, retrying!\n");
223 retry_ctx->peer_ctx->find_peer_handle =
224 GNUNET_DHT_find_peer_start (retry_ctx->peer_ctx->dht_handle, retry_ctx->next_timeout, 0, NULL, &hash,
225 &test_find_peer_processor, retry_ctx, NULL, NULL);
229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230 "test_find_peer timed out for good, failing!\n");
232 retry_ctx->peer_ctx->find_peer_handle = NULL;
235 if (retry_ctx->peer_ctx->find_peer_handle == NULL)
236 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
238 retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_ctx->next_timeout, &retry_find_peer, retry_ctx);
242 * Retry the find_peer task on timeout.
245 * @param tc context information (why was this task triggered now)
248 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 struct RetryContext *retry_ctx = cls;
251 GNUNET_HashCode hash;
252 memset (&hash, 42, sizeof (GNUNET_HashCode));
254 if (retry_ctx->peer_ctx->find_peer_handle != NULL)
255 GNUNET_DHT_find_peer_stop(retry_ctx->peer_ctx->find_peer_handle, &retry_find_peer, retry_ctx);
257 GNUNET_SCHEDULER_add_now (sched, &retry_find_peer, retry_ctx);
262 * Entry point for test of find_peer functionality.
265 * @param tc context information (why was this task triggered now)
268 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270 struct PeerContext *peer = cls;
271 GNUNET_HashCode hash;
272 memset (&hash, 42, sizeof (GNUNET_HashCode));
273 struct RetryContext *retry_ctx;
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
276 GNUNET_assert (peer->dht_handle != NULL);
278 retry_ctx = GNUNET_malloc(sizeof(struct RetryContext));
279 retry_ctx->real_timeout = GNUNET_TIME_relative_to_absolute(TOTAL_TIMEOUT);
280 retry_ctx->next_timeout = BASE_TIMEOUT;
281 retry_ctx->peer_ctx = peer;
283 peer->find_peer_handle =
284 GNUNET_DHT_find_peer_start (peer->dht_handle, retry_ctx->next_timeout, 0, NULL, &hash,
285 &test_find_peer_processor, retry_ctx, NULL, NULL);
287 if (peer->find_peer_handle == NULL)
288 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
290 retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_ctx->next_timeout, &retry_find_peer_stop, retry_ctx);
294 * Signature of the main function of a task.
297 * @param tc context information (why was this task triggered now)
300 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
302 struct PeerContext *peer = cls;
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
305 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
306 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
308 GNUNET_assert (peer->dht_handle != NULL);
310 GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
312 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
317 test_get_iterator (void *cls,
318 struct GNUNET_TIME_Absolute exp,
319 const GNUNET_HashCode * key,
320 uint32_t type, uint32_t size, const void *data)
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "test_get_iterator called (we got a result), stopping get request!\n");
325 GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
326 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
330 * Signature of the main function of a task.
333 * @param tc context information (why was this task triggered now)
336 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 struct PeerContext *peer = cls;
339 GNUNET_HashCode hash;
340 memset (&hash, 42, sizeof (GNUNET_HashCode));
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
344 GNUNET_assert (peer->dht_handle != NULL);
347 GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT, 42, &hash,
348 &test_get_iterator, NULL, NULL, NULL);
350 if (peer->get_handle == NULL)
351 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
355 * Signature of the main function of a task.
358 * @param tc context information (why was this task triggered now)
361 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
363 struct PeerContext *peer = cls;
364 GNUNET_HashCode hash;
366 size_t data_size = 42;
367 memset (&hash, 42, sizeof (GNUNET_HashCode));
368 data = GNUNET_malloc (data_size);
369 memset (data, 43, data_size);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
371 peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
373 GNUNET_assert (peer->dht_handle != NULL);
375 GNUNET_DHT_put (peer->dht_handle, &hash, 42, data_size, data,
376 GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT), TOTAL_TIMEOUT,
382 setup_peer (struct PeerContext *p, const char *cfgname)
384 p->cfg = GNUNET_CONFIGURATION_create ();
386 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
387 "gnunet-service-arm",
391 "-c", cfgname, NULL);
393 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
399 struct GNUNET_SCHEDULER_Handle *s,
401 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
403 GNUNET_assert (ok == 1);
407 die_task = GNUNET_SCHEDULER_add_delayed (sched,
408 GNUNET_TIME_relative_multiply
409 (GNUNET_TIME_UNIT_MINUTES, 1),
412 setup_peer (&p1, "test_dht_api_peer1.conf");
414 GNUNET_SCHEDULER_add_delayed (sched,
415 GNUNET_TIME_relative_multiply
416 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
424 char *const argv[] = { "test-dht-api",
426 "test_dht_api_data.conf",
433 struct GNUNET_GETOPT_CommandLineOption options[] = {
434 GNUNET_GETOPT_OPTION_END
438 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
439 argv, "test-dht-api", "nohelp", options, &run, &ok);
446 main (int argc, char *argv[])
450 return GNUNET_SYSERR;
453 GNUNET_log_setup ("test-dht-api",
462 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
467 /* end of test_dht_api.c */