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