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