2b69579a5afcaa13a08df7e520c65b2ab46b230d
[oweals/gnunet.git] / src / cadet / test_cadet.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 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, const struct GNUNET_SCHEDULER_TaskContext *tc)
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                         const struct GNUNET_SCHEDULER_TaskContext *tc)
281 {
282   long line = (long) cls;
283   unsigned int i;
284
285   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
286     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287                 "disconnecting cadet peers due to SHUTDOWN! called from %ld\n",
288                 line);
289   else
290     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
291                 "disconnecting cadet service of peers, called from line %ld\n",
292                 line);
293   disconnect_task = NULL;
294   for (i = 0; i < 2; i++)
295   {
296     GNUNET_TESTBED_operation_done (t_op[i]);
297   }
298   if (NULL != ch)
299   {
300     if (NULL != th)
301     {
302       GNUNET_CADET_notify_transmit_ready_cancel (th);
303       th = NULL;
304     }
305     GNUNET_CADET_channel_destroy (ch);
306     ch = NULL;
307   }
308   if (NULL != incoming_ch)
309   {
310     if (NULL != incoming_th)
311     {
312       GNUNET_CADET_notify_transmit_ready_cancel (incoming_th);
313       incoming_th = NULL;
314     }
315     GNUNET_CADET_channel_destroy (incoming_ch);
316     incoming_ch = NULL;
317   }
318   GNUNET_CADET_TEST_cleanup (test_ctx);
319   if (NULL != shutdown_handle)
320   {
321     GNUNET_SCHEDULER_cancel (shutdown_handle);
322   }
323   shutdown_handle = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
324 }
325
326
327
328 /**
329  * Stats callback. Finish the stats testbed operation and when all stats have
330  * been iterated, shutdown the test.
331  *
332  * @param cls Closure (line number from which termination was requested).
333  * @param op the operation that has been finished
334  * @param emsg error message in case the operation has failed; will be NULL if
335  *          operation has executed successfully.
336  */
337 static void
338 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
339 {
340   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " KA sent: %u, KA received: %u\n",
341               ka_sent, ka_received);
342   if (KEEPALIVE == test && (ka_sent < 2 || ka_sent > ka_received + 1))
343     ok--;
344   GNUNET_TESTBED_operation_done (stats_op);
345
346   if (NULL != disconnect_task)
347     GNUNET_SCHEDULER_cancel (disconnect_task);
348   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers, cls);
349
350 }
351
352
353 /**
354  * Process statistic values.
355  *
356  * @param cls closure (line number, unused)
357  * @param peer the peer the statistic belong to
358  * @param subsystem name of subsystem that created the statistic
359  * @param name the name of the datum
360  * @param value the current value
361  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
362  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
363  */
364 static int
365 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
366                 const char *subsystem, const char *name,
367                 uint64_t value, int is_persistent)
368 {
369   static const char *s_sent = "# keepalives sent";
370   static const char *s_recv = "# keepalives received";
371   uint32_t i;
372
373   i = GNUNET_TESTBED_get_index (peer);
374   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "STATS PEER %u - %s [%s]: %llu\n",
375               i, subsystem, name, value);
376   if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
377     ka_sent = value;
378
379   if (0 == strncmp(s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
380     ka_received = value;
381
382   return GNUNET_OK;
383 }
384
385
386 /**
387  * Task to gather all statistics.
388  *
389  * @param cls Closure (NULL).
390  * @param tc Task Context.
391  */
392 static void
393 gather_stats_and_exit (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
394 {
395   disconnect_task = NULL;
396   long l = (long) cls;
397
398   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "gathering statistics from line %d\n", l);
399
400   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
401   {
402     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
403                                                 (void *) __LINE__);
404     return;
405   }
406
407
408   if (NULL != ch)
409   {
410     if (NULL != th)
411     {
412       GNUNET_CADET_notify_transmit_ready_cancel (th);
413       th = NULL;
414     }
415     GNUNET_CADET_channel_destroy (ch);
416     ch = NULL;
417   }
418   stats_op = GNUNET_TESTBED_get_statistics (peers_running, testbed_peers,
419                                             "cadet", NULL,
420                                             stats_iterator, stats_cont, cls);
421 }
422
423
424
425 /**
426  * Abort test: schedule disconnect and shutdown immediately
427  *
428  * @param line Line in the code the abort is requested from (__LINE__).
429  */
430 static void
431 abort_test (long line)
432 {
433   if (disconnect_task != NULL)
434   {
435     GNUNET_SCHEDULER_cancel (disconnect_task);
436     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting test from %ld\n", line);
437     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
438                                                 (void *) line);
439   }
440 }
441
442 /**
443  * Transmit ready callback.
444  *
445  * @param cls Closure (message type).
446  * @param size Size of the tranmist buffer.
447  * @param buf Pointer to the beginning of the buffer.
448  *
449  * @return Number of bytes written to buf.
450  */
451 static size_t
452 tmt_rdy (void *cls, size_t size, void *buf);
453
454
455 /**
456  * Task to request a new data transmission.
457  *
458  * @param cls Closure (peer #).
459  * @param tc Task Context.
460  */
461 static void
462 data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
463 {
464   struct GNUNET_CADET_Channel *channel;
465   static struct GNUNET_CADET_TransmitHandle **pth;
466
467   long src;
468
469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
470
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  * @param tc Task Context.
841  */
842 static void
843 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
844 {
845   enum GNUNET_CADET_ChannelOption flags;
846
847   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
848     return;
849
850   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "do_test\n");
851
852   if (NULL != disconnect_task)
853   {
854     GNUNET_SCHEDULER_cancel (disconnect_task);
855   }
856
857   flags = GNUNET_CADET_OPTION_DEFAULT;
858   if (SPEED_REL == test)
859   {
860     test = SPEED;
861     flags |= GNUNET_CADET_OPTION_RELIABLE;
862   }
863   ch = GNUNET_CADET_channel_create (h1, NULL, p_id[1], 1, flags);
864
865   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
866                                                   &gather_stats_and_exit,
867                                                   (void *) __LINE__);
868   if (KEEPALIVE == test)
869     return; /* Don't send any data. */
870
871   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data initializer...\n");
872   data_received = 0;
873   data_sent = 0;
874   ack_received = 0;
875   ack_sent = 0;
876   th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
877                                            GNUNET_TIME_UNIT_FOREVER_REL,
878                                            size_payload + 1000,
879                                            &tmt_rdy, (void *) 0L);
880 }
881
882 /**
883  * Callback to be called when the requested peer information is available
884  *
885  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
886  * @param op the operation this callback corresponds to
887  * @param pinfo the result; will be NULL if the operation has failed
888  * @param emsg error message if the operation has failed;
889  *             NULL if the operation is successfull
890  */
891 static void
892 pi_cb (void *cls,
893        struct GNUNET_TESTBED_Operation *op,
894        const struct GNUNET_TESTBED_PeerInformation *pinfo,
895        const char *emsg)
896 {
897   long i = (long) cls;
898
899   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
900
901   if (NULL == pinfo || NULL != emsg)
902   {
903     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
904     abort_test (__LINE__);
905     return;
906   }
907   p_id[i] = pinfo->result.id;
908   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
909   p_ids++;
910   if (p_ids < 2)
911     return;
912   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
913   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
914                                             &do_test, NULL);
915 }
916
917 /**
918  * test main: start test when all peers are connected
919  *
920  * @param cls Closure.
921  * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
922  * @param num_peers Number of peers that are running.
923  * @param peers Array of peers.
924  * @param cadetes Handle to each of the CADETs of the peers.
925  */
926 static void
927 tmain (void *cls,
928        struct GNUNET_CADET_TEST_Context *ctx,
929        unsigned int num_peers,
930        struct GNUNET_TESTBED_Peer **peers,
931        struct GNUNET_CADET_Handle **cadets)
932 {
933   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
934   ok = 0;
935   test_ctx = ctx;
936   peers_running = num_peers;
937   GNUNET_assert (peers_running == peers_requested);
938   testbed_peers = peers;
939   h1 = cadets[0];
940   h2 = cadets[num_peers - 1];
941   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
942                                                   &disconnect_cadet_peers,
943                                                   (void *) __LINE__);
944   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
945                                                   &shutdown_task, NULL);
946   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
947                                                  GNUNET_TESTBED_PIT_IDENTITY,
948                                                  &pi_cb, (void *) 0L);
949   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
950                                                  GNUNET_TESTBED_PIT_IDENTITY,
951                                                  &pi_cb, (void *) 1L);
952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
953 }
954
955
956 /**
957  * Main: start test
958  */
959 int
960 main (int argc, char *argv[])
961 {
962   initialized = GNUNET_NO;
963   static uint32_t ports[2];
964   const char *config_file;
965
966   GNUNET_log_setup ("test", "DEBUG", NULL);
967   config_file = "test_cadet.conf";
968
969   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
970
971   /* Find out requested size */
972   if (strstr (argv[0], "_2_") != NULL)
973   {
974     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
975     peers_requested = 2;
976   }
977   else if (strstr (argv[0], "_5_") != NULL)
978   {
979     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
980     peers_requested = 5;
981   }
982   else
983   {
984     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
985     peers_requested = 2;
986   }
987
988   /* Find out requested test */
989   if (strstr (argv[0], "_forward") != NULL)
990   {
991     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
992     test = FORWARD;
993     test_name = "unicast";
994     ok_goal = 4;
995   }
996   else if (strstr (argv[0], "_signal") != NULL)
997   {
998     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
999     test = P2P_SIGNAL;
1000     test_name = "signal";
1001     ok_goal = 4;
1002   }
1003   else if (strstr (argv[0], "_speed_ack") != NULL)
1004   {
1005     /* Test is supposed to generate the following callbacks:
1006      * 1 incoming channel (@dest)
1007      * TOTAL_PACKETS received data packet (@dest)
1008      * TOTAL_PACKETS received data packet (@orig)
1009      * 1 received channel destroy (@dest)
1010      */
1011     ok_goal = TOTAL_PACKETS * 2 + 2;
1012     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1013     test = SPEED_ACK;
1014     test_name = "speed ack";
1015   }
1016   else if (strstr (argv[0], "_speed") != NULL)
1017   {
1018     /* Test is supposed to generate the following callbacks:
1019      * 1 incoming channel (@dest)
1020      * 1 initial packet (@dest)
1021      * TOTAL_PACKETS received data packet (@dest)
1022      * 1 received data packet (@orig)
1023      * 1 received channel destroy (@dest)
1024      */
1025     ok_goal = TOTAL_PACKETS + 4;
1026     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1027     if (strstr (argv[0], "_reliable") != NULL)
1028     {
1029       test = SPEED_REL;
1030       test_name = "speed reliable";
1031       config_file = "test_cadet_drop.conf";
1032     }
1033     else
1034     {
1035       test = SPEED;
1036       test_name = "speed";
1037     }
1038   }
1039   else if (strstr (argv[0], "_keepalive") != NULL)
1040   {
1041     test = KEEPALIVE;
1042     /* Test is supposed to generate the following callbacks:
1043      * 1 incoming channel (@dest)
1044      * [wait]
1045      * 1 received channel destroy (@dest)
1046      */
1047     ok_goal = 2;
1048   }
1049   else
1050   {
1051     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1052     test = SETUP;
1053     ok_goal = 0;
1054   }
1055
1056   if (strstr (argv[0], "backwards") != NULL)
1057   {
1058     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1059     test_backwards = GNUNET_YES;
1060     GNUNET_asprintf (&test_name, "backwards %s", test_name);
1061   }
1062
1063   p_ids = 0;
1064   ports[0] = 1;
1065   ports[1] = 0;
1066   GNUNET_CADET_TEST_run ("test_cadet_small",
1067                         config_file,
1068                         peers_requested,
1069                         &tmain,
1070                         NULL, /* tmain cls */
1071                         &incoming_channel,
1072                         &channel_cleaner,
1073                         handlers,
1074                         ports);
1075
1076   if (ok_goal > ok)
1077   {
1078     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1079                 "FAILED! (%d/%d)\n", ok, ok_goal);
1080     return 1;
1081   }
1082   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1083   return 0;
1084 }
1085
1086 /* end of test_cadet.c */
1087