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