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