changes to use names instead of numbers for topologies and topology options
[oweals/gnunet.git] / src / testing / test_testing_topology.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 2, 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 testing/test_testing_topology.c
22  * @brief base testcase for testing all the topologies provided
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
27
28 #define VERBOSE GNUNET_NO
29
30 /**
31  * How long until we fail the whole testcase?
32  */
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
34
35 /**
36  * How long until we give up on starting the peers?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 500)
39
40 #define DEFAULT_NUM_PEERS 4
41
42 #define MAX_OUTSTANDING_CONNECTIONS 300
43
44 static float fail_percentage = 0.05;
45
46 static int ok;
47
48 static unsigned long long num_peers;
49
50 static unsigned int total_connections;
51
52 static unsigned int failed_connections;
53
54 static unsigned int total_server_connections;
55
56 static unsigned int total_messages_received;
57
58 static unsigned int expected_messages;
59
60 static unsigned int expected_connections;
61
62 static unsigned long long peers_left;
63
64 static struct GNUNET_TESTING_PeerGroup *pg;
65
66 static struct GNUNET_SCHEDULER_Handle *sched;
67
68 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
69
70 GNUNET_SCHEDULER_TaskIdentifier die_task;
71
72 static char *dotOutFileName;
73
74 static FILE *dotOutFile;
75
76 static char *topology_string;
77
78 static char *blacklist_transports;
79
80 static int transmit_ready_scheduled;
81
82 static int transmit_ready_failed;
83
84 static int transmit_ready_called;
85
86 static enum GNUNET_TESTING_Topology topology;
87
88 static enum GNUNET_TESTING_Topology blacklist_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* Don't do any blacklisting */
89
90 static enum GNUNET_TESTING_Topology connection_topology = GNUNET_TESTING_TOPOLOGY_NONE; /* NONE actually means connect all allowed peers */
91
92 static enum GNUNET_TESTING_TopologyOption connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL;
93
94 static double connect_topology_option_modifier = 0.0;
95
96 static char *test_directory;
97
98 #define MTYPE 12345
99
100 struct GNUNET_TestMessage
101 {
102   /**
103    * Header of the message
104    */
105   struct GNUNET_MessageHeader header;
106
107   /**
108    * Unique identifier for this message.
109    */
110   uint32_t uid;
111 };
112
113 struct TestMessageContext
114 {
115   /* This is a linked list */
116   struct TestMessageContext *next;
117
118   /* Handle to the sending peer core */
119   struct GNUNET_CORE_Handle *peer1handle;
120
121   /* Handle to the receiving peer core */
122   struct GNUNET_CORE_Handle *peer2handle;
123
124   /* Handle to the sending peer daemon */
125   struct GNUNET_TESTING_Daemon *peer1;
126
127   /* Handle to the receiving peer daemon */
128   struct GNUNET_TESTING_Daemon *peer2;
129
130   /* Identifier for this message, so we don't disconnect other peers! */
131   uint32_t uid;
132
133   /* Task for disconnecting cores, allow task to be cancelled on shutdown */
134   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
135
136 };
137
138 static struct TestMessageContext *test_messages;
139
140 static void
141 finish_testing ()
142 {
143   GNUNET_assert (pg != NULL);
144   struct TestMessageContext *pos;
145   struct TestMessageContext *free_pos;
146 #if VERBOSE
147   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
148               "Called finish testing, stopping daemons.\n");
149 #endif
150   int count;
151   count = 0;
152   pos = test_messages;
153   while (pos != NULL)
154     {
155       if (pos->peer1handle != NULL)
156         {
157           GNUNET_CORE_disconnect(pos->peer1handle);
158           pos->peer1handle = NULL;
159         }
160       if (pos->peer2handle != NULL)
161         {
162           GNUNET_CORE_disconnect(pos->peer2handle);
163           pos->peer2handle = NULL;
164         }
165       free_pos = pos;
166       pos = pos->next;
167       if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
168         {
169           GNUNET_SCHEDULER_cancel(sched, free_pos->disconnect_task);
170         }
171       GNUNET_free(free_pos);
172     }
173 #if VERBOSE
174           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175                       "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_failed, transmit_ready_called);
176 #endif
177
178 #if VERBOSE
179           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180                       "Calling daemons_stop\n");
181 #endif
182   GNUNET_TESTING_daemons_stop (pg, TIMEOUT);
183 #if VERBOSE
184           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185                       "daemons_stop finished\n");
186 #endif
187   if (dotOutFile != NULL)
188     {
189       fprintf(dotOutFile, "}");
190       fclose(dotOutFile);
191     }
192
193   ok = 0;
194 }
195
196
197 static void
198 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
199 {
200   struct TestMessageContext *pos = cls;
201
202   /* Disconnect from the respective cores */
203 #if VERBOSE
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205               "Disconnecting from peer 1 `%4s'\n", GNUNET_i2s (&pos->peer1->id));
206 #endif
207   if (pos->peer1handle != NULL)
208     GNUNET_CORE_disconnect(pos->peer1handle);
209 #if VERBOSE
210   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211               "Disconnecting from peer 2 `%4s'\n", GNUNET_i2s (&pos->peer2->id));
212 #endif
213   if (pos->peer2handle != NULL)
214     GNUNET_CORE_disconnect(pos->peer2handle);
215   /* Set handles to NULL so test case can be ended properly */
216   pos->peer1handle = NULL;
217   pos->peer2handle = NULL;
218   pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
219   /* Decrement total connections so new can be established */
220   total_server_connections -= 2;
221 }
222
223 static int
224 process_mtype (void *cls,
225                const struct GNUNET_PeerIdentity *peer,
226                const struct GNUNET_MessageHeader *message,
227                struct GNUNET_TIME_Relative latency,
228                uint32_t distance)
229 {
230   struct TestMessageContext *pos = cls;
231   struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
232   if (pos->uid != ntohl(msg->uid))
233     return GNUNET_OK;
234
235   total_messages_received++;
236 #if VERBOSE
237   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238               "Received message from `%4s', type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
239   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
240               "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
241 #endif
242
243   if (total_messages_received == expected_messages)
244     {
245       GNUNET_SCHEDULER_cancel (sched, die_task);
246       GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
247     }
248   else
249     {
250       pos->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &disconnect_cores, pos);
251     }
252
253   return GNUNET_OK;
254 }
255
256 static void
257 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
258 {
259   char *msg = cls;
260   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
261               "End badly was called (%s)... stopping daemons.\n", msg);
262   struct TestMessageContext *pos;
263   struct TestMessageContext *free_pos;
264
265   pos = test_messages;
266   while (pos != NULL)
267     {
268       if (pos->peer1handle != NULL)
269         {
270           GNUNET_CORE_disconnect(pos->peer1handle);
271           pos->peer1handle = NULL;
272         }
273       if (pos->peer2handle != NULL)
274         {
275           GNUNET_CORE_disconnect(pos->peer2handle);
276           pos->peer2handle = NULL;
277         }
278       free_pos = pos;
279       pos = pos->next;
280       GNUNET_free(free_pos);
281     }
282
283   if (pg != NULL)
284     {
285       GNUNET_TESTING_daemons_stop (pg, TIMEOUT);
286       ok = 7331;                /* Opposite of leet */
287     }
288   else
289     ok = 401;                   /* Never got peers started */
290
291   if (dotOutFile != NULL)
292     {
293       fprintf(dotOutFile, "}");
294       fclose(dotOutFile);
295     }
296 }
297
298
299
300 static size_t
301 transmit_ready (void *cls, size_t size, void *buf)
302 {
303   struct GNUNET_TestMessage *m;
304   struct TestMessageContext *pos = cls;
305
306   GNUNET_assert (buf != NULL);
307   m = (struct GNUNET_TestMessage *) buf;
308   m->header.type = htons (MTYPE);
309   m->header.size = htons (sizeof (struct GNUNET_TestMessage));
310   m->uid = htonl(pos->uid);
311   transmit_ready_called++;
312 #if VERBOSE
313   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314               "transmit ready for peer %s\ntransmit_ready's scheduled %d, transmit_ready's called %d\n", GNUNET_i2s(&pos->peer1->id), transmit_ready_scheduled, transmit_ready_called);
315 #endif
316   return sizeof (struct GNUNET_TestMessage);
317 }
318
319
320 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
321   {NULL, 0, 0}
322 };
323
324 static struct GNUNET_CORE_MessageHandler handlers[] = {
325   {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
326   {NULL, 0, 0}
327 };
328
329 static void
330 init_notify_peer2 (void *cls,
331              struct GNUNET_CORE_Handle *server,
332              const struct GNUNET_PeerIdentity *my_identity,
333              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
334 {
335   struct TestMessageContext *pos = cls;
336
337 #if VERBOSE
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339               "Core connection to `%4s' established, scheduling message send\n",
340               GNUNET_i2s (my_identity));
341 #endif
342   total_server_connections++;
343
344   if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
345                                                  0,
346                                                  TIMEOUT,
347                                                  &pos->peer2->id,
348                                                  sizeof (struct GNUNET_TestMessage),
349                                                  &transmit_ready, pos))
350     {
351       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352                   "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
353                   GNUNET_i2s (&pos->peer2->id));
354       transmit_ready_failed++;
355     }
356   else
357     {
358       transmit_ready_scheduled++;
359     }
360 }
361
362
363 static void
364 init_notify_peer1 (void *cls,
365              struct GNUNET_CORE_Handle *server,
366              const struct GNUNET_PeerIdentity *my_identity,
367              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
368 {
369   struct TestMessageContext *pos = cls;
370   total_server_connections++;
371
372 #if VERBOSE
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374               "Core connection to `%4s' established, setting up handles\n",
375               GNUNET_i2s (my_identity));
376 #endif
377
378   /*
379    * Connect to the receiving peer
380    */
381   pos->peer2handle = GNUNET_CORE_connect (sched,
382                        pos->peer2->cfg,
383                        TIMEOUT,
384                        pos,
385                        &init_notify_peer2,
386                        NULL,
387                        NULL,
388                        NULL,
389                        GNUNET_YES, NULL, GNUNET_YES, handlers);
390
391 }
392
393
394 static void
395 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
396 {
397   struct TestMessageContext *pos = cls;
398
399   if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) || (cls == NULL))
400     return;
401
402   if (die_task == GNUNET_SCHEDULER_NO_TASK)
403     {
404       die_task = GNUNET_SCHEDULER_add_delayed (sched,
405                                                TEST_TIMEOUT,
406                                                &end_badly, "from create topology (timeout)");
407     }
408
409   if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
410     {
411       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
412                                     &send_test_messages, pos);
413       return; /* Otherwise we'll double schedule messages here! */
414     }
415
416   /*
417    * Connect to the sending peer
418    */
419   pos->peer1handle = GNUNET_CORE_connect (sched,
420                                           pos->peer1->cfg,
421                                           TIMEOUT,
422                                           pos,
423                                           &init_notify_peer1,
424                                           NULL,
425                                           NULL,
426                                           NULL,
427                                           GNUNET_NO, NULL, GNUNET_NO, no_handlers);
428
429   GNUNET_assert(pos->peer1handle != NULL);
430
431   if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
432     {
433       GNUNET_SCHEDULER_add_now (sched,
434                                 &send_test_messages, pos->next);
435     }
436   else
437     {
438       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
439                                     &send_test_messages, pos->next);
440     }
441 }
442
443
444 void
445 topology_callback (void *cls,
446                    const struct GNUNET_PeerIdentity *first,
447                    const struct GNUNET_PeerIdentity *second,
448                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
449                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
450                    struct GNUNET_TESTING_Daemon *first_daemon,
451                    struct GNUNET_TESTING_Daemon *second_daemon,
452                    const char *emsg)
453 {
454   struct TestMessageContext *temp_context;
455   if (emsg == NULL)
456     {
457       total_connections++;
458 #if VERBOSE
459       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
460                first_daemon->shortname,
461                second_daemon->shortname);
462 #endif
463       temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
464       temp_context->peer1 = first_daemon;
465       temp_context->peer2 = second_daemon;
466       temp_context->next = test_messages;
467       temp_context->uid = total_connections;
468       temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
469       test_messages = temp_context;
470
471       expected_messages++;
472       if (dotOutFile != NULL)
473         fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
474     }
475 #if VERBOSE
476   else
477     {
478       failed_connections++;
479       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
480                first_daemon->shortname,
481                second_daemon->shortname, emsg);
482     }
483 #endif
484
485   if (total_connections == expected_connections)
486     {
487 #if VERBOSE
488       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489                   "Created %d total connections, which is our target number!  Calling send messages.\n",
490                   total_connections);
491 #endif
492
493       GNUNET_SCHEDULER_cancel (sched, die_task);
494       die_task = GNUNET_SCHEDULER_NO_TASK;
495       GNUNET_SCHEDULER_add_now (sched, &send_test_messages, test_messages);
496       //GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
497     }
498   else if (total_connections + failed_connections == expected_connections)
499     {
500       if (failed_connections < (unsigned int)(fail_percentage * total_connections))
501         {
502           GNUNET_SCHEDULER_cancel (sched, die_task);
503           die_task = GNUNET_SCHEDULER_NO_TASK;
504           GNUNET_SCHEDULER_add_now (sched, &send_test_messages, test_messages);
505           //GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
506         }
507       else
508         {
509           GNUNET_SCHEDULER_cancel (sched, die_task);
510           die_task = GNUNET_SCHEDULER_add_now (sched,
511                                                &end_badly, "from topology_callback (too many failed connections)");
512         }
513     }
514   else
515     {
516 #if VERBOSE
517       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518                   "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
519                   total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
520 #endif
521     }
522 }
523
524 static void
525 connect_topology ()
526 {
527   expected_connections = -1;
528   if ((pg != NULL) && (peers_left == 0))
529     {
530       expected_connections = GNUNET_TESTING_connect_topology (pg, connection_topology, connect_topology_option, connect_topology_option_modifier);
531 #if VERBOSE
532       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
533                   "Have %d expected connections\n", expected_connections);
534 #endif
535     }
536
537   GNUNET_SCHEDULER_cancel (sched, die_task);
538   if (expected_connections == GNUNET_SYSERR)
539     {
540       die_task = GNUNET_SCHEDULER_add_now (sched,
541                                            &end_badly, "from connect topology (bad return)");
542     }
543
544   die_task = GNUNET_SCHEDULER_add_delayed (sched,
545                                            TEST_TIMEOUT,
546                                            &end_badly, "from connect topology (timeout)");
547 }
548
549 static void
550 create_topology ()
551 {
552   peers_left = num_peers; /* Reset counter */
553   if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
554     {
555 #if VERBOSE
556       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557                   "Topology set up, now starting peers!\n");
558 #endif
559       GNUNET_TESTING_daemons_continue_startup(pg);
560     }
561   else
562     {
563       GNUNET_SCHEDULER_cancel (sched, die_task);
564       die_task = GNUNET_SCHEDULER_add_now (sched,
565                                            &end_badly, "from create topology (bad return)");
566     }
567   GNUNET_SCHEDULER_cancel (sched, die_task);
568   die_task = GNUNET_SCHEDULER_add_delayed (sched,
569                                            TEST_TIMEOUT,
570                                            &end_badly, "from continue startup (timeout)");
571 }
572
573
574 static void
575 peers_started_callback (void *cls,
576        const struct GNUNET_PeerIdentity *id,
577        const struct GNUNET_CONFIGURATION_Handle *cfg,
578        struct GNUNET_TESTING_Daemon *d, const char *emsg)
579 {
580   if (emsg != NULL)
581     {
582       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
583                   emsg);
584       return;
585     }
586   GNUNET_assert (id != NULL);
587 #if VERBOSE
588   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
589               (num_peers - peers_left) + 1, num_peers);
590 #endif
591   peers_left--;
592   if (peers_left == 0)
593     {
594 #if VERBOSE
595       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596                   "All %d daemons started, now creating topology!\n",
597                   num_peers);
598 #endif
599       GNUNET_SCHEDULER_cancel (sched, die_task);
600       /* Set up task in case topology creation doesn't finish
601        * within a reasonable amount of time */
602       die_task = GNUNET_SCHEDULER_add_delayed (sched,
603                                                GNUNET_TIME_relative_multiply
604                                                (GNUNET_TIME_UNIT_MINUTES, 5),
605                                                &end_badly, "from peers_started_callback");
606
607       connect_topology ();
608       ok = 0;
609     }
610 }
611
612 /**
613  * Callback indicating that the hostkey was created for a peer.
614  *
615  * @param cls NULL
616  * @param id the peer identity
617  * @param d the daemon handle (pretty useless at this point, remove?)
618  * @param emsg non-null on failure
619  */
620 void hostkey_callback (void *cls,
621                        const struct GNUNET_PeerIdentity *id,
622                        struct GNUNET_TESTING_Daemon *d,
623                        const char *emsg)
624 {
625   if (emsg != NULL)
626     {
627       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
628     }
629
630 #if VERBOSE
631     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                 "Hostkey created for peer `%s'\n",
633                 GNUNET_i2s(id));
634 #endif
635     peers_left--;
636     if (peers_left == 0)
637       {
638 #if VERBOSE
639         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640                     "All %d hostkeys created, now creating topology!\n",
641                     num_peers);
642 #endif
643         GNUNET_SCHEDULER_cancel (sched, die_task);
644         /* Set up task in case topology creation doesn't finish
645          * within a reasonable amount of time */
646         die_task = GNUNET_SCHEDULER_add_delayed (sched,
647                                                  GNUNET_TIME_relative_multiply
648                                                  (GNUNET_TIME_UNIT_MINUTES, 5),
649                                                  &end_badly, "from hostkey_callback");
650         GNUNET_SCHEDULER_add_now(sched, &create_topology, NULL);
651         ok = 0;
652       }
653 }
654
655 static void
656 run (void *cls,
657      struct GNUNET_SCHEDULER_Handle *s,
658      char *const *args,
659      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
660 {
661   char * topology_str;
662   char * connect_topology_str;
663   char * blacklist_topology_str;
664   char * connect_topology_option_str;
665   char * connect_topology_option_modifier_string;
666   sched = s;
667   ok = 1;
668
669   dotOutFile = fopen (dotOutFileName, "w");
670   if (dotOutFile != NULL)
671     {
672       fprintf (dotOutFile, "strict graph G {\n");
673     }
674
675 #if VERBOSE
676   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677               "Starting daemons based on config file %s\n", cfgfile);
678 #endif
679
680   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
681     {
682       ok = 404;
683       return;
684     }
685
686   if ((GNUNET_YES ==
687       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
688                                             &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
689     {
690       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
691                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
692       topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
693     }
694
695   if ((GNUNET_YES ==
696       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
697                                             &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connection_topology, connect_topology_str)))
698     {
699       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
700                   "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
701     }
702
703   if ((GNUNET_YES ==
704       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
705                                             &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
706     {
707       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
708                   "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
709       connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
710     }
711
712   if (GNUNET_YES ==
713         GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
714                                                &connect_topology_option_modifier_string))
715     {
716       if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
717       {
718         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
719         _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
720         connect_topology_option_modifier_string,
721         "connect_topology_option_modifier",
722         "TESTING");
723       }
724       GNUNET_free (connect_topology_option_modifier_string);
725     }
726
727   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
728                                          &blacklist_transports))
729     blacklist_transports = NULL;
730
731   if ((GNUNET_YES ==
732       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
733                                             & blacklist_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
734     {
735       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
736                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
737     }
738
739   if (GNUNET_SYSERR ==
740       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
741                                              &num_peers))
742     num_peers = DEFAULT_NUM_PEERS;
743
744   main_cfg = cfg;
745
746   peers_left = num_peers;
747
748   /* Set up a task to end testing if peer start fails */
749   die_task = GNUNET_SCHEDULER_add_delayed (sched,
750                                            GNUNET_TIME_relative_multiply
751                                            (GNUNET_TIME_UNIT_MINUTES, 5),
752                                            &end_badly, "didn't start all daemons in reasonable amount of time!!!");
753
754   pg = GNUNET_TESTING_daemons_start (sched, cfg,
755                                      peers_left, TIMEOUT, &hostkey_callback, NULL, &peers_started_callback, NULL,
756                                      &topology_callback, NULL, NULL);
757
758 }
759
760 static int
761 check ()
762 {
763   char *binary_name;
764   char *config_file_name;
765   GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
766   GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
767   int ret;
768   char *const argv[] = {binary_name,
769     "-c",
770     config_file_name,
771 #if VERBOSE
772     "-L", "DEBUG",
773 #endif
774     NULL
775   };
776   struct GNUNET_GETOPT_CommandLineOption options[] = {
777     GNUNET_GETOPT_OPTION_END
778   };
779   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
780                       argv, binary_name, "nohelp",
781                       options, &run, &ok);
782   if (ret != GNUNET_OK)
783     {
784       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-testing-topology-%s': Failed with error code %d\n", topology_string, ret);
785     }
786   GNUNET_free(binary_name);
787   GNUNET_free(config_file_name);
788   return ok;
789 }
790
791 int
792 main (int argc, char *argv[])
793 {
794   int ret;
795   char *binary_start_pos;
796   char *our_binary_name;
797
798   binary_start_pos = rindex(argv[0], '/');
799   topology_string = strstr (binary_start_pos,
800                             "_topology");
801   GNUNET_assert (topology_string != NULL);
802   topology_string++;
803   topology_string = strstr (topology_string, "_");
804   GNUNET_assert (topology_string != NULL);
805   topology_string++;
806
807   GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
808   GNUNET_asprintf(&dotOutFileName, "topology_%s.dot", topology_string);
809
810   GNUNET_log_setup (our_binary_name,
811 #if VERBOSE
812                     "DEBUG",
813 #else
814                     "WARNING",
815 #endif
816                     NULL);
817   ret = check ();
818
819   /**
820    * Need to remove base directory, subdirectories taken care
821    * of by the testing framework.
822    */
823   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
824     {
825       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
826     }
827   GNUNET_free(our_binary_name);
828   return ret;
829 }
830
831 /* end of test_testing_topology.c */