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