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