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_NO
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, 600)
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;
86 struct GNUNET_OS_Process *arm_proc;
90 static struct PeerContext p1;
92 struct RetryContext retry_context;
97 static 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)
109 GNUNET_SCHEDULER_cancel (die_task);
110 die_task = GNUNET_SCHEDULER_NO_TASK;
111 GNUNET_DHT_disconnect (p1.dht_handle);
112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113 "DHT disconnected, returning success!\n");
118 stop_arm (struct PeerContext *p)
121 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
122 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
123 GNUNET_OS_process_wait (p->arm_proc);
124 GNUNET_OS_process_close (p->arm_proc);
127 GNUNET_CONFIGURATION_destroy (p->cfg);
136 FPRINTF (stderr, "%s", "Ending on an unhappy note.\n");
139 if ((retry_context.peer_ctx != NULL) &&
140 (retry_context.peer_ctx->get_handle != NULL))
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n");
143 GNUNET_DHT_get_stop (retry_context.peer_ctx->get_handle);
145 if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
146 GNUNET_SCHEDULER_cancel (retry_context.retry_task);
147 GNUNET_DHT_disconnect (p1.dht_handle);
153 * Signature of the main function of a task.
156 * @param tc context information (why was this task triggered now)
159 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 struct PeerContext *peer = cls;
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
164 if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
167 GNUNET_SCHEDULER_cancel (die_task);
168 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
171 GNUNET_assert (peer->dht_handle != NULL);
172 GNUNET_DHT_get_stop (peer->get_handle);
173 peer->get_handle = NULL;
174 GNUNET_SCHEDULER_add_now (&end, &p1);
179 test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
180 const GNUNET_HashCode * key,
181 const struct GNUNET_PeerIdentity *get_path,
182 unsigned int get_path_length,
183 const struct GNUNET_PeerIdentity *put_path,
184 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
185 size_t size, const void *data)
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "test_get_iterator called (we got a result), stopping get request!\n");
190 GNUNET_SCHEDULER_add_continuation (&test_get_stop, &p1,
191 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195 * Signature of the main function of a task.
198 * @param success result of PUT
201 test_get (void *cls, int success)
203 struct PeerContext *peer = cls;
204 GNUNET_HashCode hash;
206 memset (&hash, 42, sizeof (GNUNET_HashCode));
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
210 GNUNET_assert (peer->dht_handle != NULL);
211 retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT);
212 retry_context.next_timeout = BASE_TIMEOUT;
215 GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT,
216 GNUNET_BLOCK_TYPE_TEST, &hash, 1,
217 GNUNET_DHT_RO_NONE, NULL, 0, &test_get_iterator,
220 if (peer->get_handle == NULL)
223 GNUNET_SCHEDULER_cancel (die_task);
224 die_task = GNUNET_SCHEDULER_add_now (&end_badly, &p1);
228 retry_context.peer_ctx = peer;
232 * Signature of the main function of a task.
235 * @param tc context information (why was this task triggered now)
238 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 struct PeerContext *peer = cls;
241 GNUNET_HashCode hash;
243 size_t data_size = 42;
245 memset (&hash, 42, sizeof (GNUNET_HashCode));
246 data = GNUNET_malloc (data_size);
247 memset (data, 43, data_size);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
249 peer->dht_handle = GNUNET_DHT_connect (peer->cfg, 100);
251 GNUNET_assert (peer->dht_handle != NULL);
253 GNUNET_DHT_put (peer->dht_handle, &hash, 1, GNUNET_DHT_RO_NONE,
254 GNUNET_BLOCK_TYPE_TEST, data_size, data,
255 GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
256 TOTAL_TIMEOUT, &test_get, &p1);
261 setup_peer (struct PeerContext *p, const char *cfgname)
263 p->cfg = GNUNET_CONFIGURATION_create ();
266 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
267 "gnunet-service-arm",
271 "-c", cfgname, NULL);
273 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
278 run (void *cls, char *const *args, const char *cfgfile,
279 const struct GNUNET_CONFIGURATION_Handle *cfg)
281 GNUNET_assert (ok == 1);
285 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
286 (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly,
289 setup_peer (&p1, "test_dht_api_peer1.conf");
291 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
292 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
299 char *const argv[] = { "test-dht-api",
301 "test_dht_api_data.conf",
308 struct GNUNET_GETOPT_CommandLineOption options[] = {
309 GNUNET_GETOPT_OPTION_END
313 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
314 "test-dht-api", "nohelp", options, &run, &ok);
321 main (int argc, char *argv[])
325 GNUNET_log_setup ("test-dht-api",
334 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
339 /* end of test_dht_api.c */