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