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