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