24618230a8b42260b3472442857dff4f9442d808
[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                                               DEFAULT_GET_REPLICATION,
451                                               GNUNET_DHT_RO_NONE,
452                                               NULL, 0,
453                                               NULL, 0,
454                                               &get_result_iterator,
455                                               test_get);
456 #if VERBOSE
457   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
458              test_get->uid,
459              test_get->daemon->shortname);
460 #endif
461   test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed(GET_TIMEOUT, &get_stop_task, test_get);
462   GNUNET_SCHEDULER_add_now (&do_get, test_get->next);
463 }
464
465 /**
466  * Called when the PUT request has been transmitted to the DHT service.
467  * Schedule the GET request for some time in the future.
468  */
469 static void
470 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
471 {
472   struct TestPutContext *test_put = cls;
473   outstanding_puts--;
474   puts_completed++;
475
476   GNUNET_SCHEDULER_cancel(test_put->disconnect_task);
477   test_put->disconnect_task = GNUNET_SCHEDULER_add_now(&put_disconnect_task, test_put);
478   if (puts_completed == num_puts)
479     {
480       GNUNET_assert(outstanding_puts == 0);
481       GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10), &do_get, all_gets);
482       return;
483     }
484 }
485
486 /**
487  * Set up some data, and call API PUT function
488  */
489 static void
490 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
491 {
492   struct TestPutContext *test_put = cls;
493   GNUNET_HashCode key; /* Made up key to store data under */
494   char data[TEST_DATA_SIZE]; /* Made up data to store */
495
496   if (test_put == NULL)
497     return; /* End of list */
498
499   memset(data, test_put->uid, sizeof(data));
500   GNUNET_CRYPTO_hash(data, TEST_DATA_SIZE, &key);
501
502   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
503     {
504       GNUNET_SCHEDULER_add_delayed (PUT_DELAY, &do_put, test_put);
505       return;
506     }
507
508 #if VERBOSE
509     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
510                test_put->uid,
511                test_put->daemon->shortname);
512 #endif
513   test_put->dht_handle = GNUNET_DHT_connect(test_put->daemon->cfg, 10);
514
515   GNUNET_assert(test_put->dht_handle != NULL);
516   outstanding_puts++;
517   GNUNET_DHT_put(test_put->dht_handle,
518                  &key,
519                  DEFAULT_PUT_REPLICATION,
520                  GNUNET_DHT_RO_NONE,
521                  GNUNET_BLOCK_TYPE_TEST,
522                  sizeof(data), data,
523                  GNUNET_TIME_UNIT_FOREVER_ABS,
524                  GNUNET_TIME_UNIT_FOREVER_REL,
525                  &put_finished, test_put);
526   test_put->disconnect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_relative_get_forever(), &put_disconnect_task, test_put);
527   GNUNET_SCHEDULER_add_now(&do_put, test_put->next);
528 }
529
530
531 /**
532  * Set up some all of the put and get operations we want
533  * to do.  Allocate data structure for each, add to list,
534  * then call actual insert functions.
535  */
536 static void
537 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
538 {
539   int i;
540   uint32_t temp_daemon;
541   struct TestPutContext *test_put;
542   struct TestGetContext *test_get;
543   int remember[num_puts][num_peers];
544
545   for (i = 0; i < num_puts; i++)
546     {
547       test_put = GNUNET_malloc(sizeof(struct TestPutContext));
548       test_put->uid = i;
549       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
550       test_put->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
551       test_put->next = all_puts;
552       all_puts = test_put;
553     }
554
555   for (i = 0; i < num_gets; i++)
556     {
557       test_get = GNUNET_malloc(sizeof(struct TestGetContext));
558       test_get->uid = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
559       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
560       while (remember[test_get->uid][temp_daemon] == 1)
561         temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
562       test_get->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
563       remember[test_get->uid][temp_daemon] = 1;
564       test_get->next = all_gets;
565       all_gets = test_get;
566     }
567
568   GNUNET_SCHEDULER_add_now (&do_put, all_puts);
569 }
570
571
572 /**
573  * This function is called whenever a connection attempt is finished between two of
574  * the started peers (started with GNUNET_TESTING_daemons_start).  The total
575  * number of times this function is called should equal the number returned
576  * from the GNUNET_TESTING_connect_topology call.
577  *
578  * The emsg variable is NULL on success (peers connected), and non-NULL on
579  * failure (peers failed to connect).
580  */
581 void
582 topology_callback (void *cls,
583                    const struct GNUNET_PeerIdentity *first,
584                    const struct GNUNET_PeerIdentity *second,
585                    uint32_t distance,
586                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
587                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
588                    struct GNUNET_TESTING_Daemon *first_daemon,
589                    struct GNUNET_TESTING_Daemon *second_daemon,
590                    const char *emsg)
591 {
592   if (emsg == NULL)
593     {
594       total_connections++;
595 #if VERBOSE > 1
596       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s, distance %u\n",
597                  first_daemon->shortname,
598                  second_daemon->shortname,
599                  distance);
600 #endif
601     }
602 #if VERBOSE
603   else
604     {
605       failed_connections++;
606       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
607                   first_daemon->shortname,
608                   second_daemon->shortname, emsg);
609     }
610 #endif
611
612   if (total_connections == expected_connections)
613     {
614 #if VERBOSE
615       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616                   "Created %d total connections, which is our target number!  Starting next phase of testing.\n",
617                   total_connections);
618 #endif
619       GNUNET_SCHEDULER_cancel (die_task);
620       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
621                                                &end_badly, "from setup puts/gets");
622
623       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 2), &setup_puts_and_gets, NULL);
624     }
625   else if (total_connections + failed_connections == expected_connections)
626     {
627       GNUNET_SCHEDULER_cancel (die_task);
628       die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from topology_callback (too many failed connections)");
629     }
630 }
631
632 static void
633 peers_started_callback (void *cls,
634        const struct GNUNET_PeerIdentity *id,
635        const struct GNUNET_CONFIGURATION_Handle *cfg,
636        struct GNUNET_TESTING_Daemon *d, const char *emsg)
637 {
638   if (emsg != NULL)
639     {
640       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
641                   emsg);
642       return;
643     }
644   GNUNET_assert (id != NULL);
645
646 #if VERBOSE
647   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
648               (num_peers - peers_left) + 1, num_peers);
649 #endif
650
651   peers_left--;
652   if (peers_left == 0)
653     {
654
655 #if VERBOSE
656       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657                   "All %d daemons started, now connecting peers!\n",
658                   num_peers);
659 #endif
660       GNUNET_SCHEDULER_cancel (die_task);
661
662       expected_connections = -1;
663       if ((pg != NULL) && (peers_left == 0))
664         {
665           expected_connections = GNUNET_TESTING_connect_topology (pg, connection_topology, connect_topology_option, connect_topology_option_modifier, NULL, NULL);
666 #if VERBOSE
667           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668                       "Have %d expected connections\n", expected_connections);
669 #endif
670         }
671
672       if (expected_connections == GNUNET_SYSERR)
673         {
674           die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from connect topology (bad return)");
675         }
676
677       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
678                                                &end_badly, "from connect topology (timeout)");
679
680       ok = 0;
681     }
682 }
683
684 static void
685 create_topology ()
686 {
687   peers_left = num_peers; /* Reset counter */
688   if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
689     {
690 #if VERBOSE
691       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
692                   "Topology set up, now starting peers!\n");
693 #endif
694       GNUNET_TESTING_daemons_continue_startup(pg);
695     }
696   else
697     {
698       GNUNET_SCHEDULER_cancel (die_task);
699       die_task = GNUNET_SCHEDULER_add_now (&end_badly, "from create topology (bad return)");
700     }
701   GNUNET_SCHEDULER_cancel (die_task);
702   die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
703                                            &end_badly, "from continue startup (timeout)");
704 }
705
706 /**
707  * Callback indicating that the hostkey was created for a peer.
708  *
709  * @param cls NULL
710  * @param id the peer identity
711  * @param d the daemon handle (pretty useless at this point, remove?)
712  * @param emsg non-null on failure
713  */
714 void hostkey_callback (void *cls,
715                        const struct GNUNET_PeerIdentity *id,
716                        struct GNUNET_TESTING_Daemon *d,
717                        const char *emsg)
718 {
719   if (emsg != NULL)
720     {
721       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
722     }
723
724 #if VERBOSE > 1
725     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
726                 "Hostkey (%d/%d) created for peer `%s'\n",
727                 num_peers - peers_left, num_peers, GNUNET_i2s(id));
728 #endif
729
730
731     peers_left--;
732     if (peers_left == 0)
733       {
734 #if VERBOSE
735         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736                     "All %d hostkeys created, now creating topology!\n",
737                     num_peers);
738 #endif
739         GNUNET_SCHEDULER_cancel (die_task);
740         /* Set up task in case topology creation doesn't finish
741          * within a reasonable amount of time */
742         die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
743                                                  &end_badly, "from create_topology");
744         GNUNET_SCHEDULER_add_now(&create_topology, NULL);
745         ok = 0;
746       }
747 }
748
749
750 static void
751 run (void *cls,
752      char *const *args,
753      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
754 {
755   char * topology_str;
756   char * connect_topology_str;
757   char * blacklist_topology_str;
758   char * connect_topology_option_str;
759   char * connect_topology_option_modifier_string;
760
761   /* Get path from configuration file */
762   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
763     {
764       ok = 404;
765       return;
766     }
767
768   if ((GNUNET_YES ==
769       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
770                                             &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
771     {
772       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
773                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
774       topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
775     }
776
777   if ((GNUNET_YES ==
778       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
779                                             &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connection_topology, connect_topology_str)))
780     {
781       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
782                   "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
783     }
784   GNUNET_free_non_null(connect_topology_str);
785   if ((GNUNET_YES ==
786       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
787                                             &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
788     {
789       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
790                   "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
791       connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
792     }
793   GNUNET_free_non_null(connect_topology_option_str);
794   if (GNUNET_YES ==
795         GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
796                                                &connect_topology_option_modifier_string))
797     {
798       if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
799       {
800         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
801         _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
802         connect_topology_option_modifier_string,
803         "connect_topology_option_modifier",
804         "TESTING");
805       }
806       GNUNET_free (connect_topology_option_modifier_string);
807     }
808
809   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
810                                          &blacklist_transports))
811     blacklist_transports = NULL;
812
813   if ((GNUNET_YES ==
814       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
815                                             &blacklist_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
816     {
817       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
818                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
819     }
820   GNUNET_free_non_null(topology_str);
821   GNUNET_free_non_null(blacklist_topology_str);
822
823   /* Get number of peers to start from configuration */
824   if (GNUNET_SYSERR ==
825       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
826                                              &num_peers))
827     num_peers = DEFAULT_NUM_PEERS;
828
829   if (GNUNET_SYSERR ==
830       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_puts",
831                                              &num_puts))
832     num_puts = DEFAULT_NUM_PEERS;
833
834   if (GNUNET_SYSERR ==
835       GNUNET_CONFIGURATION_get_value_number (cfg, "dht_testing", "num_gets",
836                                              &num_gets))
837     num_gets = DEFAULT_NUM_PEERS;
838
839   /* Set peers_left so we know when all peers started */
840   peers_left = num_peers;
841
842   /* Set up a task to end testing if peer start fails */
843   die_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, SECONDS_PER_PEER_START * num_peers),
844                                            &end_badly, "didn't generate all hostkeys within a reasonable amount of time!!!");
845
846   pg = GNUNET_TESTING_daemons_start (cfg,
847                                      peers_left, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, SECONDS_PER_PEER_START * num_peers), &hostkey_callback, NULL, &peers_started_callback, NULL,
848                                      &topology_callback, NULL, NULL);
849
850 }
851
852 static int
853 check ()
854 {
855   int ret;
856   /* Arguments for GNUNET_PROGRAM_run */
857   char *const argv[] = {"test-dht-multipeer", /* Name to give running binary */
858     "-c",
859     "test_dht_multipeer_data.conf", /* Config file to use */
860 #if VERBOSE
861     "-L", "DEBUG",
862 #endif
863     NULL
864   };
865   struct GNUNET_GETOPT_CommandLineOption options[] = {
866     GNUNET_GETOPT_OPTION_END
867   };
868   /* Run the run function as a new program */
869   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
870                       argv, "test-dht-multipeer", "nohelp",
871                       options, &run, &ok);
872   if (ret != GNUNET_OK)
873     {
874       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-dht-multipeer': Failed with error code %d\n", ret);
875     }
876   return ok;
877 }
878
879 int
880 main (int argc, char *argv[])
881 {
882   int ret;
883
884   GNUNET_log_setup ("test-dht-multipeer",
885 #if VERBOSE
886                     "DEBUG",
887 #else
888                     "WARNING",
889 #endif
890                     NULL);
891   ret = check ();
892   /**
893    * Need to remove base directory, subdirectories taken care
894    * of by the testing framework.
895    */
896   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
897     {
898       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
899     }
900   return ret;
901 }
902
903 /* end of test_dht_twopeer_put_get.c */