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