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_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"
35 #include "gnunet_hello_lib.h"
37 #define VERBOSE GNUNET_YES
39 #define VERBOSE_ARM GNUNET_NO
41 #define START_ARM GNUNET_YES
44 * How long until we really give up on a particular testcase portion?
46 #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
49 * How long until we give up on any particular operation (and retry)?
51 #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
58 * When to really abort the operation.
60 struct GNUNET_TIME_Absolute real_timeout;
63 * What timeout to set for the current attempt (increases)
65 struct GNUNET_TIME_Relative next_timeout;
68 * The context of the peer we are dealing with.
70 struct PeerContext *peer_ctx;
73 * The task identifier of the retry task, so it can be cancelled.
75 GNUNET_SCHEDULER_TaskIdentifier retry_task;
81 struct GNUNET_CONFIGURATION_Handle *cfg;
82 struct GNUNET_DHT_Handle *dht_handle;
83 struct GNUNET_PeerIdentity id;
84 struct GNUNET_DHT_GetHandle *get_handle;
85 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
92 static struct PeerContext p1;
94 struct RetryContext retry_context;
96 static struct GNUNET_SCHEDULER_Handle *sched;
100 GNUNET_SCHEDULER_TaskIdentifier die_task;
103 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
105 #define OKPP do { ok++; } while (0)
110 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 GNUNET_SCHEDULER_cancel (sched, die_task);
116 GNUNET_DHT_disconnect (p1.dht_handle);
117 die_task = GNUNET_SCHEDULER_NO_TASK;
119 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122 "DHT disconnected, returning FAIL!\n");
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "DHT disconnected, returning success!\n");
134 stop_arm (struct PeerContext *p)
137 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
138 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
139 GNUNET_OS_process_wait (p->arm_pid);
141 GNUNET_CONFIGURATION_destroy (p->cfg);
150 fprintf (stderr, "Ending on an unhappy note.\n");
153 if ((retry_context.peer_ctx != NULL) && (retry_context.peer_ctx->find_peer_handle != NULL))
154 GNUNET_DHT_find_peer_stop(retry_context.peer_ctx->find_peer_handle, NULL, NULL);
155 if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
156 GNUNET_SCHEDULER_cancel(sched, retry_context.retry_task);
157 GNUNET_DHT_disconnect (p1.dht_handle);
164 * Signature of the main function of a task.
167 * @param tc context information (why was this task triggered now)
169 void test_set_peer_malicious_drop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 struct PeerContext *peer = cls;
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_set_peer_malicious_drop!\n");
174 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
175 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
177 GNUNET_assert (peer->dht_handle != NULL);
179 GNUNET_DHT_set_malicious_dropper (peer->dht_handle, &end, &p1);
183 * Signature of the main function of a task.
186 * @param tc context information (why was this task triggered now)
188 void test_set_peer_malicious_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 struct PeerContext *peer = cls;
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_set_peer_malicious_put!\n");
193 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
194 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
196 GNUNET_assert (peer->dht_handle != NULL);
198 GNUNET_DHT_set_malicious_putter (peer->dht_handle, 750, &test_set_peer_malicious_drop, &p1);
202 * Signature of the main function of a task.
205 * @param tc context information (why was this task triggered now)
207 void test_set_peer_malicious_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 struct PeerContext *peer = cls;
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_set_peer_malicious_get!\n");
212 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
213 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
215 GNUNET_assert (peer->dht_handle != NULL);
217 GNUNET_DHT_set_malicious_getter (peer->dht_handle, 1500, &test_set_peer_malicious_put, &p1);
221 * Signature of the main function of a task.
224 * @param tc context information (why was this task triggered now)
227 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct PeerContext *peer = cls;
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
232 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
233 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
235 GNUNET_assert (peer->dht_handle != NULL);
237 GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &test_set_peer_malicious_get, &p1);
239 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
245 * Iterator called on each result obtained from a find peer
248 * @param cls closure (NULL)
249 * @param peer the peer we learned about
250 * @param reply response
252 void test_find_peer_processor (void *cls,
253 const struct GNUNET_HELLO_Message *hello)
255 struct RetryContext *retry_ctx = cls;
256 struct GNUNET_PeerIdentity peer;
258 if (GNUNET_OK == GNUNET_HELLO_get_id(hello, &peer))
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(&peer));
263 if (retry_ctx->retry_task != GNUNET_SCHEDULER_NO_TASK)
265 GNUNET_SCHEDULER_cancel(sched, retry_ctx->retry_task);
266 retry_ctx->retry_task = GNUNET_SCHEDULER_NO_TASK;
269 GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
270 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "received find peer request, but hello_get_id failed!\n");
281 * Retry the find_peer task on timeout. (Forward declaration)
284 * @param tc context information (why was this task triggered now?)
287 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
290 * Retry the find_peer task on timeout.
293 * @param tc context information (why was this task triggered now)
296 retry_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 struct RetryContext *retry_ctx = cls;
299 GNUNET_HashCode hash;
300 memset (&hash, 42, sizeof (GNUNET_HashCode));
302 if (GNUNET_TIME_absolute_get_remaining(retry_ctx->real_timeout).value > 0)
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "test_find_peer timed out, retrying!\n");
306 retry_ctx->next_timeout = GNUNET_TIME_relative_multiply(retry_ctx->next_timeout, 2);
307 retry_ctx->peer_ctx->find_peer_handle =
308 GNUNET_DHT_find_peer_start (retry_ctx->peer_ctx->dht_handle, retry_ctx->next_timeout, 0, &hash,
309 &test_find_peer_processor, retry_ctx, NULL, NULL);
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "test_find_peer timed out for good, failing!\n");
316 retry_ctx->peer_ctx->find_peer_handle = NULL;
319 if (retry_ctx->peer_ctx->find_peer_handle == NULL)
320 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
322 retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_ctx->next_timeout, &retry_find_peer_stop, retry_ctx);
326 * Retry the find_peer task on timeout.
329 * @param tc context information (why was this task triggered now?)
332 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
334 struct RetryContext *retry_ctx = cls;
335 GNUNET_HashCode hash;
336 memset (&hash, 42, sizeof (GNUNET_HashCode));
338 if (retry_ctx->peer_ctx->find_peer_handle != NULL)
339 GNUNET_DHT_find_peer_stop(retry_ctx->peer_ctx->find_peer_handle, &retry_find_peer, retry_ctx);
341 GNUNET_SCHEDULER_add_now (sched, &retry_find_peer, retry_ctx);
346 * Entry point for test of find_peer functionality.
349 * @param tc context information (why was this task triggered now)
352 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
354 struct PeerContext *peer = cls;
355 GNUNET_HashCode hash;
356 memset (&hash, 42, sizeof (GNUNET_HashCode));
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
359 GNUNET_assert (peer->dht_handle != NULL);
361 retry_context.real_timeout = GNUNET_TIME_relative_to_absolute(TOTAL_TIMEOUT);
362 retry_context.next_timeout = BASE_TIMEOUT;
363 retry_context.peer_ctx = peer;
365 peer->find_peer_handle =
366 GNUNET_DHT_find_peer_start (peer->dht_handle, retry_context.next_timeout, 1, &hash,
367 &test_find_peer_processor, &retry_context, NULL, NULL);
369 if (peer->find_peer_handle == NULL)
370 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
372 retry_context.retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_context.next_timeout, &retry_find_peer_stop, &retry_context);
376 * Signature of the main function of a task.
379 * @param tc context information (why was this task triggered now)
382 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
384 struct PeerContext *peer = cls;
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
387 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
388 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
390 GNUNET_assert (peer->dht_handle != NULL);
392 GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
394 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
399 test_get_iterator (void *cls,
400 struct GNUNET_TIME_Absolute exp,
401 const GNUNET_HashCode * key,
402 uint32_t type, uint32_t size, const void *data)
404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405 "test_get_iterator called (we got a result), stopping get request!\n");
407 GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
408 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
412 * Signature of the main function of a task.
415 * @param tc context information (why was this task triggered now)
418 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
420 struct PeerContext *peer = cls;
421 GNUNET_HashCode hash;
422 memset (&hash, 42, sizeof (GNUNET_HashCode));
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
426 GNUNET_assert (peer->dht_handle != NULL);
429 GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT, 49, &hash,
430 &test_get_iterator, NULL, NULL, NULL);
432 if (peer->get_handle == NULL)
433 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
437 * Signature of the main function of a task.
440 * @param tc context information (why was this task triggered now)
443 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
445 struct PeerContext *peer = cls;
446 GNUNET_HashCode hash;
448 size_t data_size = 42;
449 memset (&hash, 42, sizeof (GNUNET_HashCode));
450 data = GNUNET_malloc (data_size);
451 memset (data, 43, data_size);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
453 peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
455 GNUNET_assert (peer->dht_handle != NULL);
457 GNUNET_DHT_put (peer->dht_handle, &hash, 49, data_size, data,
458 GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT), TOTAL_TIMEOUT,
464 setup_peer (struct PeerContext *p, const char *cfgname)
466 p->cfg = GNUNET_CONFIGURATION_create ();
468 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
469 "gnunet-service-arm",
473 "-c", cfgname, NULL);
475 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
481 struct GNUNET_SCHEDULER_Handle *s,
483 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
485 GNUNET_assert (ok == 1);
489 die_task = GNUNET_SCHEDULER_add_delayed (sched,
490 GNUNET_TIME_relative_multiply
491 (GNUNET_TIME_UNIT_MINUTES, 1),
494 setup_peer (&p1, "test_dht_api_peer1.conf");
496 GNUNET_SCHEDULER_add_delayed (sched,
497 GNUNET_TIME_relative_multiply
498 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
506 char *const argv[] = { "test-dht-api",
508 "test_dht_api_data.conf",
515 struct GNUNET_GETOPT_CommandLineOption options[] = {
516 GNUNET_GETOPT_OPTION_END
520 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
521 argv, "test-dht-api", "nohelp", options, &run, &ok);
528 main (int argc, char *argv[])
532 return GNUNET_SYSERR;
535 GNUNET_log_setup ("test-dht-api",
544 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
549 /* end of test_dht_api.c */