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