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