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