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