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