abort on error -- missing return
[oweals/gnunet.git] / src / dht / test_dht_2dtorus.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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_2dtorus.c
22  *
23  * @brief Test for the dht service: store and retrieve in a 2d_torus.
24  * Each peer stores it own ID in the DHT and then a different peer tries to
25  * retrieve that key from it. The GET starts after a first round of PUTS has
26  * been made. Periodically, each peer stores its ID into the DHT. If after
27  * a timeout no result has been returned, the test fails.
28  */
29 #include "platform.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_dht_service.h"
32
33 #define VERBOSE GNUNET_YES
34 #define REMOVE_DIR GNUNET_YES
35
36
37 /**
38  * How long until we give up on connecting the peers?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
41
42 static int ok;
43
44 /**
45  * Be verbose
46  */
47 static int verbose;
48
49 /**
50  * Total number of peers in the test.
51  */
52 static unsigned long long num_peers;
53
54 /**
55  * Global configuration file
56  */
57 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
58
59 /**
60  * Total number of currently running peers.
61  */
62 static unsigned long long peers_running;
63
64 /**
65  * Total number of connections in the whole network.
66  */
67 static unsigned int total_connections;
68
69 /**
70  * The currently running peer group.
71  */
72 static struct GNUNET_TESTING_PeerGroup *pg;
73
74 /**
75  * File to report results to.
76  */
77 static struct GNUNET_DISK_FileHandle *output_file;
78
79 /**
80  * File to log connection info, statistics to.
81  */
82 static struct GNUNET_DISK_FileHandle *data_file;
83
84 /**
85  * Task called to disconnect peers.
86  */
87 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
88
89 /**
90  * Task To perform tests
91  */
92 static GNUNET_SCHEDULER_TaskIdentifier test_task;
93
94 /**
95  * Task to do DHT_puts
96  */
97 static GNUNET_SCHEDULER_TaskIdentifier put_task;
98
99 /**
100  * Task called to shutdown test.
101  */
102 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
103
104 static char *topology_file;
105
106 struct GNUNET_TESTING_Daemon *d1;
107
108 struct GNUNET_TESTING_Daemon *d2;
109
110 struct GNUNET_DHT_Handle **hs;
111
112 struct GNUNET_DHT_GetHandle *get_h;
113
114 struct GNUNET_DHT_GetHandle *get_h_2;
115
116 struct GNUNET_DHT_GetHandle *get_h_far;
117
118 unsigned int found;
119
120 /**
121  * Check whether peers successfully shut down.
122  */
123 static void
124 shutdown_callback (void *cls, const char *emsg)
125 {
126   if (emsg != NULL)
127   {
128 #if VERBOSE
129     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
130 #endif
131     ok++;
132   }
133   else
134   {
135 #if VERBOSE
136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
137 #endif
138   }
139 }
140
141
142 static void
143 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 {
145 #if VERBOSE
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
147 #endif
148
149   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
150   {
151     GNUNET_SCHEDULER_cancel (disconnect_task);
152     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
153   }
154
155   if (data_file != NULL)
156     GNUNET_DISK_file_close (data_file);
157   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
158   GNUNET_CONFIGURATION_destroy (testing_cfg);
159 }
160
161
162 static void
163 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   unsigned int i;
166
167   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168               "test: disconnecting peers\n");
169   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
170   GNUNET_SCHEDULER_cancel (put_task);
171   if (NULL != get_h)
172     GNUNET_DHT_get_stop(get_h);
173   if (NULL != get_h_2)
174     GNUNET_DHT_get_stop(get_h_2);
175   if (NULL != get_h_far)
176     GNUNET_DHT_get_stop(get_h_far);
177   for (i = 0; i < num_peers;  i++)
178   {
179     GNUNET_DHT_disconnect(hs[i]);
180   }
181   GNUNET_SCHEDULER_cancel (shutdown_handle);
182   shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
183 }
184
185 static void
186 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
187                     const GNUNET_HashCode * key,
188                     const struct GNUNET_PeerIdentity *get_path,
189                     unsigned int get_path_length,
190                     const struct GNUNET_PeerIdentity *put_path,
191                     unsigned int put_path_length,
192                     enum GNUNET_BLOCK_Type type, size_t size, const void *data)
193 {
194   int i;
195
196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
197             "test: ************* FOUND!!! ***********\n");
198   if (sizeof(GNUNET_HashCode) == size)
199   {
200     const GNUNET_HashCode *h = data;
201     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202                 "test:   Contents: %s\n",
203                 GNUNET_h2s_full (h));
204
205   }
206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207             "test: PATH: (get %u, put %u)\n",
208             get_path_length,
209             put_path_length);
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211             "test:   LOCAL\n");
212   for (i = get_path_length - 1; i >= 0; i--)
213   {
214     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215             "test:   %s\n",
216             GNUNET_i2s (&get_path[i]));
217   }
218   for (i = put_path_length - 1; i >= 0; i--)
219   {
220     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221             "test:   %s\n",
222             GNUNET_i2s (&put_path[i]));
223   }
224   found++;
225   if (found < 3)
226     return;
227   ok = 0;
228   GNUNET_SCHEDULER_cancel(disconnect_task);
229   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
230 }
231
232 static void
233 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
234 {
235   struct GNUNET_TESTING_Daemon *d;
236   struct GNUNET_TESTING_Daemon *d2;
237   struct GNUNET_TESTING_Daemon *d_far;
238   struct GNUNET_TESTING_Daemon *o;
239   struct GNUNET_TESTING_Daemon *aux;
240   const char *id_aux;
241   const char *id_origin = "FC74";
242   const char *id_near = "9P6V";
243   const char *id_near2 = "2GDS";
244   const char *id_far = "KPST";
245   unsigned int i;
246
247   d = d2 = d_far = o = NULL;
248   for (i = 0; i < num_peers; i++)
249   {
250     aux = GNUNET_TESTING_daemon_get (pg, i);
251     id_aux = GNUNET_i2s (&aux->id);
252     if (strcmp (id_aux, id_origin) == 0)
253       o = aux;
254     if (strcmp (id_aux, id_far) == 0)
255       d_far = aux;
256     if (strcmp (id_aux, id_near) == 0)
257       d = aux;
258     if (strcmp (id_aux, id_near2) == 0)
259       d2 = aux;
260   }
261   if ( (NULL == o) || (NULL == d) || (NULL == d2) || (NULL == d_far))
262   {
263     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
264                 "test: Peers not found (hostkey file changed?)\n");
265     GNUNET_SCHEDULER_cancel (disconnect_task);
266     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
267     return;
268   }
269   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
270   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271               "test: looking for %s\n",
272               GNUNET_h2s_full (&d->id.hashPubKey));
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274               "test: looking for %s\n",
275               GNUNET_h2s_full (&d2->id.hashPubKey));
276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277               "test: looking for %s\n",
278               GNUNET_h2s_full (&d_far->id.hashPubKey));
279   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
280               "test:        from %s\n",
281               GNUNET_h2s_full (&o->id.hashPubKey));
282   found = 0;
283   get_h = GNUNET_DHT_get_start (hs[0],
284                                 GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
285                                 GNUNET_BLOCK_TYPE_TEST,   /* type */
286                                 &d->id.hashPubKey,   /*key to search */
287                                 4U,        /* replication level */
288                                 GNUNET_DHT_RO_RECORD_ROUTE |
289                                   GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
290                                 NULL,     /* xquery */
291                                 0,        /* xquery bits */
292                                 &dht_get_id_handler,
293                                 NULL);
294   get_h_2 = GNUNET_DHT_get_start (hs[0],
295                                   GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
296                                   GNUNET_BLOCK_TYPE_TEST,   /* type */
297                                   &d2->id.hashPubKey,   /*key to search */
298                                   4U,        /* replication level */
299                                   GNUNET_DHT_RO_RECORD_ROUTE |
300                                     GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
301                                   NULL,     /* xquery */
302                                   0,        /* xquery bits */
303                                   &dht_get_id_handler,
304                                   NULL);
305   get_h_far = GNUNET_DHT_get_start (hs[0],
306                                     GNUNET_TIME_UNIT_FOREVER_REL, /* timeout */
307                                     GNUNET_BLOCK_TYPE_TEST,   /* type */
308                                     &d_far->id.hashPubKey,   /*key to search */
309                                     4U,        /* replication level */
310                                     GNUNET_DHT_RO_RECORD_ROUTE |
311                                       GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
312                                     NULL,     /* xquery */
313                                     0,        /* xquery bits */
314                                     &dht_get_id_handler,
315                                     NULL);
316   GNUNET_SCHEDULER_cancel (disconnect_task);
317   disconnect_task = GNUNET_SCHEDULER_add_delayed(
318           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
319                                &disconnect_peers, NULL);
320 }
321
322
323 static void
324 put_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
325 {
326   struct GNUNET_TESTING_Daemon *d;
327   unsigned int i;
328
329   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: putting id's in DHT\n");
330   for (i = 0; i < num_peers;  i++)
331   {
332     d = GNUNET_TESTING_daemon_get (pg, i);
333     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334                 "test:    putting into DHT: %s\n",
335                 GNUNET_h2s_full (&d->id.hashPubKey));
336     GNUNET_DHT_put(hs[i],
337                    &d->id.hashPubKey,
338                    10U,
339                    GNUNET_DHT_RO_RECORD_ROUTE |
340                      GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
341                     GNUNET_BLOCK_TYPE_TEST,
342                    sizeof(struct GNUNET_PeerIdentity),
343                    (const char *) &d->id,
344                    GNUNET_TIME_UNIT_FOREVER_ABS,
345                    GNUNET_TIME_UNIT_FOREVER_REL,
346                    NULL,
347                    NULL);
348
349   }
350   put_task = GNUNET_SCHEDULER_add_delayed(
351                 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5),
352                                &put_id, NULL);
353 }
354
355
356 /**
357  * peergroup_ready: start test when all peers are connected
358  * @param cls closure
359  * @param emsg error message
360  */
361 static void
362 peergroup_ready (void *cls, const char *emsg)
363 {
364   struct GNUNET_TESTING_Daemon *d;
365   char *buf;
366   int buf_len;
367   unsigned int i;
368
369   if (emsg != NULL)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372                 "test: Peergroup callback called with error, aborting test!\n");
373     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374                 "test: Error from testing: `%s'\n", emsg);
375     ok++;
376     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
377     return;
378   }
379 #if VERBOSE
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381               "************************************************************\n");
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383               "test: Peer Group started successfully!\n");
384   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385               "test: Have %u connections\n",
386               total_connections);
387 #endif
388
389   if (data_file != NULL)
390   {
391     buf = NULL;
392     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
393     if (buf_len > 0)
394       GNUNET_DISK_file_write (data_file, buf, buf_len);
395     GNUNET_free (buf);
396   }
397   peers_running = GNUNET_TESTING_daemons_running (pg);
398   
399   GNUNET_assert (peers_running == num_peers);
400   hs = GNUNET_malloc (num_peers * sizeof(struct GNUNET_DHT_Handle *));
401   for (i = 0; i < num_peers;  i++)
402   {
403     d = GNUNET_TESTING_daemon_get (pg, i);
404     hs[i] = GNUNET_DHT_connect(d->cfg, 32);
405   }
406
407   put_task = GNUNET_SCHEDULER_add_now(&put_id, NULL);
408   test_task =
409       GNUNET_SCHEDULER_add_delayed(
410           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2),
411           &do_test, NULL);
412   disconnect_task =
413       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
414                                         GNUNET_TIME_UNIT_SECONDS,
415                                         30),
416                                     &disconnect_peers,
417                                     NULL);
418
419 }
420
421
422 /**
423  * Function that will be called whenever two daemons are connected by
424  * the testing library.
425  *
426  * @param cls closure
427  * @param first peer id for first daemon
428  * @param second peer id for the second daemon
429  * @param distance distance between the connected peers
430  * @param first_cfg config for the first daemon
431  * @param second_cfg config for the second daemon
432  * @param first_daemon handle for the first daemon
433  * @param second_daemon handle for the second daemon
434  * @param emsg error message (NULL on success)
435  */
436 static void
437 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
438             const struct GNUNET_PeerIdentity *second, uint32_t distance,
439             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
440             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
441             struct GNUNET_TESTING_Daemon *first_daemon,
442             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
443 {
444
445   if (emsg == NULL)
446   {
447     total_connections++;
448     GNUNET_PEER_intern(first);
449     GNUNET_PEER_intern(second);
450   }
451   else
452   {
453     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
454                 "test: Problem with new connection (%s)\n",
455                 emsg);
456   }
457
458 }
459
460
461 /**
462  * run: load configuration options and schedule test to run (start peergroup)
463  * @param cls closure
464  * @param args argv
465  * @param cfgfile configuration file name (can be NULL)
466  * @param cfg configuration handle
467  */
468 static void
469 run (void *cls, char *const *args, const char *cfgfile,
470      const struct GNUNET_CONFIGURATION_Handle *cfg)
471 {
472   char *temp_str;
473   struct GNUNET_TESTING_Host *hosts;
474   char *data_filename;
475
476   ok = 1;
477   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
478
479   GNUNET_log_setup ("test_dht_2dtorus",
480 #if VERBOSE
481                     "DEBUG",
482 #else
483                     "WARNING",
484 #endif
485                     NULL);
486
487 #if VERBOSE
488   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
489   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
490                                          "use_progressbars", "YES");
491 #endif
492
493   if (GNUNET_OK !=
494       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
495                                              "num_peers", &num_peers))
496   {
497     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
498                 "Option TESTING:NUM_PEERS is required!\n");
499     return;
500   }
501
502   if (GNUNET_OK !=
503       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
504                                              "topology_output_file",
505                                              &topology_file))
506   {
507     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
508                 "Option test_dht_2d:topology_output_file is required!\n");
509     return;
510   }
511
512   if (GNUNET_OK ==
513       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_dht_2dtorus",
514                                              "data_output_file",
515                                              &data_filename))
516   {
517     data_file =
518       GNUNET_DISK_file_open (data_filename,
519                              GNUNET_DISK_OPEN_READWRITE |
520                              GNUNET_DISK_OPEN_CREATE,
521                              GNUNET_DISK_PERM_USER_READ |
522                              GNUNET_DISK_PERM_USER_WRITE);
523     if (data_file == NULL)
524       {
525         GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
526                     data_filename);
527         GNUNET_free (data_filename);
528       }
529   }
530
531   if (GNUNET_YES ==
532       GNUNET_CONFIGURATION_get_value_string (cfg, "test_dht_2dtorus",
533                                              "output_file", &temp_str))
534   {
535     output_file =
536         GNUNET_DISK_file_open (temp_str,
537                                GNUNET_DISK_OPEN_READWRITE |
538                                GNUNET_DISK_OPEN_CREATE,
539                                GNUNET_DISK_PERM_USER_READ |
540                                GNUNET_DISK_PERM_USER_WRITE);
541     if (output_file == NULL)
542       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
543                   temp_str);
544   }
545   GNUNET_free_non_null (temp_str);
546
547   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
548
549   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
550                                        &connect_cb, &peergroup_ready, NULL,
551                                        hosts);
552   GNUNET_assert (pg != NULL);
553   shutdown_handle =
554       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
555                                     &shutdown_task, NULL);
556 }
557
558
559
560 /**
561  * test_dht_2d command line options
562  */
563 static struct GNUNET_GETOPT_CommandLineOption options[] = {
564   {'V', "verbose", NULL,
565    gettext_noop ("be verbose (print progress information)"),
566    0, &GNUNET_GETOPT_set_one, &verbose},
567   GNUNET_GETOPT_OPTION_END
568 };
569
570
571 /**
572  * Main: start test
573  */
574 int
575 main (int xargc, char *xargv[])
576 {
577   char *const argv[] = { "test-dht-2dtorus",
578     "-c",
579     "test_dht_2dtorus.conf",
580 #if VERBOSE
581     "-L", "DEBUG",
582 #endif
583     NULL
584   };
585
586   GNUNET_PROGRAM_run (sizeof(argv)/sizeof(char*) - 1, argv, "test_dht_2dtorus",
587                       gettext_noop ("Test dht in a small 2D torus."), options,
588                       &run, NULL);
589 #if REMOVE_DIR
590   GNUNET_DISK_directory_remove ("/tmp/test_dht_2dtorus");
591 #endif
592   if (0 != ok)
593   {
594     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
595   }
596   return ok;
597 }
598
599 /* end of test_dht_2dtorus.c */