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