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