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