backup errno
[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_YES
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, 120)
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   sleep(2);
114   GNUNET_SCHEDULER_cancel (sched, die_task);
115
116   GNUNET_DHT_disconnect (p1.dht_handle);
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 test_set_peer_malicious_drop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171   struct PeerContext *peer = cls;
172
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_set_peer_malicious_drop!\n");
174   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
175     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
176
177   GNUNET_assert (peer->dht_handle != NULL);
178
179   GNUNET_DHT_set_malicious_dropper (peer->dht_handle, &end, &p1);
180 }
181
182 /**
183  * Signature of the main function of a task.
184  *
185  * @param cls closure
186  * @param tc context information (why was this task triggered now)
187  */
188 void test_set_peer_malicious_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
189 {
190   struct PeerContext *peer = cls;
191
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_set_peer_malicious_put!\n");
193   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
194     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
195
196   GNUNET_assert (peer->dht_handle != NULL);
197
198   GNUNET_DHT_set_malicious_putter (peer->dht_handle, 750, &test_set_peer_malicious_drop, &p1);
199 }
200
201 /**
202  * Signature of the main function of a task.
203  *
204  * @param cls closure
205  * @param tc context information (why was this task triggered now)
206  */
207 void test_set_peer_malicious_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   struct PeerContext *peer = cls;
210
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_set_peer_malicious_get!\n");
212   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
213     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
214
215   GNUNET_assert (peer->dht_handle != NULL);
216
217   GNUNET_DHT_set_malicious_getter (peer->dht_handle, 1500, &test_set_peer_malicious_put, &p1);
218 }
219
220 /**
221  * Signature of the main function of a task.
222  *
223  * @param cls closure
224  * @param tc context information (why was this task triggered now)
225  */
226 void
227 test_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
228 {
229   struct PeerContext *peer = cls;
230
231   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer_stop!\n");
232   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
233     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
234
235   GNUNET_assert (peer->dht_handle != NULL);
236
237   GNUNET_DHT_find_peer_stop (peer->find_peer_handle, &test_set_peer_malicious_get, &p1);
238
239   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &end, &p1);
240
241 }
242
243
244 /**
245  * Iterator called on each result obtained from a find peer
246  * operation
247  *
248  * @param cls closure (NULL)
249  * @param peer the peer we learned about
250  * @param reply response
251  */
252 void test_find_peer_processor (void *cls,
253                                const struct GNUNET_HELLO_Message *hello)
254 {
255   struct RetryContext *retry_ctx = cls;
256   struct GNUNET_PeerIdentity peer;
257
258   if (GNUNET_OK == GNUNET_HELLO_get_id(hello, &peer))
259     {
260       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261                   "test_find_peer_processor called (peer `%s'), stopping find peer request!\n", GNUNET_i2s(&peer));
262
263       if (retry_ctx->retry_task != GNUNET_SCHEDULER_NO_TASK)
264         {
265           GNUNET_SCHEDULER_cancel(sched, retry_ctx->retry_task);
266           retry_ctx->retry_task = GNUNET_SCHEDULER_NO_TASK;
267         }
268
269       GNUNET_SCHEDULER_add_continuation (sched, &test_find_peer_stop, &p1,
270                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
271     }
272   else
273     {
274       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275                   "received find peer request, but hello_get_id failed!\n");
276     }
277
278 }
279
280 /**
281  * Retry the find_peer task on timeout. (Forward declaration)
282  *
283  * @param cls closure
284  * @param tc context information (why was this task triggered now?)
285  */
286 void
287 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
288
289 /**
290  * Retry the find_peer task on timeout.
291  *
292  * @param cls closure
293  * @param tc context information (why was this task triggered now)
294  */
295 void
296 retry_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
297 {
298   struct RetryContext *retry_ctx = cls;
299   GNUNET_HashCode hash;
300   memset (&hash, 42, sizeof (GNUNET_HashCode));
301
302   if (GNUNET_TIME_absolute_get_remaining(retry_ctx->real_timeout).value > 0)
303     {
304       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305                   "test_find_peer timed out, retrying!\n");
306       retry_ctx->next_timeout = GNUNET_TIME_relative_multiply(retry_ctx->next_timeout, 2);
307       retry_ctx->peer_ctx->find_peer_handle =
308           GNUNET_DHT_find_peer_start (retry_ctx->peer_ctx->dht_handle, retry_ctx->next_timeout, 0, &hash,
309                                       &test_find_peer_processor, retry_ctx, NULL, NULL);
310     }
311   else
312     {
313       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314                   "test_find_peer timed out for good, failing!\n");
315
316       retry_ctx->peer_ctx->find_peer_handle = NULL;
317     }
318
319   if (retry_ctx->peer_ctx->find_peer_handle == NULL)
320     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
321   else
322     retry_ctx->retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_ctx->next_timeout, &retry_find_peer_stop, retry_ctx);
323 }
324
325 /**
326  * Retry the find_peer task on timeout.
327  *
328  * @param cls closure
329  * @param tc context information (why was this task triggered now?)
330  */
331 void
332 retry_find_peer_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
333 {
334   struct RetryContext *retry_ctx = cls;
335   GNUNET_HashCode hash;
336   memset (&hash, 42, sizeof (GNUNET_HashCode));
337
338   if (retry_ctx->peer_ctx->find_peer_handle != NULL)
339     GNUNET_DHT_find_peer_stop(retry_ctx->peer_ctx->find_peer_handle, &retry_find_peer, retry_ctx);
340   else
341     GNUNET_SCHEDULER_add_now (sched, &retry_find_peer, retry_ctx);
342
343 }
344
345 /**
346  * Entry point for test of find_peer functionality.
347  *
348  * @param cls closure
349  * @param tc context information (why was this task triggered now)
350  */
351 void
352 test_find_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 {
354   struct PeerContext *peer = cls;
355   GNUNET_HashCode hash;
356   memset (&hash, 42, sizeof (GNUNET_HashCode));
357
358   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_find_peer!\n");
359   GNUNET_assert (peer->dht_handle != NULL);
360
361   retry_context.real_timeout = GNUNET_TIME_relative_to_absolute(TOTAL_TIMEOUT);
362   retry_context.next_timeout = BASE_TIMEOUT;
363   retry_context.peer_ctx = peer;
364
365   peer->find_peer_handle =
366     GNUNET_DHT_find_peer_start (peer->dht_handle, retry_context.next_timeout, 1, &hash,
367                                 &test_find_peer_processor, &retry_context, NULL, NULL);
368
369   if (peer->find_peer_handle == NULL)
370     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
371   else
372     retry_context.retry_task = GNUNET_SCHEDULER_add_delayed(sched, retry_context.next_timeout, &retry_find_peer_stop, &retry_context);
373 }
374
375 /**
376  * Signature of the main function of a task.
377  *
378  * @param cls closure
379  * @param tc context information (why was this task triggered now)
380  */
381 void
382 test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
383 {
384   struct PeerContext *peer = cls;
385
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
387   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
388     GNUNET_SCHEDULER_add_now (sched, &end_badly, NULL);
389
390   GNUNET_assert (peer->dht_handle != NULL);
391
392   GNUNET_DHT_get_stop (peer->get_handle, &test_find_peer, &p1);
393
394   //GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &test_put, &p1);
395
396 }
397
398 void
399 test_get_iterator (void *cls,
400                    struct GNUNET_TIME_Absolute exp,
401                    const GNUNET_HashCode * key,
402                    uint32_t type, uint32_t size, const void *data)
403 {
404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
405               "test_get_iterator called (we got a result), stopping get request!\n");
406
407   GNUNET_SCHEDULER_add_continuation (sched, &test_get_stop, &p1,
408                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
409 }
410
411 /**
412  * Signature of the main function of a task.
413  *
414  * @param cls closure
415  * @param tc context information (why was this task triggered now)
416  */
417 void
418 test_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
419 {
420   struct PeerContext *peer = cls;
421   GNUNET_HashCode hash;
422   memset (&hash, 42, sizeof (GNUNET_HashCode));
423
424   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
425
426   GNUNET_assert (peer->dht_handle != NULL);
427
428   peer->get_handle =
429     GNUNET_DHT_get_start (peer->dht_handle, TOTAL_TIMEOUT, 49, &hash,
430                           &test_get_iterator, NULL, NULL, NULL);
431
432   if (peer->get_handle == NULL)
433     GNUNET_SCHEDULER_add_now (sched, &end_badly, &p1);
434 }
435
436 /**
437  * Signature of the main function of a task.
438  *
439  * @param cls closure
440  * @param tc context information (why was this task triggered now)
441  */
442 void
443 test_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
444 {
445   struct PeerContext *peer = cls;
446   GNUNET_HashCode hash;
447   char *data;
448   size_t data_size = 42;
449   memset (&hash, 42, sizeof (GNUNET_HashCode));
450   data = GNUNET_malloc (data_size);
451   memset (data, 43, data_size);
452   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
453   peer->dht_handle = GNUNET_DHT_connect (sched, peer->cfg, 100);
454
455   GNUNET_assert (peer->dht_handle != NULL);
456
457   GNUNET_DHT_put (peer->dht_handle, &hash, 49, data_size, data,
458                   GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT), TOTAL_TIMEOUT,
459                   &test_get, &p1);
460   GNUNET_free(data);
461 }
462
463 static void
464 setup_peer (struct PeerContext *p, const char *cfgname)
465 {
466   p->cfg = GNUNET_CONFIGURATION_create ();
467 #if START_ARM
468   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
469                                         "gnunet-service-arm",
470 #if VERBOSE_ARM
471                                         "-L", "DEBUG",
472 #endif
473                                         "-c", cfgname, NULL);
474 #endif
475   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
476
477 }
478
479 static void
480 run (void *cls,
481      struct GNUNET_SCHEDULER_Handle *s,
482      char *const *args,
483      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
484 {
485   GNUNET_assert (ok == 1);
486   OKPP;
487   sched = s;
488
489   die_task = GNUNET_SCHEDULER_add_delayed (sched,
490                                            GNUNET_TIME_relative_multiply
491                                            (GNUNET_TIME_UNIT_MINUTES, 1),
492                                            &end_badly, NULL);
493
494   setup_peer (&p1, "test_dht_api_peer1.conf");
495
496   GNUNET_SCHEDULER_add_delayed (sched,
497                                 GNUNET_TIME_relative_multiply
498                                 (GNUNET_TIME_UNIT_SECONDS, 1), &test_put,
499                                 &p1);
500 }
501
502 static int
503 check ()
504 {
505
506   char *const argv[] = { "test-dht-api",
507     "-c",
508     "test_dht_api_data.conf",
509 #if VERBOSE
510     "-L", "DEBUG",
511 #endif
512     NULL
513   };
514
515   struct GNUNET_GETOPT_CommandLineOption options[] = {
516     GNUNET_GETOPT_OPTION_END
517   };
518
519   ok = 1;
520   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
521                       argv, "test-dht-api", "nohelp", options, &run, &ok);
522   stop_arm (&p1);
523   return ok;
524 }
525
526
527 int
528 main (int argc, char *argv[])
529 {
530   int ret;
531 #ifdef MINGW
532   return GNUNET_SYSERR;
533 #endif
534
535   GNUNET_log_setup ("test-dht-api",
536 #if VERBOSE
537                     "DEBUG",
538 #else
539                     "WARNING",
540 #endif
541                     NULL);
542   ret = check ();
543
544   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-dht-peer-1");
545
546   return ret;
547 }
548
549 /* end of test_dht_api.c */