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