80ba1efc17a714f1c807368ab8bb632001490763
[oweals/gnunet.git] / src / mesh / test_mesh_small_unicast.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file mesh/test_mesh_small_unicast.c
22  *
23  * @brief Test for the mesh service: retransmission of unicast traffic.
24  */
25 #include "platform.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_mesh_service_new.h"
28
29 #define VERBOSE GNUNET_YES
30 #define REMOVE_DIR GNUNET_YES
31
32 struct MeshPeer
33 {
34   struct MeshPeer *prev;
35
36   struct MeshPeer *next;
37
38   struct GNUNET_TESTING_Daemon *daemon;
39
40   struct GNUNET_MESH_Handle *mesh_handle;
41 };
42
43
44 struct StatsContext
45 {
46   unsigned long long total_mesh_bytes;
47 };
48
49
50 // static struct MeshPeer *peer_head;
51 //
52 // static struct MeshPeer *peer_tail;
53
54 /**
55  * How long until we give up on connecting the peers?
56  */
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
58
59 static int ok;
60
61 /**
62  * Be verbose
63  */
64 static int verbose;
65
66 /**
67  * Total number of peers in the test.
68  */
69 static unsigned long long num_peers;
70
71 /**
72  * Global configuration file
73  */
74 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
75
76 /**
77  * Total number of currently running peers.
78  */
79 static unsigned long long peers_running;
80
81 /**
82  * Total number of connections in the whole network.
83  */
84 static unsigned int total_connections;
85
86 /**
87  * The currently running peer group.
88  */
89 static struct GNUNET_TESTING_PeerGroup *pg;
90
91 /**
92  * File to report results to.
93  */
94 static struct GNUNET_DISK_FileHandle *output_file;
95
96 /**
97  * File to log connection info, statistics to.
98  */
99 static struct GNUNET_DISK_FileHandle *data_file;
100
101 /**
102  * How many data points to capture before triggering next round?
103  */
104 static struct GNUNET_TIME_Relative wait_time;
105
106 /**
107  * Task called to disconnect peers.
108  */
109 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
110
111 /**
112  * Task To perform tests
113  */
114 static GNUNET_SCHEDULER_TaskIdentifier test_task;
115
116 /**
117  * Task called to shutdown test.
118  */
119 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
120
121 static char *topology_file;
122
123 static char *data_filename;
124
125 struct GNUNET_TESTING_Daemon *d1;
126
127 struct GNUNET_TESTING_Daemon *d2;
128
129 struct GNUNET_MESH_Handle *h1;
130
131 struct GNUNET_MESH_Handle *h2;
132
133 struct GNUNET_MESH_Tunnel *t;
134
135 /**
136  * Check whether peers successfully shut down.
137  */
138 static void
139 shutdown_callback (void *cls, const char *emsg)
140 {
141   if (emsg != NULL)
142   {
143 #if VERBOSE
144     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Shutdown of peers failed!\n");
145 #endif
146     ok++;
147   }
148   else
149   {
150 #if VERBOSE
151     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: All peers successfully shut down!\n");
152 #endif
153   }
154 }
155
156
157 static void
158 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {
160 #if VERBOSE
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
162 #endif
163
164   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
165   {
166     GNUNET_SCHEDULER_cancel (disconnect_task);
167     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
168   }
169
170   if (data_file != NULL)
171     GNUNET_DISK_file_close (data_file);
172   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
173   GNUNET_CONFIGURATION_destroy (testing_cfg);
174 }
175
176
177 /**
178  * Handlers, for diverse services
179  */
180 static struct GNUNET_MESH_MessageHandler handlers[] = {
181 //    {&callback, 1, 0},
182   {NULL, 0, 0}
183 };
184
185
186 static void
187 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 {
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190               "test: disconnecting mesh service of peers\n");
191   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
192   GNUNET_MESH_disconnect(h1);
193   GNUNET_MESH_disconnect(h2);
194   GNUNET_SCHEDULER_cancel (shutdown_handle);
195   shutdown_handle = GNUNET_SCHEDULER_add_now(&shutdown_task, NULL);
196 }
197
198
199 /**
200  * Method called whenever another peer has added us to a tunnel
201  * the other peer initiated.
202  *
203  * @param cls closure
204  * @param tunnel new handle to the tunnel
205  * @param initiator peer that started the tunnel
206  * @param atsi performance information for the tunnel
207  * @return initial tunnel context for the tunnel
208  *         (can be NULL -- that's not an error)
209  */
210 static void *
211 incoming_tunnel (void *cls,
212                  struct GNUNET_MESH_Tunnel * tunnel,
213                  const struct GNUNET_PeerIdentity * initiator,
214                  const struct GNUNET_TRANSPORT_ATS_Information * atsi)
215 {
216   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217               "test: Incoming tunnel from %s\n",
218               GNUNET_i2s(initiator));
219   GNUNET_SCHEDULER_cancel (disconnect_task);
220   disconnect_task = GNUNET_SCHEDULER_add_now(&disconnect_mesh_peers, NULL);
221   ok = 0;
222   return NULL;
223 }
224
225 /**
226  * Function called whenever an inbound tunnel is destroyed.  Should clean up
227  * any associated state.
228  *
229  * @param cls closure (set from GNUNET_MESH_connect)
230  * @param tunnel connection to the other end (henceforth invalid)
231  * @param tunnel_ctx place where local state associated
232  *                   with the tunnel is stored
233  */
234 static void
235 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
236                 void *tunnel_ctx)
237 {
238 #if VERBOSE
239   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: tunnel disconnected\n");
240 #endif
241   
242   return;
243 }
244
245 /**
246  * Method called whenever a tunnel falls apart.
247  *
248  * @param cls closure
249  * @param peer peer identity the tunnel stopped working with
250  */
251 static void
252 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
253 {
254   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255               "test: peer %s disconnected\n",
256               GNUNET_i2s(peer));
257   return;
258 }
259
260
261 /**
262  * Method called whenever a tunnel is established.
263  *
264  * @param cls closure
265  * @param peer peer identity the tunnel was created to, NULL on timeout
266  * @param atsi performance data for the connection
267  */
268 static void
269 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
270     const struct GNUNET_TRANSPORT_ATS_Information *atsi)
271 {
272   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273               "test: peer %s connected\n",
274               GNUNET_i2s(peer));
275   return;
276 }
277
278
279 static void
280 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
281 {
282   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: test_task\n");
283   GNUNET_MESH_peer_request_connect_add(t, &d2->id);
284   GNUNET_SCHEDULER_cancel (disconnect_task);
285   disconnect_task = GNUNET_SCHEDULER_add_delayed(
286           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
287                                &disconnect_mesh_peers, NULL);
288 }
289
290
291 /**
292  * connect_mesh_service: connect to the mesh service of one of the peers
293  *
294  */
295 static void
296 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
297 {
298   GNUNET_MESH_ApplicationType app;
299
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: connect_mesh_service\n");
301
302   d1 = GNUNET_TESTING_daemon_get (pg, 1);
303   d2 = GNUNET_TESTING_daemon_get (pg, 13);
304   app = (GNUNET_MESH_ApplicationType) 0;
305
306 #if VERBOSE
307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308               "test: connecting to mesh service of peer %s\n",
309               GNUNET_i2s (&d1->id));
310   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311               "test: connecting to mesh service of peer %s\n",
312               GNUNET_i2s (&d2->id));
313 #endif
314   h1 = GNUNET_MESH_connect (d1->cfg,
315                             10,
316                             NULL,
317                             NULL,
318                             &tunnel_cleaner,
319                             handlers,
320                             &app);
321   h2 = GNUNET_MESH_connect (d2->cfg,
322                             10,
323                             NULL,
324                             &incoming_tunnel,
325                             &tunnel_cleaner,
326                             handlers,
327                             &app);
328 #if VERBOSE
329   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330               "test: connected to mesh service of peer %s\n",
331               GNUNET_i2s (&d1->id));
332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333               "test: connected to mesh service of peer %s\n",
334               GNUNET_i2s (&d2->id));
335 #endif
336   t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, NULL);
337   test_task =
338       GNUNET_SCHEDULER_add_delayed(
339           GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 3),
340           &do_test, NULL);
341 }
342
343
344
345 /**
346  * peergroup_ready: start test when all peers are connected
347  * @param cls closure
348  * @param emsg error message
349  */
350 static void
351 peergroup_ready (void *cls, const char *emsg)
352 {
353   char *buf;
354   int buf_len;
355
356   if (emsg != NULL)
357   {
358     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359                 "test: Peergroup callback called with error, aborting test!\n");
360     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
361                 "test: Error from testing: `%s'\n", emsg);
362     ok++;
363     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
364     return;
365   }
366 #if VERBOSE
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368               "************************************************************\n");
369   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370               "test: Peer Group started successfully!\n");
371   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372               "test: Have %u connections\n",
373               total_connections);
374 #endif
375
376   if (data_file != NULL)
377   {
378     buf = NULL;
379     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
380     if (buf_len > 0)
381       GNUNET_DISK_file_write (data_file, buf, buf_len);
382     GNUNET_free (buf);
383   }
384   peers_running = GNUNET_TESTING_daemons_running (pg);
385
386   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
387                                     GNUNET_TIME_UNIT_SECONDS,
388                                     2),
389                                 &connect_mesh_service,
390                                 NULL);
391   disconnect_task =
392       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
393
394 }
395
396
397 /**
398  * Function that will be called whenever two daemons are connected by
399  * the testing library.
400  *
401  * @param cls closure
402  * @param first peer id for first daemon
403  * @param second peer id for the second daemon
404  * @param distance distance between the connected peers
405  * @param first_cfg config for the first daemon
406  * @param second_cfg config for the second daemon
407  * @param first_daemon handle for the first daemon
408  * @param second_daemon handle for the second daemon
409  * @param emsg error message (NULL on success)
410  */
411 static void
412 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
413             const struct GNUNET_PeerIdentity *second, uint32_t distance,
414             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
415             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
416             struct GNUNET_TESTING_Daemon *first_daemon,
417             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
418 {
419 //   GNUNET_PEER_Id f;
420 //   GNUNET_PEER_Id s;
421
422   if (emsg == NULL)
423   {
424     total_connections++;
425     /*f = */GNUNET_PEER_intern(first);
426     /*s = */GNUNET_PEER_intern(second);
427 //   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: New connection!\n");
428 //   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 //               "test:     %s (%u)\n",
430 //               GNUNET_h2s(&first->hashPubKey),
431 //               f);
432 //   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433 //               "test:     %s (%u)\n",
434 //               GNUNET_h2s(&second->hashPubKey),
435 //               s);
436   }
437   else
438   {
439     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440                 "test: Problem with new connection (%s)\n",
441                 emsg);
442   }
443
444 }
445
446
447 /**
448  * run: load configuration options and schedule test to run (start peergroup)
449  * @param cls closure
450  * @param args argv
451  * @param cfgfile configuration file name (can be NULL)
452  * @param cfg configuration handle
453  */
454 static void
455 run (void *cls, char *const *args, const char *cfgfile,
456      const struct GNUNET_CONFIGURATION_Handle *cfg)
457 {
458   char *temp_str;
459   unsigned long long temp_wait;
460   struct GNUNET_TESTING_Host *hosts;
461
462   ok = 1;
463   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
464
465   GNUNET_log_setup ("test_mesh_small_unicast",
466 #if VERBOSE
467                     "DEBUG",
468 #else
469                     "WARNING",
470 #endif
471                     NULL);
472
473 #if VERBOSE
474   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
475   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing",
476                                          "use_progressbars", "YES");
477 #endif
478
479   if (GNUNET_OK !=
480       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
481                                              "num_peers", &num_peers))
482   {
483     GNUNET_assert (GNUNET_OK ==
484                    GNUNET_CONFIGURATION_load (testing_cfg,
485                                               "test_mesh_small.conf"));
486     if (GNUNET_OK !=
487         GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing",
488                                                "num_peers", &num_peers))
489     {
490       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491                   "Option TESTING:NUM_PEERS is required!\n");
492       return;
493     }
494   }
495
496   if (GNUNET_OK !=
497       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "test_mesh_small",
498                                              "wait_time", &temp_wait))
499   {
500     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
501                 "Option test_mesh_small:wait_time is required!\n");
502     return;
503   }
504
505   if (GNUNET_OK !=
506       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing",
507                                              "topology_output_file",
508                                              &topology_file))
509   {
510     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
511                 "Option test_mesh_small:topology_output_file is required!\n");
512     return;
513   }
514
515   if (GNUNET_OK !=
516       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
517                                              "data_output_file",
518                                              &data_filename))
519   {
520     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521                 "Option test_mesh_small:data_output_file is required!\n");
522     return;
523   }
524
525   data_file =
526       GNUNET_DISK_file_open (data_filename,
527                              GNUNET_DISK_OPEN_READWRITE |
528                              GNUNET_DISK_OPEN_CREATE,
529                              GNUNET_DISK_PERM_USER_READ |
530                              GNUNET_DISK_PERM_USER_WRITE);
531   if (data_file == NULL)
532   {
533     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
534                 data_filename);
535     GNUNET_free (data_filename);
536   }
537
538   wait_time =
539       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, temp_wait);
540
541   if (GNUNET_YES ==
542       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
543                                              "output_file", &temp_str))
544   {
545     output_file =
546         GNUNET_DISK_file_open (temp_str,
547                                GNUNET_DISK_OPEN_READWRITE |
548                                GNUNET_DISK_OPEN_CREATE,
549                                GNUNET_DISK_PERM_USER_READ |
550                                GNUNET_DISK_PERM_USER_WRITE);
551     if (output_file == NULL)
552       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
553                   temp_str);
554   }
555   GNUNET_free_non_null (temp_str);
556
557   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
558
559   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
560                                        &connect_cb, &peergroup_ready, NULL,
561                                        hosts);
562   GNUNET_assert (pg != NULL);
563   shutdown_handle =
564       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever (),
565                                     &shutdown_task, NULL);
566 }
567
568
569
570 /**
571  * test_mesh_small command line options
572  */
573 static struct GNUNET_GETOPT_CommandLineOption options[] = {
574   {'V', "verbose", NULL,
575    gettext_noop ("be verbose (print progress information)"),
576    0, &GNUNET_GETOPT_set_one, &verbose},
577   GNUNET_GETOPT_OPTION_END
578 };
579
580
581 /**
582  * Main: start test
583  */
584 int
585 main (int argc, char *argv[])
586 {
587   GNUNET_PROGRAM_run (argc, argv, "test_mesh_small_unicast",
588                       gettext_noop ("Test mesh unicast in a small network."), options,
589                       &run, NULL);
590 #if REMOVE_DIR
591   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small_unicast");
592 #endif
593   if (0 != ok)
594   {
595     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
596   }
597   return ok;
598 }
599
600 /* end of test_mesh_small_unicast.c */