indentation
[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, 5)
35
36 /* Timeout for waiting for replies to get requests */
37 #define GET_TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 90)
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 #define SECONDS_PER_PEER_START 45
46
47 /* If number of peers not in config file, use this number */
48 #define DEFAULT_NUM_PEERS 5
49
50 #define TEST_DATA_SIZE 8
51
52 #define MAX_OUTSTANDING_PUTS 10
53
54 #define MAX_OUTSTANDING_GETS 10
55
56 #define PATH_TRACKING GNUNET_YES
57
58 /* Structs */
59
60 struct TestPutContext
61 {
62   /**
63    * This is a linked list 
64    */
65   struct TestPutContext *next;
66
67   /**
68    * Handle to the first peers DHT service (via the API)
69    */
70   struct GNUNET_DHT_Handle *dht_handle;
71
72   /**
73    *  Handle to the PUT peer daemon
74    */
75   struct GNUNET_TESTING_Daemon *daemon;
76
77   /**
78    *  Identifier for this PUT
79    */
80   uint32_t uid;
81
82   /**
83    * Task for disconnecting DHT handles
84    */
85   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
86 };
87
88 struct TestGetContext
89 {
90   /* This is a linked list */
91   struct TestGetContext *next;
92
93   /**
94    * Handle to the first peers DHT service (via the API)
95    */
96   struct GNUNET_DHT_Handle *dht_handle;
97
98   /**
99    * Handle for the DHT get request
100    */
101   struct GNUNET_DHT_GetHandle *get_handle;
102
103   /**
104    *  Handle to the GET peer daemon
105    */
106   struct GNUNET_TESTING_Daemon *daemon;
107
108   /**
109    *  Identifier for this GET
110    */
111   uint32_t uid;
112
113   /**
114    * Task for disconnecting DHT handles (and stopping GET)
115    */
116   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
117
118   /**
119    * Whether or not this request has been fulfilled already.
120    */
121   int succeeded;
122 };
123
124 /* Globals */
125
126 /**
127  * List of GETS to perform
128  */
129 struct TestGetContext *all_gets;
130
131 /**
132  * List of PUTS to perform
133  */
134 struct TestPutContext *all_puts;
135
136 /**
137  * Directory to store temp data in, defined in config file
138  */
139 static char *test_directory;
140
141 /**
142  * Variable used to store the number of connections we should wait for.
143  */
144 static unsigned int expected_connections;
145
146 /**
147  * Variable used to keep track of how many peers aren't yet started.
148  */
149 static unsigned long long peers_left;
150
151 /**
152  * Handle to the set of all peers run for this test.
153  */
154 static struct GNUNET_TESTING_PeerGroup *pg;
155
156
157 /**
158  * Total number of peers to run, set based on config file.
159  */
160 static unsigned long long num_peers;
161
162 /**
163  * Total number of items to insert.
164  */
165 static unsigned long long num_puts;
166
167 /**
168  * Total number of items to attempt to get.
169  */
170 static unsigned long long num_gets;
171
172 /**
173  * How many puts do we currently have in flight?
174  */
175 static unsigned long long outstanding_puts;
176
177 /**
178  * How many puts are done?
179  */
180 static unsigned long long puts_completed;
181
182 /**
183  * How many puts do we currently have in flight?
184  */
185 static unsigned long long outstanding_gets;
186
187 /**
188  * How many gets are done?
189  */
190 static unsigned long long gets_completed;
191
192 /**
193  * How many gets failed?
194  */
195 static unsigned long long gets_failed;
196
197 /**
198  * Global used to count how many connections we have currently
199  * been notified about (how many times has topology_callback been called
200  * with success?)
201  */
202 static unsigned int total_connections;
203
204 /**
205  * Global used to count how many failed connections we have
206  * been notified about (how many times has topology_callback
207  * been called with failure?)
208  */
209 static unsigned int failed_connections;
210
211 enum GNUNET_DHT_RouteOption route_option;
212
213 /* Task handle to use to schedule test failure */
214 GNUNET_SCHEDULER_TaskIdentifier die_task;
215
216 static char *blacklist_transports;
217
218 static enum GNUNET_TESTING_Topology topology;
219
220 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE;  /* Don't do any blacklisting */
221
222 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
223
224 static enum GNUNET_TESTING_TopologyOption connect_topology_option =
225     GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
226
227 static double connect_topology_option_modifier = 0.0;
228
229 /* Global return value (0 for success, anything else for failure) */
230 static int ok;
231
232 /**
233  * Check whether peers successfully shut down.
234  */
235 void
236 shutdown_callback (void *cls, const char *emsg)
237 {
238   if (emsg != NULL)
239   {
240     if (ok == 0)
241       ok = 2;
242   }
243 }
244
245 /**
246  * Task to release DHT handles for PUT
247  */
248 static void
249 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
250 {
251   struct TestPutContext *test_put = cls;
252
253   test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
254   GNUNET_DHT_disconnect (test_put->dht_handle);
255   test_put->dht_handle = NULL;
256 }
257
258 /**
259  * Function scheduled to be run on the successful completion of this
260  * testcase.
261  */
262 static void
263 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
264 {
265   GNUNET_assert (pg != NULL);
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 = 0;
291 }
292
293
294 /**
295  * Check if the get_handle is being used, if so stop the request.  Either
296  * way, schedule the end_badly_cont function which actually shuts down the
297  * test.
298  */
299 static void
300 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
301 {
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n",
303               (char *) cls);
304
305   struct TestPutContext *test_put = all_puts;
306   struct TestGetContext *test_get = all_gets;
307
308   while (test_put != NULL)
309   {
310     if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
311       GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
312     if (test_put->dht_handle != NULL)
313       GNUNET_DHT_disconnect (test_put->dht_handle);
314     test_put = test_put->next;
315   }
316
317   while (test_get != NULL)
318   {
319     if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
320       GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
321     if (test_get->get_handle != NULL)
322       GNUNET_DHT_get_stop (test_get->get_handle);
323     if (test_get->dht_handle != NULL)
324       GNUNET_DHT_disconnect (test_get->dht_handle);
325     test_get = test_get->next;
326   }
327
328   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
329   ok = 1;
330 }
331
332 /**
333  * Task to release DHT handle associated with GET request.
334  */
335 static void
336 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 {
338   struct TestGetContext *test_get = cls;
339
340   outstanding_gets--;           /* GET is really finished */
341   GNUNET_DHT_disconnect (test_get->dht_handle);
342   test_get->dht_handle = NULL;
343
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n",
345               gets_completed, gets_failed);
346   if ((gets_completed == num_gets) && (outstanding_gets == 0))  /* All gets successful */
347   {
348     GNUNET_SCHEDULER_cancel (die_task);
349     //GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5), &get_topology, NULL);
350     GNUNET_SCHEDULER_add_now (&finish_testing, NULL);
351   }
352   else if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0))       /* Had some failures */
353   {
354     GNUNET_SCHEDULER_cancel (die_task);
355     GNUNET_SCHEDULER_add_now (&end_badly, "not all gets succeeded!\n");
356   }
357 }
358
359 /**
360  * Task to release get handle.
361  */
362 static void
363 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
364 {
365   struct TestGetContext *test_get = cls;
366   GNUNET_HashCode search_key;   /* Key stored under */
367   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
368
369   memset (original_data, test_get->uid, sizeof (original_data));
370   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
371
372   if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
373   {
374     gets_failed++;
375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376                 "Get from peer %s for key %s failed!\n",
377                 test_get->daemon->shortname, GNUNET_h2s (&search_key));
378   }
379   GNUNET_assert (test_get->get_handle != NULL);
380   GNUNET_DHT_get_stop (test_get->get_handle);
381   GNUNET_SCHEDULER_add_now (&get_stop_finished, test_get);
382   test_get->get_handle = NULL;
383   test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
384 }
385
386 /**
387  * Iterator called if the GET request initiated returns a response.
388  *
389  * @param cls closure
390  * @param exp when will this value expire
391  * @param key key of the result
392  * @param type type of the result
393  * @param size number of bytes in data
394  * @param data pointer to the result data
395  */
396 void
397 get_result_iterator (void *cls,
398                      struct GNUNET_TIME_Absolute exp,
399                      const GNUNET_HashCode * key,
400                      const struct GNUNET_PeerIdentity *const *get_path,
401                      const struct GNUNET_PeerIdentity *const *put_path,
402                      enum GNUNET_BLOCK_Type type, size_t size, const void *data)
403 {
404   struct TestGetContext *test_get = cls;
405   GNUNET_HashCode search_key;   /* Key stored under */
406   char original_data[TEST_DATA_SIZE];   /* Made up data to store */
407   unsigned int i;
408
409   memset (original_data, test_get->uid, sizeof (original_data));
410   GNUNET_CRYPTO_hash (original_data, TEST_DATA_SIZE, &search_key);
411
412   if (test_get->succeeded == GNUNET_YES)
413     return;                     /* Get has already been successful, probably ending now */
414
415 #if PATH_TRACKING
416   if (put_path != NULL)
417   {
418     fprintf (stderr, "PUT Path: ");
419     for (i = 0; put_path[i] != NULL; i++)
420       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (put_path[i]));
421     fprintf (stderr, "\n");
422   }
423   if (get_path != NULL)
424   {
425     fprintf (stderr, "GET Path: ");
426     for (i = 0; get_path[i] != NULL; i++)
427       fprintf (stderr, "%s%s", i == 0 ? "" : "->", GNUNET_i2s (get_path[i]));
428     fprintf (stderr, "\n");
429   }
430 #endif
431
432   if ((0 != memcmp (&search_key, key, sizeof (GNUNET_HashCode))) ||
433       (0 != memcmp (original_data, data, sizeof (original_data))))
434   {
435     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
436                 "Key or data is not the same as was inserted!\n");
437   }
438   else
439   {
440     gets_completed++;
441     test_get->succeeded = GNUNET_YES;
442   }
443
444   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
445   GNUNET_SCHEDULER_cancel (test_get->disconnect_task);
446   GNUNET_SCHEDULER_add_continuation (&get_stop_task, test_get,
447                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
448 }
449
450
451 /**
452  * Set up some data, and call API PUT function
453  */
454 static void
455 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
456 {
457   struct TestGetContext *test_get = cls;
458   GNUNET_HashCode key;          /* Made up key to store data under */
459   char data[TEST_DATA_SIZE];    /* Made up data to store */
460
461   if (test_get == NULL)
462     return;                     /* End of the list */
463   memset (data, test_get->uid, sizeof (data));
464   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
465
466   if (outstanding_gets > MAX_OUTSTANDING_GETS)
467   {
468     GNUNET_SCHEDULER_add_delayed (GET_DELAY, &do_get, test_get);
469     return;
470   }
471
472   test_get->dht_handle = GNUNET_DHT_connect (test_get->daemon->cfg, 10);
473   /* Insert the data at the first peer */
474   GNUNET_assert (test_get->dht_handle != NULL);
475   outstanding_gets++;
476   test_get->get_handle = GNUNET_DHT_get_start (test_get->dht_handle,
477                                                GNUNET_TIME_UNIT_FOREVER_REL,
478                                                GNUNET_BLOCK_TYPE_TEST,
479                                                &key,
480                                                DEFAULT_GET_REPLICATION,
481                                                route_option,
482                                                NULL, 0,
483                                                NULL, 0,
484                                                &get_result_iterator, test_get);
485 #if VERBOSE
486   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
487               test_get->uid, test_get->daemon->shortname);
488 #endif
489   test_get->disconnect_task =
490       GNUNET_SCHEDULER_add_delayed (GET_TIMEOUT, &get_stop_task, test_get);
491   GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
492 }
493
494 /**
495  * Called when the PUT request has been transmitted to the DHT service.
496  * Schedule the GET request for some time in the future.
497  */
498 static void
499 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
500 {
501   struct TestPutContext *test_put = cls;
502
503   outstanding_puts--;
504   puts_completed++;
505
506   GNUNET_SCHEDULER_cancel (test_put->disconnect_task);
507   test_put->disconnect_task =
508       GNUNET_SCHEDULER_add_now (&put_disconnect_task, test_put);
509   if (puts_completed == num_puts)
510   {
511     GNUNET_assert (outstanding_puts == 0);
512     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
513                                   (GNUNET_TIME_UNIT_SECONDS, 10), &do_get,
514                                   all_gets);
515     return;
516   }
517 }
518
519 /**
520  * Set up some data, and call API PUT function
521  */
522 static void
523 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
524 {
525   struct TestPutContext *test_put = cls;
526   GNUNET_HashCode key;          /* Made up key to store data under */
527   char data[TEST_DATA_SIZE];    /* Made up data to store */
528
529   if (test_put == NULL)
530     return;                     /* End of list */
531
532   memset (data, test_put->uid, sizeof (data));
533   GNUNET_CRYPTO_hash (data, TEST_DATA_SIZE, &key);
534
535   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
536   {
537     GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
538     return;
539   }
540
541 #if VERBOSE
542   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
543               test_put->uid, test_put->daemon->shortname);
544 #endif
545   test_put->dht_handle = GNUNET_DHT_connect (test_put->daemon->cfg, 10);
546
547   GNUNET_assert (test_put->dht_handle != NULL);
548   outstanding_puts++;
549   GNUNET_DHT_put (test_put->dht_handle,
550                   &key,
551                   DEFAULT_PUT_REPLICATION,
552                   route_option,
553                   GNUNET_BLOCK_TYPE_TEST,
554                   sizeof (data), data,
555                   GNUNET_TIME_UNIT_FOREVER_ABS,
556                   GNUNET_TIME_UNIT_FOREVER_REL, &put_finished, test_put);
557   test_put->disconnect_task =
558       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
559                                     &put_disconnect_task, test_put);
560   GNUNET_SCHEDULER_add_now (&do_put, test_put->next);
561 }
562
563
564 /**
565  * Set up some all of the put and get operations we want
566  * to do.  Allocate data structure for each, add to list,
567  * then call actual insert functions.
568  */
569 static void
570 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 {
572   int i;
573   uint32_t temp_daemon;
574   struct TestPutContext *test_put;
575   struct TestGetContext *test_get;
576   int remember[num_puts][num_peers];
577
578   for (i = 0; i < num_puts; i++)
579   {
580     test_put = GNUNET_malloc (sizeof (struct TestPutContext));
581     test_put->uid = i;
582     temp_daemon =
583         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
584     test_put->daemon = GNUNET_TESTING_daemon_get (pg, temp_daemon);
585     test_put->next = all_puts;
586     all_puts = test_put;
587   }
588
589   for (i = 0; i < num_gets; i++)
590   {
591     test_get = GNUNET_malloc (sizeof (struct TestGetContext));
592     test_get->uid =
593         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
594     temp_daemon =
595         GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
596     while (remember[test_get->uid][temp_daemon] == 1)
597       temp_daemon =
598           GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
599     test_get->daemon = GNUNET_TESTING_daemon_get (pg, temp_daemon);
600     remember[test_get->uid][temp_daemon] = 1;
601     test_get->next = all_gets;
602     all_gets = test_get;
603   }
604
605   GNUNET_SCHEDULER_add_now (&do_put, all_puts);
606 }
607
608
609 /**
610  * This function is called whenever a connection attempt is finished between two of
611  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
612  * number of times this function is called should equal the number returned
613  * from the GNUNET_TESTING_connect_topology call.
614  *
615  * The emsg variable is NULL on success (peers connected), and non-NULL on
616  * failure (peers failed to connect).
617  */
618 void
619 topology_callback (void *cls,
620                    const struct GNUNET_PeerIdentity *first,
621                    const struct GNUNET_PeerIdentity *second,
622                    uint32_t distance,
623                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
624                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
625                    struct GNUNET_TESTING_Daemon *first_daemon,
626                    struct GNUNET_TESTING_Daemon *second_daemon,
627                    const char *emsg)
628 {
629   if (emsg == NULL)
630   {
631     total_connections++;
632 #if VERBOSE > 1
633     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634                 "connected peer %s to peer %s, distance %u\n",
635                 first_daemon->shortname, second_daemon->shortname, distance);
636 #endif
637   }
638 #if VERBOSE
639   else
640   {
641     failed_connections++;
642     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
643                 "Failed to connect peer %s to peer %s with error :\n%s\n",
644                 first_daemon->shortname, second_daemon->shortname, emsg);
645   }
646 #endif
647
648   if (total_connections == expected_connections)
649   {
650 #if VERBOSE
651     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
652                 "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
653                 total_connections);
654 #endif
655     GNUNET_SCHEDULER_cancel (die_task);
656     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
657                                              &end_badly,
658                                              "from setup puts/gets");
659
660     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
661                                   (GNUNET_TIME_UNIT_SECONDS, 2),
662                                   &setup_puts_and_gets, NULL);
663   }
664   else if (total_connections + failed_connections == expected_connections)
665   {
666     GNUNET_SCHEDULER_cancel (die_task);
667     die_task =
668         GNUNET_SCHEDULER_add_now (&end_badly,
669                                   "from topology_callback (too many failed connections)");
670   }
671 }
672
673 static void
674 peers_started_callback (void *cls,
675                         const struct GNUNET_PeerIdentity *id,
676                         const struct GNUNET_CONFIGURATION_Handle *cfg,
677                         struct GNUNET_TESTING_Daemon *d, const char *emsg)
678 {
679   if (emsg != NULL)
680   {
681     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682                 "Failed to start daemon with error: `%s'\n", emsg);
683     return;
684   }
685   GNUNET_assert (id != NULL);
686
687 #if VERBOSE
688   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
689               (num_peers - peers_left) + 1, num_peers);
690 #endif
691
692   peers_left--;
693   if (peers_left == 0)
694   {
695
696 #if VERBOSE
697     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698                 "All %d daemons started, now connecting peers!\n", num_peers);
699 #endif
700     GNUNET_SCHEDULER_cancel (die_task);
701
702     expected_connections = -1;
703     if ((pg != NULL) && (peers_left == 0))
704     {
705       expected_connections = GNUNET_TESTING_connect_topology (pg,
706                                                               connection_topology,
707                                                               connect_topology_option,
708                                                               connect_topology_option_modifier,
709                                                               TIMEOUT,
710                                                               num_peers,
711                                                               NULL, NULL);
712 #if VERBOSE
713       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
714                   "Have %d expected connections\n", expected_connections);
715 #endif
716     }
717
718     if (expected_connections == GNUNET_SYSERR)
719     {
720       die_task =
721           GNUNET_SCHEDULER_add_now (&end_badly,
722                                     "from connect topology (bad return)");
723     }
724
725     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
726                                              &end_badly,
727                                              "from connect topology (timeout)");
728
729     ok = 0;
730   }
731 }
732
733 static void
734 create_topology ()
735 {
736   peers_left = num_peers;       /* Reset counter */
737   if (GNUNET_TESTING_create_topology
738       (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
739   {
740 #if VERBOSE
741     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
742                 "Topology set up, now starting peers!\n");
743 #endif
744     GNUNET_TESTING_daemons_continue_startup (pg);
745   }
746   else
747   {
748     GNUNET_SCHEDULER_cancel (die_task);
749     die_task =
750         GNUNET_SCHEDULER_add_now (&end_badly,
751                                   "from create topology (bad return)");
752   }
753   GNUNET_SCHEDULER_cancel (die_task);
754   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
755                                            &end_badly,
756                                            "from continue startup (timeout)");
757 }
758
759 /**
760  * Callback indicating that the hostkey was created for a peer.
761  *
762  * @param cls NULL
763  * @param id the peer identity
764  * @param d the daemon handle (pretty useless at this point, remove?)
765  * @param emsg non-null on failure
766  */
767 void
768 hostkey_callback (void *cls,
769                   const struct GNUNET_PeerIdentity *id,
770                   struct GNUNET_TESTING_Daemon *d, const char *emsg)
771 {
772   if (emsg != NULL)
773   {
774     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
775                 "Hostkey callback received error: %s\n", emsg);
776   }
777
778 #if VERBOSE > 1
779   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780               "Hostkey (%d/%d) created for peer `%s'\n",
781               num_peers - peers_left, num_peers, GNUNET_i2s (id));
782 #endif
783
784
785   peers_left--;
786   if (peers_left == 0)
787   {
788 #if VERBOSE
789     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
790                 "All %d hostkeys created, now creating topology!\n", num_peers);
791 #endif
792     GNUNET_SCHEDULER_cancel (die_task);
793     /* Set up task in case topology creation doesn't finish
794      * within a reasonable amount of time */
795     die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
796                                              &end_badly,
797                                              "from create_topology");
798     GNUNET_SCHEDULER_add_now (&create_topology, NULL);
799     ok = 0;
800   }
801 }
802
803
804 static void
805 run (void *cls,
806      char *const *args,
807      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
808 {
809   char *topology_str;
810   char *connect_topology_str;
811   char *blacklist_topology_str;
812   char *connect_topology_option_str;
813   char *connect_topology_option_modifier_string;
814
815 #if PATH_TRACKING
816   route_option = GNUNET_DHT_RO_RECORD_ROUTE;
817 #else
818   route_option = GNUNET_DHT_RO_NONE;
819 #endif
820
821   /* Get path from configuration file */
822   if (GNUNET_YES !=
823       GNUNET_CONFIGURATION_get_value_string (cfg, "paths", "servicehome",
824                                              &test_directory))
825   {
826     ok = 404;
827     return;
828   }
829
830   if ((GNUNET_YES ==
831        GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "topology",
832                                               &topology_str)) &&
833       (GNUNET_NO == GNUNET_TESTING_topology_get (&topology, topology_str)))
834   {
835     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
836                 "Invalid topology `%s' given for section %s option %s\n",
837                 topology_str, "TESTING", "TOPOLOGY");
838     topology = GNUNET_TESTING_TOPOLOGY_CLIQUE;  /* Defaults to NONE, so set better default here */
839   }
840
841   if ((GNUNET_YES ==
842        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
843                                               "connect_topology",
844                                               &connect_topology_str)) &&
845       (GNUNET_NO ==
846        GNUNET_TESTING_topology_get (&connection_topology,
847                                     connect_topology_str)))
848   {
849     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
850                 "Invalid connect topology `%s' given for section %s option %s\n",
851                 connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
852   }
853   GNUNET_free_non_null (connect_topology_str);
854   if ((GNUNET_YES ==
855        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
856                                               "connect_topology_option",
857                                               &connect_topology_option_str)) &&
858       (GNUNET_NO ==
859        GNUNET_TESTING_topology_option_get (&connect_topology_option,
860                                            connect_topology_option_str)))
861   {
862     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
863                 "Invalid connect topology option `%s' given for section %s option %s\n",
864                 connect_topology_option_str, "TESTING",
865                 "CONNECT_TOPOLOGY_OPTION");
866     connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;       /* Defaults to NONE, set to ALL */
867   }
868   GNUNET_free_non_null (connect_topology_option_str);
869   if (GNUNET_YES ==
870       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
871                                              "connect_topology_option_modifier",
872                                              &connect_topology_option_modifier_string))
873   {
874     if (sscanf
875         (connect_topology_option_modifier_string, "%lf",
876          &connect_topology_option_modifier) != 1)
877     {
878       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
879                   _
880                   ("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
881                   connect_topology_option_modifier_string,
882                   "connect_topology_option_modifier", "TESTING");
883     }
884     GNUNET_free (connect_topology_option_modifier_string);
885   }
886
887   if (GNUNET_YES !=
888       GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
889                                              "blacklist_transports",
890                                              &blacklist_transports))
891     blacklist_transports = NULL;
892
893   if ((GNUNET_YES ==
894        GNUNET_CONFIGURATION_get_value_string (cfg, "testing",
895                                               "blacklist_topology",
896                                               &blacklist_topology_str)) &&
897       (GNUNET_NO ==
898        GNUNET_TESTING_topology_get (&blacklist_topology,
899                                     blacklist_topology_str)))
900   {
901     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
902                 "Invalid topology `%s' given for section %s option %s\n",
903                 topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
904   }
905   GNUNET_free_non_null (topology_str);
906   GNUNET_free_non_null (blacklist_topology_str);
907
908   /* Get number of peers to start from configuration */
909   if (GNUNET_SYSERR ==
910       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
911                                              &num_peers))
912     num_peers = DEFAULT_NUM_PEERS;
913
914   if (GNUNET_SYSERR ==
915       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_puts",
916                                              &num_puts))
917     num_puts = DEFAULT_NUM_PEERS;
918
919   if (GNUNET_SYSERR ==
920       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_gets",
921                                              &num_gets))
922     num_gets = DEFAULT_NUM_PEERS;
923
924   /* Set peers_left so we know when all peers started */
925   peers_left = num_peers;
926
927   /* Set up a task to end testing if peer start fails */
928   die_task =
929       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
930                                     (GNUNET_TIME_UNIT_SECONDS,
931                                      SECONDS_PER_PEER_START * num_peers),
932                                     &end_badly,
933                                     "didn't generate all hostkeys within a reasonable amount of time!!!");
934
935   pg = GNUNET_TESTING_daemons_start (cfg, peers_left,   /* Total number of peers */
936                                      peers_left,        /* Number of outstanding connections */
937                                      peers_left,        /* Number of parallel ssh connections, or peers being started at once */
938                                      GNUNET_TIME_relative_multiply
939                                      (GNUNET_TIME_UNIT_SECONDS,
940                                       SECONDS_PER_PEER_START * num_peers),
941                                      &hostkey_callback, NULL,
942                                      &peers_started_callback, NULL,
943                                      &topology_callback, NULL, NULL);
944
945 }
946
947 static int
948 check ()
949 {
950   int ret;
951
952   /* Arguments for GNUNET_PROGRAM_run */
953   char *const argv[] = { "test-dht-multipeer",  /* Name to give running binary */
954     "-c",
955     "test_dht_multipeer_data.conf",     /* Config file to use */
956 #if VERBOSE
957     "-L", "DEBUG",
958 #endif
959     NULL
960   };
961   struct GNUNET_GETOPT_CommandLineOption options[] = {
962     GNUNET_GETOPT_OPTION_END
963   };
964   /* Run the run function as a new program */
965   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
966                             argv, "test-dht-multipeer", "nohelp",
967                             options, &run, &ok);
968   if (ret != GNUNET_OK)
969   {
970     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
971                 "`test-dht-multipeer': Failed with error code %d\n", ret);
972   }
973   return ok;
974 }
975
976 int
977 main (int argc, char *argv[])
978 {
979   int ret;
980
981   GNUNET_log_setup ("test-dht-multipeer",
982 #if VERBOSE
983                     "DEBUG",
984 #else
985                     "WARNING",
986 #endif
987                     NULL);
988   ret = check ();
989   /**
990    * Need to remove base directory, subdirectories taken care
991    * of by the testing framework.
992    */
993   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
994   {
995     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
996                 "Failed to remove testing directory %s\n", test_directory);
997   }
998   return ret;
999 }
1000
1001 /* end of test_dht_twopeer_put_get.c */