service does simple put and get into datacache, test case verifies it works
[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, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   /* do work here */
81   GNUNET_SCHEDULER_cancel (sched, die_task);
82
83   GNUNET_DHT_disconnect (p1.dht_handle);
84
85   die_task = GNUNET_SCHEDULER_NO_TASK;
86
87   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
88     {
89       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90                   "DHT disconnected, returning FAIL!\n");
91       ok = 365;
92     }
93   else
94     {
95       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96                   "DHT disconnected, returning success!\n");
97       ok = 0;
98     }
99 }
100
101 static void
102 stop_arm (struct PeerContext *p)
103 {
104 #if START_ARM
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);
108 #endif
109   GNUNET_CONFIGURATION_destroy (p->cfg);
110 }
111
112
113 static void
114 end_badly ()
115 {
116   /* do work here */
117 #if VERBOSE
118   fprintf (stderr, "Ending on an unhappy note.\n");
119 #endif
120
121   GNUNET_DHT_disconnect (p1.dht_handle);
122
123   ok = 1;
124   return;
125 }
126
127 /**
128  * Signature of the main function of a task.
129  *
130  * @param cls closure
131  * @param tc context information (why was this task triggered now)
132  */
133 void
134 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
135 {
136   struct PeerContext *peer = cls;
137
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);
141
142   GNUNET_assert (peer->dht_handle != NULL);
143
144   GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &end, &p1);
145
146   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
147
148 }
149
150 /**
151  * Signature of the main function of a task.
152  *
153  * @param cls closure
154  * @param tc context information (why was this task triggered now)
155  */
156 void
157 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 {
159   struct PeerContext *peer = cls;
160   GNUNET_HashCode hash;
161   memset (&hash, 42, sizeof (GNUNET_HashCode));
162
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
164   GNUNET_assert (peer->dht_handle != NULL);
165
166   peer->find_peer_handle =
167     GNUNET_DHT_find_peer_start (peer->dht_handle, TIMEOUT, 0, NULL, &hash,
168                                 NULL, NULL, &test_find_peer_stop, &p1);
169
170   if (peer->find_peer_handle == NULL)
171     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
172 }
173
174 /**
175  * Signature of the main function of a task.
176  *
177  * @param cls closure
178  * @param tc context information (why was this task triggered now)
179  */
180 void
181 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 {
183   struct PeerContext *peer = cls;
184
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
186   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
187     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
188
189   GNUNET_assert (peer->dht_handle != NULL);
190
191   GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
192
193   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
194
195 }
196
197 void
198 test_get_iterator (void *cls,
199                    struct GNUNET_TIME_Absolute exp,
200                    const GNUNET_HashCode * key,
201                    uint32_t type, uint32_t size, const void *data)
202 {
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "test_get_iterator called (we got a result), stopping get request!\n");
205
206   GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
207                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
208 }
209
210 /**
211  * Signature of the main function of a task.
212  *
213  * @param cls closure
214  * @param tc context information (why was this task triggered now)
215  */
216 void
217 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
218 {
219   struct PeerContext *peer = cls;
220   GNUNET_HashCode hash;
221   memset (&hash, 42, sizeof (GNUNET_HashCode));
222
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
224
225   GNUNET_assert (peer->dht_handle != NULL);
226
227   peer->get_handle =
228     GNUNET_DHT_get_start (peer->dht_handle, TIMEOUT, 42, &hash,
229                           &test_get_iterator, NULL, NULL, NULL);
230
231   if (peer->get_handle == NULL)
232     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
233 }
234
235 /**
236  * Signature of the main function of a task.
237  *
238  * @param cls closure
239  * @param tc context information (why was this task triggered now)
240  */
241 void
242 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
243 {
244   struct PeerContext *peer = cls;
245   GNUNET_HashCode hash;
246   char *data;
247   size_t data_size = 42;
248   memset (&hash, 42, sizeof (GNUNET_HashCode));
249   data = GNUNET_malloc (data_size);
250   memset (data, 43, data_size);
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
252   peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
253
254   GNUNET_assert (peer->dht_handle != NULL);
255
256   GNUNET_DHT_put (peer->dht_handle, &hash, 0, data_size, data,
257                   GNUNET_TIME_relative_to_absolute (TIMEOUT), TIMEOUT,
258                   &test_get, &p1);
259
260 }
261
262 static void
263 setup_peer (struct PeerContext *p, const char *cfgname)
264 {
265   p->cfg = GNUNET_CONFIGURATION_create ();
266 #if START_ARM
267   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
268                                         "gnunet-service-arm",
269 #if VERBOSE_ARM
270                                         "-L", "DEBUG",
271 #endif
272                                         "-c", cfgname, NULL);
273 #endif
274   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
275
276 }
277
278 static void
279 run (void *cls,
280      struct GNUNET_SCHEDULER_Handle *s,
281      char *const *args,
282      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
283 {
284   GNUNET_assert (ok == 1);
285   OKPP;
286   sched = s;
287
288   die_task = GNUNET_SCHEDULER_add_delayed (sched,
289                                            GNUNET_TIME_relative_multiply
290                                            (GNUNET_TIME_UNIT_MINUTES, 1),
291                                            &end_badly, NULL);
292
293   setup_peer (&p1, "test_dht_api_peer1.conf");
294
295   GNUNET_SCHEDULER_add_delayed (sched,
296                                 GNUNET_TIME_relative_multiply
297                                 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
298                                 &p1);
299 }
300
301 static int
302 check ()
303 {
304
305   char *const argv[] = { "test-dht-api",
306     "-c",
307     "test_dht_api_data.conf",
308 #if VERBOSE
309     "-L", "DEBUG",
310 #endif
311     NULL
312   };
313
314   struct GNUNET_GETOPT_CommandLineOption options[] = {
315     GNUNET_GETOPT_OPTION_END
316   };
317
318   ok = 1;
319   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
320                       argv, "test-dht-api", "nohelp", options, &run, &ok);
321   stop_arm (&p1);
322   return ok;
323 }
324
325
326 int
327 main (int argc, char *argv[])
328 {
329   int ret;
330 #ifdef MINGW
331   return GNUNET_SYSERR;
332 #endif
333
334   GNUNET_log_setup ("test-dht-api",
335 #if VERBOSE
336                     "DEBUG",
337 #else
338                     "WARNING",
339 #endif
340                     NULL);
341   ret = check ();
342
343   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
344
345   return ret;
346 }
347
348 /* end of test_dht_api.c */