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_RouteHandle *get_handle;
55 struct GNUNET_DHT_RouteHandle *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);
128 * Signature of the main function of a task.
131 * @param tc context information (why was this task triggered now)
133 void test_put (void *cls,
134 const struct GNUNET_SCHEDULER_TaskContext * tc)
136 struct PeerContext *peer = cls;
137 GNUNET_HashCode hash;
139 size_t data_size = 42;
140 memset(&hash, 42, sizeof(GNUNET_HashCode));
141 data = GNUNET_malloc(data_size);
142 memset(data, 43, data_size);
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
144 GNUNET_assert (peer->dht_handle != NULL);
146 GNUNET_DHT_put(peer->dht_handle, &hash, 0, data_size, data, GNUNET_TIME_relative_to_absolute(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 360)) ,GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 360), &end, NULL);
148 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
150 //GNUNET_SCHEDULER_add_now(sched, &end, NULL);
154 * Signature of the main function of a task.
157 * @param tc context information (why was this task triggered now)
159 void test_get_stop (void *cls,
160 const struct GNUNET_SCHEDULER_TaskContext * tc)
162 struct PeerContext *peer = cls;
163 GNUNET_HashCode hash;
164 memset(&hash, 42, sizeof(GNUNET_HashCode));
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
167 if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
168 GNUNET_SCHEDULER_add_now(sched, &end_badly, NULL);
170 GNUNET_assert (peer->dht_handle != NULL);
172 GNUNET_DHT_get_stop(peer->get_handle);
174 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
175 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
180 * Signature of the main function of a task.
183 * @param tc context information (why was this task triggered now)
185 void test_get (void *cls,
186 const struct GNUNET_SCHEDULER_TaskContext * tc)
188 struct PeerContext *peer = cls;
189 GNUNET_HashCode hash;
190 memset(&hash, 42, sizeof(GNUNET_HashCode));
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
193 peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
194 GNUNET_assert (peer->dht_handle != NULL);
196 peer->get_handle = GNUNET_DHT_get_start(peer->dht_handle, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 100), 42, &hash, NULL, NULL, &test_get_stop, &p1);
198 if (peer->get_handle == NULL)
199 GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
201 //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get_stop, &p1);
205 setup_peer (struct PeerContext *p, const char *cfgname)
207 p->cfg = GNUNET_CONFIGURATION_create ();
209 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
210 "gnunet-service-arm",
214 "-c", cfgname, NULL);
216 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
222 struct GNUNET_SCHEDULER_Handle *s,
224 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
226 GNUNET_assert (ok == 1);
230 die_task = GNUNET_SCHEDULER_add_delayed (sched,
231 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
233 setup_peer (&p1, "test_dht_api_peer1.conf");
235 GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get, &p1);
242 char *const argv[] = { "test-dht-api",
244 "test_dht_api_data.conf",
251 struct GNUNET_GETOPT_CommandLineOption options[] = {
252 GNUNET_GETOPT_OPTION_END
256 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
257 argv, "test-dht-api", "nohelp",
265 main (int argc, char *argv[])
269 return GNUNET_SYSERR;
272 GNUNET_log_setup ("test-dht-api",
281 //GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
286 /* end of test_dht_api.c */