rename
[oweals/gnunet.git] / src / mesh / test_mesh2_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_mesh2_small.c
22  *
23  * @brief Test for the mesh service: retransmission of traffic.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "mesh2_test_lib.h"
28 #include "gnunet_mesh2_service.h"
29 #include <gauger.h>
30
31
32 /**
33  * How namy messages to send
34  */
35 #define TOTAL_PACKETS 1000
36
37 /**
38  * How long until we give up on connecting the peers?
39  */
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
41
42 /**
43  * Time to wait for stuff that should be rather fast
44  */
45 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
46
47 /**
48  * DIFFERENT TESTS TO RUN
49  */
50 #define SETUP 0
51 #define FORWARD 1
52 #define SPEED 3
53 #define SPEED_ACK 4
54 #define SPEED_NOBUF 6
55 #define P2P_SIGNAL 10
56
57 /**
58  * Which test are we running?
59  */
60 static int test;
61
62 /**
63  * String with test name
64  */
65 char *test_name;
66
67 /**
68  * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
69  */
70 static int test_backwards = GNUNET_NO;
71
72 /**
73  * How many events have happened
74  */
75 static int ok;
76
77  /**
78   * Each peer is supposed to generate the following callbacks:
79   * 1 incoming tunnel (@dest)
80   * 1 connected peer (@orig)
81   * 1 received data packet (@dest)
82   * 1 received data packet (@orig)
83   * 1 received tunnel destroy (@dest)
84   * _________________________________
85   * 5 x ok expected per peer
86   */
87 int ok_goal;
88
89
90 /**
91  * Size of each test packet
92  */
93 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
94
95 /**
96  * Operation to get peer ids.
97  */
98 struct GNUNET_TESTBED_Operation *t_op[2];
99
100 /**
101  * Peer ids.
102  */
103 struct GNUNET_PeerIdentity *p_id[2];
104
105 /**
106  * Peer ids counter.
107  */
108 unsigned int p_ids;
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  * Total number of currently running peers.
142  */
143 static unsigned long long peers_running;
144
145 /**
146  * Test context (to shut down).
147  */
148 struct GNUNET_MESH_TEST_Context *test_ctx;
149
150 /**
151  * Task called to disconnect peers.
152  */
153 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
154
155 /**
156  * Task To perform tests
157  */
158 static GNUNET_SCHEDULER_TaskIdentifier test_task;
159
160 /**
161  * Task called to shutdown test.
162  */
163 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
164
165 /**
166  * Mesh handle for the root peer
167  */
168 static struct GNUNET_MESH_Handle *h1;
169
170 /**
171  * Mesh handle for the first leaf peer
172  */
173 static struct GNUNET_MESH_Handle *h2;
174
175 /**
176  * Tunnel handle for the root peer
177  */
178 static struct GNUNET_MESH_Tunnel *t;
179
180 /**
181  * Tunnel handle for the first leaf peer
182  */
183 static struct GNUNET_MESH_Tunnel *incoming_t;
184
185 /**
186  * Time we started the data transmission (after tunnel has been established
187  * and initilized).
188  */
189 static struct GNUNET_TIME_Absolute start_time;
190
191
192 /**
193  * Show the results of the test (banwidth acheived) and log them to GAUGER
194  */
195 static void
196 show_end_data (void)
197 {
198   static struct GNUNET_TIME_Absolute end_time;
199   static struct GNUNET_TIME_Relative total_time;
200
201   end_time = GNUNET_TIME_absolute_get();
202   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
203   FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
204   FPRINTF (stderr, "Test time %llu ms\n",
205             (unsigned long long) total_time.rel_value);
206   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
207             4 * TOTAL_PACKETS * 1.0 / total_time.rel_value); // 4bytes * ms
208   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
209             TOTAL_PACKETS * 1000.0 / total_time.rel_value); // packets * ms
210   GAUGER ("MESH", test_name,
211           TOTAL_PACKETS * 1000.0 / total_time.rel_value,
212           "packets/s");
213 }
214
215
216 /**
217  * Shut down peergroup, clean up.
218  * 
219  * @param cls Closure (unused).
220  * @param tc Task Context.
221  */
222 static void
223 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
224 {
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
226   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
227 }
228
229
230 /**
231  * Disconnect from mesh services af all peers, call shutdown.
232  * 
233  * @param cls Closure (unused).
234  * @param tc Task Context.
235  */
236 static void
237 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
238 {
239   long line = (long) cls;
240   unsigned int i;
241
242   for (i = 0; i < 3; i++)
243     if (NULL != t_op[i])
244     {
245       GNUNET_TESTBED_operation_done (t_op[i]);
246       t_op[i] = NULL;
247     }
248   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249               "disconnecting mesh service of peers, called from line %ld\n",
250               line);
251   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
252   if (NULL != t)
253   {
254     GNUNET_MESH_tunnel_destroy (t);
255     t = NULL;
256   }
257   if (NULL != incoming_t)
258   {
259     GNUNET_MESH_tunnel_destroy (incoming_t);
260     incoming_t = NULL;
261   }
262   GNUNET_MESH_TEST_cleanup (test_ctx);
263   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
264   {
265     GNUNET_SCHEDULER_cancel (shutdown_handle);
266   }
267   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
268 }
269
270
271 /**
272  * Abort test: schedule disconnect and shutdown immediately
273  * 
274  * @param line Line in the code the abort is requested from (__LINE__).
275  */
276 void
277 abort_test (long line)
278 {
279   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
280   {
281     GNUNET_SCHEDULER_cancel (disconnect_task);
282   }
283   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
284                                                   &disconnect_mesh_peers,
285                                                   (void *) line);
286 }
287
288 /**
289  * Transmit ready callback.
290  * 
291  * @param cls Closure (message type).
292  * @param size Size of the tranmist buffer.
293  * @param buf Pointer to the beginning of the buffer.
294  * 
295  * @return Number of bytes written to buf.
296  */
297 static size_t
298 tmt_rdy (void *cls, size_t size, void *buf);
299
300
301 /**
302  * Task to schedule a new data transmission.
303  * 
304  * @param cls Closure (peer #).
305  * @param tc Task Context.
306  */
307 static void
308 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 {
310   struct GNUNET_MESH_TransmitHandle *th;
311   struct GNUNET_MESH_Tunnel *tunnel;
312
313   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
314     return;
315
316   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
317   if (GNUNET_YES == test_backwards)
318   {
319     tunnel = incoming_t;
320   }
321   else
322   {
323     tunnel = t;
324   }
325   th = GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
326                                           GNUNET_TIME_UNIT_FOREVER_REL,
327                                           size_payload, &tmt_rdy, (void *) 1L);
328   if (NULL == th)
329   {
330     unsigned long i = (unsigned long) cls;
331
332     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
333     if (0 == i)
334     {
335       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  in 1 ms\n");
336       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
337                                     &data_task, (void *)1UL);
338     }
339     else
340     {
341       i++;
342       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
343       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
344                                       GNUNET_TIME_UNIT_MILLISECONDS,
345                                       i),
346                                     &data_task, (void *)i);
347     }
348   }
349 }
350
351
352 /**
353  * Transmit ready callback
354  *
355  * @param cls Closure (message type).
356  * @param size Size of the buffer we have.
357  * @param buf Buffer to copy data to.
358  */
359 size_t
360 tmt_rdy (void *cls, size_t size, void *buf)
361 {
362   struct GNUNET_MessageHeader *msg = buf;
363   uint32_t *data;
364
365   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366               " tmt_rdy called\n");
367   if (size < size_payload || NULL == buf)
368   {
369     GNUNET_break (0);
370     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371                 "size %u, buf %p, data_sent %u, data_received %u\n",
372                 size,
373                 buf,
374                 data_sent,
375                 data_received);
376     return 0;
377   }
378   msg->size = htons (size);
379   msg->type = htons ((long) cls);
380   data = (uint32_t *) &msg[1];
381   *data = htonl (data_sent);
382   if (SPEED == test && GNUNET_YES == initialized)
383   {
384     data_sent++;
385     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386               " Sent packet %d\n", data_sent);
387     if (data_sent < TOTAL_PACKETS)
388     {
389       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
390               " Scheduling packet %d\n", data_sent + 1);
391       GNUNET_SCHEDULER_add_now(&data_task, NULL);
392     }
393   }
394   return size_payload;
395 }
396
397
398 /**
399  * Function is called whenever a message is received.
400  *
401  * @param cls closure (set from GNUNET_MESH_connect)
402  * @param tunnel connection to the other end
403  * @param tunnel_ctx place to store local state associated with the tunnel
404  * @param sender who sent the message
405  * @param message the actual message
406  * @return GNUNET_OK to keep the connection open,
407  *         GNUNET_SYSERR to close it (signal serious error)
408  */
409 int
410 data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
411                const struct GNUNET_PeerIdentity *sender,
412                const struct GNUNET_MessageHeader *message)
413 {
414   long client = (long) cls;
415   long expected_target_client;
416   uint32_t *data;
417
418   ok++;
419
420   GNUNET_MESH_receive_done (tunnel);
421
422   if ((ok % 20) == 0)
423   {
424     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
425     {
426       GNUNET_SCHEDULER_cancel (disconnect_task);
427     }
428     disconnect_task =
429               GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
430                                             (void *) __LINE__);
431   }
432
433   switch (client)
434   {
435   case 0L:
436     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
437     peers_responded++;
438     break;
439   case 4L:
440     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
441                 "Leaf client %li got a message.\n",
442                 client);
443     client = 4L;
444     break;
445   default:
446     GNUNET_assert (0);
447     break;
448   }
449   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
450   data = (uint32_t *) &message[1];
451   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
452   if (SPEED == test && GNUNET_YES == test_backwards)
453   {
454     expected_target_client = 0L;
455   }
456   else
457   {
458     expected_target_client = 4L;
459   }
460
461   if (GNUNET_NO == initialized)
462   {
463     initialized = GNUNET_YES;
464     start_time = GNUNET_TIME_absolute_get ();
465     if (SPEED == test)
466     {
467       GNUNET_assert (4L == client);
468       GNUNET_SCHEDULER_add_now (&data_task, NULL);
469       return GNUNET_OK;
470     }
471   }
472
473   if (client == expected_target_client) // Normally 3 or 4
474   {
475     data_received++;
476     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
477                 " received data %u\n", data_received);
478     if (SPEED != test || (ok_goal - 2) == ok)
479     {
480       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
481                                          GNUNET_TIME_UNIT_FOREVER_REL,
482                                          size_payload, &tmt_rdy, (void *) 1L);
483       return GNUNET_OK;
484     }
485     else
486     {
487       if (data_received < TOTAL_PACKETS)
488         return GNUNET_OK;
489     }
490   }
491   else // Normally 0
492   {
493     if (test == SPEED_ACK || test == SPEED)
494     {
495       data_ack++;
496       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
497               " received ack %u\n", data_ack);
498       GNUNET_MESH_notify_transmit_ready (tunnel, GNUNET_NO,
499                                          GNUNET_TIME_UNIT_FOREVER_REL,
500                                          size_payload, &tmt_rdy, (void *) 1L);
501       if (data_ack < TOTAL_PACKETS && SPEED != test)
502         return GNUNET_OK;
503       if (ok == 2 && SPEED == test)
504         return GNUNET_OK;
505       show_end_data();
506     }
507     if (test == P2P_SIGNAL)
508     {
509       GNUNET_MESH_tunnel_destroy (incoming_t);
510       incoming_t = NULL;
511     }
512     else
513     {
514       GNUNET_MESH_tunnel_destroy (t);
515       t = NULL;
516     }
517   }
518
519   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
520   {
521     GNUNET_SCHEDULER_cancel (disconnect_task);
522   }
523   disconnect_task =
524         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
525                                       (void *) __LINE__);
526
527   return GNUNET_OK;
528 }
529
530
531 /**
532  * Handlers, for diverse services
533  */
534 static struct GNUNET_MESH_MessageHandler handlers[] = {
535   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
536   {NULL, 0, 0}
537 };
538
539
540 /**
541  * Method called whenever another peer has added us to a tunnel
542  * the other peer initiated.
543  *
544  * @param cls Closure.
545  * @param tunnel New handle to the tunnel.
546  * @param initiator Peer that started the tunnel.
547  * @param port Port this tunnels is connected to.
548  * @return Initial tunnel context for the tunnel
549  *         (can be NULL -- that's not an error).
550  */
551 static void *
552 incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
553                  const struct GNUNET_PeerIdentity *initiator,
554                  uint32_t port)
555 {
556   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
557               "Incoming tunnel from %s to peer %d\n",
558               GNUNET_i2s (initiator), (long) cls);
559   ok++;
560   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
561   if ((long) cls == 4L)
562     incoming_t = tunnel;
563   else
564   {
565     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566                 "Incoming tunnel for unknown client %lu\n", (long) cls);
567     GNUNET_break(0);
568   }
569   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
570   {
571     GNUNET_SCHEDULER_cancel (disconnect_task);
572   }
573   disconnect_task =
574         GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
575                                       (void *) __LINE__);
576
577   return NULL;
578 }
579
580 /**
581  * Function called whenever an inbound tunnel is destroyed.  Should clean up
582  * any associated state.
583  *
584  * @param cls closure (set from GNUNET_MESH_connect)
585  * @param tunnel connection to the other end (henceforth invalid)
586  * @param tunnel_ctx place where local state associated
587  *                   with the tunnel is stored
588  */
589 static void
590 tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
591                 void *tunnel_ctx)
592 {
593   long i = (long) cls;
594
595   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
596               "Incoming tunnel disconnected at peer %d\n",
597               i);
598   if (4L == i)
599   {
600     ok++;
601     incoming_t = NULL;
602   }
603   else if (0L == i && P2P_SIGNAL == test)
604   {
605     ok ++;
606     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
607     {
608       GNUNET_SCHEDULER_cancel (disconnect_task);
609     }
610     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
611                                                 (void *) __LINE__);
612   }
613   else
614     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
615                 "Unknown peer! %d\n", i);
616   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
617   peers_in_tunnel--;
618   if (peers_in_tunnel > 0)
619     return;
620
621   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
622   {
623     GNUNET_SCHEDULER_cancel (disconnect_task);
624   }
625   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
626                                               (void *) __LINE__);
627
628   return;
629 }
630
631
632 /**
633  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
634  * 
635  * Testcase continues when the root receives confirmation of connected peers,
636  * on callback funtion ch.
637  * 
638  * @param cls Closure (unsued).
639  * @param tc Task Context.
640  */
641 static void
642 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
643 {
644   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
645   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
646
647   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648               "schedule timeout in TIMEOUT\n");
649   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
650   {
651     GNUNET_SCHEDULER_cancel (disconnect_task);
652   }
653   t = GNUNET_MESH_tunnel_create (h1, NULL, p_id[1], 1);
654   if (SPEED_NOBUF == test)
655   {
656     GNUNET_MESH_tunnel_buffer(t, GNUNET_NO);
657     test = SPEED;
658   }
659
660   disconnect_task =
661       GNUNET_SCHEDULER_add_delayed (SHORT_TIME, &disconnect_mesh_peers,
662                                     (void *) __LINE__);
663   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664               "Sending data initializer...\n");
665   peers_responded = 0;
666   data_ack = 0;
667   data_received = 0;
668   data_sent = 0;
669   GNUNET_MESH_notify_transmit_ready (t, GNUNET_NO,
670                                      GNUNET_TIME_UNIT_FOREVER_REL, 
671                                      size_payload, &tmt_rdy, (void *) 1L);
672 }
673
674 /**
675  * Callback to be called when the requested peer information is available
676  *
677  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
678  * @param op the operation this callback corresponds to
679  * @param pinfo the result; will be NULL if the operation has failed
680  * @param emsg error message if the operation has failed;
681  *             NULL if the operation is successfull
682  */
683 static void
684 pi_cb (void *cls,
685        struct GNUNET_TESTBED_Operation *op,
686        const struct GNUNET_TESTBED_PeerInformation *pinfo,
687        const char *emsg)
688 {
689   long i = (long) cls;
690
691   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
692   if (NULL == pinfo || NULL != emsg)
693   {
694     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
695     abort_test (__LINE__);
696     return;
697   }
698   p_id[i] = pinfo->result.id;
699   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
700   p_ids++;
701   if (p_ids < 2)
702     return;
703   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
704   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
705                                             &do_test, NULL);
706 }
707
708 /**
709  * test main: start test when all peers are connected
710  *
711  * @param cls Closure.
712  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
713  * @param num_peers Number of peers that are running.
714  * @param peers Array of peers.
715  * @param meshes Handle to each of the MESHs of the peers.
716  */
717 static void
718 tmain (void *cls,
719        struct GNUNET_MESH_TEST_Context *ctx,
720        unsigned int num_peers,
721        struct GNUNET_TESTBED_Peer **peers,
722        struct GNUNET_MESH_Handle **meshes)
723 {
724   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
725   ok = 0;
726   test_ctx = ctx;
727   peers_running = num_peers;
728   h1 = meshes[0];
729   h2 = meshes[num_peers - 1];
730   peers_in_tunnel = 0;
731   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
732                                                   &disconnect_mesh_peers,
733                                                   (void *) __LINE__);
734   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
735                                                   &shutdown_task, NULL);
736   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
737                                                  GNUNET_TESTBED_PIT_IDENTITY,
738                                                  &pi_cb, (void *) 0L);
739   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
740                                                  GNUNET_TESTBED_PIT_IDENTITY,
741                                                  &pi_cb, (void *) 1L);
742   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
743 }
744
745
746 /**
747  * Main: start test
748  */
749 int
750 main (int argc, char *argv[])
751 {
752   initialized = GNUNET_NO;
753
754   GNUNET_log_setup ("test", "DEBUG", NULL);
755
756   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
757   if (strstr (argv[0], "test_mesh2_small_forward") != NULL)
758   {
759     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
760     test = FORWARD;
761     test_name = "unicast";
762     ok_goal = 4;
763   }
764   else if (strstr (argv[0], "test_mesh_small_signal") != NULL)
765   {
766     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
767     test = P2P_SIGNAL;
768     test_name = "signal";
769     ok_goal = 4;
770   }
771   else if (strstr (argv[0], "test_mesh_small_speed_ack") != NULL)
772   {
773    /* Each peer is supposed to generate the following callbacks:
774     * 1 incoming tunnel (@dest)
775     * TOTAL_PACKETS received data packet (@dest)
776     * TOTAL_PACKETS received data packet (@orig)
777     * 1 received tunnel destroy (@dest)
778     * _________________________________
779     * 5 x ok expected per peer
780     */
781     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
782     test = SPEED_ACK;
783     test_name = "speed ack";
784     ok_goal = TOTAL_PACKETS * 2 + 2;
785   }
786   else if (strstr (argv[0], "test_mesh_small_speed") != NULL)
787   {
788    /* Each peer is supposed to generate the following callbacks:
789     * 1 incoming tunnel (@dest)
790     * 1 initial packet (@dest)
791     * TOTAL_PACKETS received data packet (@dest)
792     * 1 received data packet (@orig)
793     * 1 received tunnel destroy (@dest)
794     * _________________________________
795     */
796     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
797     ok_goal = TOTAL_PACKETS + 4;
798     if (strstr (argv[0], "_nobuf") != NULL)
799     {
800       test = SPEED_NOBUF;
801       test_name = "speed nobuf";
802     }
803     else
804     {
805       test = SPEED;
806       test_name = "speed";
807     }
808   }
809   else
810   {
811     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
812     test = SETUP;
813     ok_goal = 0;
814   }
815
816   if (strstr (argv[0], "backwards") != NULL)
817   {
818     char *aux;
819
820     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
821     test_backwards = GNUNET_YES;
822     aux = GNUNET_malloc (32);
823     sprintf (aux, "backwards %s", test_name);
824     test_name = aux;
825   }
826
827   p_ids = 0;
828   GNUNET_MESH_TEST_run ("test_mesh_small",
829                         "test_mesh_small.conf",
830                         5,
831                         &tmain,
832                         NULL,
833                         &incoming_tunnel,
834                         &tunnel_cleaner,
835                         handlers,
836                         NULL);
837
838   if (ok_goal > ok)
839   {
840     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
841                 "FAILED! (%d/%d)\n", ok, ok_goal);
842     return 1;
843   }
844   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
845   return 0;
846 }
847
848 /* end of test_mesh_small.c */