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