-remove trailing whitespace
[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_enc.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 channel (@dest)
81   * 1 connected peer (@orig)
82   * 1 received data packet (@dest)
83   * 1 received data packet (@orig)
84   * 1 received channel 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  * Channel handle for the root peer
168  */
169 static struct GNUNET_MESH_Channel *ch;
170
171 /**
172  * Channel handle for the dest peer
173  */
174 static struct GNUNET_MESH_Channel *incoming_ch;
175
176 /**
177  * Time we started the data transmission (after channel 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 != ch)
243   {
244     GNUNET_MESH_channel_destroy (ch);
245     ch = NULL;
246   }
247   if (NULL != incoming_ch)
248   {
249     GNUNET_MESH_channel_destroy (incoming_ch);
250     incoming_ch = 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_Channel *channel;
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     channel = incoming_ch;
309   }
310   else
311   {
312     channel = ch;
313   }
314   th = GNUNET_MESH_notify_transmit_ready (channel, 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, filling buffer\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 (GNUNET_NO == initialized)
372   {
373     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
374                 "sending initializer\n");
375   }
376   else if (SPEED == test)
377   {
378     data_sent++;
379     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380               " Sent packet %d\n", data_sent);
381     if (data_sent < TOTAL_PACKETS)
382     {
383       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384               " Scheduling packet %d\n", data_sent + 1);
385       GNUNET_SCHEDULER_add_now (&data_task, NULL);
386     }
387   }
388
389   return size_payload;
390 }
391
392
393 /**
394  * Function is called whenever a message is received.
395  *
396  * @param cls closure (set from GNUNET_MESH_connect)
397  * @param channel connection to the other end
398  * @param channel_ctx place to store local state associated with the channel
399  * @param message the actual message
400  * @return GNUNET_OK to keep the connection open,
401  *         GNUNET_SYSERR to close it (signal serious error)
402  */
403 int
404 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
405                void **channel_ctx,
406                const struct GNUNET_MessageHeader *message)
407 {
408   long client = (long) cls;
409   long expected_target_client;
410   uint32_t *data;
411
412   ok++;
413
414   GNUNET_MESH_receive_done (channel);
415
416   if ((ok % 20) == 0)
417   {
418     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
419     {
420       GNUNET_SCHEDULER_cancel (disconnect_task);
421       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
422                                                       &disconnect_mesh_peers,
423                                                       (void *) __LINE__);
424     }
425   }
426
427   switch (client)
428   {
429   case 0L:
430     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
431     break;
432   case 4L:
433     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
434                 "Leaf client %li got a message.\n",
435                 client);
436     break;
437   default:
438     GNUNET_assert (0);
439     break;
440   }
441   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
442   data = (uint32_t *) &message[1];
443   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
444   if (SPEED == test && GNUNET_YES == test_backwards)
445   {
446     expected_target_client = 0L;
447   }
448   else
449   {
450     expected_target_client = 4L;
451   }
452
453   if (GNUNET_NO == initialized)
454   {
455     initialized = GNUNET_YES;
456     start_time = GNUNET_TIME_absolute_get ();
457     if (SPEED == test)
458     {
459       GNUNET_assert (4L == client);
460       GNUNET_SCHEDULER_add_now (&data_task, NULL);
461       return GNUNET_OK;
462     }
463   }
464
465   if (client == expected_target_client) // Normally 4
466   {
467     data_received++;
468     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
469                 " received data %u\n", data_received);
470     if (SPEED != test || (ok_goal - 2) == ok)
471     {
472       GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
473                                          GNUNET_TIME_UNIT_FOREVER_REL,
474                                          size_payload, &tmt_rdy, (void *) 1L);
475       return GNUNET_OK;
476     }
477     else
478     {
479       if (data_received < TOTAL_PACKETS)
480         return GNUNET_OK;
481     }
482   }
483   else // Normally 0
484   {
485     if (test == SPEED_ACK || test == SPEED)
486     {
487       data_ack++;
488       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
489               " received ack %u\n", data_ack);
490       GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
491                                          GNUNET_TIME_UNIT_FOREVER_REL,
492                                          size_payload, &tmt_rdy, (void *) 1L);
493       if (data_ack < TOTAL_PACKETS && SPEED != test)
494         return GNUNET_OK;
495       if (ok == 2 && SPEED == test)
496         return GNUNET_OK;
497       show_end_data();
498     }
499     if (test == P2P_SIGNAL)
500     {
501       GNUNET_MESH_channel_destroy (incoming_ch);
502       incoming_ch = NULL;
503     }
504     else
505     {
506       GNUNET_MESH_channel_destroy (ch);
507       ch = NULL;
508     }
509   }
510
511   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
512   {
513     GNUNET_SCHEDULER_cancel (disconnect_task);
514     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
515                                                     &disconnect_mesh_peers,
516                                                     (void *) __LINE__);
517   }
518
519   return GNUNET_OK;
520 }
521
522
523 /**
524  * Handlers, for diverse services
525  */
526 static struct GNUNET_MESH_MessageHandler handlers[] = {
527   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
528   {NULL, 0, 0}
529 };
530
531
532 /**
533  * Method called whenever another peer has added us to a channel
534  * the other peer initiated.
535  *
536  * @param cls Closure.
537  * @param channel New handle to the channel.
538  * @param initiator Peer that started the channel.
539  * @param port Port this channel is connected to.
540  * @return Initial channel context for the channel
541  *         (can be NULL -- that's not an error).
542  */
543 static void *
544 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
545                  const struct GNUNET_PeerIdentity *initiator,
546                  uint32_t port)
547 {
548   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
549               "Incoming channel from %s to peer %d\n",
550               GNUNET_i2s (initiator), (long) cls);
551   ok++;
552   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
553   if ((long) cls == 4L)
554     incoming_ch = channel;
555   else
556   {
557     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
558                 "Incoming channel for unknown client %lu\n", (long) cls);
559     GNUNET_break(0);
560   }
561   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
562   {
563     GNUNET_SCHEDULER_cancel (disconnect_task);
564     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
565                                                     &disconnect_mesh_peers,
566                                                     (void *) __LINE__);
567   }
568
569   return NULL;
570 }
571
572 /**
573  * Function called whenever an inbound channel is destroyed.  Should clean up
574  * any associated state.
575  *
576  * @param cls closure (set from GNUNET_MESH_connect)
577  * @param channel connection to the other end (henceforth invalid)
578  * @param channel_ctx place where local state associated
579  *                   with the channel is stored
580  */
581 static void
582 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
583                  void *channel_ctx)
584 {
585   long i = (long) cls;
586
587   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
588               "Incoming channel disconnected at peer %d\n",
589               i);
590   if (4L == i)
591   {
592     ok++;
593     incoming_ch = NULL;
594   }
595   else if (0L == i && P2P_SIGNAL == test)
596   {
597     ok ++;
598     ch = NULL;
599   }
600   else
601     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
602                 "Unknown peer! %d\n", i);
603   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
604
605   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
606   {
607     GNUNET_SCHEDULER_cancel (disconnect_task);
608     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
609                                                 (void *) __LINE__);
610   }
611
612   return;
613 }
614
615
616 /**
617  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
618  *
619  * Testcase continues when the root receives confirmation of connected peers,
620  * on callback funtion ch.
621  *
622  * @param cls Closure (unsued).
623  * @param tc Task Context.
624  */
625 static void
626 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
627 {
628   int nobuf;
629   int rel;
630
631   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
632   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
633
634   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635               "schedule timeout in TIMEOUT\n");
636   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
637   {
638     GNUNET_SCHEDULER_cancel (disconnect_task);
639   }
640   if (SPEED_NOBUF == test)
641   {
642     test = SPEED;
643     nobuf = GNUNET_YES;
644   }
645   else
646     nobuf = GNUNET_NO;
647
648   if (SPEED_REL == test)
649   {
650     test = SPEED;
651     rel = GNUNET_YES;
652   }
653   else
654     rel = GNUNET_NO;
655   ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, nobuf, rel);
656
657   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
658                                                   &disconnect_mesh_peers,
659                                                   (void *) __LINE__);
660   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661               "Sending data initializer...\n");
662   data_ack = 0;
663   data_received = 0;
664   data_sent = 0;
665   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
666                                      GNUNET_TIME_UNIT_FOREVER_REL,
667                                      size_payload, &tmt_rdy, (void *) 1L);
668 }
669
670 /**
671  * Callback to be called when the requested peer information is available
672  *
673  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
674  * @param op the operation this callback corresponds to
675  * @param pinfo the result; will be NULL if the operation has failed
676  * @param emsg error message if the operation has failed;
677  *             NULL if the operation is successfull
678  */
679 static void
680 pi_cb (void *cls,
681        struct GNUNET_TESTBED_Operation *op,
682        const struct GNUNET_TESTBED_PeerInformation *pinfo,
683        const char *emsg)
684 {
685   long i = (long) cls;
686
687   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
688
689   if (NULL == pinfo || NULL != emsg)
690   {
691     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
692     abort_test (__LINE__);
693     return;
694   }
695   p_id[i] = pinfo->result.id;
696   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
697   p_ids++;
698   if (p_ids < 2)
699     return;
700   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
701   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
702                                             &do_test, NULL);
703 }
704
705 /**
706  * test main: start test when all peers are connected
707  *
708  * @param cls Closure.
709  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
710  * @param num_peers Number of peers that are running.
711  * @param peers Array of peers.
712  * @param meshes Handle to each of the MESHs of the peers.
713  */
714 static void
715 tmain (void *cls,
716        struct GNUNET_MESH_TEST_Context *ctx,
717        unsigned int num_peers,
718        struct GNUNET_TESTBED_Peer **peers,
719        struct GNUNET_MESH_Handle **meshes)
720 {
721   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
722   ok = 0;
723   test_ctx = ctx;
724   peers_running = num_peers;
725   h1 = meshes[0];
726   h2 = meshes[num_peers - 1];
727   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
728                                                   &disconnect_mesh_peers,
729                                                   (void *) __LINE__);
730   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
731                                                   &shutdown_task, NULL);
732   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
733                                                  GNUNET_TESTBED_PIT_IDENTITY,
734                                                  &pi_cb, (void *) 0L);
735   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
736                                                  GNUNET_TESTBED_PIT_IDENTITY,
737                                                  &pi_cb, (void *) 1L);
738   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
739 }
740
741
742 /**
743  * Main: start test
744  */
745 int
746 main (int argc, char *argv[])
747 {
748   initialized = GNUNET_NO;
749   uint32_t ports[2];
750   const char *config_file;
751
752   GNUNET_log_setup ("test", "DEBUG", NULL);
753   config_file = "test_mesh.conf";
754
755   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
756   if (strstr (argv[0], "_small_forward") != NULL)
757   {
758     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
759     test = FORWARD;
760     test_name = "unicast";
761     ok_goal = 4;
762   }
763   else if (strstr (argv[0], "_small_signal") != NULL)
764   {
765     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
766     test = P2P_SIGNAL;
767     test_name = "signal";
768     ok_goal = 4;
769   }
770   else if (strstr (argv[0], "_small_speed_ack") != NULL)
771   {
772    /* Each peer is supposed to generate the following callbacks:
773     * 1 incoming channel (@dest)
774     * TOTAL_PACKETS received data packet (@dest)
775     * TOTAL_PACKETS received data packet (@orig)
776     * 1 received channel destroy (@dest)
777     * _________________________________
778     * 5 x ok expected per peer
779     */
780     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
781     test = SPEED_ACK;
782     test_name = "speed ack";
783     ok_goal = TOTAL_PACKETS * 2 + 2;
784   }
785   else if (strstr (argv[0], "_small_speed") != NULL)
786   {
787    /* Each peer is supposed to generate the following callbacks:
788     * 1 incoming channel (@dest)
789     * 1 initial packet (@dest)
790     * TOTAL_PACKETS received data packet (@dest)
791     * 1 received data packet (@orig)
792     * 1 received channel destroy (@dest)
793     * _________________________________
794     */
795     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
796     ok_goal = TOTAL_PACKETS + 4;
797     if (strstr (argv[0], "_nobuf") != NULL)
798     {
799       test = SPEED_NOBUF;
800       test_name = "speed nobuf";
801     }
802     else if (strstr (argv[0], "_reliable") != NULL)
803     {
804       test = SPEED_REL;
805       test_name = "speed reliable";
806       config_file = "test_mesh_drop.conf";
807     }
808     else
809     {
810       test = SPEED;
811       test_name = "speed";
812     }
813   }
814   else
815   {
816     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
817     test = SETUP;
818     ok_goal = 0;
819   }
820
821   if (strstr (argv[0], "backwards") != NULL)
822   {
823     char *aux;
824
825     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
826     test_backwards = GNUNET_YES;
827     aux = GNUNET_malloc (32);
828     sprintf (aux, "backwards %s", test_name);
829     test_name = aux;
830   }
831
832   p_ids = 0;
833   ports[0] = 1;
834   ports[1] = 0;
835   GNUNET_MESH_TEST_run ("test_mesh_small",
836                         config_file,
837                         5,
838                         &tmain,
839                         NULL, /* tmain cls */
840                         &incoming_channel,
841                         &channel_cleaner,
842                         handlers,
843                         ports);
844
845   if (ok_goal > ok)
846   {
847     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
848                 "FAILED! (%d/%d)\n", ok, ok_goal);
849     return 1;
850   }
851   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
852   return 0;
853 }
854
855 /* end of test_mesh_small.c */
856