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