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