minor testing changes
[oweals/gnunet.git] / src / testing / test_testing_topology.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 testing/test_testing_topology.c
22  * @brief base testcase for testing all the topologies provided
23  */
24 #include "platform.h"
25 #include "gnunet_testing_lib.h"
26 #include "gnunet_core_service.h"
27
28 #define VERBOSE GNUNET_NO
29
30 /**
31  * How long until we fail the whole testcase?
32  */
33 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
34
35 /**
36  * How long until we give up on connecting the peers?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
39
40 #define DEFAULT_NUM_PEERS 4
41
42 #define MAX_OUTSTANDING_CONNECTIONS 300
43
44 static float fail_percentage = 0.05;
45
46 static int ok;
47
48 static unsigned long long num_peers;
49
50 static unsigned int total_connections;
51
52 static unsigned int failed_connections;
53
54 static unsigned int total_server_connections;
55
56 static unsigned int total_messages_received;
57
58 static unsigned int expected_messages;
59
60 static unsigned int expected_connections;
61
62 static unsigned long long peers_left;
63
64 static struct GNUNET_TESTING_PeerGroup *pg;
65
66 static struct GNUNET_SCHEDULER_Handle *sched;
67
68 const struct GNUNET_CONFIGURATION_Handle *main_cfg;
69
70 GNUNET_SCHEDULER_TaskIdentifier die_task;
71
72 static char *dotOutFileName = "topology.dot";
73
74 static FILE *dotOutFile;
75
76 static char *topology_string;
77
78 static int transmit_ready_scheduled;
79
80 static int transmit_ready_failed;
81
82 static int transmit_ready_called;
83
84 static enum GNUNET_TESTING_Topology topology;
85
86 #define MTYPE 12345
87
88 struct GNUNET_TestMessage
89 {
90   /**
91    * Header of the message
92    */
93   struct GNUNET_MessageHeader header;
94
95   /**
96    * Unique identifier for this message.
97    */
98   uint32_t uid;
99 };
100
101 struct TestMessageContext
102 {
103   /* This is a linked list */
104   struct TestMessageContext *next;
105
106   /* Handle to the sending peer core */
107   struct GNUNET_CORE_Handle *peer1handle;
108
109   /* Handle to the receiving peer core */
110   struct GNUNET_CORE_Handle *peer2handle;
111
112   /* Handle to the sending peer daemon */
113   struct GNUNET_TESTING_Daemon *peer1;
114
115   /* Handle to the receiving peer daemon */
116   struct GNUNET_TESTING_Daemon *peer2;
117
118   /* Identifier for this message, so we don't disconnect other peers! */
119   uint32_t uid;
120
121   /* Task for disconnecting cores, allow task to be cancelled on shutdown */
122   GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
123
124 };
125
126 static struct TestMessageContext *test_messages;
127
128 static void
129 finish_testing ()
130 {
131   GNUNET_assert (pg != NULL);
132   struct TestMessageContext *pos;
133   struct TestMessageContext *free_pos;
134 #if VERBOSE
135   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
136               "Called finish testing, stopping daemons.\n");
137 #endif
138   int count;
139   count = 0;
140   pos = test_messages;
141   while (pos != NULL)
142     {
143       if (pos->peer1handle != NULL)
144         {
145           GNUNET_CORE_disconnect(pos->peer1handle);
146           pos->peer1handle = NULL;
147         }
148       if (pos->peer2handle != NULL)
149         {
150           GNUNET_CORE_disconnect(pos->peer2handle);
151           pos->peer2handle = NULL;
152         }
153       free_pos = pos;
154       pos = pos->next;
155       if (free_pos->disconnect_task != GNUNET_SCHEDULER_NO_TASK)
156         {
157           GNUNET_SCHEDULER_cancel(sched, free_pos->disconnect_task);
158         }
159       GNUNET_free(free_pos);
160     }
161 #if VERBOSE
162           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163                       "transmit_ready's scheduled %d, failed %d, transmit_ready's called %d\n", transmit_ready_scheduled, transmit_ready_failed, transmit_ready_called);
164 #endif
165   sleep(1);
166 #if VERBOSE
167           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168                       "Calling daemons_stop\n");
169 #endif
170   GNUNET_TESTING_daemons_stop (pg);
171 #if VERBOSE
172           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173                       "daemons_stop finished\n");
174 #endif
175   if (dotOutFile != NULL)
176     {
177       fprintf(dotOutFile, "}");
178       fclose(dotOutFile);
179     }
180
181   ok = 0;
182 }
183
184
185 static void
186 disconnect_cores (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
187 {
188   struct TestMessageContext *pos = cls;
189
190   /* Disconnect from the respective cores */
191 #if VERBOSE
192   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193               "Disconnecting from peer 1 `%4s'\n", GNUNET_i2s (&pos->peer1->id));
194 #endif
195   if (pos->peer1handle != NULL)
196     GNUNET_CORE_disconnect(pos->peer1handle);
197 #if VERBOSE
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "Disconnecting from peer 2 `%4s'\n", GNUNET_i2s (&pos->peer2->id));
200 #endif
201   if (pos->peer2handle != NULL)
202     GNUNET_CORE_disconnect(pos->peer2handle);
203   /* Set handles to NULL so test case can be ended properly */
204   pos->peer1handle = NULL;
205   pos->peer2handle = NULL;
206   pos->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
207   /* Decrement total connections so new can be established */
208   total_server_connections -= 2;
209 }
210
211 static int
212 process_mtype (void *cls,
213                const struct GNUNET_PeerIdentity *peer,
214                const struct GNUNET_MessageHeader *message,
215                struct GNUNET_TIME_Relative latency,
216                uint32_t distance)
217 {
218   struct TestMessageContext *pos = cls;
219   struct GNUNET_TestMessage *msg = (struct GNUNET_TestMessage *)message;
220   if (pos->uid != ntohl(msg->uid))
221     return GNUNET_OK;
222
223   total_messages_received++;
224 #if VERBOSE
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "Received message from `%4s', type %d.\n", GNUNET_i2s (peer), ntohs(message->type));
227   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228               "Total messages received %d, expected %d.\n", total_messages_received, expected_messages);
229 #endif
230
231   if (total_messages_received == expected_messages)
232     {
233       GNUNET_SCHEDULER_cancel (sched, die_task);
234       GNUNET_SCHEDULER_add_now (sched, &finish_testing, NULL);
235     }
236   else
237     {
238       pos->disconnect_task = GNUNET_SCHEDULER_add_now(sched, &disconnect_cores, pos);
239     }
240
241   return GNUNET_OK;
242 }
243
244 static void
245 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
246 {
247   char *msg = cls;
248   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
249               "End badly was called (%s)... stopping daemons.\n", msg);
250   struct TestMessageContext *pos;
251   struct TestMessageContext *free_pos;
252
253   pos = test_messages;
254   while (pos != NULL)
255     {
256       if (pos->peer1handle != NULL)
257         {
258           GNUNET_CORE_disconnect(pos->peer1handle);
259           pos->peer1handle = NULL;
260         }
261       if (pos->peer2handle != NULL)
262         {
263           GNUNET_CORE_disconnect(pos->peer2handle);
264           pos->peer2handle = NULL;
265         }
266       free_pos = pos;
267       pos = pos->next;
268       GNUNET_free(free_pos);
269     }
270
271   if (pg != NULL)
272     {
273       GNUNET_TESTING_daemons_stop (pg);
274       ok = 7331;                /* Opposite of leet */
275     }
276   else
277     ok = 401;                   /* Never got peers started */
278
279   if (dotOutFile != NULL)
280     {
281       fprintf(dotOutFile, "}");
282       fclose(dotOutFile);
283     }
284 }
285
286
287
288 static size_t
289 transmit_ready (void *cls, size_t size, void *buf)
290 {
291   struct GNUNET_TestMessage *m;
292   struct TestMessageContext *pos = cls;
293
294   GNUNET_assert (buf != NULL);
295   m = (struct GNUNET_TestMessage *) buf;
296   m->header.type = htons (MTYPE);
297   m->header.size = htons (sizeof (struct GNUNET_TestMessage));
298   m->uid = htonl(pos->uid);
299   transmit_ready_called++;
300 #if VERBOSE
301   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302               "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);
303 #endif
304   return sizeof (struct GNUNET_TestMessage);
305 }
306
307
308 static struct GNUNET_CORE_MessageHandler no_handlers[] = {
309   {NULL, 0, 0}
310 };
311
312 static struct GNUNET_CORE_MessageHandler handlers[] = {
313   {&process_mtype, MTYPE, sizeof (struct GNUNET_TestMessage)},
314   {NULL, 0, 0}
315 };
316
317 static void
318 init_notify_peer2 (void *cls,
319              struct GNUNET_CORE_Handle *server,
320              const struct GNUNET_PeerIdentity *my_identity,
321              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
322 {
323   struct TestMessageContext *pos = cls;
324
325 #if VERBOSE
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327               "Core connection to `%4s' established, scheduling message send\n",
328               GNUNET_i2s (my_identity));
329 #endif
330   total_server_connections++;
331
332   if (NULL == GNUNET_CORE_notify_transmit_ready (pos->peer1handle,
333                                                  0,
334                                                  TIMEOUT,
335                                                  &pos->peer2->id,
336                                                  sizeof (struct GNUNET_TestMessage),
337                                                  &transmit_ready, pos))
338     {
339       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340                   "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
341                   GNUNET_i2s (&pos->peer2->id));
342       transmit_ready_failed++;
343     }
344   else
345     {
346       transmit_ready_scheduled++;
347     }
348 }
349
350
351 static void
352 init_notify_peer1 (void *cls,
353              struct GNUNET_CORE_Handle *server,
354              const struct GNUNET_PeerIdentity *my_identity,
355              const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
356 {
357   struct TestMessageContext *pos = cls;
358   total_server_connections++;
359
360 #if VERBOSE
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362               "Core connection to `%4s' established, setting up handles\n",
363               GNUNET_i2s (my_identity));
364 #endif
365
366   /*
367    * Connect to the receiving peer
368    */
369   pos->peer2handle = GNUNET_CORE_connect (sched,
370                        pos->peer2->cfg,
371                        TIMEOUT,
372                        pos,
373                        &init_notify_peer2,
374                        NULL,
375                        NULL,
376                        NULL,
377                        GNUNET_YES, NULL, GNUNET_YES, handlers);
378
379 }
380
381
382 static void
383 send_test_messages (void *cls, const struct GNUNET_SCHEDULER_TaskContext * tc)
384 {
385   struct TestMessageContext *pos = cls;
386
387   if ((tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN) || (cls == NULL))
388     return;
389
390   if (die_task == GNUNET_SCHEDULER_NO_TASK)
391     {
392       die_task = GNUNET_SCHEDULER_add_delayed (sched,
393                                                TEST_TIMEOUT,
394                                                &end_badly, "from create topology (timeout)");
395     }
396
397   if (total_server_connections >= MAX_OUTSTANDING_CONNECTIONS)
398     {
399       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
400                                     &send_test_messages, pos);
401       return; /* Otherwise we'll double schedule messages here! */
402     }
403
404   /*
405    * Connect to the sending peer
406    */
407   pos->peer1handle = GNUNET_CORE_connect (sched,
408                                           pos->peer1->cfg,
409                                           TIMEOUT,
410                                           pos,
411                                           &init_notify_peer1,
412                                           NULL,
413                                           NULL,
414                                           NULL,
415                                           GNUNET_NO, NULL, GNUNET_NO, no_handlers);
416
417   GNUNET_assert(pos->peer1handle != NULL);
418
419   if (total_server_connections < MAX_OUTSTANDING_CONNECTIONS)
420     {
421       GNUNET_SCHEDULER_add_now (sched,
422                                 &send_test_messages, pos->next);
423     }
424   else
425     {
426       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
427                                     &send_test_messages, pos->next);
428     }
429 }
430
431
432 void
433 topology_callback (void *cls,
434                    const struct GNUNET_PeerIdentity *first,
435                    const struct GNUNET_PeerIdentity *second,
436                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
437                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
438                    struct GNUNET_TESTING_Daemon *first_daemon,
439                    struct GNUNET_TESTING_Daemon *second_daemon,
440                    const char *emsg)
441 {
442   struct TestMessageContext *temp_context;
443   if (emsg == NULL)
444     {
445       total_connections++;
446 #if VERBOSE
447       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connected peer %s to peer %s\n",
448                first_daemon->shortname,
449                second_daemon->shortname);
450 #endif
451       temp_context = GNUNET_malloc(sizeof(struct TestMessageContext));
452       temp_context->peer1 = first_daemon;
453       temp_context->peer2 = second_daemon;
454       temp_context->next = test_messages;
455       temp_context->uid = total_connections;
456       temp_context->disconnect_task = GNUNET_SCHEDULER_NO_TASK;
457       test_messages = temp_context;
458
459       expected_messages++;
460       if (dotOutFile != NULL)
461         fprintf(dotOutFile, "\tn%s -- n%s;\n", first_daemon->shortname, second_daemon->shortname);
462     }
463 #if VERBOSE
464   else
465     {
466       failed_connections++;
467       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to connect peer %s to peer %s with error :\n%s\n",
468                first_daemon->shortname,
469                second_daemon->shortname, emsg);
470     }
471 #endif
472
473   if (total_connections == expected_connections)
474     {
475 #if VERBOSE
476       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477                   "Created %d total connections, which is our target number!  Calling send messages.\n",
478                   total_connections);
479 #endif
480
481       GNUNET_SCHEDULER_cancel (sched, die_task);
482       die_task = GNUNET_SCHEDULER_NO_TASK;
483       GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
484     }
485   else if (total_connections + failed_connections == expected_connections)
486     {
487       if (failed_connections < (unsigned int)(fail_percentage * total_connections))
488         {
489           GNUNET_SCHEDULER_cancel (sched, die_task);
490           die_task = GNUNET_SCHEDULER_NO_TASK;
491           GNUNET_SCHEDULER_add_delayed (sched, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 1), &send_test_messages, test_messages);
492         }
493       else
494         {
495           GNUNET_SCHEDULER_cancel (sched, die_task);
496           die_task = GNUNET_SCHEDULER_add_now (sched,
497                                                &end_badly, "from topology_callback (too many failed connections)");
498         }
499     }
500   else
501     {
502 #if VERBOSE
503       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
504                   "Have %d total connections, %d failed connections, Want %d (at least %d)\n",
505                   total_connections, failed_connections, expected_connections, expected_connections - (unsigned int)(fail_percentage * expected_connections));
506 #endif
507     }
508 }
509
510
511 static void
512 create_topology ()
513 {
514   expected_connections = -1;
515   if ((pg != NULL) && (peers_left == 0))
516     {
517       /* create_topology will read the topology information from
518          the config already contained in the peer group, so should
519          we have create_topology called from start peers?  I think
520          maybe this way is best so that the client can know both
521          when peers are started, and when they are connected.
522        */
523       expected_connections = GNUNET_TESTING_create_topology (pg, topology);
524 #if VERBOSE
525       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526                   "Have %d expected connections\n", expected_connections);
527 #endif
528     }
529
530   GNUNET_SCHEDULER_cancel (sched, die_task);
531   if (expected_connections == GNUNET_SYSERR)
532     {
533       die_task = GNUNET_SCHEDULER_add_now (sched,
534                                            &end_badly, "from create topology (bad return)");
535     }
536   die_task = GNUNET_SCHEDULER_add_delayed (sched,
537                                            TEST_TIMEOUT,
538                                            &end_badly, "from create topology (timeout)");
539 }
540
541
542 static void
543 my_cb (void *cls,
544        const struct GNUNET_PeerIdentity *id,
545        const struct GNUNET_CONFIGURATION_Handle *cfg,
546        struct GNUNET_TESTING_Daemon *d, const char *emsg)
547 {
548   GNUNET_assert (id != NULL);
549 #if VERBOSE
550   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Started daemon %llu out of %llu\n",
551               (num_peers - peers_left) + 1, num_peers);
552 #endif
553   peers_left--;
554   if (peers_left == 0)
555     {
556 #if VERBOSE
557       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
558                   "All %d daemons started, now creating topology!\n",
559                   num_peers);
560 #endif
561       GNUNET_SCHEDULER_cancel (sched, die_task);
562       /* Set up task in case topology creation doesn't finish
563        * within a reasonable amount of time */
564       die_task = GNUNET_SCHEDULER_add_delayed (sched,
565                                                GNUNET_TIME_relative_multiply
566                                                (GNUNET_TIME_UNIT_MINUTES, 5),
567                                                &end_badly, "from my_cb");
568       create_topology ();
569       ok = 0;
570     }
571 }
572
573
574 static void
575 run (void *cls,
576      struct GNUNET_SCHEDULER_Handle *s,
577      char *const *args,
578      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
579 {
580   unsigned long long topology_num;
581   sched = s;
582   ok = 1;
583
584   dotOutFile = fopen (dotOutFileName, "w");
585   if (dotOutFile != NULL)
586     {
587       fprintf (dotOutFile, "strict graph G {\n");
588     }
589
590 #if VERBOSE
591   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592               "Starting daemons based on config file %s\n", cfgfile);
593 #endif
594
595   if (GNUNET_YES ==
596       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "topology",
597                                              &topology_num))
598     topology = topology_num;
599
600   if (GNUNET_SYSERR ==
601       GNUNET_CONFIGURATION_get_value_number (cfg, "testing", "num_peers",
602                                              &num_peers))
603     num_peers = DEFAULT_NUM_PEERS;
604
605   main_cfg = cfg;
606
607   peers_left = num_peers;
608
609   /* Set up a task to end testing if peer start fails */
610   die_task = GNUNET_SCHEDULER_add_delayed (sched,
611                                            GNUNET_TIME_relative_multiply
612                                            (GNUNET_TIME_UNIT_MINUTES, 5),
613                                            &end_badly, "didn't start all daemons in reasonable amount of time!!!");
614
615   pg = GNUNET_TESTING_daemons_start (sched, cfg,
616                                      peers_left, &my_cb, NULL,
617                                      &topology_callback, NULL, NULL);
618
619 }
620
621 static int
622 check ()
623 {
624   char *binary_name;
625   char *config_file_name;
626   GNUNET_asprintf(&binary_name, "test-testing-topology-%s", topology_string);
627   GNUNET_asprintf(&config_file_name, "test_testing_data_topology_%s.conf", topology_string);
628   char *const argv[] = {binary_name,
629     "-c",
630     config_file_name,
631 #if VERBOSE
632     "-L", "DEBUG",
633 #endif
634     NULL
635   };
636   struct GNUNET_GETOPT_CommandLineOption options[] = {
637     GNUNET_GETOPT_OPTION_END
638   };
639   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
640                       argv, binary_name, "nohelp",
641                       options, &run, &ok);
642   GNUNET_free(binary_name);
643   GNUNET_free(config_file_name);
644   return ok;
645 }
646
647 int
648 main (int argc, char *argv[])
649 {
650   int ret;
651   char *binary_start_pos;
652   char *our_binary_name;
653
654   binary_start_pos = rindex(argv[0], '/');
655   topology_string = strstr (binary_start_pos,
656                             "_topology");
657   GNUNET_assert (topology_string != NULL);
658   topology_string++;
659   topology_string = strstr (topology_string, "_");
660   GNUNET_assert (topology_string != NULL);
661   topology_string++;
662
663   GNUNET_asprintf(&our_binary_name, "test-testing-topology_%s", topology_string);
664   GNUNET_log_setup (our_binary_name,
665 #if VERBOSE
666                     "DEBUG",
667 #else
668                     "WARNING",
669 #endif
670                     NULL);
671   ret = check ();
672
673   /**
674    * Need to remove base directory, subdirectories taken care
675    * of by the testing framework.
676    */
677   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-testing");
678   GNUNET_free(our_binary_name);
679   return ret;
680 }
681
682 /* end of test_testing_group.c */