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