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