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