better reporting
[oweals/gnunet.git] / src / dht / test_dht_multipeer.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_multipeer.c
22  * @brief testcase for testing DHT service with
23  *        multiple 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 /* Timeout for entire testcase */
34 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 30)
35
36 /* Timeout for waiting for replies to get requests */
37 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
38
39 /* Timeout for waiting for gets to complete */
40 #define GET_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
41
42 /* Timeout for waiting for puts to complete */
43 #define PUT_DELAY GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1)
44
45 /* If number of peers not in config file, use this number */
46 #define DEFAULT_NUM_PEERS 10
47
48 #define TEST_DATA_SIZE 8
49
50 #define MAX_OUTSTANDING_PUTS 10
51
52 #define MAX_OUTSTANDING_GETS 10
53
54 #define PATH_TRACKING GNUNET_YES
55
56 /* Structs */
57
58 struct TestPutContext
59 {
60   /**
61    * This is a linked list
62    */
63   struct TestPutContext *next;
64
65   /**
66    * Handle to the first peers DHT service (via the API)
67    */
68   struct GNUNET_DHT_Handle *dht_handle;
69
70   /**
71    *  Handle to the PUT peer daemon
72    */
73   struct GNUNET_TESTING_Daemon *daemon;
74
75   /**
76    *  Identifier for this PUT
77    */
78   uint32_t uid;
79
80   /**
81    * Task for disconnecting DHT handles
82    */
83   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
84 };
85
86 struct TestGetContext
87 {
88   /* This is a linked list */
89   struct TestGetContext *next;
90
91   /**
92    * Handle to the first peers DHT service (via the API)
93    */
94   struct GNUNET_DHT_Handle *dht_handle;
95
96   /**
97    * Handle for the DHT get request
98    */
99   struct GNUNET_DHT_GetHandle *get_handle;
100
101   /**
102    *  Handle to the GET peer daemon
103    */
104   struct GNUNET_TESTING_Daemon *daemon;
105
106   /**
107    *  Identifier for this GET
108    */
109   uint32_t uid;
110
111   /**
112    * Task for disconnecting DHT handles (and stopping GET)
113    */
114   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
115
116   /**
117    * Whether or not this request has been fulfilled already.
118    */
119   int succeeded;
120 };
121
122 /* Globals */
123
124 /**
125  * List of GETS to perform
126  */
127 struct TestGetContext *all_gets;
128
129 /**
130  * List of PUTS to perform
131  */
132 struct TestPutContext *all_puts;
133
134 /**
135  * Handle to the set of all peers run for this test.
136  */
137 static struct GNUNET_TESTING_PeerGroup *pg;
138
139 /**
140  * Total number of peers to run, set based on config file.
141  */
142 static unsigned long long num_peers;
143
144 /**
145  * How many puts do we currently have in flight?
146  */
147 static unsigned long long outstanding_puts;
148
149 /**
150  * How many puts are done?
151  */
152 static unsigned long long puts_completed;
153
154 /**
155  * How many puts do we currently have in flight?
156  */
157 static unsigned long long outstanding_gets;
158
159 /**
160  * How many gets are done?
161  */
162 static unsigned long long gets_completed;
163
164 /**
165  * How many gets failed?
166  */
167 static unsigned long long gets_failed;
168
169 /**
170  * Directory to remove on shutdown.
171  */
172 static char *test_directory;
173
174 /**
175  * Option to use when routing.
176  */
177 static enum GNUNET_DHT_RouteOption route_option;
178
179 /**
180  * Task handle to use to schedule test failure / success.
181  */
182 static GNUNET_SCHEDULER_TaskIdentifier die_task;
183
184 /* Global return value (0 for success, anything else for failure) */
185 static int ok;
186
187 /**
188  * Check whether peers successfully shut down.
189  */
190 static void
191 shutdown_callback (void *cls, const char *emsg)
192 {
193   if (emsg != NULL)
194   {
195     fprintf (stderr,
196              "Failed to shutdown testing topology: %s\n",
197              emsg);
198     if (ok == 0)
199       ok = 2;
200   }
201 }
202
203 /**
204  * Task to release DHT handles for PUT
205  */
206 static void
207 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   struct TestPutContext *test_put = cls;
210
211   test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
212   GNUNET_DHT_disconnect (test_put->dht_handle);
213   test_put->dht_handle = NULL;
214 }
215
216 /**
217  * Function scheduled to be run on the successful completion of this
218  * testcase.
219  */
220 static void
221 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 {
223   GNUNET_assert (pg != NULL);
224   struct TestPutContext *test_put = all_puts;
225   struct TestGetContext *test_get = all_gets;
226
227   while (test_put != NULL)
228   {
229     if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
230       GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
231     if (test_put->dht_handle != NULL)
232       GNUNET_DHT_disconnect (test_put->dht_handle);
233     test_put = test_put->next;
234   }
235
236   while (test_get != NULL)
237   {
238     if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
239       GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
240     if (test_get->get_handle != NULL)
241       GNUNET_DHT_get_stop (test_get->get_handle);
242     if (test_get->dht_handle != NULL)
243       GNUNET_DHT_disconnect (test_get->dht_handle);
244     test_get = test_get->next;
245   }
246
247   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
248   ok = 0;
249 }
250
251
252 /**
253  * Check if the get_handle is being used, if so stop the request.  Either
254  * way, schedule the end_badly_cont function which actually shuts down the
255  * test.
256  */
257 static void
258 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 {
260   const char *emsg = cls;
261
262   fprintf (stderr, 
263            "Failing test with error: `%s'!\n",
264            emsg);
265
266   struct TestPutContext *test_put = all_puts;
267   struct TestGetContext *test_get = all_gets;
268
269   while (test_put != NULL)
270   {
271     if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
272       GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
273     if (test_put->dht_handle != NULL)
274       GNUNET_DHT_disconnect (test_put->dht_handle);
275     test_put = test_put->next;
276   }
277
278   while (test_get != NULL)
279   {
280     if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
281       GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
282     if (test_get->get_handle != NULL)
283       GNUNET_DHT_get_stop (test_get->get_handle);
284     if (test_get->dht_handle != NULL)
285       GNUNET_DHT_disconnect (test_get->dht_handle);
286     test_get = test_get->next;
287   }
288
289   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
290   ok = 1;
291 }
292
293
294 /**
295  * Task to release get handle.
296  */
297 static void
298 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
299 {
300   struct TestGetContext *test_get = cls;
301   GNUNET_HashCode search_key;   /* Key stored under */
302   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
303
304   test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
305   memset (original_data, test_get->uid, sizeof (original_data));
306   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
307
308   if (test_get->succeeded != GNUNET_YES)
309   {
310     gets_failed++;
311     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312                 "Get from peer %s for key %s failed!\n",
313                 test_get->daemon->shortname, GNUNET_h2s (&search_key));
314   }
315   GNUNET_assert (test_get->get_handle != NULL);
316   GNUNET_DHT_get_stop (test_get->get_handle);
317   test_get->get_handle = NULL;
318
319   outstanding_gets--;           /* GET is really finished */
320   GNUNET_DHT_disconnect (test_get->dht_handle);
321   test_get->dht_handle = NULL;
322
323   fprintf (stderr,
324            "%llu gets succeeded, %llu gets failed!\n",
325            gets_completed, gets_failed);
326   if ((gets_failed > 0) && (outstanding_gets == 0))       /* Had some failures */
327   {
328       GNUNET_SCHEDULER_cancel (die_task);
329       die_task =
330         GNUNET_SCHEDULER_add_now (&end_badly, "not all gets succeeded");
331       return;
332   }
333
334   if ( (gets_completed == num_peers * num_peers) && 
335        (outstanding_gets == 0) )  /* All gets successful */
336   {
337     GNUNET_SCHEDULER_cancel (die_task);
338     //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &get_topology, NULL);
339     die_task = GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
340   }
341 }
342
343 /**
344  * Iterator called if the GET request initiated returns a response.
345  *
346  * @param cls closure
347  * @param exp when will this value expire
348  * @param key key of the result
349  * @param type type of the result
350  * @param size number of bytes in data
351  * @param data pointer to the result data
352  */
353 static void
354 get_result_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
355                      const GNUNET_HashCode * key,
356                      const struct GNUNET_PeerIdentity *get_path,
357                      unsigned int get_path_length,
358                      const struct GNUNET_PeerIdentity *put_path,
359                      unsigned int put_path_length,
360                      enum GNUNET_BLOCK_Type type, size_t size, const void *data)
361 {
362   struct TestGetContext *test_get = cls;
363   GNUNET_HashCode search_key;   /* Key stored under */
364   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
365   unsigned int i;
366
367   memset (original_data, test_get->uid, sizeof (original_data));
368   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
369
370   if (test_get->succeeded == GNUNET_YES)
371     return;                     /* Get has already been successful, probably ending now */
372
373 #if PATH_TRACKING
374   if (put_path != NULL)
375   {
376     fprintf (stderr, "PUT (%u) Path: ",
377              test_get->uid);
378     for (i = 0; i<put_path_length; i++)
379       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&put_path[i]));
380     fprintf (stderr, "\n");
381   }
382   if (get_path != NULL)
383   {
384     fprintf (stderr, "GET (%u) Path: ",
385              test_get->uid);
386     for (i = 0; i < get_path_length; i++)
387       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (&get_path[i]));
388     fprintf (stderr, "->%s\n",
389              GNUNET_i2s (&test_get->daemon->id));
390   }
391 #endif
392
393   if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
394       (0 != memcmp (original_data, data, sizeof (original_data))))
395   {
396     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397                 "Key or data is not the same as was inserted!\n");
398   }
399   else
400   {
401     gets_completed++;
402     test_get->succeeded = GNUNET_YES;
403   }
404
405   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
406   GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
407   test_get->disconnect_task = GNUNET_SCHEDULER_add_now (&get_stop_task, test_get);
408 }
409
410
411 /**
412  * Set up some data, and call API PUT function
413  */
414 static void
415 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
416 {
417   struct TestGetContext *test_get = cls;
418   GNUNET_HashCode key;          /* Made up key to store data under */
419   char data[TEST_DATA_SIZE];    /* Made up data to store */
420
421   if (test_get == NULL)
422     return;                     /* End of the list */
423   memset (data, test_get->uid, sizeof (data));
424   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
425
426   if (outstanding_gets > MAX_OUTSTANDING_GETS)
427   {
428     GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
429     return;
430   }
431
432   test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
433   /* Insert the data at the first peer */
434   GNUNET_assert (test_get->dht_handle != NULL);
435   outstanding_gets++;
436   test_get->get_handle =
437       GNUNET_DHT_get_start (test_get->dht_handle, GNUNET_TIME_UNIT_FOREVER_REL,
438                             GNUNET_BLOCK_TYPE_TEST, &key,
439                             1, route_option, NULL, 0,
440                             &get_result_iterator, test_get);
441 #if VERBOSE
442   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
443               test_get->uid, test_get->daemon->shortname);
444 #endif
445   test_get->disconnect_task =
446       GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
447   GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
448 }
449
450 /**
451  * Called when the PUT request has been transmitted to the DHT service.
452  * Schedule the GET request for some time in the future.
453  */
454 static void
455 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
456 {
457   struct TestPutContext *test_put = cls;
458
459   outstanding_puts--;
460   puts_completed++;
461
462   GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
463   test_put->disconnect_task =
464       GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
465   if (puts_completed == num_peers)
466   {
467     GNUNET_assert (outstanding_puts == 0);
468     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
469                                   (GNUNET_TIME_UNIT_SECONDS, 10), &do_get,
470                                   all_gets);
471     return;
472   }
473 }
474
475 /**
476  * Set up some data, and call API PUT function
477  */
478 static void
479 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
480 {
481   struct TestPutContext *test_put = cls;
482   GNUNET_HashCode key;          /* Made up key to store data under */
483   char data[TEST_DATA_SIZE];    /* Made up data to store */
484
485   if (test_put == NULL)
486     return;                     /* End of list */
487
488   memset (data, test_put->uid, sizeof (data));
489   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
490
491   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
492   {
493     GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
494     return;
495   }
496
497 #if VERBOSE
498   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
499               test_put->uid, test_put->daemon->shortname);
500 #endif
501   test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
502
503   GNUNET_assert (test_put->dht_handle != NULL);
504   outstanding_puts++;
505   fprintf (stderr, "PUT %u at `%s'\n",
506            test_put->uid,
507            GNUNET_i2s (&test_put->daemon->id));
508   GNUNET_DHT_put (test_put->dht_handle, &key, 1,
509                   route_option, GNUNET_BLOCK_TYPE_TEST, sizeof (data), data,
510                   GNUNET_TIME_UNIT_FOREVER_ABS, GNUNET_TIME_UNIT_FOREVER_REL,
511                   &put_finished, test_put);
512   test_put->disconnect_task =
513     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
514                                   &put_disconnect_task, test_put);
515   GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
516 }
517
518
519
520 /**
521  * This function is called once testing has finished setting up the topology.
522  *
523  * @param cls unused
524  * @param emsg variable is NULL on success (peers connected), and non-NULL on
525  * failure (peers failed to connect).
526  */
527 static void
528 run_dht_test (void *cls, const char *emsg)
529 {
530   unsigned long long i;
531   unsigned long long j;
532   struct TestPutContext *test_put;
533   struct TestGetContext *test_get;
534
535   if (emsg != NULL)
536   {
537     fprintf (stderr,
538              "Failed to setup topology: %s\n",
539              emsg);
540     die_task =
541       GNUNET_SCHEDULER_add_now (&end_badly,
542                                 "topology setup failed");
543     return;
544   }
545
546 #if PATH_TRACKING
547   route_option = GNUNET_DHT_RO_RECORD_ROUTE;
548 #else
549   route_option = GNUNET_DHT_RO_NONE;
550 #endif
551   die_task =
552     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly,
553                                   "from setup puts/gets");
554   fprintf (stderr, 
555            "Issuing %llu PUTs (one per peer)\n", 
556            num_peers);
557   for (i = 0; i < num_peers; i++)
558   {
559     test_put = GNUNET_malloc (sizeof (struct TestPutContext));
560     test_put->uid = i;
561     test_put->daemon = GNUNET_TESTING_daemon_get (pg, i);    
562     test_put->next = all_puts;
563     all_puts = test_put;
564   }
565   GNUNET_SCHEDULER_add_now (&do_put, all_puts);
566
567   fprintf (stderr, 
568            "Issuing %llu GETs\n",
569            num_peers * num_peers);
570   for (i = 0; i < num_peers; i++)
571     for (j = 0; j < num_peers; j++)
572       {
573         test_get = GNUNET_malloc (sizeof (struct TestGetContext));
574         test_get->uid = i;
575         test_get->daemon = GNUNET_TESTING_daemon_get (pg, j);
576         test_get->next = all_gets;
577         all_gets = test_get;
578       }
579 }
580
581
582 static void
583 run (void *cls, char *const *args, const char *cfgfile,
584      const struct GNUNET_CONFIGURATION_Handle *cfg)
585 {
586   /* Get path from configuration file */
587   if (GNUNET_YES !=
588       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
589                                              &test_directory))
590   {
591     GNUNET_break (0);
592     ok = 404;
593     return;
594   }
595   if (GNUNET_SYSERR ==
596       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
597                                              &num_peers))
598     num_peers = DEFAULT_NUM_PEERS;
599   pg = GNUNET_TESTING_peergroup_start (cfg,
600                                        num_peers,
601                                        TIMEOUT,
602                                        NULL,
603                                        &run_dht_test,
604                                        NULL,
605                                        NULL);
606   if (NULL == pg)
607     {
608       GNUNET_break (0);
609       return;
610     }
611 }
612
613
614 static int
615 check ()
616 {
617   int ret;
618
619   /* Arguments for GNUNET_PROGRAM_run */
620   char *const argv[] = { "test-dht-multipeer",  /* Name to give running binary */
621     "-c",
622     "test_dht_multipeer_data.conf",     /* Config file to use */
623 #if VERBOSE
624     "-L", "DEBUG",
625 #endif
626     NULL
627   };
628   struct GNUNET_GETOPT_CommandLineOption options[] = {
629     GNUNET_GETOPT_OPTION_END
630   };
631   /* Run the run function as a new program */
632   ret =
633       GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
634                           "test-dht-multipeer", "nohelp", options, &run, &ok);
635   if (ret != GNUNET_OK)
636   {
637     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
638                 "`test-dht-multipeer': Failed with error code %d\n", ret);
639   }
640   return ok;
641 }
642
643
644 int
645 main (int argc, char *argv[])
646 {
647   int ret;
648
649
650   GNUNET_log_setup ("test-dht-multipeer",
651 #if VERBOSE
652                     "DEBUG",
653 #else
654                     "WARNING",
655 #endif
656                     NULL);
657   ret = check ();
658   /**
659    * Need to remove base directory, subdirectories taken care
660    * of by the testing framework.
661    */
662   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
663   {
664     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
665                 "Failed to remove testing directory %s\n", test_directory);
666   }
667   return ret;
668 }
669
670 /* end of test_dht_multipeer.c */