fixing double-adding of the 'die-task'
[oweals/gnunet.git] / src / dht / test_dht_twopeer.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_twopeer.c
22  * @brief base testcase for testing DHT service with
23  *        two running peers
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_dht_service.h"
29
30 /* DEFINES */
31 #define VERBOSE GNUNET_NO
32
33 #define MAX_GET_ATTEMPTS 10
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
36
37 #define DEFAULT_NUM_PEERS 2
38
39 /* Structs */
40
41 struct PeerGetContext
42 {
43   struct GNUNET_PeerIdentity *peer;
44
45   struct GNUNET_DHT_Handle *dht_handle;
46
47   struct GNUNET_DHT_GetHandle *get_handle;
48
49   unsigned int get_attempts;
50
51   GNUNET_SCHEDULER_TaskIdentifier retry_task;
52 };
53
54 /* Globals */
55 static char *test_directory;
56
57 static struct PeerGetContext curr_get_ctx;
58
59 static unsigned int expected_connections;
60
61 static unsigned long long peers_left;
62
63 static struct GNUNET_TESTING_PeerGroup *pg;
64
65 static unsigned long long num_peers;
66
67 static unsigned int total_gets;
68
69 static unsigned int gets_succeeded;
70
71 static unsigned int total_connections;
72
73 static unsigned int failed_connections;
74
75 static GNUNET_SCHEDULER_TaskIdentifier die_task;
76
77 static int ok;
78
79 static struct GNUNET_PeerIdentity peer1id;
80
81 static struct GNUNET_PeerIdentity peer2id;
82
83 static struct GNUNET_DHT_Handle *peer1dht;
84
85 static struct GNUNET_DHT_Handle *peer2dht;
86
87 /**
88  * Check whether peers successfully shut down.
89  */
90 void
91 shutdown_callback (void *cls, const char *emsg)
92 {
93   if (emsg != NULL)
94   {
95     if (ok == 0)
96       ok = 2;
97   }
98 }
99
100 static void
101 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 {
103   GNUNET_assert (pg != NULL);
104   GNUNET_assert (peer1dht != NULL);
105   GNUNET_assert (peer2dht != NULL);
106   GNUNET_DHT_disconnect (peer1dht);
107   GNUNET_DHT_disconnect (peer2dht);
108   fprintf (stderr, "Testing STOP! - finished\n");
109   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
110   pg = NULL;
111   ok = 0;
112 }
113
114 static void
115 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   if (peer1dht != NULL)
118     GNUNET_DHT_disconnect (peer1dht);
119
120   if (peer2dht != NULL)
121     GNUNET_DHT_disconnect (peer2dht);
122
123   fprintf (stderr, "Testing STOP - Badly!\n");
124   if (pg != NULL)
125   {
126     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
127     pg = NULL;
128   }
129
130   if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
131     GNUNET_SCHEDULER_cancel (curr_get_ctx.retry_task);
132 }
133
134 static void
135 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   if (curr_get_ctx.retry_task != GNUNET_SCHEDULER_NO_TASK)
138     GNUNET_SCHEDULER_cancel (curr_get_ctx.retry_task);
139
140   if (curr_get_ctx.get_handle != NULL)
141   {
142     GNUNET_DHT_get_stop (curr_get_ctx.get_handle);
143   }
144
145   GNUNET_SCHEDULER_add_now (&end_badly_cont, NULL);
146   ok = 1;
147 }
148
149 /* Forward declaration */
150 static void
151 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
152
153 /**
154  * Iterator called on each result obtained for a DHT
155  * operation that expects a reply
156  *
157  * @param cls closure
158  * @param exp when will this value expire
159  * @param key key of the result
160  * @param type type of the result
161  * @param size number of bytes in data
162  * @param data pointer to the result data
163  */
164 void
165 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
166                      const GNUNET_HashCode * key,
167                      const struct GNUNET_PeerIdentity *const *get_path,
168                      const struct GNUNET_PeerIdentity *const *put_path,
169                      enum GNUNET_BLOCK_Type type, size_t size, const void *data)
170 {
171   struct PeerGetContext *get_context = cls;
172
173   if (0 !=
174       memcmp (&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode)))
175   {
176     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
177                 "Key returned is not the same key as was searched for!\n");
178     GNUNET_SCHEDULER_cancel (die_task);
179     die_task =
180         GNUNET_SCHEDULER_add_now (&end_badly,
181                                   "key mismatch in get response!\n");
182     return;
183   }
184
185   if (get_context->retry_task != GNUNET_SCHEDULER_NO_TASK)
186   {
187     GNUNET_SCHEDULER_cancel (get_context->retry_task);
188     get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
189   }
190
191   if (get_context->peer == &peer2id)
192   {
193     get_context->peer = &peer1id;
194     get_context->dht_handle = peer2dht;
195     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196                 "Received first correct GET request response!\n");
197     GNUNET_DHT_get_stop (get_context->get_handle);
198     GNUNET_SCHEDULER_add_now (&do_get, get_context);
199   }
200   else
201   {
202     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
203                 "Received second correct GET request response!\n");
204     GNUNET_SCHEDULER_cancel (die_task);
205     GNUNET_DHT_get_stop (get_context->get_handle);
206     die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
207   }
208
209 }
210
211 static void
212 stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
213
214 static void
215 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 {
217   struct PeerGetContext *get_context = cls;
218
219   if (get_context->get_attempts < MAX_GET_ATTEMPTS)
220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221                 "Get attempt %u failed, retrying request!\n",
222                 get_context->get_attempts);
223   else
224   {
225     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226                 "Too many attempts failed, ending test!\n",
227                 get_context->get_attempts);
228     GNUNET_SCHEDULER_cancel (die_task);
229     die_task =
230         GNUNET_SCHEDULER_add_now (&end_badly,
231                                   "key mismatch in get response!\n");
232     return;
233   }
234   get_context->get_attempts++;
235   get_context->retry_task =
236       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
237                                     (GNUNET_TIME_UNIT_SECONDS, 10),
238                                     &stop_retry_get, get_context);
239   get_context->get_handle =
240       GNUNET_DHT_get_start (get_context->dht_handle,
241                             GNUNET_TIME_relative_multiply
242                             (GNUNET_TIME_UNIT_SECONDS, 5),
243                             GNUNET_BLOCK_TYPE_DHT_HELLO,
244                             &get_context->peer->hashPubKey,
245                             DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
246                             0, NULL, 0, &get_result_iterator, get_context);
247 }
248
249 static void
250 stop_retry_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 {
252   struct PeerGetContext *get_context = cls;
253
254   get_context->retry_task = GNUNET_SCHEDULER_NO_TASK;
255   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256               "Get attempt %u failed, canceling request!\n",
257               get_context->get_attempts);
258   GNUNET_DHT_get_stop (get_context->get_handle);
259   get_context->get_handle = NULL;
260   GNUNET_SCHEDULER_add_now (&get_stop_finished, get_context);
261 }
262
263 static void
264 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 {
266   struct PeerGetContext *get_context = cls;
267
268   get_context->retry_task =
269       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
270                                     (GNUNET_TIME_UNIT_SECONDS, 10),
271                                     &stop_retry_get, get_context);
272
273   get_context->get_handle =
274       GNUNET_DHT_get_start (get_context->dht_handle,
275                             GNUNET_TIME_relative_multiply
276                             (GNUNET_TIME_UNIT_SECONDS, 5),
277                             GNUNET_BLOCK_TYPE_DHT_HELLO,
278                             &get_context->peer->hashPubKey,
279                             DEFAULT_GET_REPLICATION, GNUNET_DHT_RO_NONE, NULL,
280                             0, NULL, 0, &get_result_iterator, get_context);
281 }
282
283
284 void
285 topology_callback (void *cls, const struct GNUNET_PeerIdentity *first,
286                    const struct GNUNET_PeerIdentity *second, uint32_t distance,
287                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
288                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
289                    struct GNUNET_TESTING_Daemon *first_daemon,
290                    struct GNUNET_TESTING_Daemon *second_daemon,
291                    const char *emsg)
292 {
293   if (emsg == NULL)
294   {
295     total_connections++;
296 #if VERBOSE
297     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298                 "connected peer %s to peer %s, distance %u\n",
299                 first_daemon->shortname, second_daemon->shortname, distance);
300 #endif
301   }
302 #if VERBOSE
303   else
304   {
305     failed_connections++;
306     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307                 "Failed to connect peer %s to peer %s with error :\n%s\n",
308                 first_daemon->shortname, second_daemon->shortname, emsg);
309   }
310 #endif
311
312   if (total_connections == expected_connections)
313   {
314 #if VERBOSE
315     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
317                 total_connections);
318 #endif
319     GNUNET_SCHEDULER_cancel (die_task);
320     die_task =
321         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, "from test gets");
322
323     curr_get_ctx.dht_handle = peer1dht;
324     curr_get_ctx.peer = &peer2id;
325     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
326                                   (GNUNET_TIME_UNIT_SECONDS, 2), &do_get,
327                                   &curr_get_ctx);
328   }
329   else if (total_connections + failed_connections == expected_connections)
330   {
331     GNUNET_SCHEDULER_cancel (die_task);
332     die_task =
333         GNUNET_SCHEDULER_add_now (&end_badly,
334                                   "from topology_callback (too many failed connections)");
335   }
336 }
337
338 static void
339 connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
340 {
341   expected_connections = -1;
342   if ((pg != NULL) && (peers_left == 0))
343     expected_connections =
344         GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE,
345                                          GNUNET_TESTING_TOPOLOGY_OPTION_ALL,
346                                          0.0, TIMEOUT, 12, NULL, NULL);
347
348   GNUNET_SCHEDULER_cancel (die_task);
349   if (expected_connections == GNUNET_SYSERR)
350     die_task =
351         GNUNET_SCHEDULER_add_now (&end_badly,
352                                   "from connect topology (bad return)");
353   else
354     die_task =
355         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
356                                       "from connect topology (timeout)");
357 }
358
359 static void
360 peers_started_callback (void *cls, const struct GNUNET_PeerIdentity *id,
361                         const struct GNUNET_CONFIGURATION_Handle *cfg,
362                         struct GNUNET_TESTING_Daemon *d, const char *emsg)
363 {
364   if (emsg != NULL)
365   {
366     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
367                 "Failed to start daemon with error: `%s'\n", emsg);
368     return;
369   }
370   GNUNET_assert (id != NULL);
371   if (peers_left == num_peers)
372   {
373     memcpy (&peer1id, id, sizeof (struct GNUNET_PeerIdentity));
374     peer1dht = GNUNET_DHT_connect (cfg, 100);
375     if (peer1dht == NULL)
376     {
377       GNUNET_SCHEDULER_cancel (die_task);
378       die_task =
379           GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
380     }
381   }
382   else
383   {
384     memcpy (&peer2id, id, sizeof (struct GNUNET_PeerIdentity));
385     peer2dht = GNUNET_DHT_connect (cfg, 100);
386     if (peer2dht == NULL)
387     {
388       GNUNET_SCHEDULER_cancel (die_task);
389       die_task =
390           GNUNET_SCHEDULER_add_now (&end_badly, "Failed to get dht handle!\n");
391     }
392   }
393
394
395   peers_left--;
396
397   if (peers_left == 0)
398   {
399 #if VERBOSE
400     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
401                 "All %d daemons started, now connecting peers!\n", num_peers);
402 #endif
403     GNUNET_SCHEDULER_cancel (die_task);
404     /* Set up task in case topology creation doesn't finish
405      * within a reasonable amount of time */
406     die_task =
407         GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
408                                       "from peers_started_callback");
409
410     GNUNET_SCHEDULER_add_now (&connect_topology, NULL);
411     ok = 0;
412   }
413 }
414
415 static void
416 run (void *cls, char *const *args, const char *cfgfile,
417      const struct GNUNET_CONFIGURATION_Handle *cfg)
418 {
419
420   if (GNUNET_YES !=
421       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
422                                              &test_directory))
423   {
424     ok = 404;
425     return;
426   }
427
428   if (GNUNET_SYSERR ==
429       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
430                                              &num_peers))
431     num_peers = DEFAULT_NUM_PEERS;
432
433   peers_left = num_peers;
434   total_gets = num_peers;
435   gets_succeeded = 0;
436   /* Set up a task to end testing if peer start fails */
437   die_task =
438       GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
439                                     "didn't start all daemons in reasonable amount of time!!!");
440
441   pg = GNUNET_TESTING_daemons_start (cfg, num_peers, 10, num_peers, TIMEOUT,
442                                      NULL, NULL, &peers_started_callback, NULL,
443                                      &topology_callback, NULL, NULL);
444
445 }
446
447 static int
448 check ()
449 {
450   int ret;
451
452   char *const argv[] = { "test-dht-twopeer",
453     "-c",
454     "test_dht_twopeer_data.conf",
455 #if VERBOSE
456     "-L", "DEBUG",
457 #endif
458     NULL
459   };
460   struct GNUNET_GETOPT_CommandLineOption options[] = {
461     GNUNET_GETOPT_OPTION_END
462   };
463   ret =
464       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
465                           "test-dht-twopeer", "nohelp", options, &run, &ok);
466   if (ret != GNUNET_OK)
467   {
468     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
469                 "`test-dht-twopeer': Failed with error code %d\n", ret);
470   }
471   return ok;
472 }
473
474 int
475 main (int argc, char *argv[])
476 {
477   int ret;
478
479   GNUNET_log_setup ("test-dht-twopeer",
480 #if VERBOSE
481                     "DEBUG",
482 #else
483                     "WARNING",
484 #endif
485                     NULL);
486   ret = check ();
487   /**
488    * Need to remove base directory, subdirectories taken care
489    * of by the testing framework.
490    */
491   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
492   {
493     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
494                 "Failed to remove testing directory %s\n", test_directory);
495   }
496   return ret;
497 }
498
499 /* end of test_dht_twopeer.c */