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