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