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