some testing changes, including an api change that likely breaks things for others
[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_YES
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                    uint32_t distance,
449                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
450                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
451                    struct GNUNET_TESTING_Daemon *first_daemon,
452                    struct GNUNET_TESTING_Daemon *second_daemon,
453                    const char *emsg)
454 {
455   struct TestMessageContext *temp_context;
456   if (emsg == NULL)
457     {
458       total_connections++;
459 #if VERBOSE
460       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
461                first_daemon->shortname,
462                second_daemon->shortname);
463 #endif
464       temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
465       temp_context->peer1 = first_daemon;
466       temp_context->peer2 = second_daemon;
467       temp_context->next = test_messages;
468       temp_context->uid = total_connections;
469       temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
470       test_messages = temp_context;
471
472       expected_messages++;
473       if (dotOutFile != NULL)
474         fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
475     }
476 #if VERBOSE
477   else
478     {
479       failed_connections++;
480       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
481                first_daemon->shortname,
482                second_daemon->shortname, emsg);
483     }
484 #endif
485
486   if (total_connections == expected_connections)
487     {
488 #if VERBOSE
489       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490                   "Created %d total connections, which is our target number!  Calling send messages.\n",
491                   total_connections);
492 #endif
493
494       GNUNET_SCHEDULER_cancel (sched, die_task);
495       die_task = GNUNET_SCHEDULER_NO_TASK;
496       GNUNET_SCHEDULER_add_now (sched, &send_test_messages, test_messages);
497       //GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
498     }
499   else if (total_connections + failed_connections == expected_connections)
500     {
501       if (failed_connections < (unsigned int)(fail_percentage * total_connections))
502         {
503           GNUNET_SCHEDULER_cancel (sched, die_task);
504           die_task = GNUNET_SCHEDULER_NO_TASK;
505           GNUNET_SCHEDULER_add_now (sched, &send_test_messages, test_messages);
506           //GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
507         }
508       else
509         {
510           GNUNET_SCHEDULER_cancel (sched, die_task);
511           die_task = GNUNET_SCHEDULER_add_now (sched,
512                                                &end_badly, "from topology_callback (too many failed connections)");
513         }
514     }
515   else
516     {
517 #if VERBOSE
518       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519                   "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
520                   total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
521 #endif
522     }
523 }
524
525 static void
526 connect_topology ()
527 {
528   expected_connections = -1;
529   if ((pg != NULL) && (peers_left == 0))
530     {
531       expected_connections = GNUNET_TESTING_connect_topology (pg, connection_topology, connect_topology_option, connect_topology_option_modifier);
532 #if VERBOSE
533       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534                   "Have %d expected connections\n", expected_connections);
535 #endif
536     }
537
538   GNUNET_SCHEDULER_cancel (sched, die_task);
539   if (expected_connections == GNUNET_SYSERR)
540     {
541       die_task = GNUNET_SCHEDULER_add_now (sched,
542                                            &end_badly, "from connect topology (bad return)");
543     }
544
545   die_task = GNUNET_SCHEDULER_add_delayed (sched,
546                                            TEST_TIMEOUT,
547                                            &end_badly, "from connect topology (timeout)");
548 }
549
550 static void
551 create_topology ()
552 {
553   peers_left = num_peers; /* Reset counter */
554   if (GNUNET_TESTING_create_topology (pg, topology, blacklist_topology, blacklist_transports) != GNUNET_SYSERR)
555     {
556 #if VERBOSE
557       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558                   "Topology set up, now starting peers!\n");
559 #endif
560       GNUNET_TESTING_daemons_continue_startup(pg);
561     }
562   else
563     {
564       GNUNET_SCHEDULER_cancel (sched, die_task);
565       die_task = GNUNET_SCHEDULER_add_now (sched,
566                                            &end_badly, "from create topology (bad return)");
567     }
568   GNUNET_SCHEDULER_cancel (sched, die_task);
569   die_task = GNUNET_SCHEDULER_add_delayed (sched,
570                                            TEST_TIMEOUT,
571                                            &end_badly, "from continue startup (timeout)");
572 }
573
574
575 static void
576 peers_started_callback (void *cls,
577        const struct GNUNET_PeerIdentity *id,
578        const struct GNUNET_CONFIGURATION_Handle *cfg,
579        struct GNUNET_TESTING_Daemon *d, const char *emsg)
580 {
581   if (emsg != NULL)
582     {
583       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start daemon with error: `%s'\n",
584                   emsg);
585       return;
586     }
587   GNUNET_assert (id != NULL);
588 #if VERBOSE
589   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
590               (num_peers - peers_left) + 1, num_peers);
591 #endif
592   peers_left--;
593   if (peers_left == 0)
594     {
595 #if VERBOSE
596       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597                   "All %d daemons started, now creating topology!\n",
598                   num_peers);
599 #endif
600       GNUNET_SCHEDULER_cancel (sched, die_task);
601       /* Set up task in case topology creation doesn't finish
602        * within a reasonable amount of time */
603       die_task = GNUNET_SCHEDULER_add_delayed (sched,
604                                                GNUNET_TIME_relative_multiply
605                                                (GNUNET_TIME_UNIT_MINUTES, 5),
606                                                &end_badly, "from peers_started_callback");
607
608       connect_topology ();
609       ok = 0;
610     }
611 }
612
613 /**
614  * Callback indicating that the hostkey was created for a peer.
615  *
616  * @param cls NULL
617  * @param id the peer identity
618  * @param d the daemon handle (pretty useless at this point, remove?)
619  * @param emsg non-null on failure
620  */
621 void hostkey_callback (void *cls,
622                        const struct GNUNET_PeerIdentity *id,
623                        struct GNUNET_TESTING_Daemon *d,
624                        const char *emsg)
625 {
626   if (emsg != NULL)
627     {
628       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Hostkey callback received error: %s\n", emsg);
629     }
630
631 #if VERBOSE
632     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
633                 "Hostkey created for peer `%s'\n",
634                 GNUNET_i2s(id));
635 #endif
636     peers_left--;
637     if (peers_left == 0)
638       {
639 #if VERBOSE
640         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641                     "All %d hostkeys created, now creating topology!\n",
642                     num_peers);
643 #endif
644         GNUNET_SCHEDULER_cancel (sched, die_task);
645         /* Set up task in case topology creation doesn't finish
646          * within a reasonable amount of time */
647         die_task = GNUNET_SCHEDULER_add_delayed (sched,
648                                                  GNUNET_TIME_relative_multiply
649                                                  (GNUNET_TIME_UNIT_MINUTES, 5),
650                                                  &end_badly, "from hostkey_callback");
651         GNUNET_SCHEDULER_add_now(sched, &create_topology, NULL);
652         ok = 0;
653       }
654 }
655
656 static void
657 run (void *cls,
658      struct GNUNET_SCHEDULER_Handle *s,
659      char *const *args,
660      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
661 {
662   char * topology_str;
663   char * connect_topology_str;
664   char * blacklist_topology_str;
665   char * connect_topology_option_str;
666   char * connect_topology_option_modifier_string;
667   sched = s;
668   ok = 1;
669
670   dotOutFile = fopen (dotOutFileName, "w");
671   if (dotOutFile != NULL)
672     {
673       fprintf (dotOutFile, "strict graph G {\n");
674     }
675
676 #if VERBOSE
677   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
678               "Starting daemons based on config file %s\n", cfgfile);
679 #endif
680
681   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string(cfg, "paths", "servicehome", &test_directory))
682     {
683       ok = 404;
684       return;
685     }
686
687   if ((GNUNET_YES ==
688       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "topology",
689                                             &topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&topology, topology_str)))
690     {
691       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
692                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "TOPOLOGY");
693       topology = GNUNET_TESTING_TOPOLOGY_CLIQUE; /* Defaults to NONE, so set better default here */
694     }
695
696   if ((GNUNET_YES ==
697       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology",
698                                             &connect_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&connection_topology, connect_topology_str)))
699     {
700       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
701                   "Invalid connect topology `%s' given for section %s option %s\n", connect_topology_str, "TESTING", "CONNECT_TOPOLOGY");
702     }
703   GNUNET_free_non_null(connect_topology_str);
704   if ((GNUNET_YES ==
705       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "connect_topology_option",
706                                             &connect_topology_option_str)) && (GNUNET_NO == GNUNET_TESTING_topology_option_get(&connect_topology_option, connect_topology_option_str)))
707     {
708       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
709                   "Invalid connect topology option `%s' given for section %s option %s\n", connect_topology_option_str, "TESTING", "CONNECT_TOPOLOGY_OPTION");
710       connect_topology_option = GNUNET_TESTING_TOPOLOGY_OPTION_ALL; /* Defaults to NONE, set to ALL */
711     }
712   GNUNET_free_non_null(connect_topology_option_str);
713   if (GNUNET_YES ==
714         GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "connect_topology_option_modifier",
715                                                &connect_topology_option_modifier_string))
716     {
717       if (sscanf(connect_topology_option_modifier_string, "%lf", &connect_topology_option_modifier) != 1)
718       {
719         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
720         _("Invalid value `%s' for option `%s' in section `%s': expected float\n"),
721         connect_topology_option_modifier_string,
722         "connect_topology_option_modifier",
723         "TESTING");
724       }
725       GNUNET_free (connect_topology_option_modifier_string);
726     }
727
728   if (GNUNET_YES != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "blacklist_transports",
729                                          &blacklist_transports))
730     blacklist_transports = NULL;
731
732   if ((GNUNET_YES ==
733       GNUNET_CONFIGURATION_get_value_string(cfg, "testing", "blacklist_topology",
734                                             &blacklist_topology_str)) && (GNUNET_NO == GNUNET_TESTING_topology_get(&blacklist_topology, blacklist_topology_str)))
735     {
736       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
737                   "Invalid topology `%s' given for section %s option %s\n", topology_str, "TESTING", "BLACKLIST_TOPOLOGY");
738     }
739   GNUNET_free_non_null(topology_str);
740   GNUNET_free_non_null(blacklist_topology_str);
741
742   if (GNUNET_SYSERR ==
743       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
744                                              &num_peers))
745     num_peers = DEFAULT_NUM_PEERS;
746
747   main_cfg = cfg;
748
749   peers_left = num_peers;
750
751   /* Set up a task to end testing if peer start fails */
752   die_task = GNUNET_SCHEDULER_add_delayed (sched,
753                                            GNUNET_TIME_relative_multiply
754                                            (GNUNET_TIME_UNIT_MINUTES, 5),
755                                            &end_badly, "didn't start all daemons in reasonable amount of time!!!");
756
757   pg = GNUNET_TESTING_daemons_start (sched, cfg,
758                                      peers_left, TIMEOUT, &hostkey_callback, NULL, &peers_started_callback, NULL,
759                                      &topology_callback, NULL, NULL);
760
761 }
762
763 static int
764 check ()
765 {
766   char *binary_name;
767   char *config_file_name;
768   GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
769   GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
770   int ret;
771   char *const argv[] = {binary_name,
772     "-c",
773     config_file_name,
774 #if VERBOSE
775     "-L", "DEBUG",
776 #endif
777     NULL
778   };
779   struct GNUNET_GETOPT_CommandLineOption options[] = {
780     GNUNET_GETOPT_OPTION_END
781   };
782   ret = GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
783                       argv, binary_name, "nohelp",
784                       options, &run, &ok);
785   if (ret != GNUNET_OK)
786     {
787       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "`test-testing-topology-%s': Failed with error code %d\n", topology_string, ret);
788     }
789   GNUNET_free(binary_name);
790   GNUNET_free(config_file_name);
791   return ok;
792 }
793
794 int
795 main (int argc, char *argv[])
796 {
797   int ret;
798   char *binary_start_pos;
799   char *our_binary_name;
800
801   binary_start_pos = rindex(argv[0], '/');
802   topology_string = strstr (binary_start_pos,
803                             "_topology");
804   GNUNET_assert (topology_string != NULL);
805   topology_string++;
806   topology_string = strstr (topology_string, "_");
807   GNUNET_assert (topology_string != NULL);
808   topology_string++;
809
810   GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
811   GNUNET_asprintf(&dotOutFileName, "topology_%s.dot", topology_string);
812
813   GNUNET_log_setup (our_binary_name,
814 #if VERBOSE
815                     "DEBUG",
816 #else
817                     "WARNING",
818 #endif
819                     NULL);
820   ret = check ();
821
822   /**
823    * Need to remove base directory, subdirectories taken care
824    * of by the testing framework.
825    */
826   if (GNUNET_DISK_directory_remove (test_directory) != GNUNET_OK)
827     {
828       GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to remove testing directory %s\n", test_directory);
829     }
830   GNUNET_free(our_binary_name);
831   return ret;
832 }
833
834 /* end of test_testing_topology.c */