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 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)
79 const struct GNUNET_SCHEDULER_TaskContext * tc)
82 GNUNET_SCHEDULER_cancel (sched, die_task);
84 GNUNET_DHT_disconnect (p1.dht_handle);
86 die_task = GNUNET_SCHEDULER_NO_TASK;
88 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning FAIL!\n");
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning success!\n");
101 stop_arm (struct PeerContext *p)
104 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
105 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
106 GNUNET_OS_process_wait (p->arm_pid);
108 GNUNET_CONFIGURATION_destroy (p->cfg);
117 fprintf(stderr, "Ending on an unhappy note.\n");
120 GNUNET_DHT_disconnect (p1.dht_handle);
127 * Signature of the main function of a task.
130 * @param tc context information (why was this task triggered now)
132 void test_find_peer_stop (void *cls,
133 const struct GNUNET_SCHEDULER_TaskContext * tc)
135 struct PeerContext *peer = cls;
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
138 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
139 GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
141 GNUNET_assert (peer->dht_handle != NULL);
143 GNUNET_DHT_find_peer_stop(peer->find_peer_handle, &end, &p1);
145 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
150 * Signature of the main function of a task.
153 * @param tc context information (why was this task triggered now)
155 void test_find_peer (void *cls,
156 const struct GNUNET_SCHEDULER_TaskContext * tc)
158 struct PeerContext *peer = cls;
159 GNUNET_HashCode hash;
160 memset(&hash, 42, sizeof(GNUNET_HashCode));
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
163 GNUNET_assert (peer->dht_handle != NULL);
165 peer->find_peer_handle = GNUNET_DHT_find_peer_start(peer->dht_handle, TIMEOUT, 0, NULL, &hash, NULL, NULL, &test_find_peer_stop, &p1);
167 if (peer->find_peer_handle == NULL)
168 GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
172 * Signature of the main function of a task.
175 * @param tc context information (why was this task triggered now)
177 void test_put (void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext * tc)
180 struct PeerContext *peer = cls;
181 GNUNET_HashCode hash;
183 size_t data_size = 42;
184 memset(&hash, 42, sizeof(GNUNET_HashCode));
185 data = GNUNET_malloc(data_size);
186 memset(data, 43, data_size);
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
188 GNUNET_assert (peer->dht_handle != NULL);
190 GNUNET_DHT_put(peer->dht_handle, &hash, 0, data_size, data, GNUNET_TIME_relative_to_absolute(TIMEOUT), TIMEOUT, &test_find_peer, &p1);
195 * Signature of the main function of a task.
198 * @param tc context information (why was this task triggered now)
200 void test_get_stop (void *cls,
201 const struct GNUNET_SCHEDULER_TaskContext * tc)
203 struct PeerContext *peer = cls;
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
206 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
207 GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
209 GNUNET_assert (peer->dht_handle != NULL);
211 GNUNET_DHT_get_stop(peer->get_handle, &test_put, &p1);
213 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
218 * Signature of the main function of a task.
221 * @param tc context information (why was this task triggered now)
223 void test_get (void *cls,
224 const struct GNUNET_SCHEDULER_TaskContext * tc)
226 struct PeerContext *peer = cls;
227 GNUNET_HashCode hash;
228 memset(&hash, 42, sizeof(GNUNET_HashCode));
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
231 peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
232 GNUNET_assert (peer->dht_handle != NULL);
234 peer->get_handle = GNUNET_DHT_get_start(peer->dht_handle, TIMEOUT, 42, &hash, NULL, NULL, &test_get_stop, &p1);
236 if (peer->get_handle == NULL)
237 GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
241 setup_peer (struct PeerContext *p, const char *cfgname)
243 p->cfg = GNUNET_CONFIGURATION_create ();
245 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
246 "gnunet-service-arm",
250 "-c", cfgname, NULL);
252 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
258 struct GNUNET_SCHEDULER_Handle *s,
260 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
262 GNUNET_assert (ok == 1);
266 die_task = GNUNET_SCHEDULER_add_delayed (sched,
267 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
269 setup_peer (&p1, "test_dht_api_peer1.conf");
271 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get, &p1);
278 char *const argv[] = { "test-dht-api",
280 "test_dht_api_data.conf",
287 struct GNUNET_GETOPT_CommandLineOption options[] = {
288 GNUNET_GETOPT_OPTION_END
292 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
293 argv, "test-dht-api", "nohelp",
301 main (int argc, char *argv[])
305 return GNUNET_SYSERR;
308 GNUNET_log_setup ("test-dht-api",
317 //GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
322 /* end of test_dht_api.c */