39bfc5fed4b02b34621185c052441c8b4444419e
[oweals/gnunet.git] / src / cadet / test_cadet.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file cadet/test_cadet.c
22  *
23  * @brief Test for the cadet service: retransmission of traffic.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "cadet_test_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_statistics_service.h"
30 #include <gauger.h>
31
32
33 /**
34  * How many messages to send
35  */
36 #define TOTAL_PACKETS 200 /* Cannot exceed 64k! */
37
38 /**
39  * How long until we give up on connecting the peers?
40  */
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
42
43 /**
44  * Time to wait for stuff that should be rather fast
45  */
46 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
47
48 /**
49  * DIFFERENT TESTS TO RUN
50  */
51 #define SETUP 0
52 #define FORWARD 1
53 #define KEEPALIVE 2
54 #define SPEED 3
55 #define SPEED_ACK 4
56 #define SPEED_REL 8
57 #define P2P_SIGNAL 10
58
59 /**
60  * Which test are we running?
61  */
62 static int test;
63
64 /**
65  * String with test name
66  */
67 char *test_name;
68
69 /**
70  * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
71  */
72 static int test_backwards = GNUNET_NO;
73
74 /**
75  * How many events have happened
76  */
77 static int ok;
78
79 /**
80  * Number of events expected to conclude the test successfully.
81  */
82 int ok_goal;
83
84 /**
85  * Size of each test packet
86  */
87 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
88
89 /**
90  * Operation to get peer ids.
91  */
92 struct GNUNET_TESTBED_Operation *t_op[2];
93
94 /**
95  * Peer ids.
96  */
97 struct GNUNET_PeerIdentity *p_id[2];
98
99 /**
100  * Peer ids counter.
101  */
102 unsigned int p_ids;
103
104 /**
105  * Is the setup initialized?
106  */
107 static int initialized;
108
109 /**
110  * Number of payload packes sent.
111  */
112 static int data_sent;
113
114 /**
115  * Number of payload packets received.
116  */
117 static int data_received;
118
119 /**
120  * Number of payload packed acknowledgements sent.
121  */
122 static int ack_sent;
123
124 /**
125  * Number of payload packed explicitly (app level) acknowledged.
126  */
127 static int ack_received;
128
129 /**
130  * Total number of peers asked to run.
131  */
132 static unsigned long long peers_requested;
133
134 /**
135  * Number of currently running peers (should be same as @c peers_requested).
136  */
137 static unsigned long long peers_running;
138
139 /**
140  * Test context (to shut down).
141  */
142 struct GNUNET_CADET_TEST_Context *test_ctx;
143
144 /**
145  * Task called to disconnect peers.
146  */
147 static struct GNUNET_SCHEDULER_Task * disconnect_task;
148
149 /**
150  * Task To perform tests
151  */
152 static struct GNUNET_SCHEDULER_Task * test_task;
153
154 /**
155  * Task called to shutdown test.
156  */
157 static struct GNUNET_SCHEDULER_Task * shutdown_handle;
158
159 /**
160  * Cadet handle for the root peer
161  */
162 static struct GNUNET_CADET_Handle *h1;
163
164 /**
165  * Cadet handle for the first leaf peer
166  */
167 static struct GNUNET_CADET_Handle *h2;
168
169 /**
170  * Channel handle for the root peer
171  */
172 static struct GNUNET_CADET_Channel *ch;
173
174 /**
175  * Channel handle for the dest peer
176  */
177 static struct GNUNET_CADET_Channel *incoming_ch;
178
179 /**
180  * Transmit handle for root data calls
181  */
182 static struct GNUNET_CADET_TransmitHandle *th;
183
184 /**
185  * Transmit handle for root data calls
186  */
187 static struct GNUNET_CADET_TransmitHandle *incoming_th;
188
189
190 /**
191  * Time we started the data transmission (after channel has been established
192  * and initilized).
193  */
194 static struct GNUNET_TIME_Absolute start_time;
195
196 /**
197  * Peers handle.
198  */
199 static struct GNUNET_TESTBED_Peer **testbed_peers;
200
201 /**
202  * Statistics operation handle.
203  */
204 static struct GNUNET_TESTBED_Operation *stats_op;
205
206 /**
207  * Keepalives sent.
208  */
209 static unsigned int ka_sent;
210
211 /**
212  * Keepalives received.
213  */
214 static unsigned int ka_received;
215
216
217 /**
218  * Get the client number considered as the "target" or "receiver", depending on
219  * the test type and size.
220  *
221  * @return Peer # of the target client, either 0 (for backward tests) or
222  *         the last peer in the line (for other tests).
223  */
224 static unsigned int
225 get_expected_target ()
226 {
227   if (SPEED == test && GNUNET_YES == test_backwards)
228     return 0;
229   else
230     return peers_requested - 1;
231 }
232
233 /**
234  * Show the results of the test (banwidth acheived) and log them to GAUGER
235  */
236 static void
237 show_end_data (void)
238 {
239   static struct GNUNET_TIME_Absolute end_time;
240   static struct GNUNET_TIME_Relative total_time;
241
242   end_time = GNUNET_TIME_absolute_get();
243   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
244   FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
245   FPRINTF (stderr, "Test time %s\n",
246            GNUNET_STRINGS_relative_time_to_string (total_time,
247                                                    GNUNET_YES));
248   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
249            4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
250   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
251            TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
252   GAUGER ("CADET", test_name,
253           TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
254           "packets/s");
255 }
256
257
258 /**
259  * Shut down peergroup, clean up.
260  *
261  * @param cls Closure (unused).
262  * @param tc Task Context.
263  */
264 static void
265 shutdown_task (void *cls)
266 {
267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
268   shutdown_handle = NULL;
269 }
270
271
272 /**
273  * Disconnect from cadet services af all peers, call shutdown.
274  *
275  * @param cls Closure (line number from which termination was requested).
276  * @param tc Task Context.
277  */
278 static void
279 disconnect_cadet_peers (void *cls)
280 {
281   long line = (long) cls;
282   unsigned int i;
283   const struct GNUNET_SCHEDULER_TaskContext *tc;
284
285   tc = GNUNET_SCHEDULER_get_task_context ();
286   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
287     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288                 "disconnecting cadet peers due to SHUTDOWN! called from %ld\n",
289                 line);
290   else
291     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
292                 "disconnecting cadet service of peers, called from line %ld\n",
293                 line);
294   disconnect_task = NULL;
295   for (i = 0; i < 2; i++)
296   {
297     GNUNET_TESTBED_operation_done (t_op[i]);
298   }
299   if (NULL != ch)
300   {
301     if (NULL != th)
302     {
303       GNUNET_CADET_notify_transmit_ready_cancel (th);
304       th = NULL;
305     }
306     GNUNET_CADET_channel_destroy (ch);
307     ch = NULL;
308   }
309   if (NULL != incoming_ch)
310   {
311     if (NULL != incoming_th)
312     {
313       GNUNET_CADET_notify_transmit_ready_cancel (incoming_th);
314       incoming_th = NULL;
315     }
316     GNUNET_CADET_channel_destroy (incoming_ch);
317     incoming_ch = NULL;
318   }
319   GNUNET_CADET_TEST_cleanup (test_ctx);
320   if (NULL != shutdown_handle)
321   {
322     GNUNET_SCHEDULER_cancel (shutdown_handle);
323   }
324   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
325 }
326
327
328
329 /**
330  * Stats callback. Finish the stats testbed operation and when all stats have
331  * been iterated, shutdown the test.
332  *
333  * @param cls Closure (line number from which termination was requested).
334  * @param op the operation that has been finished
335  * @param emsg error message in case the operation has failed; will be NULL if
336  *          operation has executed successfully.
337  */
338 static void
339 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
340 {
341   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " KA sent: %u, KA received: %u\n",
342               ka_sent, ka_received);
343   if (KEEPALIVE == test && (ka_sent < 2 || ka_sent > ka_received + 1))
344     ok--;
345   GNUNET_TESTBED_operation_done (stats_op);
346
347   if (NULL != disconnect_task)
348     GNUNET_SCHEDULER_cancel (disconnect_task);
349   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, cls);
350
351 }
352
353
354 /**
355  * Process statistic values.
356  *
357  * @param cls closure (line number, unused)
358  * @param peer the peer the statistic belong to
359  * @param subsystem name of subsystem that created the statistic
360  * @param name the name of the datum
361  * @param value the current value
362  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
363  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
364  */
365 static int
366 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
367                 const char *subsystem, const char *name,
368                 uint64_t value, int is_persistent)
369 {
370   static const char *s_sent = "# keepalives sent";
371   static const char *s_recv = "# keepalives received";
372   uint32_t i;
373
374   i = GNUNET_TESTBED_get_index (peer);
375   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n",
376               i, subsystem, name, value);
377   if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
378     ka_sent = value;
379
380   if (0 == strncmp(s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
381     ka_received = value;
382
383   return GNUNET_OK;
384 }
385
386
387 /**
388  * Task to gather all statistics.
389  *
390  * @param cls Closure (NULL).
391  */
392 static void
393 gather_stats_and_exit (void *cls)
394 {
395   long l = (long) cls;
396   const struct GNUNET_SCHEDULER_TaskContext *tc;
397
398   disconnect_task = NULL;
399   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "gathering statistics from line %d\n", l);
400   tc = GNUNET_SCHEDULER_get_task_context ();
401   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
402   {
403     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
404                                                 (void *) __LINE__);
405     return;
406   }
407
408
409   if (NULL != ch)
410   {
411     if (NULL != th)
412     {
413       GNUNET_CADET_notify_transmit_ready_cancel (th);
414       th = NULL;
415     }
416     GNUNET_CADET_channel_destroy (ch);
417     ch = NULL;
418   }
419   stats_op = GNUNET_TESTBED_get_statistics (peers_running, testbed_peers,
420                                             "cadet", NULL,
421                                             stats_iterator, stats_cont, cls);
422 }
423
424
425
426 /**
427  * Abort test: schedule disconnect and shutdown immediately
428  *
429  * @param line Line in the code the abort is requested from (__LINE__).
430  */
431 static void
432 abort_test (long line)
433 {
434   if (disconnect_task != NULL)
435   {
436     GNUNET_SCHEDULER_cancel (disconnect_task);
437     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting test from %ld\n", line);
438     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
439                                                 (void *) line);
440   }
441 }
442
443 /**
444  * Transmit ready callback.
445  *
446  * @param cls Closure (message type).
447  * @param size Size of the tranmist buffer.
448  * @param buf Pointer to the beginning of the buffer.
449  *
450  * @return Number of bytes written to buf.
451  */
452 static size_t
453 tmt_rdy (void *cls, size_t size, void *buf);
454
455
456 /**
457  * Task to request a new data transmission.
458  *
459  * @param cls Closure (peer #).
460  */
461 static void
462 data_task (void *cls)
463 {
464   struct GNUNET_CADET_Channel *channel;
465   static struct GNUNET_CADET_TransmitHandle **pth;
466   const struct GNUNET_SCHEDULER_TaskContext *tc;
467   long src;
468
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
470   tc = GNUNET_SCHEDULER_get_task_context ();
471   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
472     return;
473
474   if (GNUNET_YES == test_backwards)
475   {
476     channel = incoming_ch;
477     pth = &incoming_th;
478     src = peers_requested - 1;
479   }
480   else
481   {
482     channel = ch;
483     pth = &th;
484     src = 0;
485   }
486
487   GNUNET_assert (NULL != channel);
488   GNUNET_assert (NULL == *pth);
489
490   *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
491                                              GNUNET_TIME_UNIT_FOREVER_REL,
492                                              size_payload + data_sent,
493                                              &tmt_rdy, (void *) src);
494   if (NULL == *pth)
495   {
496     unsigned long i = (unsigned long) cls;
497
498     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retransmission\n");
499     if (0 == i)
500     {
501       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "  in 1 ms\n");
502       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
503                                     &data_task, (void *) 1L);
504     }
505     else
506     {
507       i++;
508       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "in %u ms\n", i);
509       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
510                                         GNUNET_TIME_UNIT_MILLISECONDS,
511                                         i),
512                                     &data_task, (void *) i);
513     }
514   }
515 }
516
517
518 /**
519  * Transmit ready callback
520  *
521  * @param cls Closure (peer # which is sending the data).
522  * @param size Size of the buffer we have.
523  * @param buf Buffer to copy data to.
524  */
525 size_t
526 tmt_rdy (void *cls, size_t size, void *buf)
527 {
528   struct GNUNET_MessageHeader *msg = buf;
529   size_t msg_size;
530   uint32_t *data;
531   long id = (long) cls;
532   unsigned int counter;
533
534   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tmt_rdy on %ld, filling buffer\n", id);
535   if (0 == id)
536     th = NULL;
537   else if ((peers_requested - 1) == id)
538     incoming_th = NULL;
539   else
540     GNUNET_assert (0);
541   counter = get_expected_target () == id ? ack_sent : data_sent;
542   msg_size = size_payload + counter;
543   if (size < msg_size || NULL == buf)
544   {
545     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546                 "size %u, buf %p, data_sent %u, ack_received %u\n",
547                 size, buf, data_sent, ack_received);
548     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
549     GNUNET_break (ok >= ok_goal - 2);
550
551     return 0;
552   }
553   msg->size = htons (msg_size);
554   msg->type = htons (1);
555   data = (uint32_t *) &msg[1];
556   *data = htonl (counter);
557   if (GNUNET_NO == initialized)
558   {
559     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending initializer\n");
560     msg_size = size_payload + 1000;
561     if (SPEED_ACK == test)
562       data_sent++;
563   }
564   else if (SPEED == test || SPEED_ACK == test)
565   {
566     if (get_expected_target() == id)
567       ack_sent++;
568     else
569       data_sent++;
570     counter++;
571     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " Sent message %d size %u\n",
572                 counter, msg_size);
573     if (data_sent < TOTAL_PACKETS && SPEED == test)
574     {
575       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Scheduling message %d\n",
576                   counter + 1);
577       GNUNET_SCHEDULER_add_now (&data_task, NULL);
578     }
579   }
580
581   return msg_size;
582 }
583
584
585 /**
586  * Function is called whenever a message is received.
587  *
588  * @param cls closure (set from GNUNET_CADET_connect, peer number)
589  * @param channel connection to the other end
590  * @param channel_ctx place to store local state associated with the channel
591  * @param message the actual message
592  * @return GNUNET_OK to keep the connection open,
593  *         GNUNET_SYSERR to close it (signal serious error)
594  */
595 int
596 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
597                void **channel_ctx,
598                const struct GNUNET_MessageHeader *message)
599 {
600   struct GNUNET_CADET_TransmitHandle **pth;
601   long client = (long) cls;
602   long expected_target_client;
603   uint32_t *data;
604   uint32_t payload;
605   unsigned int counter;
606
607   ok++;
608   counter = get_expected_target () == client ? data_received : ack_received;
609
610   GNUNET_CADET_receive_done (channel);
611
612   if ((ok % 10) == 0)
613   {
614     if (NULL != disconnect_task)
615     {
616       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " reschedule timeout\n");
617       GNUNET_SCHEDULER_cancel (disconnect_task);
618       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
619                                                       &gather_stats_and_exit,
620                                                       (void *) __LINE__);
621     }
622   }
623
624   switch (client)
625   {
626   case 0L:
627     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
628     GNUNET_assert (channel == ch);
629     pth = &th;
630     break;
631   case 1L:
632   case 4L:
633     GNUNET_assert (client == peers_requested - 1);
634     GNUNET_assert (channel == incoming_ch);
635     pth = &incoming_th;
636     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Leaf client %ld got a message.\n",
637                 client);
638     break;
639   default:
640     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Client %ld not valid.\n", client);
641     GNUNET_assert (0);
642   }
643   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
644   data = (uint32_t *) &message[1];
645   payload = ntohl (*data);
646   if (payload == counter)
647   {
648     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload as expected: %u\n", payload);
649   }
650   else
651   {
652     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " payload %u, expected: %u\n",
653                 payload, counter);
654   }
655   expected_target_client = get_expected_target ();
656
657   if (GNUNET_NO == initialized)
658   {
659     initialized = GNUNET_YES;
660     start_time = GNUNET_TIME_absolute_get ();
661     if (SPEED == test)
662     {
663       GNUNET_assert (peers_requested - 1 == client);
664       GNUNET_SCHEDULER_add_now (&data_task, NULL);
665       return GNUNET_OK;
666     }
667   }
668
669   counter++;
670   if (client == expected_target_client) /* Normally 4 */
671   {
672     data_received++;
673     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
674     if (SPEED != test || (ok_goal - 2) == ok)
675     {
676       /* Send ACK */
677       GNUNET_assert (NULL == *pth);
678       *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
679                                                  GNUNET_TIME_UNIT_FOREVER_REL,
680                                                  size_payload + ack_sent,
681                                                  &tmt_rdy, (void *) client);
682       return GNUNET_OK;
683     }
684     else
685     {
686       if (data_received < TOTAL_PACKETS)
687         return GNUNET_OK;
688     }
689   }
690   else /* Normally 0 */
691   {
692     if (SPEED_ACK == test || SPEED == test)
693     {
694       ack_received++;
695       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
696       /* send more data */
697       GNUNET_assert (NULL == *pth);
698       *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
699                                                  GNUNET_TIME_UNIT_FOREVER_REL,
700                                                  size_payload + data_sent,
701                                                  &tmt_rdy, (void *) client);
702       if (ack_received < TOTAL_PACKETS && SPEED != test)
703         return GNUNET_OK;
704       if (ok == 2 && SPEED == test)
705         return GNUNET_OK;
706       show_end_data();
707     }
708     if (test == P2P_SIGNAL)
709     {
710       if (NULL != incoming_th)
711       {
712         GNUNET_CADET_notify_transmit_ready_cancel (incoming_th);
713         incoming_th = NULL;
714       }
715       GNUNET_CADET_channel_destroy (incoming_ch);
716       incoming_ch = NULL;
717     }
718     else
719     {
720       if (NULL != th)
721       {
722         GNUNET_CADET_notify_transmit_ready_cancel (th);
723         th = NULL;
724       }
725       GNUNET_CADET_channel_destroy (ch);
726       ch = NULL;
727     }
728   }
729
730   return GNUNET_OK;
731 }
732
733
734 /**
735  * Data handlers for every message type of CADET's payload.
736  * {callback_function, message_type, size_expected}
737  */
738 static struct GNUNET_CADET_MessageHandler handlers[] = {
739   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
740   {NULL, 0, 0}
741 };
742
743
744 /**
745  * Method called whenever another peer has added us to a channel
746  * the other peer initiated.
747  *
748  * @param cls Closure.
749  * @param channel New handle to the channel.
750  * @param initiator Peer that started the channel.
751  * @param port Port this channel is connected to.
752  * @param options channel option flags
753  * @return Initial channel context for the channel
754  *         (can be NULL -- that's not an error).
755  */
756 static void *
757 incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
758                  const struct GNUNET_PeerIdentity *initiator,
759                  uint32_t port, enum GNUNET_CADET_ChannelOption options)
760 {
761   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
762               "Incoming channel from %s to peer %d\n",
763               GNUNET_i2s (initiator), (long) cls);
764   ok++;
765   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
766   if ((long) cls == peers_requested - 1)
767     incoming_ch = channel;
768   else
769   {
770     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
771                 "Incoming channel for unknown client %lu\n", (long) cls);
772     GNUNET_break(0);
773   }
774   if (NULL != disconnect_task)
775   {
776     GNUNET_SCHEDULER_cancel (disconnect_task);
777     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
778                                                     &gather_stats_and_exit,
779                                                     (void *) __LINE__);
780   }
781
782   return NULL;
783 }
784
785 /**
786  * Function called whenever an inbound channel is destroyed.  Should clean up
787  * any associated state.
788  *
789  * @param cls closure (set from GNUNET_CADET_connect, peer number)
790  * @param channel connection to the other end (henceforth invalid)
791  * @param channel_ctx place where local state associated
792  *                   with the channel is stored
793  */
794 static void
795 channel_cleaner (void *cls, const struct GNUNET_CADET_Channel *channel,
796                  void *channel_ctx)
797 {
798   long i = (long) cls;
799
800   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
801               "Incoming channel disconnected at peer %ld\n", i);
802   if (peers_running - 1 == i)
803   {
804     ok++;
805     GNUNET_break (channel == incoming_ch);
806     incoming_ch = NULL;
807   }
808   else if (0L == i)
809   {
810     if (P2P_SIGNAL == test)
811     {
812       ok ++;
813     }
814     GNUNET_break (channel == ch);
815     ch = NULL;
816   }
817   else
818     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
819                 "Unknown peer! %d\n", i);
820   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
821
822   if (NULL != disconnect_task)
823   {
824     GNUNET_SCHEDULER_cancel (disconnect_task);
825     disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit,
826                                                 (void *) __LINE__);
827   }
828
829   return;
830 }
831
832
833 /**
834  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
835  *
836  * Testcase continues when the root receives confirmation of connected peers,
837  * on callback function ch.
838  *
839  * @param cls Closure (unused).
840  */
841 static void
842 do_test (void *cls)
843 {
844   enum GNUNET_CADET_ChannelOption flags;
845   const struct GNUNET_SCHEDULER_TaskContext *tc;
846
847   tc = GNUNET_SCHEDULER_get_task_context ();
848   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
849     return;
850
851   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "do_test\n");
852
853   if (NULL != disconnect_task)
854   {
855     GNUNET_SCHEDULER_cancel (disconnect_task);
856   }
857
858   flags = GNUNET_CADET_OPTION_DEFAULT;
859   if (SPEED_REL == test)
860   {
861     test = SPEED;
862     flags |= GNUNET_CADET_OPTION_RELIABLE;
863   }
864   ch = GNUNET_CADET_channel_create (h1, NULL, p_id[1], 1, flags);
865
866   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
867                                                   &gather_stats_and_exit,
868                                                   (void *) __LINE__);
869   if (KEEPALIVE == test)
870     return; /* Don't send any data. */
871
872   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data initializer...\n");
873   data_received = 0;
874   data_sent = 0;
875   ack_received = 0;
876   ack_sent = 0;
877   th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
878                                            GNUNET_TIME_UNIT_FOREVER_REL,
879                                            size_payload + 1000,
880                                            &tmt_rdy, (void *) 0L);
881 }
882
883 /**
884  * Callback to be called when the requested peer information is available
885  *
886  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
887  * @param op the operation this callback corresponds to
888  * @param pinfo the result; will be NULL if the operation has failed
889  * @param emsg error message if the operation has failed;
890  *             NULL if the operation is successfull
891  */
892 static void
893 pi_cb (void *cls,
894        struct GNUNET_TESTBED_Operation *op,
895        const struct GNUNET_TESTBED_PeerInformation *pinfo,
896        const char *emsg)
897 {
898   long i = (long) cls;
899
900   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
901
902   if (NULL == pinfo || NULL != emsg)
903   {
904     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
905     abort_test (__LINE__);
906     return;
907   }
908   p_id[i] = pinfo->result.id;
909   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
910   p_ids++;
911   if (p_ids < 2)
912     return;
913   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
914   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
915                                             &do_test, NULL);
916 }
917
918 /**
919  * test main: start test when all peers are connected
920  *
921  * @param cls Closure.
922  * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
923  * @param num_peers Number of peers that are running.
924  * @param peers Array of peers.
925  * @param cadetes Handle to each of the CADETs of the peers.
926  */
927 static void
928 tmain (void *cls,
929        struct GNUNET_CADET_TEST_Context *ctx,
930        unsigned int num_peers,
931        struct GNUNET_TESTBED_Peer **peers,
932        struct GNUNET_CADET_Handle **cadets)
933 {
934   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
935   ok = 0;
936   test_ctx = ctx;
937   peers_running = num_peers;
938   GNUNET_assert (peers_running == peers_requested);
939   testbed_peers = peers;
940   h1 = cadets[0];
941   h2 = cadets[num_peers - 1];
942   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
943                                                   &disconnect_cadet_peers,
944                                                   (void *) __LINE__);
945   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
946                                                   &shutdown_task, NULL);
947   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
948                                                  GNUNET_TESTBED_PIT_IDENTITY,
949                                                  &pi_cb, (void *) 0L);
950   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
951                                                  GNUNET_TESTBED_PIT_IDENTITY,
952                                                  &pi_cb, (void *) 1L);
953   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
954 }
955
956
957 /**
958  * Main: start test
959  */
960 int
961 main (int argc, char *argv[])
962 {
963   initialized = GNUNET_NO;
964   static uint32_t ports[2];
965   const char *config_file;
966
967   GNUNET_log_setup ("test", "DEBUG", NULL);
968   config_file = "test_cadet.conf";
969
970   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
971
972   /* Find out requested size */
973   if (strstr (argv[0], "_2_") != NULL)
974   {
975     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
976     peers_requested = 2;
977   }
978   else if (strstr (argv[0], "_5_") != NULL)
979   {
980     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
981     peers_requested = 5;
982   }
983   else
984   {
985     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
986     peers_requested = 2;
987   }
988
989   /* Find out requested test */
990   if (strstr (argv[0], "_forward") != NULL)
991   {
992     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
993     test = FORWARD;
994     test_name = "unicast";
995     ok_goal = 4;
996   }
997   else if (strstr (argv[0], "_signal") != NULL)
998   {
999     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
1000     test = P2P_SIGNAL;
1001     test_name = "signal";
1002     ok_goal = 4;
1003   }
1004   else if (strstr (argv[0], "_speed_ack") != NULL)
1005   {
1006     /* Test is supposed to generate the following callbacks:
1007      * 1 incoming channel (@dest)
1008      * TOTAL_PACKETS received data packet (@dest)
1009      * TOTAL_PACKETS received data packet (@orig)
1010      * 1 received channel destroy (@dest)
1011      */
1012     ok_goal = TOTAL_PACKETS * 2 + 2;
1013     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1014     test = SPEED_ACK;
1015     test_name = "speed ack";
1016   }
1017   else if (strstr (argv[0], "_speed") != NULL)
1018   {
1019     /* Test is supposed to generate the following callbacks:
1020      * 1 incoming channel (@dest)
1021      * 1 initial packet (@dest)
1022      * TOTAL_PACKETS received data packet (@dest)
1023      * 1 received data packet (@orig)
1024      * 1 received channel destroy (@dest)
1025      */
1026     ok_goal = TOTAL_PACKETS + 4;
1027     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1028     if (strstr (argv[0], "_reliable") != NULL)
1029     {
1030       test = SPEED_REL;
1031       test_name = "speed reliable";
1032       config_file = "test_cadet_drop.conf";
1033     }
1034     else
1035     {
1036       test = SPEED;
1037       test_name = "speed";
1038     }
1039   }
1040   else if (strstr (argv[0], "_keepalive") != NULL)
1041   {
1042     test = KEEPALIVE;
1043     /* Test is supposed to generate the following callbacks:
1044      * 1 incoming channel (@dest)
1045      * [wait]
1046      * 1 received channel destroy (@dest)
1047      */
1048     ok_goal = 2;
1049   }
1050   else
1051   {
1052     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1053     test = SETUP;
1054     ok_goal = 0;
1055   }
1056
1057   if (strstr (argv[0], "backwards") != NULL)
1058   {
1059     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1060     test_backwards = GNUNET_YES;
1061     GNUNET_asprintf (&test_name, "backwards %s", test_name);
1062   }
1063
1064   p_ids = 0;
1065   ports[0] = 1;
1066   ports[1] = 0;
1067   GNUNET_CADET_TEST_run ("test_cadet_small",
1068                         config_file,
1069                         peers_requested,
1070                         &tmain,
1071                         NULL, /* tmain cls */
1072                         &incoming_channel,
1073                         &channel_cleaner,
1074                         handlers,
1075                         ports);
1076
1077   if (ok_goal > ok)
1078   {
1079     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1080                 "FAILED! (%d/%d)\n", ok, ok_goal);
1081     return 1;
1082   }
1083   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1084   return 0;
1085 }
1086
1087 /* end of test_cadet.c */