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_YES
38 #define VERBOSE_ARM GNUNET_NO
40 #define START_ARM GNUNET_YES
43 * How long until we give up on transmitting the message?
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_DHT_Handle *dht_handle;
53 struct GNUNET_PeerIdentity id;
54 struct GNUNET_DHT_GetHandle *get_handle;
55 struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
62 static struct PeerContext p1;
64 static struct GNUNET_SCHEDULER_Handle *sched;
68 GNUNET_SCHEDULER_TaskIdentifier die_task;
71 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
73 #define OKPP do { ok++; } while (0)
78 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 GNUNET_SCHEDULER_cancel (sched, die_task);
83 GNUNET_DHT_disconnect (p1.dht_handle);
85 die_task = GNUNET_SCHEDULER_NO_TASK;
87 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90 "DHT disconnected, returning FAIL!\n");
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "DHT disconnected, returning success!\n");
102 stop_arm (struct PeerContext *p)
105 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
106 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
107 GNUNET_OS_process_wait (p->arm_pid);
109 GNUNET_CONFIGURATION_destroy (p->cfg);
118 fprintf (stderr, "Ending on an unhappy note.\n");
121 GNUNET_DHT_disconnect (p1.dht_handle);
128 * Signature of the main function of a task.
131 * @param tc context information (why was this task triggered now)
134 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 struct PeerContext *peer = cls;
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
139 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
140 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
142 GNUNET_assert (peer->dht_handle != NULL);
144 GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &end, &p1);
146 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
152 * Iterator called on each result obtained from a find peer
155 * @param cls closure (NULL)
156 * @param peer the peer we learned about
157 * @param reply response
159 void test_find_peer_processor (void *cls,
160 const struct GNUNET_PeerIdentity *peer,
161 const struct GNUNET_MessageHeader *reply)
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(peer));
167 GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
168 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
173 * Signature of the main function of a task.
176 * @param tc context information (why was this task triggered now)
179 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct PeerContext *peer = cls;
182 GNUNET_HashCode hash;
183 memset (&hash, 42, sizeof (GNUNET_HashCode));
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
186 GNUNET_assert (peer->dht_handle != NULL);
188 peer->find_peer_handle =
189 GNUNET_DHT_find_peer_start (peer->dht_handle, TIMEOUT, 0, NULL, &hash,
190 &test_find_peer_processor, NULL, NULL, NULL);
192 if (peer->find_peer_handle == NULL)
193 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
197 * Signature of the main function of a task.
200 * @param tc context information (why was this task triggered now)
203 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
205 struct PeerContext *peer = cls;
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
208 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
209 GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
211 GNUNET_assert (peer->dht_handle != NULL);
213 GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
215 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
220 test_get_iterator (void *cls,
221 struct GNUNET_TIME_Absolute exp,
222 const GNUNET_HashCode * key,
223 uint32_t type, uint32_t size, const void *data)
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "test_get_iterator called (we got a result), stopping get request!\n");
228 GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
229 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
233 * Signature of the main function of a task.
236 * @param tc context information (why was this task triggered now)
239 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 struct PeerContext *peer = cls;
242 GNUNET_HashCode hash;
243 memset (&hash, 42, sizeof (GNUNET_HashCode));
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
247 GNUNET_assert (peer->dht_handle != NULL);
250 GNUNET_DHT_get_start (peer->dht_handle, TIMEOUT, 42, &hash,
251 &test_get_iterator, NULL, NULL, NULL);
253 if (peer->get_handle == NULL)
254 GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
258 * Signature of the main function of a task.
261 * @param tc context information (why was this task triggered now)
264 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
266 struct PeerContext *peer = cls;
267 GNUNET_HashCode hash;
269 size_t data_size = 42;
270 memset (&hash, 42, sizeof (GNUNET_HashCode));
271 data = GNUNET_malloc (data_size);
272 memset (data, 43, data_size);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
274 peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
276 GNUNET_assert (peer->dht_handle != NULL);
278 GNUNET_DHT_put (peer->dht_handle, &hash, 42, data_size, data,
279 GNUNET_TIME_relative_to_absolute (TIMEOUT), TIMEOUT,
285 setup_peer (struct PeerContext *p, const char *cfgname)
287 p->cfg = GNUNET_CONFIGURATION_create ();
289 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
290 "gnunet-service-arm",
294 "-c", cfgname, NULL);
296 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
302 struct GNUNET_SCHEDULER_Handle *s,
304 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
306 GNUNET_assert (ok == 1);
310 die_task = GNUNET_SCHEDULER_add_delayed (sched,
311 GNUNET_TIME_relative_multiply
312 (GNUNET_TIME_UNIT_MINUTES, 1),
315 setup_peer (&p1, "test_dht_api_peer1.conf");
317 GNUNET_SCHEDULER_add_delayed (sched,
318 GNUNET_TIME_relative_multiply
319 (GNUNET_TIME_UNIT_SECONDS, 3), &test_put,
327 char *const argv[] = { "test-dht-api",
329 "test_dht_api_data.conf",
336 struct GNUNET_GETOPT_CommandLineOption options[] = {
337 GNUNET_GETOPT_OPTION_END
341 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
342 argv, "test-dht-api", "nohelp", options, &run, &ok);
349 main (int argc, char *argv[])
353 return GNUNET_SYSERR;
356 GNUNET_log_setup ("test-dht-api",
365 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
370 /* end of test_dht_api.c */