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