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