-fix #3233
[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, buf, data_sent, data_received);
361     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
362
363     return 0;
364   }
365   msg->size = htons (size);
366   msg->type = htons ((long) cls);
367   data = (uint32_t *) &msg[1];
368   *data = htonl (data_sent);
369   if (GNUNET_NO == initialized)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372                 "sending initializer\n");
373   }
374   else if (SPEED == test)
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
387   return size_payload;
388 }
389
390
391 /**
392  * Function is called whenever a message is received.
393  *
394  * @param cls closure (set from GNUNET_MESH_connect)
395  * @param channel connection to the other end
396  * @param channel_ctx place to store local state associated with the channel
397  * @param message the actual message
398  * @return GNUNET_OK to keep the connection open,
399  *         GNUNET_SYSERR to close it (signal serious error)
400  */
401 int
402 data_callback (void *cls, struct GNUNET_MESH_Channel *channel,
403                void **channel_ctx,
404                const struct GNUNET_MessageHeader *message)
405 {
406   long client = (long) cls;
407   long expected_target_client;
408   uint32_t *data;
409
410   ok++;
411
412   GNUNET_MESH_receive_done (channel);
413
414   if ((ok % 20) == 0)
415   {
416     if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
417     {
418       GNUNET_SCHEDULER_cancel (disconnect_task);
419       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
420                                                       &disconnect_mesh_peers,
421                                                       (void *) __LINE__);
422     }
423   }
424
425   switch (client)
426   {
427   case 0L:
428     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
429     break;
430   case 4L:
431     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
432                 "Leaf client %li got a message.\n",
433                 client);
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 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 (channel, 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 (channel, 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_channel_destroy (incoming_ch);
500       incoming_ch = NULL;
501     }
502     else
503     {
504       GNUNET_MESH_channel_destroy (ch);
505       ch = 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 channel
532  * the other peer initiated.
533  *
534  * @param cls Closure.
535  * @param channel New handle to the channel.
536  * @param initiator Peer that started the channel.
537  * @param port Port this channel is connected to.
538  * @param options channel option flags
539  * @return Initial channel context for the channel
540  *         (can be NULL -- that's not an error).
541  */
542 static void *
543 incoming_channel (void *cls, struct GNUNET_MESH_Channel *channel,
544                  const struct GNUNET_PeerIdentity *initiator,
545                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
546 {
547   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
548               "Incoming channel from %s to peer %d\n",
549               GNUNET_i2s (initiator), (long) cls);
550   ok++;
551   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
552   if ((long) cls == 4L)
553     incoming_ch = channel;
554   else
555   {
556     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
557                 "Incoming channel for unknown client %lu\n", (long) cls);
558     GNUNET_break(0);
559   }
560   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
561   {
562     GNUNET_SCHEDULER_cancel (disconnect_task);
563     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
564                                                     &disconnect_mesh_peers,
565                                                     (void *) __LINE__);
566   }
567
568   return NULL;
569 }
570
571 /**
572  * Function called whenever an inbound channel is destroyed.  Should clean up
573  * any associated state.
574  *
575  * @param cls closure (set from GNUNET_MESH_connect)
576  * @param channel connection to the other end (henceforth invalid)
577  * @param channel_ctx place where local state associated
578  *                   with the channel is stored
579  */
580 static void
581 channel_cleaner (void *cls, const struct GNUNET_MESH_Channel *channel,
582                  void *channel_ctx)
583 {
584   long i = (long) cls;
585
586   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587               "Incoming channel disconnected at peer %d\n",
588               i);
589   if (4L == i)
590   {
591     ok++;
592     GNUNET_break (channel == incoming_ch);
593     incoming_ch = NULL;
594   }
595   else if (0L == i)
596   {
597     if (P2P_SIGNAL == test)
598     {
599       ok ++;
600     }
601     GNUNET_break (channel == ch);
602     ch = NULL;
603   }
604   else
605     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606                 "Unknown peer! %d\n", i);
607   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
608
609   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
610   {
611     GNUNET_SCHEDULER_cancel (disconnect_task);
612     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_mesh_peers,
613                                                 (void *) __LINE__);
614   }
615
616   return;
617 }
618
619
620 /**
621  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE MESH SERVICES.
622  *
623  * Testcase continues when the root receives confirmation of connected peers,
624  * on callback funtion ch.
625  *
626  * @param cls Closure (unsued).
627  * @param tc Task Context.
628  */
629 static void
630 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
631 {
632   enum GNUNET_MESH_ChannelOption flags;
633
634   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
635   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "add peer 2\n");
636
637   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638               "schedule timeout in TIMEOUT\n");
639   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
640   {
641     GNUNET_SCHEDULER_cancel (disconnect_task);
642   }
643
644   flags = GNUNET_MESH_OPTION_DEFAULT;
645   if (SPEED_REL == test)
646   {
647     test = SPEED;
648     flags |= GNUNET_MESH_OPTION_RELIABLE;
649   }
650   ch = GNUNET_MESH_channel_create (h1, NULL, p_id[1], 1, flags);
651
652   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
653                                                   &disconnect_mesh_peers,
654                                                   (void *) __LINE__);
655   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
656               "Sending data initializer...\n");
657   data_ack = 0;
658   data_received = 0;
659   data_sent = 0;
660   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
661                                      GNUNET_TIME_UNIT_FOREVER_REL,
662                                      size_payload, &tmt_rdy, (void *) 1L);
663 }
664
665 /**
666  * Callback to be called when the requested peer information is available
667  *
668  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
669  * @param op the operation this callback corresponds to
670  * @param pinfo the result; will be NULL if the operation has failed
671  * @param emsg error message if the operation has failed;
672  *             NULL if the operation is successfull
673  */
674 static void
675 pi_cb (void *cls,
676        struct GNUNET_TESTBED_Operation *op,
677        const struct GNUNET_TESTBED_PeerInformation *pinfo,
678        const char *emsg)
679 {
680   long i = (long) cls;
681
682   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
683
684   if (NULL == pinfo || NULL != emsg)
685   {
686     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
687     abort_test (__LINE__);
688     return;
689   }
690   p_id[i] = pinfo->result.id;
691   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
692   p_ids++;
693   if (p_ids < 2)
694     return;
695   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
696   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
697                                             &do_test, NULL);
698 }
699
700 /**
701  * test main: start test when all peers are connected
702  *
703  * @param cls Closure.
704  * @param ctx Argument to give to GNUNET_MESH_TEST_cleanup on test end.
705  * @param num_peers Number of peers that are running.
706  * @param peers Array of peers.
707  * @param meshes Handle to each of the MESHs of the peers.
708  */
709 static void
710 tmain (void *cls,
711        struct GNUNET_MESH_TEST_Context *ctx,
712        unsigned int num_peers,
713        struct GNUNET_TESTBED_Peer **peers,
714        struct GNUNET_MESH_Handle **meshes)
715 {
716   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
717   ok = 0;
718   test_ctx = ctx;
719   peers_running = num_peers;
720   h1 = meshes[0];
721   h2 = meshes[num_peers - 1];
722   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
723                                                   &disconnect_mesh_peers,
724                                                   (void *) __LINE__);
725   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
726                                                   &shutdown_task, NULL);
727   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
728                                                  GNUNET_TESTBED_PIT_IDENTITY,
729                                                  &pi_cb, (void *) 0L);
730   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
731                                                  GNUNET_TESTBED_PIT_IDENTITY,
732                                                  &pi_cb, (void *) 1L);
733   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
734 }
735
736
737 /**
738  * Main: start test
739  */
740 int
741 main (int argc, char *argv[])
742 {
743   initialized = GNUNET_NO;
744   uint32_t ports[2];
745   const char *config_file;
746
747   GNUNET_log_setup ("test", "DEBUG", NULL);
748   config_file = "test_mesh.conf";
749
750   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
751   if (strstr (argv[0], "_small_forward") != NULL)
752   {
753     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
754     test = FORWARD;
755     test_name = "unicast";
756     ok_goal = 4;
757   }
758   else if (strstr (argv[0], "_small_signal") != NULL)
759   {
760     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
761     test = P2P_SIGNAL;
762     test_name = "signal";
763     ok_goal = 4;
764   }
765   else if (strstr (argv[0], "_small_speed_ack") != NULL)
766   {
767    /* Each peer is supposed to generate the following callbacks:
768     * 1 incoming channel (@dest)
769     * TOTAL_PACKETS received data packet (@dest)
770     * TOTAL_PACKETS received data packet (@orig)
771     * 1 received channel destroy (@dest)
772     * _________________________________
773     * 5 x ok expected per peer
774     */
775     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
776     test = SPEED_ACK;
777     test_name = "speed ack";
778     ok_goal = TOTAL_PACKETS * 2 + 2;
779   }
780   else if (strstr (argv[0], "_small_speed") != NULL)
781   {
782    /* Each peer is supposed to generate the following callbacks:
783     * 1 incoming channel (@dest)
784     * 1 initial packet (@dest)
785     * TOTAL_PACKETS received data packet (@dest)
786     * 1 received data packet (@orig)
787     * 1 received channel destroy (@dest)
788     * _________________________________
789     */
790     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
791     ok_goal = TOTAL_PACKETS + 4;
792     if (strstr (argv[0], "_reliable") != NULL)
793     {
794       test = SPEED_REL;
795       test_name = "speed reliable";
796       config_file = "test_mesh_drop.conf";
797     }
798     else
799     {
800       test = SPEED;
801       test_name = "speed";
802     }
803   }
804   else
805   {
806     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
807     test = SETUP;
808     ok_goal = 0;
809   }
810
811   if (strstr (argv[0], "backwards") != NULL)
812   {
813     char *aux;
814
815     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
816     test_backwards = GNUNET_YES;
817     aux = GNUNET_malloc (32);
818     sprintf (aux, "backwards %s", test_name);
819     test_name = aux;
820   }
821
822   p_ids = 0;
823   ports[0] = 1;
824   ports[1] = 0;
825   GNUNET_MESH_TEST_run ("test_mesh_small",
826                         config_file,
827                         5,
828                         &tmain,
829                         NULL, /* tmain cls */
830                         &incoming_channel,
831                         &channel_cleaner,
832                         handlers,
833                         ports);
834
835   if (ok_goal > ok)
836   {
837     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
838                 "FAILED! (%d/%d)\n", ok, ok_goal);
839     return 1;
840   }
841   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
842   return 0;
843 }
844
845 /* end of test_mesh_small.c */
846