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