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