9dc429148129917e61053e3bd35cd545f5cc3b58
[oweals/gnunet.git] / src / dht / test_dht_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file dht/test_dht_api.c
22  * @brief base test case for dht api
23  *
24  * This test case tests DHT api to DUMMY DHT service communication.
25  *
26  */
27 #include "platform.h"
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
36 #define VERBOSE GNUNET_NO
37
38 #define VERBOSE_ARM GNUNET_NO
39
40 #define START_ARM GNUNET_YES
41
42 /**
43  * How long until we give up on transmitting the message?
44  */
45 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
46
47 #define MTYPE 12345
48
49 struct PeerContext
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;
56
57 #if START_ARM
58   pid_t arm_pid;
59 #endif
60 };
61
62 static struct PeerContext p1;
63
64 static struct GNUNET_SCHEDULER_Handle *sched;
65
66 static int ok;
67
68 GNUNET_SCHEDULER_TaskIdentifier die_task;
69
70 #if VERBOSE
71 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
72 #else
73 #define OKPP do { ok++; } while (0)
74 #endif
75
76
77 static void
78 end (void *cls,
79     const struct GNUNET_SCHEDULER_TaskContext * tc)
80 {
81   /* do work here */
82   GNUNET_SCHEDULER_cancel (sched, die_task);
83
84   GNUNET_DHT_disconnect (p1.dht_handle);
85
86   die_task = GNUNET_SCHEDULER_NO_TASK;
87
88   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
89   {
90     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning FAIL!\n");
91     ok = 365;
92   }
93   else
94   {
95     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DHT disconnected, returning success!\n");
96     ok = 0;
97   }
98 }
99
100 static void
101 stop_arm (struct PeerContext *p)
102 {
103 #if START_ARM
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);
107 #endif
108   GNUNET_CONFIGURATION_destroy (p->cfg);
109 }
110
111
112 static void
113 end_badly ()
114 {
115   /* do work here */
116 #if VERBOSE
117   fprintf(stderr, "Ending on an unhappy note.\n");
118 #endif
119
120   GNUNET_DHT_disconnect (p1.dht_handle);
121
122   ok = 1;
123   return;
124 }
125
126 /**
127  * Signature of the main function of a task.
128  *
129  * @param cls closure
130  * @param tc context information (why was this task triggered now)
131  */
132 void test_find_peer_stop (void *cls,
133                const struct GNUNET_SCHEDULER_TaskContext * tc)
134 {
135   struct PeerContext *peer = cls;
136
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);
140
141   GNUNET_assert (peer->dht_handle != NULL);
142
143   GNUNET_DHT_find_peer_stop(peer->find_peer_handle, &end, &p1);
144
145   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
146
147 }
148
149 /**
150  * Signature of the main function of a task.
151  *
152  * @param cls closure
153  * @param tc context information (why was this task triggered now)
154  */
155 void test_find_peer (void *cls,
156                const struct GNUNET_SCHEDULER_TaskContext * tc)
157 {
158   struct PeerContext *peer = cls;
159   GNUNET_HashCode hash;
160   memset(&hash, 42, sizeof(GNUNET_HashCode));
161
162   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
163   GNUNET_assert (peer->dht_handle != NULL);
164
165   peer->find_peer_handle = GNUNET_DHT_find_peer_start(peer->dht_handle, TIMEOUT, 0, NULL, &hash, NULL, NULL, &test_find_peer_stop, &p1);
166
167   if (peer->find_peer_handle == NULL)
168     GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
169 }
170
171 /**
172  * Signature of the main function of a task.
173  *
174  * @param cls closure
175  * @param tc context information (why was this task triggered now)
176  */
177 void test_put (void *cls,
178                const struct GNUNET_SCHEDULER_TaskContext * tc)
179 {
180   struct PeerContext *peer = cls;
181   GNUNET_HashCode hash;
182   char *data;
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);
189
190   GNUNET_DHT_put(peer->dht_handle, &hash, 0, data_size, data, GNUNET_TIME_relative_to_absolute(TIMEOUT), TIMEOUT, &test_find_peer, &p1);
191
192 }
193
194 /**
195  * Signature of the main function of a task.
196  *
197  * @param cls closure
198  * @param tc context information (why was this task triggered now)
199  */
200 void test_get_stop (void *cls,
201                const struct GNUNET_SCHEDULER_TaskContext * tc)
202 {
203   struct PeerContext *peer = cls;
204
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);
208
209   GNUNET_assert (peer->dht_handle != NULL);
210
211   GNUNET_DHT_get_stop(peer->get_handle, &test_put, &p1);
212
213   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
214
215 }
216
217 /**
218  * Signature of the main function of a task.
219  *
220  * @param cls closure
221  * @param tc context information (why was this task triggered now)
222  */
223 void test_get (void *cls,
224                const struct GNUNET_SCHEDULER_TaskContext * tc)
225 {
226   struct PeerContext *peer = cls;
227   GNUNET_HashCode hash;
228   memset(&hash, 42, sizeof(GNUNET_HashCode));
229
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);
233
234   peer->get_handle = GNUNET_DHT_get_start(peer->dht_handle, TIMEOUT, 42, &hash, NULL, NULL, &test_get_stop, &p1);
235
236   if (peer->get_handle == NULL)
237     GNUNET_SCHEDULER_add_now(sched, &end_badly, &p1);
238 }
239
240 static void
241 setup_peer (struct PeerContext *p, const char *cfgname)
242 {
243   p->cfg = GNUNET_CONFIGURATION_create ();
244 #if START_ARM
245   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
246                                         "gnunet-service-arm",
247 #if VERBOSE_ARM
248                                         "-L", "DEBUG",
249 #endif
250                                         "-c", cfgname, NULL);
251 #endif
252   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
253
254 }
255
256 static void
257 run (void *cls,
258      struct GNUNET_SCHEDULER_Handle *s,
259      char *const *args,
260      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
261 {
262   GNUNET_assert (ok == 1);
263   OKPP;
264   sched = s;
265
266   die_task = GNUNET_SCHEDULER_add_delayed (sched,
267       GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 1), &end_badly, NULL);
268
269   setup_peer (&p1, "test_dht_api_peer1.conf");
270
271   GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_get, &p1);
272 }
273
274 static int
275 check ()
276 {
277
278   char *const argv[] = { "test-dht-api",
279     "-c",
280     "test_dht_api_data.conf",
281 #if VERBOSE
282     "-L", "DEBUG",
283 #endif
284     NULL
285   };
286
287   struct GNUNET_GETOPT_CommandLineOption options[] = {
288     GNUNET_GETOPT_OPTION_END
289   };
290
291   ok = 1;
292   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
293                       argv, "test-dht-api", "nohelp",
294                       options, &run, &ok);
295   stop_arm (&p1);
296   return ok;
297 }
298
299
300 int
301 main (int argc, char *argv[])
302 {
303   int ret;
304 #ifdef MINGW
305   return GNUNET_SYSERR;
306 #endif
307
308   GNUNET_log_setup ("test-dht-api",
309 #if VERBOSE
310                     "DEBUG",
311 #else
312                     "WARNING",
313 #endif
314                     NULL);
315   ret = check ();
316
317   //GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
318
319   return ret;
320 }
321
322 /* end of test_dht_api.c */