- begin work on enhanced multipart receiving
[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_REL 8
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 channel (@dest)
80   * 1 connected peer (@orig)
81   * 1 received data packet (@dest)
82   * 1 received data packet (@orig)
83   * 1 received channel 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  * Number of payload packes sent
117  */
118 static int data_sent;
119
120 /**
121  * Number of payload packets received
122  */
123 static int data_received;
124
125 /**
126  * Number of payload packed explicitly (app level) acknowledged
127  */
128 static int data_ack;
129
130 /**
131  * Total number of currently running peers.
132  */
133 static unsigned long long peers_running;
134
135 /**
136  * Test context (to shut down).
137  */
138 struct GNUNET_MESH_TEST_Context *test_ctx;
139
140 /**
141  * Task called to disconnect peers.
142  */
143 static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
144
145 /**
146  * Task To perform tests
147  */
148 static GNUNET_SCHEDULER_TaskIdentifier test_task;
149
150 /**
151  * Task called to shutdown test.
152  */
153 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
154
155 /**
156  * Mesh handle for the root peer
157  */
158 static struct GNUNET_MESH_Handle *h1;
159
160 /**
161  * Mesh handle for the first leaf peer
162  */
163 static struct GNUNET_MESH_Handle *h2;
164
165 /**
166  * Channel handle for the root peer
167  */
168 static struct GNUNET_MESH_Channel *ch;
169
170 /**
171  * Channel handle for the dest peer
172  */
173 static struct GNUNET_MESH_Channel *incoming_ch;
174
175 /**
176  * Time we started the data transmission (after channel has been established
177  * and initilized).
178  */
179 static struct GNUNET_TIME_Absolute start_time;
180
181
182 /**
183  * Show the results of the test (banwidth acheived) and log them to GAUGER
184  */
185 static void
186 show_end_data (void)
187 {
188   static struct GNUNET_TIME_Absolute end_time;
189   static struct GNUNET_TIME_Relative total_time;
190
191   end_time = GNUNET_TIME_absolute_get();
192   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
193   FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
194   FPRINTF (stderr, "Test time %s\n",
195            GNUNET_STRINGS_relative_time_to_string (total_time,
196                                                    GNUNET_YES));
197   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
198            4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
199   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
200            TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
201   GAUGER ("MESH", test_name,
202           TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
203           "packets/s");
204 }
205
206
207 /**
208  * Shut down peergroup, clean up.
209  *
210  * @param cls Closure (unused).
211  * @param tc Task Context.
212  */
213 static void
214 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 {
216   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
217   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
218 }
219
220
221 /**
222  * Disconnect from mesh services af all peers, call shutdown.
223  *
224  * @param cls Closure (unused).
225  * @param tc Task Context.
226  */
227 static void
228 disconnect_mesh_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 {
230   long line = (long) cls;
231   unsigned int i;
232
233   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234               "disconnecting mesh service of peers, called from line %ld\n",
235               line);
236   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
237   for (i = 0; i < 2; i++)
238   {
239     GNUNET_TESTBED_operation_done (t_op[i]);
240   }
241   if (NULL != ch)
242   {
243     GNUNET_MESH_channel_destroy (ch);
244     ch = NULL;
245   }
246   if (NULL != incoming_ch)
247   {
248     GNUNET_MESH_channel_destroy (incoming_ch);
249     incoming_ch = NULL;
250   }
251   GNUNET_MESH_TEST_cleanup (test_ctx);
252   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
253   {
254     GNUNET_SCHEDULER_cancel (shutdown_handle);
255   }
256   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
257 }
258
259
260 /**
261  * Abort test: schedule disconnect and shutdown immediately
262  *
263  * @param line Line in the code the abort is requested from (__LINE__).
264  */
265 static void
266 abort_test (long line)
267 {
268   if (disconnect_task != GNUNET_SCHEDULER_NO_TASK)
269   {
270     GNUNET_SCHEDULER_cancel (disconnect_task);
271     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
272                                                 (void *) line);
273   }
274 }
275
276 /**
277  * Transmit ready callback.
278  *
279  * @param cls Closure (message type).
280  * @param size Size of the tranmist buffer.
281  * @param buf Pointer to the beginning of the buffer.
282  *
283  * @return Number of bytes written to buf.
284  */
285 static size_t
286 tmt_rdy (void *cls, size_t size, void *buf);
287
288
289 /**
290  * Task to schedule a new data transmission.
291  *
292  * @param cls Closure (peer #).
293  * @param tc Task Context.
294  */
295 static void
296 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
297 {
298   struct GNUNET_MESH_TransmitHandle *th;
299   struct GNUNET_MESH_Channel *channel;
300
301   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
302     return;
303
304   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
305   if (GNUNET_YES == test_backwards)
306   {
307     channel = incoming_ch;
308   }
309   else
310   {
311     channel = ch;
312   }
313   th = GNUNET_MESH_notify_transmit_ready (channel, GNUNET_NO,
314                                           GNUNET_TIME_UNIT_FOREVER_REL,
315                                           size_payload, &tmt_rdy, (void *) 1L);
316   if (NULL == th)
317   {
318     unsigned long i = (unsigned long) cls;
319
320     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Retransmission\n");
321     if (0 == i)
322     {
323       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  in 1 ms\n");
324       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
325                                     &data_task, (void *)1UL);
326     }
327     else
328     {
329       i++;
330       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "in %u ms\n", i);
331       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(
332                                       GNUNET_TIME_UNIT_MILLISECONDS,
333                                       i),
334                                     &data_task, (void *)i);
335     }
336   }
337 }
338
339
340 /**
341  * Transmit ready callback
342  *
343  * @param cls Closure (message type).
344  * @param size Size of the buffer we have.
345  * @param buf Buffer to copy data to.
346  */
347 size_t
348 tmt_rdy (void *cls, size_t size, void *buf)
349 {
350   struct GNUNET_MessageHeader *msg = buf;
351   uint32_t *data;
352
353   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
354               "tmt_rdy called, filling buffer\n");
355   if (size < size_payload || NULL == buf)
356   {
357     GNUNET_break (ok >= ok_goal - 2);
358     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359                 "size %u, buf %p, data_sent %u, data_received %u\n",
360                 size,
361                 buf,
362                 data_sent,
363                 data_received);
364     return 0;
365   }
366   msg->size = htons (size);
367   msg->type = htons ((long) cls);
368   data = (uint32_t *) &msg[1];
369   *data = htonl (data_sent);
370   if (GNUNET_NO == initialized)
371   {
372     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
373                 "sending initializer\n");
374   }
375   else if (SPEED == test)
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
388   return size_payload;
389 }
390
391
392 /**
393  * Function is called whenever a message is received.
394  *
395  * @param cls closure (set from GNUNET_MESH_connect)
396  * @param channel connection to the other end
397  * @param channel_ctx place to store local state associated with the channel
398  * @param message the actual message
399  * @return GNUNET_OK to keep the connection open,
400  *         GNUNET_SYSERR to close it (signal serious error)
401  */
402 int
403 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
404                void **channel_ctx,
405                const struct GNUNET_MessageHeader *message)
406 {
407   long client = (long) cls;
408   long expected_target_client;
409   uint32_t *data;
410
411   ok++;
412
413   GNUNET_MESH_receive_done (channel);
414
415   if ((ok % 20) == 0)
416   {
417     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
418     {
419       GNUNET_SCHEDULER_cancel (disconnect_task);
420       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
421                                                       &disconnect_mesh_peers,
422                                                       (void *) __LINE__);
423     }
424   }
425
426   switch (client)
427   {
428   case 0L:
429     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
430     break;
431   case 4L:
432     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
433                 "Leaf client %li got a message.\n",
434                 client);
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 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 (channel, 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 (channel, 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_channel_destroy (incoming_ch);
501       incoming_ch = NULL;
502     }
503     else
504     {
505       GNUNET_MESH_channel_destroy (ch);
506       ch = 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 channel
533  * the other peer initiated.
534  *
535  * @param cls Closure.
536  * @param channel New handle to the channel.
537  * @param initiator Peer that started the channel.
538  * @param port Port this channel is connected to.
539  * @param options channel option flags
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, enum GNUNET_MESH_ChannelOption options)
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     GNUNET_break (channel == incoming_ch);
594     incoming_ch = NULL;
595   }
596   else if (0L == i)
597   {
598     if (P2P_SIGNAL == test)
599     {
600       ok ++;
601     }
602     GNUNET_break (channel == ch);
603     ch = NULL;
604   }
605   else
606     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
607                 "Unknown peer! %d\n", i);
608   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
609
610   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
611   {
612     GNUNET_SCHEDULER_cancel (disconnect_task);
613     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
614                                                 (void *) __LINE__);
615   }
616
617   return;
618 }
619
620
621 /**
622  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
623  *
624  * Testcase continues when the root receives confirmation of connected peers,
625  * on callback funtion ch.
626  *
627  * @param cls Closure (unsued).
628  * @param tc Task Context.
629  */
630 static void
631 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
632 {
633   enum GNUNET_MESH_ChannelOption flags;
634
635   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
636   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
637
638   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
639               "schedule timeout in TIMEOUT\n");
640   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
641   {
642     GNUNET_SCHEDULER_cancel (disconnect_task);
643   }
644
645   flags = GNUNET_MESH_OPTION_DEFAULT;
646   if (SPEED_REL == test)
647   {
648     test = SPEED;
649     flags |= GNUNET_MESH_OPTION_RELIABLE;
650   }
651   ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags);
652
653   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
654                                                   &disconnect_mesh_peers,
655                                                   (void *) __LINE__);
656   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657               "Sending data initializer...\n");
658   data_ack = 0;
659   data_received = 0;
660   data_sent = 0;
661   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
662                                      GNUNET_TIME_UNIT_FOREVER_REL,
663                                      size_payload, &tmt_rdy, (void *) 1L);
664 }
665
666 /**
667  * Callback to be called when the requested peer information is available
668  *
669  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
670  * @param op the operation this callback corresponds to
671  * @param pinfo the result; will be NULL if the operation has failed
672  * @param emsg error message if the operation has failed;
673  *             NULL if the operation is successfull
674  */
675 static void
676 pi_cb (void *cls,
677        struct GNUNET_TESTBED_Operation *op,
678        const struct GNUNET_TESTBED_PeerInformation *pinfo,
679        const char *emsg)
680 {
681   long i = (long) cls;
682
683   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
684
685   if (NULL == pinfo || NULL != emsg)
686   {
687     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
688     abort_test (__LINE__);
689     return;
690   }
691   p_id[i] = pinfo->result.id;
692   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
693   p_ids++;
694   if (p_ids < 2)
695     return;
696   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
697   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
698                                             &do_test, NULL);
699 }
700
701 /**
702  * test main: start test when all peers are connected
703  *
704  * @param cls Closure.
705  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
706  * @param num_peers Number of peers that are running.
707  * @param peers Array of peers.
708  * @param meshes Handle to each of the MESHs of the peers.
709  */
710 static void
711 tmain (void *cls,
712        struct GNUNET_MESH_TEST_Context *ctx,
713        unsigned int num_peers,
714        struct GNUNET_TESTBED_Peer **peers,
715        struct GNUNET_MESH_Handle **meshes)
716 {
717   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
718   ok = 0;
719   test_ctx = ctx;
720   peers_running = num_peers;
721   h1 = meshes[0];
722   h2 = meshes[num_peers - 1];
723   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
724                                                   &disconnect_mesh_peers,
725                                                   (void *) __LINE__);
726   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
727                                                   &shutdown_task, NULL);
728   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
729                                                  GNUNET_TESTBED_PIT_IDENTITY,
730                                                  &pi_cb, (void *) 0L);
731   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
732                                                  GNUNET_TESTBED_PIT_IDENTITY,
733                                                  &pi_cb, (void *) 1L);
734   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
735 }
736
737
738 /**
739  * Main: start test
740  */
741 int
742 main (int argc, char *argv[])
743 {
744   initialized = GNUNET_NO;
745   uint32_t ports[2];
746   const char *config_file;
747
748   GNUNET_log_setup ("test", "DEBUG", NULL);
749   config_file = "test_mesh.conf";
750
751   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
752   if (strstr (argv[0], "_small_forward") != NULL)
753   {
754     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
755     test = FORWARD;
756     test_name = "unicast";
757     ok_goal = 4;
758   }
759   else if (strstr (argv[0], "_small_signal") != NULL)
760   {
761     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
762     test = P2P_SIGNAL;
763     test_name = "signal";
764     ok_goal = 4;
765   }
766   else if (strstr (argv[0], "_small_speed_ack") != NULL)
767   {
768    /* Each peer is supposed to generate the following callbacks:
769     * 1 incoming channel (@dest)
770     * TOTAL_PACKETS received data packet (@dest)
771     * TOTAL_PACKETS received data packet (@orig)
772     * 1 received channel destroy (@dest)
773     * _________________________________
774     * 5 x ok expected per peer
775     */
776     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
777     test = SPEED_ACK;
778     test_name = "speed ack";
779     ok_goal = TOTAL_PACKETS * 2 + 2;
780   }
781   else if (strstr (argv[0], "_small_speed") != NULL)
782   {
783    /* Each peer is supposed to generate the following callbacks:
784     * 1 incoming channel (@dest)
785     * 1 initial packet (@dest)
786     * TOTAL_PACKETS received data packet (@dest)
787     * 1 received data packet (@orig)
788     * 1 received channel destroy (@dest)
789     * _________________________________
790     */
791     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
792     ok_goal = TOTAL_PACKETS + 4;
793     if (strstr (argv[0], "_reliable") != NULL)
794     {
795       test = SPEED_REL;
796       test_name = "speed reliable";
797       config_file = "test_mesh_drop.conf";
798     }
799     else
800     {
801       test = SPEED;
802       test_name = "speed";
803     }
804   }
805   else
806   {
807     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
808     test = SETUP;
809     ok_goal = 0;
810   }
811
812   if (strstr (argv[0], "backwards") != NULL)
813   {
814     char *aux;
815
816     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
817     test_backwards = GNUNET_YES;
818     aux = GNUNET_malloc (32);
819     sprintf (aux, "backwards %s", test_name);
820     test_name = aux;
821   }
822
823   p_ids = 0;
824   ports[0] = 1;
825   ports[1] = 0;
826   GNUNET_MESH_TEST_run ("test_mesh_small",
827                         config_file,
828                         5,
829                         &tmain,
830                         NULL, /* tmain cls */
831                         &incoming_channel,
832                         &channel_cleaner,
833                         handlers,
834                         ports);
835
836   if (ok_goal > ok)
837   {
838     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
839                 "FAILED! (%d/%d)\n", ok, ok_goal);
840     return 1;
841   }
842   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
843   return 0;
844 }
845
846 /* end of test_mesh_small.c */
847