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