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