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