bad commit fix
[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_YES
32
33 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
34
35 #define DEFAULT_NUM_PEERS 2
36
37 /* Structs */
38
39 struct PeerGetContext
40 {
41   struct GNUNET_PeerIdentity *peer;
42
43   struct GNUNET_DHT_Handle *dht_handle;
44
45   struct GNUNET_DHT_GetHandle *get_handle;
46 };
47
48 /* Globals */
49 static char *test_directory;
50
51 static struct PeerGetContext curr_get_ctx;
52
53 static unsigned int expected_connections;
54
55 static unsigned long long peers_left;
56
57 static struct GNUNET_TESTING_PeerGroup *pg;
58
59 static struct GNUNET_SCHEDULER_Handle *sched;
60
61 static unsigned long long num_peers;
62
63 static unsigned int total_gets;
64
65 static unsigned int gets_succeeded;
66
67 static unsigned int total_connections;
68
69 static unsigned int failed_connections;
70
71 GNUNET_SCHEDULER_TaskIdentifier die_task;
72
73 static int ok;
74
75 static struct GNUNET_PeerIdentity peer1id;
76
77 static struct GNUNET_PeerIdentity peer2id;
78
79 static struct GNUNET_DHT_Handle *peer1dht;
80
81 static struct GNUNET_DHT_Handle *peer2dht;
82
83 /**
84  * Check whether peers successfully shut down.
85  */
86 void shutdown_callback (void *cls,
87                         const char *emsg)
88 {
89   if (emsg != NULL)
90     {
91       if (ok == 0)
92         ok = 2;
93     }
94 }
95
96 static void
97 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
98 {
99   GNUNET_assert (pg != NULL);
100   GNUNET_assert (peer1dht != NULL);
101   GNUNET_assert (peer2dht != NULL);
102   GNUNET_DHT_disconnect(peer1dht);
103   GNUNET_DHT_disconnect(peer2dht);
104   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
105   ok = 0;
106 }
107
108 static void
109 end_badly_cont (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
110 {
111   if (peer1dht != NULL)
112       GNUNET_DHT_disconnect(peer1dht);
113
114   if (peer2dht != NULL)
115     GNUNET_DHT_disconnect(peer2dht);
116
117   if (pg != NULL)
118     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
119 }
120
121 static void
122 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
123 {
124   if (curr_get_ctx.get_handle != NULL)
125   {
126     GNUNET_DHT_get_stop(curr_get_ctx.get_handle, &end_badly_cont, NULL);
127   }
128
129   ok = 1;
130 }
131
132 /* Forward declaration */
133 static void
134 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc);
135
136 /**
137  * Iterator called on each result obtained for a DHT
138  * operation that expects a reply
139  *
140  * @param cls closure
141  * @param exp when will this value expire
142  * @param key key of the result
143  * @param type type of the result
144  * @param size number of bytes in data
145  * @param data pointer to the result data
146  */
147 void get_result_iterator (void *cls,
148                           struct GNUNET_TIME_Absolute exp,
149                           const GNUNET_HashCode * key,
150                           uint32_t type,
151                           uint32_t size,
152                           const void *data)
153 {
154   struct PeerGetContext *get_context = cls;
155   if (0 != memcmp(&get_context->peer->hashPubKey, key, sizeof (GNUNET_HashCode)))
156   {
157     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key returned is not the same key as was searched for!\n");
158     GNUNET_SCHEDULER_cancel(sched, die_task);
159     GNUNET_SCHEDULER_add_now(sched, &end_badly, "key mismatch in get response!\n");
160     return;
161   }
162
163   if (get_context->peer == &peer2id)
164   {
165     get_context->peer = &peer1id;
166     get_context->dht_handle = peer2dht;
167     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received first correct GET request response!\n");
168     GNUNET_DHT_get_stop(get_context->get_handle, &do_get, get_context);
169   }
170   else
171   {
172     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received second correct GET request response!\n");
173     GNUNET_SCHEDULER_cancel(sched, die_task);
174     GNUNET_DHT_get_stop(get_context->get_handle, &finish_testing, NULL);
175   }
176
177
178 }
179
180 static void
181 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
182 {
183   struct PeerGetContext *get_context = cls;
184
185   get_context->get_handle = GNUNET_DHT_get_start(get_context->dht_handle, GNUNET_TIME_relative_get_forever(), 130, &get_context->peer->hashPubKey, &get_result_iterator, get_context, NULL, NULL);
186 }
187
188
189 void
190 topology_callback (void *cls,
191                    const struct GNUNET_PeerIdentity *first,
192                    const struct GNUNET_PeerIdentity *second,
193                    uint32_t distance,
194                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
195                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
196                    struct GNUNET_TESTING_Daemon *first_daemon,
197                    struct GNUNET_TESTING_Daemon *second_daemon,
198                    const char *emsg)
199 {
200   if (emsg == NULL)
201     {
202       total_connections++;
203 #if VERBOSE
204       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
205                  first_daemon->shortname,
206                  second_daemon->shortname,
207                  distance);
208 #endif
209     }
210 #if VERBOSE
211   else
212     {
213       failed_connections++;
214       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
215                   first_daemon->shortname,
216                   second_daemon->shortname, emsg);
217     }
218 #endif
219
220   if (total_connections == expected_connections)
221     {
222 #if VERBOSE
223       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224                   "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
225                   total_connections);
226 #endif
227       GNUNET_SCHEDULER_cancel (sched, die_task);
228       die_task = GNUNET_SCHEDULER_add_delayed (sched, TIMEOUT,
229                                                &end_badly, "from test gets");
230
231       curr_get_ctx.dht_handle = peer1dht;
232       curr_get_ctx.peer = &peer2id;
233       //GNUNET_SCHEDULER_add_now (sched, &do_get, &curr_get_ctx);
234       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &do_get, &curr_get_ctx);
235     }
236   else if (total_connections + failed_connections == expected_connections)
237     {
238       GNUNET_SCHEDULER_cancel (sched, die_task);
239       die_task = GNUNET_SCHEDULER_add_now (sched,
240                                            &end_badly, "from topology_callback (too many failed connections)");
241     }
242 }
243
244 static void
245 connect_topology (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
246 {
247   expected_connections = -1;
248   if ((pg != NULL) && (peers_left == 0))
249     expected_connections = GNUNET_TESTING_connect_topology (pg, GNUNET_TESTING_TOPOLOGY_CLIQUE, GNUNET_TESTING_TOPOLOGY_OPTION_ALL, 0.0);
250
251   GNUNET_SCHEDULER_cancel (sched, die_task);
252   if (expected_connections == GNUNET_SYSERR)
253     die_task = GNUNET_SCHEDULER_add_now (sched,
254                                          &end_badly, "from connect topology (bad return)");
255
256
257   die_task = GNUNET_SCHEDULER_add_delayed (sched,
258                                            TIMEOUT,
259                                            &end_badly, "from connect topology (timeout)");
260 }
261
262 static void
263 peers_started_callback (void *cls,
264        const struct GNUNET_PeerIdentity *id,
265        const struct GNUNET_CONFIGURATION_Handle *cfg,
266        struct GNUNET_TESTING_Daemon *d, const char *emsg)
267 {
268   if (emsg != NULL)
269     {
270       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to start daemon with error: `%s'\n",
271                   emsg);
272       return;
273     }
274   GNUNET_assert (id != NULL);
275   if (peers_left == num_peers)
276   {
277     memcpy(&peer1id, id, sizeof(struct GNUNET_PeerIdentity));
278     peer1dht = GNUNET_DHT_connect(sched, cfg, 100);
279     if (peer1dht == NULL)
280     {
281       GNUNET_SCHEDULER_cancel (sched, die_task);
282       GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
283     }
284   }
285   else
286   {
287     memcpy(&peer2id, id, sizeof(struct GNUNET_PeerIdentity));
288     peer2dht = GNUNET_DHT_connect(sched, cfg, 100);
289     if (peer2dht == NULL)
290     {
291       GNUNET_SCHEDULER_cancel (sched, die_task);
292       GNUNET_SCHEDULER_add_now(sched, &end_badly, "Failed to get dht handle!\n");
293     }
294   }
295
296
297   peers_left--;
298
299   if (peers_left == 0)
300     {
301 #if VERBOSE
302       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303                   "All %d daemons started, now connecting peers!\n",
304                   num_peers);
305 #endif
306       GNUNET_SCHEDULER_cancel (sched, die_task);
307       /* Set up task in case topology creation doesn't finish
308        * within a reasonable amount of time */
309       die_task = GNUNET_SCHEDULER_add_delayed (sched,
310                                                TIMEOUT,
311                                                &end_badly, "from peers_started_callback");
312
313       GNUNET_SCHEDULER_add_now(sched, &connect_topology, NULL);
314       ok = 0;
315     }
316 }
317
318 static void
319 run (void *cls,
320      struct GNUNET_SCHEDULER_Handle *s,
321      char *const *args,
322      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
323 {
324   sched = s;
325
326   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
327     {
328       ok = 404;
329       return;
330     }
331
332   if (GNUNET_SYSERR ==
333       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
334                                              &num_peers))
335     num_peers = DEFAULT_NUM_PEERS;
336
337   peers_left = num_peers;
338   total_gets = num_peers;
339   gets_succeeded = 0;
340   /* Set up a task to end testing if peer start fails */
341   die_task = GNUNET_SCHEDULER_add_delayed (sched,
342                                            TIMEOUT,
343                                            &end_badly, "didn't start all daemons in reasonable amount of time!!!");
344
345   pg = GNUNET_TESTING_daemons_start (sched, cfg,
346                                      num_peers, TIMEOUT, NULL, NULL, &peers_started_callback, NULL,
347                                      &topology_callback, NULL, NULL);
348
349 }
350
351 static int
352 check ()
353 {
354   int ret;
355   char *const argv[] = {"test-dht-twopeer",
356     "-c",
357     "test_dht_twopeer_data.conf",
358 #if VERBOSE
359     "-L", "DEBUG",
360 #endif
361     NULL
362   };
363   struct GNUNET_GETOPT_CommandLineOption options[] = {
364     GNUNET_GETOPT_OPTION_END
365   };
366   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
367                       argv, "test-dht-twopeer", "nohelp",
368                       options, &run, &ok);
369   if (ret != GNUNET_OK)
370     {
371       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-twopeer': Failed with error code %d\n", ret);
372     }
373   return ok;
374 }
375
376 int
377 main (int argc, char *argv[])
378 {
379   int ret;
380
381   GNUNET_log_setup ("test-dht-twopeer",
382 #if VERBOSE
383                     "DEBUG",
384 #else
385                     "WARNING",
386 #endif
387                     NULL);
388   ret = check ();
389   /**
390    * Need to remove base directory, subdirectories taken care
391    * of by the testing framework.
392    */
393   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
394     {
395       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
396     }
397   return ret;
398 }
399
400 /* end of test_dht_twopeer.c */