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