- Makefile changes to compile new tests for leaf to root speed
[oweals/gnunet.git] / src / mesh / test_mesh_small.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.c
22  *
23  * @brief Test for the mesh service: retransmission of traffic.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_mesh_service.h"
29 #include <gauger.h>
30
31
32 #define VERBOSE GNUNET_YES
33 #define REMOVE_DIR GNUNET_YES
34
35 struct MeshPeer
36 {
37   struct MeshPeer *prev;
38
39   struct MeshPeer *next;
40
41   struct GNUNET_TESTING_Daemon *daemon;
42
43   struct GNUNET_MESH_Handle *mesh_handle;
44 };
45
46
47 /**
48  * How long until we give up on connecting the peers?
49  */
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
51
52 /**
53  * Time to wait for stuff that should be rather fast
54  */
55 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
56
57 /**
58  * DIFFERENT TESTS TO RUN
59  */
60 #define SETUP 0
61 #define UNICAST 1
62 #define MULTICAST 2
63 #define SPEED 3
64 #define SPEED_ACK 4
65 #define SPEED_MIN 5
66 #define SPEED_NOBUF 6
67
68 /**
69  * Which test are we running?
70  */
71 static int test;
72
73 /**
74  * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
75  */
76 static int test_backwards = GNUNET_NO;
77
78 /**
79  * How many events have happened
80  */
81 static int ok;
82
83 /**
84  * Peers that have been connected
85  */
86 static int peers_in_tunnel;
87
88 /**
89  * Peers that have responded
90  */
91 static int peers_responded;
92
93 /**
94  * Number of payload packes sent
95  */
96 static int data_sent;
97
98 /**
99  * Number of payload packets received
100  */
101 static int data_received;
102
103 /**
104  * Number of payload packed explicitly (app/level) acknowledged
105  */
106 static int data_ack;
107
108 /**
109  * Be verbose
110  */
111 static int verbose;
112
113 /**
114  * Total number of peers in the test.
115  */
116 static unsigned long long num_peers;
117
118 /**
119  * Global configuration file
120  */
121 static struct GNUNET_CONFIGURATION_Handle *testing_cfg;
122
123 /**
124  * Total number of currently running peers.
125  */
126 static unsigned long long peers_running;
127
128 /**
129  * Total number of connections in the whole network.
130  */
131 static unsigned int total_connections;
132
133 /**
134  * The currently running peer group.
135  */
136 static struct GNUNET_TESTING_PeerGroup *pg;
137
138 /**
139  * File to report results to.
140  */
141 static struct GNUNET_DISK_FileHandle *output_file;
142
143 /**
144  * File to log connection info, statistics to.
145  */
146 static struct GNUNET_DISK_FileHandle *data_file;
147
148 /**
149  * Wait time
150  */
151 static struct GNUNET_TIME_Relative wait_time;
152
153 /**
154  * Task called to disconnect peers.
155  */
156 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
157
158 /**
159  * Task To perform tests
160  */
161 static GNUNET_SCHEDULER_TaskIdentifier test_task;
162
163 /**
164  * Task called to shutdown test.
165  */
166 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
167
168 static char *topology_file;
169
170 static struct GNUNET_TESTING_Daemon *d1;
171
172 static GNUNET_PEER_Id pid1;
173
174 static struct GNUNET_TESTING_Daemon *d2;
175
176 static struct GNUNET_TESTING_Daemon *d3;
177
178 static struct GNUNET_MESH_Handle *h1;
179
180 static struct GNUNET_MESH_Handle *h2;
181
182 static struct GNUNET_MESH_Handle *h3;
183
184 static struct GNUNET_MESH_Tunnel *t;
185
186 static struct GNUNET_MESH_Tunnel *incoming_t;
187
188 static struct GNUNET_MESH_Tunnel *incoming_t2;
189
190 static struct GNUNET_TIME_Absolute start_time;
191
192 static struct GNUNET_TIME_Absolute end_time;
193
194 static struct GNUNET_TIME_Relative total_time;
195
196
197 /**
198  * Check whether peers successfully shut down.
199  */
200 static void
201 shutdown_callback (void *cls, const char *emsg)
202 {
203   if (emsg != NULL)
204   {
205 #if VERBOSE
206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207                 "Shutdown of peers failed!\n");
208 #endif
209     ok--;
210   }
211   else
212   {
213 #if VERBOSE
214     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215                 "All peers successfully shut down!\n");
216 #endif
217   }
218   GNUNET_CONFIGURATION_destroy (testing_cfg);
219 }
220
221
222 /**
223  * Shut down peergroup, clean up.
224  */
225 static void
226 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
227 {
228 #if VERBOSE
229   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
230               "Ending test.\n");
231 #endif
232
233   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
234   {
235     GNUNET_SCHEDULER_cancel (disconnect_task);
236     disconnect_task = GNUNET_SCHEDULER_NO_TASK;
237   }
238
239   if (NULL != h1)
240   {
241     GNUNET_MESH_disconnect (h1);
242     h1 = NULL;
243   }
244   if (NULL != h2)
245   {
246     GNUNET_MESH_disconnect (h2);
247     h2 = NULL;
248   }
249   if (test == MULTICAST && NULL != h3)
250   {
251     GNUNET_MESH_disconnect (h3);
252     h3 = NULL;
253   }
254   
255   if (data_file != NULL)
256     GNUNET_DISK_file_close (data_file);
257   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
258 }
259
260
261 /**
262  * Disconnect from mesh services af all peers, call shutdown.
263  */
264 static void
265 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
266 {
267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268               "disconnecting mesh service of peers\n");
269   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
270   if (NULL != t)
271   {
272     GNUNET_MESH_tunnel_destroy(t);
273     t = NULL;
274   }
275   if (NULL != incoming_t)
276   {
277     GNUNET_MESH_tunnel_destroy(incoming_t);
278     incoming_t = NULL;
279   }
280   if (NULL != incoming_t2)
281   {
282     GNUNET_MESH_tunnel_destroy(incoming_t2);
283     incoming_t2 = NULL;
284   }
285   GNUNET_MESH_disconnect (h1);
286   GNUNET_MESH_disconnect (h2);
287   h1 = h2 = NULL;
288   if (test == MULTICAST)
289   {
290     GNUNET_MESH_disconnect (h3);
291     h3 = NULL;
292   }
293   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
294   {
295     GNUNET_SCHEDULER_cancel (shutdown_handle);
296     shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
297   }
298 }
299
300 static size_t
301 tmt_rdy (void *cls, size_t size, void *buf);
302
303 static void
304 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
305 {
306   struct GNUNET_MESH_TransmitHandle *th;
307   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
308     return;
309   th = GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
310                                     GNUNET_TIME_UNIT_FOREVER_REL, &d2->id,
311                                     sizeof (struct GNUNET_MessageHeader),
312                                     &tmt_rdy, (void *) 1L);
313   if (NULL == th)
314   {
315     unsigned long i = (unsigned long) cls;
316
317     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
318     if (0 == i)
319     {
320       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  in 1 ms\n");
321       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
322                                     &data_task, (void *)1UL);
323     }
324     else
325     {
326       i++;
327       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
328       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
329                                       GNUNET_TIME_UNIT_MILLISECONDS,
330                                       i),
331                                     &data_task, (void *)i);
332     }
333   }
334 }
335
336 /**
337  * Transmit ready callback
338  *
339  * @param cls Closure.
340  * @param size Size of the buffer we have.
341  * @param buf Buffer to copy data to.
342  */
343 size_t
344 tmt_rdy (void *cls, size_t size, void *buf)
345 {
346   struct GNUNET_MessageHeader *msg = buf;
347
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               " tmt_rdy called\n");
350   if (size < sizeof (struct GNUNET_MessageHeader) || NULL == buf)
351     return 0;
352   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
353   msg->type = htons ((long) cls);
354   if (test == SPEED)
355   {
356     data_sent++;
357     if (data_sent < 1000)
358     {
359       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360               " Scheduling %d packet\n", data_sent);
361       GNUNET_SCHEDULER_add_now(&data_task, NULL);
362     }
363   }
364   return sizeof (struct GNUNET_MessageHeader);
365 }
366
367
368 /**
369  * Function is called whenever a message is received.
370  *
371  * @param cls closure (set from GNUNET_MESH_connect)
372  * @param tunnel connection to the other end
373  * @param tunnel_ctx place to store local state associated with the tunnel
374  * @param sender who sent the message
375  * @param message the actual message
376  * @param atsi performance data for the connection
377  * @return GNUNET_OK to keep the connection open,
378  *         GNUNET_SYSERR to close it (signal serious error)
379  */
380 int
381 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
382                const struct GNUNET_PeerIdentity *sender,
383                const struct GNUNET_MessageHeader *message,
384                const struct GNUNET_ATS_Information *atsi)
385 {
386   long client = (long) cls;
387
388   switch (client)
389   {
390   case 1L:
391     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Origin client got a response!\n");
392     ok++;
393     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
394     peers_responded++;
395     data_ack++;
396     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
397     {
398       GNUNET_SCHEDULER_cancel (disconnect_task);
399       disconnect_task =
400           GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
401                                         NULL);
402     }
403     if (test == MULTICAST && peers_responded < 2)
404       return GNUNET_OK;
405     if (test == SPEED_ACK || test == SPEED)
406     {
407       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
408               " received ack %u\n", data_ack);
409       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
410                                         GNUNET_TIME_UNIT_FOREVER_REL, sender,
411                                         sizeof (struct GNUNET_MessageHeader),
412                                         &tmt_rdy, (void *) 1L);
413       if (data_ack < 1000 && test != SPEED)
414         return GNUNET_OK;
415       end_time = GNUNET_TIME_absolute_get();
416       total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
417       FPRINTF (stderr, "\nTest time %llu ms\n",
418                (unsigned long long) total_time.rel_value);
419       FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
420                4 * 1000.0 / total_time.rel_value); // 4bytes * ms
421       FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
422                1000.0 * 1000.0 / total_time.rel_value); // 1000 packets * ms
423       GAUGER ("MESH", "Tunnel 5 peers", 1000.0 * 1000.0 / total_time.rel_value,
424               "packets/s");
425     }
426     GNUNET_assert (tunnel == t);
427     GNUNET_MESH_tunnel_destroy (t);
428     t = NULL;
429     break;
430   case 2L:
431   case 3L:
432     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433                 "Destination client %u got a message.\n",
434                 client);
435     ok++;
436     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
437     if (SPEED != test || 1002 == ok)
438     {
439       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
440                                         GNUNET_TIME_UNIT_FOREVER_REL, sender,
441                                         sizeof (struct GNUNET_MessageHeader),
442                                         &tmt_rdy, (void *) 1L);
443     }
444     else
445     {
446       data_received++;
447       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
448               " received data %u\n", data_received);
449       if (data_received < 1000)
450         return GNUNET_OK;
451     }
452     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
453     {
454       GNUNET_SCHEDULER_cancel (disconnect_task);
455       disconnect_task =
456           GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
457                                         NULL);
458     }
459     break;
460   default:
461     break;
462   }
463   return GNUNET_OK;
464 }
465
466
467 /**
468  * Handlers, for diverse services
469  */
470 static struct GNUNET_MESH_MessageHandler handlers[] = {
471   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
472   {NULL, 0, 0}
473 };
474
475
476 /**
477  * Method called whenever another peer has added us to a tunnel
478  * the other peer initiated.
479  *
480  * @param cls closure
481  * @param tunnel new handle to the tunnel
482  * @param initiator peer that started the tunnel
483  * @param atsi performance information for the tunnel
484  * @return initial tunnel context for the tunnel
485  *         (can be NULL -- that's not an error)
486  */
487 static void *
488 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
489                  const struct GNUNET_PeerIdentity *initiator,
490                  const struct GNUNET_ATS_Information *atsi)
491 {
492   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
493               "Incoming tunnel from %s to peer %d\n",
494               GNUNET_i2s (initiator), (long) cls);
495   ok++;
496   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
497   if ((long) cls == 2L)
498     incoming_t = tunnel;
499   else if ((long) cls == 3L)
500     incoming_t2 = tunnel;
501   else
502   {
503     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
504                 "Incoming tunnel for unknown client %lu\n", (long) cls);
505   }
506   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
507   {
508     GNUNET_SCHEDULER_cancel (disconnect_task);
509     disconnect_task =
510         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
511   }
512   return NULL;
513 }
514
515 /**
516  * Function called whenever an inbound tunnel is destroyed.  Should clean up
517  * any associated state.
518  *
519  * @param cls closure (set from GNUNET_MESH_connect)
520  * @param tunnel connection to the other end (henceforth invalid)
521  * @param tunnel_ctx place where local state associated
522  *                   with the tunnel is stored
523  */
524 static void
525 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
526                 void *tunnel_ctx)
527 {
528   long i = (long) cls;
529
530   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
531               "Incoming tunnel disconnected at peer %d\n",
532               i);
533   if (2L == i)
534   {
535     ok++;
536     incoming_t = NULL;
537   }
538   else if (3L == i)
539   {
540     ok++;
541     incoming_t2 = NULL;
542   }
543   else
544     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
545                 "Unknown peer! %d\n", i);
546   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
547   peers_in_tunnel--;
548   if (peers_in_tunnel > 0)
549     return;
550
551   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
552   {
553     GNUNET_SCHEDULER_cancel (disconnect_task);
554     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers, NULL);
555   }
556
557   return;
558 }
559
560
561 /**
562  * Method called whenever a tunnel falls apart.
563  *
564  * @param cls closure
565  * @param peer peer identity the tunnel stopped working with
566  */
567 static void
568 dh (void *cls, const struct GNUNET_PeerIdentity *peer)
569 {
570   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571               "peer %s disconnected\n",
572               GNUNET_i2s (peer));
573   return;
574 }
575
576
577 /**
578  * Method called whenever a peer connects to a tunnel.
579  *
580  * @param cls closure
581  * @param peer peer identity the tunnel was created to, NULL on timeout
582  * @param atsi performance data for the connection
583  */
584 static void
585 ch (void *cls, const struct GNUNET_PeerIdentity *peer,
586     const struct GNUNET_ATS_Information *atsi)
587 {
588   struct GNUNET_PeerIdentity *dest;
589
590   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
591               "peer %s connected\n", GNUNET_i2s (peer));
592
593   if (0 == memcmp (&d2->id, peer, sizeof (d2->id)) && (long) cls == 1L)
594   {
595     ok++;
596   }
597   if (test == MULTICAST && 0 == memcmp (&d3->id, peer, sizeof (d3->id)) &&
598       (long) cls == 1L)
599   {
600     ok++;
601   }
602   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
603   switch (test)
604   {
605   case UNICAST:
606   case SPEED:
607   case SPEED_ACK:
608     dest = &d2->id;
609     break;
610   case MULTICAST:
611     peers_in_tunnel++;
612     if (peers_in_tunnel < 2)
613       return;
614     dest = NULL;
615     break;
616   default:
617     return;
618   }
619   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
620   {
621     GNUNET_SCHEDULER_cancel (disconnect_task);
622     disconnect_task =
623         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
624     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
625                 "Sending data...\n");
626     peers_responded = 0;
627     data_ack = 0;
628     data_received = 0;
629     data_sent = 0;
630     start_time = GNUNET_TIME_absolute_get();
631     GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
632                                        GNUNET_TIME_UNIT_FOREVER_REL, dest,
633                                        sizeof (struct GNUNET_MessageHeader),
634                                        &tmt_rdy, (void *) 1L);
635   }
636   else
637   {
638     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639                 "Disconnect already run?\n");
640     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641                 "Aborting...\n");
642   }
643   return;
644 }
645
646
647 static void
648 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
649 {
650   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
651   if (test == MULTICAST)
652   {
653     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654                 "add peer 3\n");
655     GNUNET_MESH_peer_request_connect_add (t, &d3->id);
656   }
657   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
658   GNUNET_MESH_peer_request_connect_add (t, &d2->id);
659   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
660               "schedule timeout in SHORT_TIME\n");
661   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
662   {
663     GNUNET_SCHEDULER_cancel (disconnect_task);
664     disconnect_task =
665         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers, NULL);
666   }
667 }
668
669
670 /**
671  * connect_mesh_service: connect to the mesh service of one of the peers
672  *
673  */
674 static void
675 connect_mesh_service (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
676 {
677   GNUNET_MESH_ApplicationType app;
678
679   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
680               "connect_mesh_service\n");
681
682   d2 = GNUNET_TESTING_daemon_get (pg, 4);
683   if (test == MULTICAST)
684   {
685     d3 = GNUNET_TESTING_daemon_get (pg, 3);
686   }
687   app = (GNUNET_MESH_ApplicationType) 0;
688
689 #if VERBOSE
690   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691               "connecting to mesh service of peer %s\n",
692               GNUNET_i2s (&d1->id));
693   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
694               "connecting to mesh service of peer %s\n",
695               GNUNET_i2s (&d2->id));
696   if (test == MULTICAST)
697   {
698     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699                 "connecting to mesh service of peer %s\n",
700                 GNUNET_i2s (&d3->id));
701   }
702 #endif
703   h1 = GNUNET_MESH_connect (d1->cfg, (void *) 1L, NULL, &tunnel_cleaner,
704                             handlers, &app);
705   h2 = GNUNET_MESH_connect (d2->cfg, (void *) 2L, &incoming_tunnel,
706                             &tunnel_cleaner, handlers, &app);
707   if (test == MULTICAST)
708   {
709     h3 = GNUNET_MESH_connect (d3->cfg, (void *) 3L, &incoming_tunnel,
710                               &tunnel_cleaner, handlers, &app);
711   }
712   t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
713   if (SPEED_MIN == test)
714   {
715     GNUNET_MESH_tunnel_speed_min(t);
716     test = SPEED;
717   }
718   if (SPEED_NOBUF == test)
719   {
720     GNUNET_MESH_tunnel_buffer(t, GNUNET_NO);
721     test = SPEED;
722   }
723   peers_in_tunnel = 0;
724   test_task =
725       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
726                                     (GNUNET_TIME_UNIT_SECONDS, 1), &do_test,
727                                     NULL);
728 }
729
730
731
732 /**
733  * peergroup_ready: start test when all peers are connected
734  * @param cls closure
735  * @param emsg error message
736  */
737 static void
738 peergroup_ready (void *cls, const char *emsg)
739 {
740   char *buf;
741   int buf_len;
742   unsigned int i;
743
744   if (emsg != NULL)
745   {
746     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
747                 "Peergroup callback called with error, aborting test!\n");
748     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
749                 "Error from testing: `%s'\n", emsg);
750     ok--;
751     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
752     return;
753   }
754 #if VERBOSE
755   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
756               "************************************************************\n");
757   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758               "Peer Group started successfully!\n");
759   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760               "Have %u connections\n",
761               total_connections);
762 #endif
763
764   if (data_file != NULL)
765   {
766     buf = NULL;
767     buf_len = GNUNET_asprintf (&buf, "CONNECTIONS_0: %u\n", total_connections);
768     if (buf_len > 0)
769       GNUNET_DISK_file_write (data_file, buf, buf_len);
770     GNUNET_free (buf);
771   }
772   peers_running = GNUNET_TESTING_daemons_running (pg);
773   for (i = 0; i < num_peers; i++)
774   {
775     GNUNET_PEER_Id peer_id;
776
777     d1 = GNUNET_TESTING_daemon_get (pg, i);
778     peer_id = GNUNET_PEER_intern (&d1->id);
779     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u: %s\n",
780                 peer_id, GNUNET_i2s (&d1->id));
781   }
782   d1 = GNUNET_TESTING_daemon_get (pg, 0);
783   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784               "Peer looking: %s\n",
785               GNUNET_i2s (&d1->id));
786   pid1 = GNUNET_PEER_intern (&d1->id);
787
788   GNUNET_SCHEDULER_add_now (&connect_mesh_service, NULL);
789   disconnect_task =
790       GNUNET_SCHEDULER_add_delayed (wait_time, &disconnect_mesh_peers, NULL);
791
792 }
793
794
795 /**
796  * Function that will be called whenever two daemons are connected by
797  * the testing library.
798  *
799  * @param cls closure
800  * @param first peer id for first daemon
801  * @param second peer id for the second daemon
802  * @param distance distance between the connected peers
803  * @param first_cfg config for the first daemon
804  * @param second_cfg config for the second daemon
805  * @param first_daemon handle for the first daemon
806  * @param second_daemon handle for the second daemon
807  * @param emsg error message (NULL on success)
808  */
809 static void
810 connect_cb (void *cls, const struct GNUNET_PeerIdentity *first,
811             const struct GNUNET_PeerIdentity *second, uint32_t distance,
812             const struct GNUNET_CONFIGURATION_Handle *first_cfg,
813             const struct GNUNET_CONFIGURATION_Handle *second_cfg,
814             struct GNUNET_TESTING_Daemon *first_daemon,
815             struct GNUNET_TESTING_Daemon *second_daemon, const char *emsg)
816 {
817   if (emsg == NULL)
818   {
819     total_connections++;
820   }
821   else
822   {
823     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
824                 "Problem with new connection (%s)\n",
825                 emsg);
826     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (first));
827     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (%s)\n", GNUNET_i2s (second));
828   }
829
830 }
831
832
833 /**
834  * run: load configuration options and schedule test to run (start peergroup)
835  * @param cls closure
836  * @param args argv
837  * @param cfgfile configuration file name (can be NULL)
838  * @param cfg configuration handle
839  */
840 static void
841 run (void *cls, char *const *args, const char *cfgfile,
842      const struct GNUNET_CONFIGURATION_Handle *cfg)
843 {
844   char *temp_str;
845   struct GNUNET_TESTING_Host *hosts;
846   char *data_filename;
847
848   ok = 0;
849   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
850
851   GNUNET_log_setup ("test_mesh_small",
852 #if VERBOSE
853                     "DEBUG",
854 #else
855                     "WARNING",
856 #endif
857                     NULL);
858
859 #if VERBOSE
860   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861               "Starting daemons.\n");
862   GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
863                                          "use_progressbars", "YES");
864 #endif
865
866   if (GNUNET_OK !=
867       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
868                                              "num_peers", &num_peers))
869   {
870     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
871                 "Option TESTING:NUM_PEERS is required!\n");
872     return;
873   }
874
875   if (GNUNET_OK !=
876       GNUNET_CONFIGURATION_get_value_time (testing_cfg, "test_mesh_small",
877                                            "WAIT_TIME", &wait_time))
878   {
879     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
880                 "Option test_mesh_small:wait_time is required!\n");
881     return;
882   }
883
884   if (GNUNET_OK !=
885       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "testing_old",
886                                              "topology_output_file",
887                                              &topology_file))
888   {
889     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
890                 "Option test_mesh_small:topology_output_file is required!\n");
891     return;
892   }
893
894   if (GNUNET_OK ==
895       GNUNET_CONFIGURATION_get_value_string (testing_cfg, "test_mesh_small",
896                                              "data_output_file",
897                                              &data_filename))
898   {
899     data_file =
900         GNUNET_DISK_file_open (data_filename,
901                                GNUNET_DISK_OPEN_READWRITE |
902                                GNUNET_DISK_OPEN_CREATE,
903                                GNUNET_DISK_PERM_USER_READ |
904                                GNUNET_DISK_PERM_USER_WRITE);
905     if (data_file == NULL)
906     {
907       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
908                   data_filename);
909       GNUNET_free (data_filename);
910     }
911   }
912
913   if (GNUNET_YES ==
914       GNUNET_CONFIGURATION_get_value_string (cfg, "test_mesh_small",
915                                              "output_file", &temp_str))
916   {
917     output_file =
918         GNUNET_DISK_file_open (temp_str,
919                                GNUNET_DISK_OPEN_READWRITE |
920                                GNUNET_DISK_OPEN_CREATE,
921                                GNUNET_DISK_PERM_USER_READ |
922                                GNUNET_DISK_PERM_USER_WRITE);
923     if (output_file == NULL)
924       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to open %s for output!\n",
925                   temp_str);
926   }
927   GNUNET_free_non_null (temp_str);
928
929   hosts = GNUNET_TESTING_hosts_load (testing_cfg);
930
931   pg = GNUNET_TESTING_peergroup_start (testing_cfg, num_peers, TIMEOUT,
932                                        &connect_cb, &peergroup_ready, NULL,
933                                        hosts);
934   GNUNET_assert (pg != NULL);
935   shutdown_handle =
936     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
937                                     &shutdown_task, NULL);
938 }
939
940
941
942 /**
943  * test_mesh_small command line options
944  */
945 static struct GNUNET_GETOPT_CommandLineOption options[] = {
946   {'V', "verbose", NULL,
947    gettext_noop ("be verbose (print progress information)"),
948    0, &GNUNET_GETOPT_set_one, &verbose},
949   GNUNET_GETOPT_OPTION_END
950 };
951
952
953 /**
954  * Main: start test
955  */
956 int
957 main (int argc, char *argv[])
958 {
959   char * argv2[] = {
960     argv[0],
961     "-c",
962     "test_mesh_small.conf",
963 #if VERBOSE
964     "-L",
965     "DEBUG",
966 #endif
967     NULL
968   };
969   int argc2 = (sizeof (argv2) / sizeof (char *)) - 1;
970
971   /* Each peer is supposed to generate the following callbacks:
972    * 1 incoming tunnel (@dest)
973    * 1 connected peer (@orig)
974    * 1 received data packet (@dest)
975    * 1 received data packet (@orig)
976    * 1 received tunnel destroy (@dest)
977    * _________________________________
978    * 5 x ok expected per peer
979    */
980   int ok_goal;
981
982   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
983   if (strstr (argv[0], "test_mesh_small_unicast") != NULL)
984   {
985     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNICAST\n");
986     test = UNICAST;
987     ok_goal = 5;
988   }
989   else if (strstr (argv[0], "test_mesh_small_multicast") != NULL)
990   {
991     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "MULTICAST\n");
992     test = MULTICAST;
993     ok_goal = 10;
994   }
995   else if (strstr (argv[0], "test_mesh_small_speed_ack") != NULL)
996   {
997    /* Each peer is supposed to generate the following callbacks:
998     * 1 incoming tunnel (@dest)
999     * 1 connected peer (@orig)
1000     * 1000 received data packet (@dest)
1001     * 1000 received data packet (@orig)
1002     * 1 received tunnel destroy (@dest)
1003     * _________________________________
1004     * 5 x ok expected per peer
1005     */
1006     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1007     test = SPEED_ACK;
1008     ok_goal = 2003;
1009     argv2 [3] = NULL; // remove -L DEBUG
1010 #if VERBOSE
1011     argc2 -= 2;
1012 #endif
1013   }
1014   else if (strstr (argv[0], "test_mesh_small_speed") != NULL)
1015   {
1016    /* Each peer is supposed to generate the following callbacks:
1017     * 1 incoming tunnel (@dest)
1018     * 1 connected peer (@orig)
1019     * 1000 received data packet (@dest)
1020     * 1received data packet (@orig)
1021     * 1 received tunnel destroy (@dest)
1022     * _________________________________
1023     */
1024     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1025     ok_goal = 1004;
1026     if (strstr (argv[0], "_min") != NULL)
1027       test = SPEED_MIN;
1028     else if (strstr (argv[0], "_nobuf") != NULL)
1029       test = SPEED_NOBUF;
1030     else
1031       test = SPEED;
1032   }
1033   else
1034   {
1035     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1036     test = SETUP;
1037     ok_goal = 0;
1038   }
1039
1040   if (strstr (argv[0], "backwards") != NULL)
1041   {
1042     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1043     test_backwards = GNUNET_YES;
1044   }
1045
1046   GNUNET_PROGRAM_run (argc2, argv2,
1047                       "test_mesh_small",
1048                       gettext_noop ("Test mesh in a small network."), options,
1049                       &run, NULL);
1050 #if REMOVE_DIR
1051   GNUNET_DISK_directory_remove ("/tmp/test_mesh_small");
1052 #endif
1053   if (ok_goal > ok)
1054   {
1055     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1056                 "FAILED! (%d/%d)\n", ok, ok_goal);
1057     return 1;
1058   }
1059   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1060   return 0;
1061 }
1062
1063 /* end of test_mesh_small.c */