dht testcases and some minor other changes...
[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   /* This is a linked list */
61   struct TestPutContext *next;
62
63   /**
64    * Handle to the first peers DHT service (via the API)
65    */
66   struct GNUNET_DHT_Handle *dht_handle;
67
68   /**
69    *  Handle to the PUT peer daemon
70    */
71   struct GNUNET_TESTING_Daemon *daemon;
72
73   /**
74    *  Identifier for this PUT
75    */
76   uint32_t uid;
77
78   /**
79    * Task for disconnecting DHT handles
80    */
81   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
82 };
83
84 struct TestGetContext
85 {
86   /* This is a linked list */
87   struct TestGetContext *next;
88
89   /**
90    * Handle to the first peers DHT service (via the API)
91    */
92   struct GNUNET_DHT_Handle *dht_handle;
93
94   /**
95    * Handle for the DHT get request
96    */
97   struct GNUNET_DHT_GetHandle *get_handle;
98
99   /**
100    *  Handle to the GET peer daemon
101    */
102   struct GNUNET_TESTING_Daemon *daemon;
103
104   /**
105    *  Identifier for this GET
106    */
107   uint32_t uid;
108
109   /**
110    * Task for disconnecting DHT handles (and stopping GET)
111    */
112   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
113
114   /**
115    * Whether or not this request has been fulfilled already.
116    */
117   int succeeded;
118 };
119
120 /* Globals */
121
122 /**
123  * List of GETS to perform
124  */
125 struct TestGetContext *all_gets;
126
127 /**
128  * List of PUTS to perform
129  */
130 struct TestPutContext *all_puts;
131
132 /**
133  * Directory to store temp data in, defined in config file
134  */
135 static char *test_directory;
136
137 /**
138  * Variable used to store the number of connections we should wait for.
139  */
140 static unsigned int expected_connections;
141
142 /**
143  * Variable used to keep track of how many peers aren't yet started.
144  */
145 static unsigned long long peers_left;
146
147 /**
148  * Handle to the set of all peers run for this test.
149  */
150 static struct GNUNET_TESTING_PeerGroup *pg;
151
152
153 /**
154  * Global scheduler, used for all GNUNET_SCHEDULER_* functions.
155  */
156 static struct GNUNET_SCHEDULER_Handle *sched;
157
158 /**
159  * Total number of peers to run, set based on config file.
160  */
161 static unsigned long long num_peers;
162
163 /**
164  * Total number of items to insert.
165  */
166 static unsigned long long num_puts;
167
168 /**
169  * Total number of items to attempt to get.
170  */
171 static unsigned long long num_gets;
172
173 /**
174  * How many puts do we currently have in flight?
175  */
176 static unsigned long long outstanding_puts;
177
178 /**
179  * How many puts are done?
180  */
181 static unsigned long long puts_completed;
182
183 /**
184  * How many puts do we currently have in flight?
185  */
186 static unsigned long long outstanding_gets;
187
188 /**
189  * How many gets are done?
190  */
191 static unsigned long long gets_completed;
192
193 /**
194  * How many gets failed?
195  */
196 static unsigned long long gets_failed;
197
198 /**
199  * Global used to count how many connections we have currently
200  * been notified about (how many times has topology_callback been called
201  * with success?)
202  */
203 static unsigned int total_connections;
204
205 /**
206  * Global used to count how many failed connections we have
207  * been notified about (how many times has topology_callback
208  * been called with failure?)
209  */
210 static unsigned int failed_connections;
211
212 /* Task handle to use to schedule test failure */
213 GNUNET_SCHEDULER_TaskIdentifier die_task;
214
215 static char *blacklist_transports;
216
217 static enum GNUNET_TESTING_Topology topology;
218
219 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
220
221 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
222
223 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
224
225 static double connect_topology_option_modifier = 0.0;
226
227 /* Global return value (0 for success, anything else for failure) */
228 static int ok;
229
230
231 /**
232  * Check whether peers successfully shut down.
233  */
234 void shutdown_callback (void *cls,
235                         const char *emsg)
236 {
237   if (emsg != NULL)
238     {
239       if (ok == 0)
240         ok = 2;
241     }
242 }
243
244 /**
245  * Task to release DHT handles for PUT
246  */
247 static void
248 put_disconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
249 {
250   struct TestPutContext *test_put = cls;
251   test_put->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
252   GNUNET_DHT_disconnect(test_put->dht_handle);
253   test_put->dht_handle = NULL;
254 }
255
256 /**
257  * Function scheduled to be run on the successful completion of this
258  * testcase.
259  */
260 static void
261 finish_testing (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
262 {
263   GNUNET_assert (pg != NULL);
264   struct TestPutContext *test_put = all_puts;
265   struct TestGetContext *test_get = all_gets;
266
267   while (test_put != NULL)
268     {
269       if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
270         GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
271       if (test_put->dht_handle != NULL)
272         GNUNET_DHT_disconnect(test_put->dht_handle);
273       test_put = test_put->next;
274     }
275
276   while (test_get != NULL)
277     {
278       if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
279         GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
280       if (test_get->get_handle != NULL)
281         GNUNET_DHT_get_stop(test_get->get_handle, NULL, NULL);
282       if (test_get->dht_handle != NULL)
283         GNUNET_DHT_disconnect(test_get->dht_handle);
284       test_get = test_get->next;
285     }
286
287   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
288   ok = 0;
289 }
290
291
292 /**
293  * Check if the get_handle is being used, if so stop the request.  Either
294  * way, schedule the end_badly_cont function which actually shuts down the
295  * test.
296  */
297 static void
298 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
299 {
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failing test with error: `%s'!\n", (char *)cls);
301
302   struct TestPutContext *test_put = all_puts;
303   struct TestGetContext *test_get = all_gets;
304
305   while (test_put != NULL)
306     {
307       if (test_put->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
308         GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
309       if (test_put->dht_handle != NULL)
310         GNUNET_DHT_disconnect(test_put->dht_handle);
311       test_put = test_put->next;
312     }
313
314   while (test_get != NULL)
315     {
316       if (test_get->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
317         GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
318       if (test_get->get_handle != NULL)
319         GNUNET_DHT_get_stop(test_get->get_handle, NULL, NULL);
320       if (test_get->dht_handle != NULL)
321         GNUNET_DHT_disconnect(test_get->dht_handle);
322       test_get = test_get->next;
323     }
324
325   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
326   ok = 1;
327 }
328
329 /**
330  * Task to release DHT handle associated with GET request.
331  */
332 static void
333 get_stop_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
334 {
335   struct TestGetContext *test_get = cls;
336   outstanding_gets--; /* GET is really finished */
337   GNUNET_DHT_disconnect(test_get->dht_handle);
338   test_get->dht_handle = NULL;
339
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%d gets succeeded, %d gets failed!\n", gets_completed, gets_failed);
341   if ((gets_completed == num_gets) && (outstanding_gets == 0))/* All gets successful */
342     {
343       GNUNET_SCHEDULER_cancel(sched, die_task);
344       GNUNET_SCHEDULER_add_now(sched, &finish_testing, NULL);
345     }
346   else if ((gets_completed + gets_failed == num_gets) && (outstanding_gets == 0)) /* Had some failures */
347     {
348       GNUNET_SCHEDULER_cancel(sched, die_task);
349       GNUNET_SCHEDULER_add_now(sched, &end_badly, "not all gets succeeded!\n");
350     }
351 }
352
353 /**
354  * Task to release get handle.
355  */
356 static void
357 get_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
358 {
359   struct TestGetContext *test_get = cls;
360   GNUNET_HashCode search_key; /* Key stored under */
361   char original_data[TEST_DATA_SIZE]; /* Made up data to store */
362
363   memset(original_data, test_get->uid, sizeof(original_data));
364   GNUNET_CRYPTO_hash(original_data, TEST_DATA_SIZE, &search_key);
365
366   if (tc->reason == GNUNET_SCHEDULER_REASON_TIMEOUT)
367     {
368       gets_failed++;
369       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Get from peer %s for key %s failed!\n", test_get->daemon->shortname, GNUNET_h2s(&search_key));
370     }
371   GNUNET_assert(test_get->get_handle != NULL);
372   GNUNET_DHT_get_stop(test_get->get_handle, &get_stop_finished, test_get);
373   test_get->get_handle = NULL;
374   test_get->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
375 }
376
377 /**
378  * Iterator called if the GET request initiated returns a response.
379  *
380  * @param cls closure
381  * @param exp when will this value expire
382  * @param key key of the result
383  * @param type type of the result
384  * @param size number of bytes in data
385  * @param data pointer to the result data
386  */
387 void get_result_iterator (void *cls,
388                           struct GNUNET_TIME_Absolute exp,
389                           const GNUNET_HashCode * key,
390                           uint32_t type,
391                           uint32_t size,
392                           const void *data)
393 {
394   struct TestGetContext *test_get = cls;
395   GNUNET_HashCode search_key; /* Key stored under */
396   char original_data[TEST_DATA_SIZE]; /* Made up data to store */
397
398   memset(original_data, test_get->uid, sizeof(original_data));
399   GNUNET_CRYPTO_hash(original_data, TEST_DATA_SIZE, &search_key);
400
401   if (test_get->succeeded == GNUNET_YES)
402     return; /* Get has already been successful, probably ending now */
403
404   if ((0 != memcmp(&search_key, key, sizeof (GNUNET_HashCode))) || (0 != memcmp(original_data, data, sizeof(original_data))))
405     {
406       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Key or data is not the same as was inserted!\n");
407     }
408   else
409     {
410       gets_completed++;
411       test_get->succeeded = GNUNET_YES;
412     }
413
414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received correct GET response!\n");
415   GNUNET_SCHEDULER_cancel(sched, test_get->disconnect_task);
416   GNUNET_SCHEDULER_add_continuation(sched, &get_stop_task, test_get, GNUNET_SCHEDULER_REASON_PREREQ_DONE);
417 }
418
419 /**
420  * Continuation telling us GET request was sent.
421  */
422 static void
423 get_continuation (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
424 {
425   // Is there something to be done here?
426   if (tc->reason != GNUNET_SCHEDULER_REASON_PREREQ_DONE)
427     return;
428 }
429
430 /**
431  * Set up some data, and call API PUT function
432  */
433 static void
434 do_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
435 {
436   struct TestGetContext *test_get = cls;
437   GNUNET_HashCode key; /* Made up key to store data under */
438   char data[TEST_DATA_SIZE]; /* Made up data to store */
439
440   if (test_get == NULL)
441     return; /* End of the list */
442   memset(data, test_get->uid, sizeof(data));
443   GNUNET_CRYPTO_hash(data, TEST_DATA_SIZE, &key);
444
445   if (outstanding_gets > MAX_OUTSTANDING_GETS)
446     {
447       GNUNET_SCHEDULER_add_delayed (sched, GET_DELAY, &do_get, test_get);
448       return;
449     }
450
451   test_get->dht_handle = GNUNET_DHT_connect(sched, test_get->daemon->cfg, 10);
452   /* Insert the data at the first peer */
453   GNUNET_assert(test_get->dht_handle != NULL);
454   outstanding_gets++;
455   test_get->get_handle = GNUNET_DHT_get_start(test_get->dht_handle,
456                                               GNUNET_TIME_relative_get_forever(),
457                                               1,
458                                               &key,
459                                               &get_result_iterator,
460                                               test_get,
461                                               &get_continuation,
462                                               test_get);
463 #if VERBOSE
464   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting get for uid %u from peer %s\n",
465              test_get->uid,
466              test_get->daemon->shortname);
467 #endif
468   test_get->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, GET_TIMEOUT, &get_stop_task, test_get);
469   GNUNET_SCHEDULER_add_now (sched, &do_get, test_get->next);
470 }
471
472 /**
473  * Called when the PUT request has been transmitted to the DHT service.
474  * Schedule the GET request for some time in the future.
475  */
476 static void
477 put_finished (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
478 {
479   struct TestPutContext *test_put = cls;
480   outstanding_puts--;
481   puts_completed++;
482
483   GNUNET_SCHEDULER_cancel(sched, test_put->disconnect_task);
484   test_put->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &put_disconnect_task, test_put);
485   if (puts_completed == num_puts)
486     {
487       GNUNET_assert(outstanding_puts == 0);
488       GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 10), &do_get, all_gets);
489       return;
490     }
491 }
492
493 /**
494  * Set up some data, and call API PUT function
495  */
496 static void
497 do_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
498 {
499   struct TestPutContext *test_put = cls;
500   GNUNET_HashCode key; /* Made up key to store data under */
501   char data[TEST_DATA_SIZE]; /* Made up data to store */
502
503   if (test_put == NULL)
504     return; /* End of list */
505
506   memset(data, test_put->uid, sizeof(data));
507   GNUNET_CRYPTO_hash(data, TEST_DATA_SIZE, &key);
508
509   if (outstanding_puts > MAX_OUTSTANDING_PUTS)
510     {
511       GNUNET_SCHEDULER_add_delayed (sched, PUT_DELAY, &do_put, test_put);
512       return;
513     }
514
515 #if VERBOSE
516     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting put for uid %u from peer %s\n",
517                test_put->uid,
518                test_put->daemon->shortname);
519 #endif
520   test_put->dht_handle = GNUNET_DHT_connect(sched, test_put->daemon->cfg, 10);
521
522   GNUNET_assert(test_put->dht_handle != NULL);
523   outstanding_puts++;
524   GNUNET_DHT_put(test_put->dht_handle,
525                  &key,
526                  1,
527                  sizeof(data), data,
528                  GNUNET_TIME_absolute_get_forever(),
529                  GNUNET_TIME_relative_get_forever(),
530                  &put_finished, test_put);
531   test_put->disconnect_task = GNUNET_SCHEDULER_add_delayed(sched, GNUNET_TIME_relative_get_forever(), &put_disconnect_task, test_put);
532   GNUNET_SCHEDULER_add_now(sched, &do_put, test_put->next);
533 }
534
535
536 /**
537  * Set up some all of the put and get operations we want
538  * to do.  Allocate data structure for each, add to list,
539  * then call actual insert functions.
540  */
541 static void
542 setup_puts_and_gets (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
543 {
544   int i;
545   uint32_t temp_daemon;
546   struct TestPutContext *test_put;
547   struct TestGetContext *test_get;
548   int remember[num_puts][num_peers];
549
550   for (i = 0; i < num_puts; i++)
551     {
552       test_put = GNUNET_malloc(sizeof(struct TestPutContext));
553       test_put->uid = i;
554       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
555       test_put->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
556       test_put->next = all_puts;
557       all_puts = test_put;
558     }
559
560   for (i = 0; i < num_gets; i++)
561     {
562       test_get = GNUNET_malloc(sizeof(struct TestGetContext));
563       test_get->uid = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_puts);
564       temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
565       while (remember[test_get->uid][temp_daemon] == 1)
566         temp_daemon = GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_WEAK, num_peers);
567       test_get->daemon = GNUNET_TESTING_daemon_get(pg, temp_daemon);
568       remember[test_get->uid][temp_daemon] = 1;
569       test_get->next = all_gets;
570       all_gets = test_get;
571     }
572
573   GNUNET_SCHEDULER_add_now (sched, &do_put, all_puts);
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 */