dead
[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 3, 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 #include "gnunet_hello_lib.h"
36
37 #define VERBOSE GNUNET_NO
38
39 #define VERBOSE_ARM GNUNET_NO
40
41 #define START_ARM GNUNET_YES
42
43 /**
44  * How long until we really give up on a particular testcase portion?
45  */
46 #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 50)
47
48 /**
49  * How long until we give up on any particular operation (and retry)?
50  */
51 #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
52
53 #define MTYPE 12345
54
55 struct RetryContext
56 {
57   /**
58    * When to really abort the operation.
59    */
60   struct GNUNET_TIME_Absolute real_timeout;
61
62   /**
63    * What timeout to set for the current attempt (increases)
64    */
65   struct GNUNET_TIME_Relative next_timeout;
66
67   /**
68    * The context of the peer we are dealing with.
69    */
70   struct PeerContext *peer_ctx;
71
72   /**
73    * The task identifier of the retry task, so it can be cancelled.
74    */
75   GNUNET_SCHEDULER_TaskIdentifier retry_task;
76
77 };
78
79 struct PeerContext
80 {
81   struct GNUNET_CONFIGURATION_Handle *cfg;
82   struct GNUNET_DHT_Handle *dht_handle;
83   struct GNUNET_PeerIdentity id;
84   struct GNUNET_DHT_GetHandle *get_handle;
85   struct GNUNET_DHT_FindPeerHandle *find_peer_handle;
86
87 #if START_ARM
88   pid_t arm_pid;
89 #endif
90 };
91
92 static struct PeerContext p1;
93
94 struct RetryContext retry_context;
95
96 static struct GNUNET_SCHEDULER_Handle *sched;
97
98 static int ok;
99
100 GNUNET_SCHEDULER_TaskIdentifier die_task;
101
102 #if VERBOSE
103 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
104 #else
105 #define OKPP do { ok++; } while (0)
106 #endif
107
108
109 static void
110 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 {
112   /* do work here */
113   GNUNET_SCHEDULER_cancel (sched, die_task);
114
115   GNUNET_DHT_disconnect (p1.dht_handle);
116
117   die_task = GNUNET_SCHEDULER_NO_TASK;
118
119   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
120     {
121       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122                   "DHT disconnected, returning FAIL!\n");
123       ok = 365;
124     }
125   else
126     {
127       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128                   "DHT disconnected, returning success!\n");
129       ok = 0;
130     }
131 }
132
133 static void
134 stop_arm (struct PeerContext *p)
135 {
136 #if START_ARM
137   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
138     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
139   GNUNET_OS_process_wait (p->arm_pid);
140 #endif
141   GNUNET_CONFIGURATION_destroy (p->cfg);
142 }
143
144
145 static void
146 end_badly ()
147 {
148   /* do work here */
149 #if VERBOSE
150   fprintf (stderr, "Ending on an unhappy note.\n");
151 #endif
152
153   if ((retry_context.peer_ctx != NULL) && (retry_context.peer_ctx->find_peer_handle != NULL))
154     GNUNET_DHT_find_peer_stop(retry_context.peer_ctx->find_peer_handle, NULL, NULL);
155   if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
156     GNUNET_SCHEDULER_cancel(sched, retry_context.retry_task);
157   GNUNET_DHT_disconnect (p1.dht_handle);
158
159   ok = 1;
160   return;
161 }
162
163 /**
164  * Signature of the main function of a task.
165  *
166  * @param cls closure
167  * @param tc context information (why was this task triggered now)
168  */
169 void
170 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 {
172   struct PeerContext *peer = cls;
173
174   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
175   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
176     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
177
178   GNUNET_assert (peer->dht_handle != NULL);
179
180   GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &end, &p1);
181
182   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
183
184 }
185
186
187 /**
188  * Iterator called on each result obtained from a find peer
189  * operation
190  *
191  * @param cls closure (NULL)
192  * @param peer the peer we learned about
193  * @param reply response
194  */
195 void test_find_peer_processor (void *cls,
196                                const struct GNUNET_HELLO_Message *hello)
197 {
198   struct RetryContext *retry_ctx = cls;
199   struct GNUNET_PeerIdentity peer;
200
201   if (GNUNET_OK == GNUNET_HELLO_get_id(hello, &peer))
202     {
203       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204                   "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(&peer));
205
206       if (retry_ctx->retry_task != GNUNET_SCHEDULER_NO_TASK)
207         {
208           GNUNET_SCHEDULER_cancel(sched, retry_ctx->retry_task);
209           retry_ctx->retry_task = GNUNET_SCHEDULER_NO_TASK;
210         }
211
212       GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
213                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
214     }
215   else
216     {
217       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218                   "received find peer request, but hello_get_id failed!\n");
219     }
220
221 }
222
223 /**
224  * Retry the find_peer task on timeout. (Forward declaration)
225  *
226  * @param cls closure
227  * @param tc context information (why was this task triggered now?)
228  */
229 void
230 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
231
232 /**
233  * Retry the find_peer task on timeout.
234  *
235  * @param cls closure
236  * @param tc context information (why was this task triggered now)
237  */
238 void
239 retry_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 {
241   struct RetryContext *retry_ctx = cls;
242   GNUNET_HashCode hash;
243   memset (&hash, 42, sizeof (GNUNET_HashCode));
244
245   if (GNUNET_TIME_absolute_get_remaining(retry_ctx->real_timeout).value > 0)
246     {
247       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248                   "test_find_peer timed out, retrying!\n");
249       retry_ctx->next_timeout = GNUNET_TIME_relative_multiply(retry_ctx->next_timeout, 2);
250       retry_ctx->peer_ctx->find_peer_handle =
251           GNUNET_DHT_find_peer_start (retry_ctx->peer_ctx->dht_handle, retry_ctx->next_timeout, 0, &hash,
252                                       &test_find_peer_processor, retry_ctx, NULL, NULL);
253     }
254   else
255     {
256       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
257                   "test_find_peer timed out for good, failing!\n");
258
259       retry_ctx->peer_ctx->find_peer_handle = NULL;
260     }
261
262   if (retry_ctx->peer_ctx->find_peer_handle == NULL)
263     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
264   else
265     retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_ctx->next_timeout, &retry_find_peer_stop, retry_ctx);
266 }
267
268 /**
269  * Retry the find_peer task on timeout.
270  *
271  * @param cls closure
272  * @param tc context information (why was this task triggered now?)
273  */
274 void
275 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
276 {
277   struct RetryContext *retry_ctx = cls;
278   GNUNET_HashCode hash;
279   memset (&hash, 42, sizeof (GNUNET_HashCode));
280
281   if (retry_ctx->peer_ctx->find_peer_handle != NULL)
282     GNUNET_DHT_find_peer_stop(retry_ctx->peer_ctx->find_peer_handle, &retry_find_peer, retry_ctx);
283   else
284     GNUNET_SCHEDULER_add_now (sched, &retry_find_peer, retry_ctx);
285
286 }
287
288 /**
289  * Entry point for test of find_peer functionality.
290  *
291  * @param cls closure
292  * @param tc context information (why was this task triggered now)
293  */
294 void
295 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
296 {
297   struct PeerContext *peer = cls;
298   GNUNET_HashCode hash;
299   memset (&hash, 42, sizeof (GNUNET_HashCode));
300
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
302   GNUNET_assert (peer->dht_handle != NULL);
303
304   retry_context.real_timeout = GNUNET_TIME_relative_to_absolute(TOTAL_TIMEOUT);
305   retry_context.next_timeout = BASE_TIMEOUT;
306   retry_context.peer_ctx = peer;
307
308   peer->find_peer_handle =
309     GNUNET_DHT_find_peer_start (peer->dht_handle, retry_context.next_timeout, 0, &hash,
310                                 &test_find_peer_processor, &retry_context, NULL, NULL);
311
312   if (peer->find_peer_handle == NULL)
313     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
314   else
315     retry_context.retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_context.next_timeout, &retry_find_peer_stop, &retry_context);
316 }
317
318 /**
319  * Signature of the main function of a task.
320  *
321  * @param cls closure
322  * @param tc context information (why was this task triggered now)
323  */
324 void
325 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 {
327   struct PeerContext *peer = cls;
328
329   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
330   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
331     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
332
333   GNUNET_assert (peer->dht_handle != NULL);
334
335   GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
336
337   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
338
339 }
340
341 void
342 test_get_iterator (void *cls,
343                    struct GNUNET_TIME_Absolute exp,
344                    const GNUNET_HashCode * key,
345                    uint32_t type, uint32_t size, const void *data)
346 {
347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348               "test_get_iterator called (we got a result), stopping get request!\n");
349
350   GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
351                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
352 }
353
354 /**
355  * Signature of the main function of a task.
356  *
357  * @param cls closure
358  * @param tc context information (why was this task triggered now)
359  */
360 void
361 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
362 {
363   struct PeerContext *peer = cls;
364   GNUNET_HashCode hash;
365   memset (&hash, 42, sizeof (GNUNET_HashCode));
366
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
368
369   GNUNET_assert (peer->dht_handle != NULL);
370
371   peer->get_handle =
372     GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT, 42, &hash,
373                           &test_get_iterator, NULL, NULL, NULL);
374
375   if (peer->get_handle == NULL)
376     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
377 }
378
379 /**
380  * Signature of the main function of a task.
381  *
382  * @param cls closure
383  * @param tc context information (why was this task triggered now)
384  */
385 void
386 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 {
388   struct PeerContext *peer = cls;
389   GNUNET_HashCode hash;
390   char *data;
391   size_t data_size = 42;
392   memset (&hash, 42, sizeof (GNUNET_HashCode));
393   data = GNUNET_malloc (data_size);
394   memset (data, 43, data_size);
395   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
396   peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
397
398   GNUNET_assert (peer->dht_handle != NULL);
399
400   GNUNET_DHT_put (peer->dht_handle, &hash, 42, data_size, data,
401                   GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT), TOTAL_TIMEOUT,
402                   &test_get, &p1);
403   GNUNET_free(data);
404 }
405
406 static void
407 setup_peer (struct PeerContext *p, const char *cfgname)
408 {
409   p->cfg = GNUNET_CONFIGURATION_create ();
410 #if START_ARM
411   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
412                                         "gnunet-service-arm",
413 #if VERBOSE_ARM
414                                         "-L", "DEBUG",
415 #endif
416                                         "-c", cfgname, NULL);
417 #endif
418   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
419
420 }
421
422 static void
423 run (void *cls,
424      struct GNUNET_SCHEDULER_Handle *s,
425      char *const *args,
426      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
427 {
428   GNUNET_assert (ok == 1);
429   OKPP;
430   sched = s;
431
432   die_task = GNUNET_SCHEDULER_add_delayed (sched,
433                                            GNUNET_TIME_relative_multiply
434                                            (GNUNET_TIME_UNIT_MINUTES, 1),
435                                            &end_badly, NULL);
436
437   setup_peer (&p1, "test_dht_api_peer1.conf");
438
439   GNUNET_SCHEDULER_add_delayed (sched,
440                                 GNUNET_TIME_relative_multiply
441                                 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
442                                 &p1);
443 }
444
445 static int
446 check ()
447 {
448
449   char *const argv[] = { "test-dht-api",
450     "-c",
451     "test_dht_api_data.conf",
452 #if VERBOSE
453     "-L", "DEBUG",
454 #endif
455     NULL
456   };
457
458   struct GNUNET_GETOPT_CommandLineOption options[] = {
459     GNUNET_GETOPT_OPTION_END
460   };
461
462   ok = 1;
463   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
464                       argv, "test-dht-api", "nohelp", options, &run, &ok);
465   stop_arm (&p1);
466   return ok;
467 }
468
469
470 int
471 main (int argc, char *argv[])
472 {
473   int ret;
474 #ifdef MINGW
475   return GNUNET_SYSERR;
476 #endif
477
478   GNUNET_log_setup ("test-dht-api",
479 #if VERBOSE
480                     "DEBUG",
481 #else
482                     "WARNING",
483 #endif
484                     NULL);
485   ret = check ();
486
487   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
488
489   return ret;
490 }
491
492 /* end of test_dht_api.c */