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