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