big scheduler refactoring, expect some issues
[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, 360)
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   struct GNUNET_OS_Process *arm_proc;
89 #endif
90 };
91
92 static struct PeerContext p1;
93
94 struct RetryContext retry_context;
95
96
97 static int ok;
98
99 GNUNET_SCHEDULER_TaskIdentifier die_task;
100
101 #if VERBOSE
102 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
103 #else
104 #define OKPP do { ok++; } while (0)
105 #endif
106
107
108 static void
109 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   GNUNET_SCHEDULER_cancel (die_task);
112   die_task = GNUNET_SCHEDULER_NO_TASK;
113   GNUNET_DHT_disconnect (p1.dht_handle);
114   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115               "DHT disconnected, returning success!\n");
116   ok = 0;
117 }
118
119 static void
120 stop_arm (struct PeerContext *p)
121 {
122 #if START_ARM
123   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
124     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
125   GNUNET_OS_process_wait (p->arm_proc);
126   GNUNET_OS_process_close (p->arm_proc);
127   p->arm_proc = NULL;
128 #endif
129   GNUNET_CONFIGURATION_destroy (p->cfg);
130 }
131
132
133 static void
134 end_badly ()
135 {
136   /* do work here */
137 #if VERBOSE
138   fprintf (stderr, "Ending on an unhappy note.\n");
139 #endif
140
141   if ( (retry_context.peer_ctx != NULL) && 
142        (retry_context.peer_ctx->find_peer_handle != NULL) )
143     {
144       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping find peer request!\n");
145       GNUNET_DHT_find_peer_stop(retry_context.peer_ctx->find_peer_handle);
146     }
147   if ( (retry_context.peer_ctx != NULL) && 
148        (retry_context.peer_ctx->get_handle != NULL) )
149     {
150       GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n");
151       GNUNET_DHT_get_stop (retry_context.peer_ctx->get_handle);
152     }
153   if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
154     GNUNET_SCHEDULER_cancel(retry_context.retry_task);
155   GNUNET_DHT_disconnect (p1.dht_handle);
156   ok = 1;
157 }
158
159
160 /**
161  * Signature of the main function of a task.
162  *
163  * @param cls closure
164  * @param tc context information (why was this task triggered now)
165  */
166 void
167 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 {
169   struct PeerContext *peer = cls;
170
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
172   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
173     {
174       GNUNET_break (0);
175       GNUNET_SCHEDULER_cancel (die_task);
176       GNUNET_SCHEDULER_add_now (&end_badly, NULL);
177       return;
178     }
179
180   GNUNET_assert (peer->dht_handle != NULL);
181
182   GNUNET_DHT_find_peer_stop (peer->find_peer_handle);
183
184 #if HAVE_MALICIOUS
185   GNUNET_DHT_set_malicious_getter (peer->dht_handle, GNUNET_TIME_UNIT_SECONDS);
186   GNUNET_DHT_set_malicious_putter (peer->dht_handle, GNUNET_TIME_UNIT_SECONDS);
187   GNUNET_DHT_set_malicious_dropper (peer->dht_handle);
188 #endif
189   GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1),
190                                &end, &p1);
191 }
192
193
194 /**
195  * Iterator called on each result obtained from a find peer
196  * operation
197  *
198  * @param cls closure (NULL)
199  * @param peer the peer we learned about
200  * @param reply response
201  */
202 void test_find_peer_processor (void *cls,
203                                const struct GNUNET_HELLO_Message *hello)
204 {
205   struct RetryContext *retry_ctx = cls;
206   struct GNUNET_PeerIdentity peer;
207
208   if (GNUNET_OK == GNUNET_HELLO_get_id(hello, &peer))
209     {
210       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211                   "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(&peer));
212
213       if (retry_ctx->retry_task != GNUNET_SCHEDULER_NO_TASK)
214         {
215           GNUNET_SCHEDULER_cancel(retry_ctx->retry_task);
216           retry_ctx->retry_task = GNUNET_SCHEDULER_NO_TASK;
217         }
218
219       GNUNET_SCHEDULER_add_continuation (&test_find_peer_stop, &p1,
220                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
221     }
222   else
223     {
224       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                   "received find peer request, but hello_get_id failed!\n");
226     }
227
228 }
229
230 /**
231  * Retry the find_peer task on timeout. (Forward declaration)
232  *
233  * @param cls closure
234  * @param tc context information (why was this task triggered now?)
235  */
236 void
237 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
238
239 /**
240  * Retry the find_peer task on timeout.
241  *
242  * @param cls closure
243  * @param tc context information (why was this task triggered now)
244  */
245 void
246 retry_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
247 {
248   struct RetryContext *retry_ctx = cls;
249   GNUNET_HashCode hash;
250   memset (&hash, 42, sizeof (GNUNET_HashCode));
251
252   if (GNUNET_TIME_absolute_get_remaining(retry_ctx->real_timeout).rel_value > 0)
253     {
254       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255                   "test_find_peer timed out, retrying!\n");
256       retry_ctx->next_timeout = GNUNET_TIME_relative_multiply(retry_ctx->next_timeout, 2);
257       retry_ctx->peer_ctx->find_peer_handle 
258         = GNUNET_DHT_find_peer_start (retry_ctx->peer_ctx->dht_handle, 
259                                       retry_ctx->next_timeout, &hash,
260                                       GNUNET_DHT_RO_NONE,
261                                       &test_find_peer_processor, retry_ctx);
262     }
263   else
264     {
265       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266                   "test_find_peer timed out for good, failing!\n");
267
268       retry_ctx->peer_ctx->find_peer_handle = NULL;
269     }
270
271   if (retry_ctx->peer_ctx->find_peer_handle == NULL)
272     {
273       GNUNET_break (0);
274       GNUNET_SCHEDULER_cancel (die_task);
275       GNUNET_SCHEDULER_add_now (&end_badly, &p1);
276       return;
277     }
278   retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(retry_ctx->next_timeout, &retry_find_peer_stop, retry_ctx);
279 }
280
281 /**
282  * Retry the find_peer task on timeout.
283  *
284  * @param cls closure
285  * @param tc context information (why was this task triggered now?)
286  */
287 void
288 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 {
290   struct RetryContext *retry_ctx = cls;
291   GNUNET_HashCode hash;
292   memset (&hash, 42, sizeof (GNUNET_HashCode));
293
294   if (retry_ctx->peer_ctx->find_peer_handle != NULL)
295     {
296       GNUNET_DHT_find_peer_stop(retry_ctx->peer_ctx->find_peer_handle);
297       retry_ctx->peer_ctx->find_peer_handle = NULL;
298     }  
299   GNUNET_SCHEDULER_add_now (&retry_find_peer, retry_ctx);
300 }
301
302 /**
303  * Entry point for test of find_peer functionality.
304  *
305  * @param cls closure
306  * @param tc context information (why was this task triggered now)
307  */
308 void
309 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
310 {
311   struct PeerContext *peer = cls;
312   GNUNET_HashCode hash;
313   memset (&hash, 42, sizeof (GNUNET_HashCode));
314
315   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
316   GNUNET_assert (peer->dht_handle != NULL);
317
318   retry_context.real_timeout = GNUNET_TIME_relative_to_absolute(TOTAL_TIMEOUT);
319   retry_context.next_timeout = BASE_TIMEOUT;
320   retry_context.peer_ctx = peer;
321
322   peer->find_peer_handle
323     = GNUNET_DHT_find_peer_start (peer->dht_handle, retry_context.next_timeout, 
324                                   &hash,
325                                   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
326                                   &test_find_peer_processor, &retry_context);
327
328   if (peer->find_peer_handle == NULL)
329     {
330       GNUNET_break (0);
331       GNUNET_SCHEDULER_cancel (die_task);
332       GNUNET_SCHEDULER_add_now (&end_badly, &p1);
333       return;
334     }
335   retry_context.retry_task = GNUNET_SCHEDULER_add_delayed(retry_context.next_timeout, &retry_find_peer_stop, &retry_context);
336 }
337
338 /**
339  * Signature of the main function of a task.
340  *
341  * @param cls closure
342  * @param tc context information (why was this task triggered now)
343  */
344 void
345 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 {
347   struct PeerContext *peer = cls;
348
349   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
350   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
351     {
352       GNUNET_break (0);
353       GNUNET_SCHEDULER_cancel (die_task);
354       GNUNET_SCHEDULER_add_now (&end_badly, NULL);
355       return;
356     }
357   GNUNET_assert (peer->dht_handle != NULL);
358   GNUNET_DHT_get_stop (peer->get_handle);
359   GNUNET_SCHEDULER_add_now(&test_find_peer,
360                            &p1);
361 }
362
363 void
364 test_get_iterator (void *cls,
365                    struct GNUNET_TIME_Absolute exp,
366                    const GNUNET_HashCode * key,
367                    const struct GNUNET_PeerIdentity * const *get_path,
368                    const struct GNUNET_PeerIdentity * const *put_path,
369                    enum GNUNET_BLOCK_Type type, 
370                    size_t size, const void *data)
371 {
372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373               "test_get_iterator called (we got a result), stopping get request!\n");
374
375   GNUNET_SCHEDULER_add_continuation (&test_get_stop, &p1,
376                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 {
388   struct PeerContext *peer = cls;
389   GNUNET_HashCode hash;
390   memset (&hash, 42, sizeof (GNUNET_HashCode));
391
392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
393
394   GNUNET_assert (peer->dht_handle != NULL);
395   retry_context.real_timeout = GNUNET_TIME_relative_to_absolute(TOTAL_TIMEOUT);
396   retry_context.next_timeout = BASE_TIMEOUT;
397
398   peer->get_handle =
399     GNUNET_DHT_get_start (peer->dht_handle, 
400                           TOTAL_TIMEOUT,
401                           GNUNET_BLOCK_TYPE_TEST,
402                           &hash,
403                           GNUNET_DHT_RO_NONE,
404                           NULL, 0,
405                           NULL, 0,
406                           &test_get_iterator, NULL);
407
408   if (peer->get_handle == NULL)
409     {
410       GNUNET_break (0);
411       GNUNET_SCHEDULER_cancel (die_task);
412       GNUNET_SCHEDULER_add_now (&end_badly, &p1);
413       return;
414     }
415
416   retry_context.peer_ctx = peer;
417 }
418
419 /**
420  * Signature of the main function of a task.
421  *
422  * @param cls closure
423  * @param tc context information (why was this task triggered now)
424  */
425 void
426 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
427 {
428   struct PeerContext *peer = cls;
429   GNUNET_HashCode hash;
430   char *data;
431   size_t data_size = 42;
432   memset (&hash, 42, sizeof (GNUNET_HashCode));
433   data = GNUNET_malloc (data_size);
434   memset (data, 43, data_size);
435   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
436   peer->dht_handle = GNUNET_DHT_connect (peer->cfg, 100);
437
438   GNUNET_assert (peer->dht_handle != NULL);
439
440   GNUNET_DHT_put (peer->dht_handle, &hash, 
441                   GNUNET_DHT_RO_NONE,
442                   GNUNET_BLOCK_TYPE_TEST,
443                   data_size, data,
444                   GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
445                   TOTAL_TIMEOUT,
446                   &test_get, &p1);
447   GNUNET_free(data);
448 }
449
450 static void
451 setup_peer (struct PeerContext *p, const char *cfgname)
452 {
453   p->cfg = GNUNET_CONFIGURATION_create ();
454 #if START_ARM
455   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
456                                         "gnunet-service-arm",
457 #if VERBOSE_ARM
458                                         "-L", "DEBUG",
459 #endif
460                                         "-c", cfgname, NULL);
461 #endif
462   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
463
464 }
465
466 static void
467 run (void *cls,
468      char *const *args,
469      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
470 {
471   GNUNET_assert (ok == 1);
472   OKPP;
473
474   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
475                                            (GNUNET_TIME_UNIT_MINUTES, 1),
476                                            &end_badly, NULL);
477
478   setup_peer (&p1, "test_dht_api_peer1.conf");
479
480   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
481                                 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
482                                 &p1);
483 }
484
485 static int
486 check ()
487 {
488
489   char *const argv[] = { "test-dht-api",
490     "-c",
491     "test_dht_api_data.conf",
492 #if VERBOSE
493     "-L", "DEBUG",
494 #endif
495     NULL
496   };
497
498   struct GNUNET_GETOPT_CommandLineOption options[] = {
499     GNUNET_GETOPT_OPTION_END
500   };
501
502   ok = 1;
503   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
504                       argv, "test-dht-api", "nohelp", options, &run, &ok);
505   stop_arm (&p1);
506   return ok;
507 }
508
509
510 int
511 main (int argc, char *argv[])
512 {
513   int ret;
514 #ifdef MINGW
515   return GNUNET_SYSERR;
516 #endif
517
518   GNUNET_log_setup ("test-dht-api",
519 #if VERBOSE
520                     "DEBUG",
521 #else
522                     "WARNING",
523 #endif
524                     NULL);
525   ret = check ();
526
527   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
528
529   return ret;
530 }
531
532 /* end of test_dht_api.c */