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