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