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