- fix coverity
[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 runnining #data_task().
156  */
157 static struct GNUNET_SCHEDULER_Task *data_job;
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 /**
235  * Show the results of the test (banwidth acheived) and log them to GAUGER
236  */
237 static void
238 show_end_data (void)
239 {
240   static struct GNUNET_TIME_Absolute end_time;
241   static struct GNUNET_TIME_Relative total_time;
242
243   end_time = GNUNET_TIME_absolute_get();
244   total_time = GNUNET_TIME_absolute_get_difference(start_time, end_time);
245   FPRINTF (stderr, "\nResults of test \"%s\"\n", test_name);
246   FPRINTF (stderr, "Test time %s\n",
247            GNUNET_STRINGS_relative_time_to_string (total_time,
248                                                    GNUNET_YES));
249   FPRINTF (stderr, "Test bandwidth: %f kb/s\n",
250            4 * TOTAL_PACKETS * 1.0 / (total_time.rel_value_us / 1000)); // 4bytes * ms
251   FPRINTF (stderr, "Test throughput: %f packets/s\n\n",
252            TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000)); // packets * ms
253   GAUGER ("CADET", test_name,
254           TOTAL_PACKETS * 1000.0 / (total_time.rel_value_us / 1000),
255           "packets/s");
256 }
257
258
259 /**
260  * Disconnect from cadet services af all peers, call shutdown.
261  *
262  * @param cls Closure (line number from which termination was requested).
263  * @param tc Task Context.
264  */
265 static void
266 disconnect_cadet_peers (void *cls)
267 {
268   long line = (long) cls;
269   unsigned int i;
270
271   disconnect_task = NULL;
272   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
273               "disconnecting cadet service of peers, called from line %ld\n",
274               line);
275   for (i = 0; i < 2; i++)
276   {
277     GNUNET_TESTBED_operation_done (t_op[i]);
278   }
279   if (NULL != ch)
280   {
281     if (NULL != th)
282     {
283       GNUNET_CADET_notify_transmit_ready_cancel (th);
284       th = NULL;
285     }
286     GNUNET_CADET_channel_destroy (ch);
287     ch = NULL;
288   }
289   if (NULL != incoming_ch)
290   {
291     if (NULL != incoming_th)
292     {
293       GNUNET_CADET_notify_transmit_ready_cancel (incoming_th);
294       incoming_th = NULL;
295     }
296     GNUNET_CADET_channel_destroy (incoming_ch);
297     incoming_ch = NULL;
298   }
299   GNUNET_CADET_TEST_cleanup (test_ctx);
300   GNUNET_SCHEDULER_shutdown ();
301 }
302
303
304 /**
305  * Shut down peergroup, clean up.
306  *
307  * @param cls Closure (unused).
308  * @param tc Task Context.
309  */
310 static void
311 shutdown_task (void *cls)
312 {
313   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Ending test.\n");
314   if (NULL != data_job)
315   {
316     GNUNET_SCHEDULER_cancel (data_job);
317     data_job = NULL;
318   }
319   if (NULL != test_task)
320   {
321     GNUNET_SCHEDULER_cancel (test_task);
322     test_task = NULL;
323   }
324   if (NULL != disconnect_task)
325   {
326     GNUNET_SCHEDULER_cancel (disconnect_task);
327     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
328                                                 (void *) __LINE__);
329   }
330 }
331
332
333 /**
334  * Stats callback. Finish the stats testbed operation and when all stats have
335  * been iterated, shutdown the test.
336  *
337  * @param cls Closure (line number from which termination was requested).
338  * @param op the operation that has been finished
339  * @param emsg error message in case the operation has failed; will be NULL if
340  *          operation has executed successfully.
341  */
342 static void
343 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
344 {
345   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " KA sent: %u, KA received: %u\n",
346               ka_sent, ka_received);
347   if (KEEPALIVE == test && (ka_sent < 2 || ka_sent > ka_received + 1))
348     ok--;
349   GNUNET_TESTBED_operation_done (stats_op);
350
351   if (NULL != disconnect_task)
352     GNUNET_SCHEDULER_cancel (disconnect_task);
353   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
354                                               cls);
355 }
356
357
358 /**
359  * Process statistic values.
360  *
361  * @param cls closure (line number, unused)
362  * @param peer the peer the statistic belong to
363  * @param subsystem name of subsystem that created the statistic
364  * @param name the name of the datum
365  * @param value the current value
366  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
367  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
368  */
369 static int
370 stats_iterator (void *cls,
371                 const struct GNUNET_TESTBED_Peer *peer,
372                 const char *subsystem,
373                 const char *name,
374                 uint64_t value,
375                 int is_persistent)
376 {
377   static const char *s_sent = "# keepalives sent";
378   static const char *s_recv = "# keepalives received";
379   uint32_t i;
380
381   i = GNUNET_TESTBED_get_index (peer);
382   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
383               "STATS PEER %u - %s [%s]: %llu\n",
384               i,
385               subsystem,
386               name,
387               (unsigned long long) value);
388   if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
389     ka_sent = value;
390
391   if (0 == strncmp(s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
392     ka_received = value;
393
394   return GNUNET_OK;
395 }
396
397
398 /**
399  * Task to gather all statistics.
400  *
401  * @param cls Closure (NULL).
402  */
403 static void
404 gather_stats_and_exit (void *cls)
405 {
406   long l = (long) cls;
407
408   disconnect_task = NULL;
409   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
410               "gathering statistics from line %d\n",
411               (int) l);
412   if (NULL != ch)
413   {
414     if (NULL != th)
415     {
416       GNUNET_CADET_notify_transmit_ready_cancel (th);
417       th = NULL;
418     }
419     GNUNET_CADET_channel_destroy (ch);
420     ch = NULL;
421   }
422   stats_op = GNUNET_TESTBED_get_statistics (peers_running, testbed_peers,
423                                             "cadet", NULL,
424                                             stats_iterator, stats_cont, cls);
425 }
426
427
428
429 /**
430  * Abort test: schedule disconnect and shutdown immediately
431  *
432  * @param line Line in the code the abort is requested from (__LINE__).
433  */
434 static void
435 abort_test (long line)
436 {
437   if (disconnect_task != NULL)
438   {
439     GNUNET_SCHEDULER_cancel (disconnect_task);
440     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting test from %ld\n", line);
441     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
442                                                 (void *) line);
443   }
444 }
445
446 /**
447  * Transmit ready callback.
448  *
449  * @param cls Closure (message type).
450  * @param size Size of the tranmist buffer.
451  * @param buf Pointer to the beginning of the buffer.
452  *
453  * @return Number of bytes written to buf.
454  */
455 static size_t
456 tmt_rdy (void *cls, size_t size, void *buf);
457
458
459 /**
460  * Task to request a new data transmission.
461  *
462  * @param cls Closure (peer #).
463  */
464 static void
465 data_task (void *cls)
466 {
467   struct GNUNET_CADET_Channel *channel;
468   static struct GNUNET_CADET_TransmitHandle **pth;
469   long src;
470
471   data_job = NULL;
472   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
473   if (GNUNET_YES == test_backwards)
474   {
475     channel = incoming_ch;
476     pth = &incoming_th;
477     src = peers_requested - 1;
478   }
479   else
480   {
481     channel = ch;
482     pth = &th;
483     src = 0;
484   }
485
486   GNUNET_assert (NULL != channel);
487   GNUNET_assert (NULL == *pth);
488
489   *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
490                                              GNUNET_TIME_UNIT_FOREVER_REL,
491                                              size_payload + data_sent,
492                                              &tmt_rdy, (void *) src);
493   if (NULL == *pth)
494   {
495     unsigned long i = (unsigned long) cls;
496
497     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retransmission\n");
498     if (0 == i)
499     {
500       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "  in 1 ms\n");
501       data_job = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
502                                                &data_task, (void *) 1L);
503     }
504     else
505     {
506       i++;
507       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
508                   "in %llu ms\n",
509                   (unsigned long long) i);
510       data_job = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (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                 (unsigned int) size,
548                 buf,
549                 data_sent,
550                 ack_received);
551     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
552     GNUNET_break (ok >= ok_goal - 2);
553
554     return 0;
555   }
556   msg->size = htons (msg_size);
557   msg->type = htons (1);
558   data = (uint32_t *) &msg[1];
559   *data = htonl (counter);
560   if (GNUNET_NO == initialized)
561   {
562     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending initializer\n");
563     msg_size = size_payload + 1000;
564     if (SPEED_ACK == test)
565       data_sent++;
566   }
567   else if (SPEED == test || SPEED_ACK == test)
568   {
569     if (get_expected_target() == id)
570       ack_sent++;
571     else
572       data_sent++;
573     counter++;
574     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
575                 " Sent message %u size %u\n",
576                 counter,
577                 (unsigned int) msg_size);
578     if (data_sent < TOTAL_PACKETS && SPEED == test)
579     {
580       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Scheduling message %d\n",
581                   counter + 1);
582       data_job = GNUNET_SCHEDULER_add_now (&data_task, NULL);
583     }
584   }
585
586   return msg_size;
587 }
588
589
590 /**
591  * Function is called whenever a message is received.
592  *
593  * @param cls closure (set from GNUNET_CADET_connect, peer number)
594  * @param channel connection to the other end
595  * @param channel_ctx place to store local state associated with the channel
596  * @param message the actual message
597  * @return GNUNET_OK to keep the connection open,
598  *         GNUNET_SYSERR to close it (signal serious error)
599  */
600 int
601 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
602                void **channel_ctx,
603                const struct GNUNET_MessageHeader *message)
604 {
605   struct GNUNET_CADET_TransmitHandle **pth;
606   long client = (long) cls;
607   long expected_target_client;
608   uint32_t *data;
609   uint32_t payload;
610   unsigned int counter;
611
612   ok++;
613   counter = get_expected_target () == client ? data_received : ack_received;
614
615   GNUNET_CADET_receive_done (channel);
616
617   if ((ok % 10) == 0)
618   {
619     if (NULL != disconnect_task)
620     {
621       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " reschedule timeout\n");
622       GNUNET_SCHEDULER_cancel (disconnect_task);
623       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
624                                                       &gather_stats_and_exit,
625                                                       (void *) __LINE__);
626     }
627   }
628
629   switch (client)
630   {
631   case 0L:
632     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
633     GNUNET_assert (channel == ch);
634     pth = &th;
635     break;
636   case 1L:
637   case 4L:
638     GNUNET_assert (client == peers_requested - 1);
639     GNUNET_assert (channel == incoming_ch);
640     pth = &incoming_th;
641     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Leaf client %ld got a message.\n",
642                 client);
643     break;
644   default:
645     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Client %ld not valid.\n", client);
646     GNUNET_assert (0);
647   }
648   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
649   data = (uint32_t *) &message[1];
650   payload = ntohl (*data);
651   if (payload == counter)
652   {
653     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload as expected: %u\n", payload);
654   }
655   else
656   {
657     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " payload %u, expected: %u\n",
658                 payload, counter);
659   }
660   expected_target_client = get_expected_target ();
661
662   if (GNUNET_NO == initialized)
663   {
664     initialized = GNUNET_YES;
665     start_time = GNUNET_TIME_absolute_get ();
666     if (SPEED == test)
667     {
668       GNUNET_assert (peers_requested - 1 == client);
669       data_job = GNUNET_SCHEDULER_add_now (&data_task, NULL);
670       return GNUNET_OK;
671     }
672   }
673
674   counter++;
675   if (client == expected_target_client) /* Normally 4 */
676   {
677     data_received++;
678     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
679     if (SPEED != test || (ok_goal - 2) == ok)
680     {
681       /* Send ACK */
682       GNUNET_assert (NULL == *pth);
683       *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
684                                                  GNUNET_TIME_UNIT_FOREVER_REL,
685                                                  size_payload + ack_sent,
686                                                  &tmt_rdy, (void *) client);
687       return GNUNET_OK;
688     }
689     else
690     {
691       if (data_received < TOTAL_PACKETS)
692         return GNUNET_OK;
693     }
694   }
695   else /* Normally 0 */
696   {
697     if (SPEED_ACK == test || SPEED == test)
698     {
699       ack_received++;
700       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
701       /* send more data */
702       GNUNET_assert (NULL == *pth);
703       *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
704                                                  GNUNET_TIME_UNIT_FOREVER_REL,
705                                                  size_payload + data_sent,
706                                                  &tmt_rdy, (void *) client);
707       if (ack_received < TOTAL_PACKETS && SPEED != test)
708         return GNUNET_OK;
709       if (ok == 2 && SPEED == test)
710         return GNUNET_OK;
711       show_end_data();
712     }
713     if (test == P2P_SIGNAL)
714     {
715       if (NULL != incoming_th)
716       {
717         GNUNET_CADET_notify_transmit_ready_cancel (incoming_th);
718         incoming_th = NULL;
719       }
720       GNUNET_CADET_channel_destroy (incoming_ch);
721       incoming_ch = NULL;
722     }
723     else
724     {
725       if (NULL != th)
726       {
727         GNUNET_CADET_notify_transmit_ready_cancel (th);
728         th = NULL;
729       }
730       GNUNET_CADET_channel_destroy (ch);
731       ch = NULL;
732     }
733   }
734
735   return GNUNET_OK;
736 }
737
738
739 /**
740  * Data handlers for every message type of CADET's payload.
741  * {callback_function, message_type, size_expected}
742  */
743 static struct GNUNET_CADET_MessageHandler handlers[] = {
744   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
745   {NULL, 0, 0}
746 };
747
748
749 /**
750  * Method called whenever another peer has added us to a channel
751  * the other peer initiated.
752  *
753  * @param cls Closure.
754  * @param channel New handle to the channel.
755  * @param initiator Peer that started the channel.
756  * @param port Port this channel is connected to.
757  * @param options channel option flags
758  * @return Initial channel context for the channel
759  *         (can be NULL -- that's not an error).
760  */
761 static void *
762 incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
763                  const struct GNUNET_PeerIdentity *initiator,
764                  uint32_t port, enum GNUNET_CADET_ChannelOption options)
765 {
766   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
767               "Incoming channel from %s to peer %d\n",
768               GNUNET_i2s (initiator),
769               (int) (long) cls);
770   ok++;
771   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
772   if ((long) cls == peers_requested - 1)
773     incoming_ch = channel;
774   else
775   {
776     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
777                 "Incoming channel for unknown client %lu\n", (long) cls);
778     GNUNET_break(0);
779   }
780   if (NULL != disconnect_task)
781   {
782     GNUNET_SCHEDULER_cancel (disconnect_task);
783     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
784                                                     &gather_stats_and_exit,
785                                                     (void *) __LINE__);
786   }
787
788   return NULL;
789 }
790
791 /**
792  * Function called whenever an inbound channel is destroyed.  Should clean up
793  * any associated state.
794  *
795  * @param cls closure (set from GNUNET_CADET_connect, peer number)
796  * @param channel connection to the other end (henceforth invalid)
797  * @param channel_ctx place where local state associated
798  *                   with the channel is stored
799  */
800 static void
801 channel_cleaner (void *cls, const struct GNUNET_CADET_Channel *channel,
802                  void *channel_ctx)
803 {
804   long i = (long) cls;
805
806   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
807               "Incoming channel disconnected at peer %ld\n", i);
808   if (peers_running - 1 == i)
809   {
810     ok++;
811     GNUNET_break (channel == incoming_ch);
812     incoming_ch = NULL;
813   }
814   else if (0L == i)
815   {
816     if (P2P_SIGNAL == test)
817     {
818       ok ++;
819     }
820     GNUNET_break (channel == ch);
821     ch = NULL;
822   }
823   else
824     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
825                 "Unknown peer! %d\n",
826                 (int) i);
827   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
828
829   if (NULL != disconnect_task)
830   {
831     GNUNET_SCHEDULER_cancel (disconnect_task);
832     disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit,
833                                                 (void *) __LINE__);
834   }
835
836   return;
837 }
838
839
840 /**
841  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
842  *
843  * Testcase continues when the root receives confirmation of connected peers,
844  * on callback function ch.
845  *
846  * @param cls Closure (unused).
847  */
848 static void
849 do_test (void *cls)
850 {
851   enum GNUNET_CADET_ChannelOption flags;
852
853   test_task = NULL;
854   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "do_test\n");
855
856   if (NULL != disconnect_task)
857   {
858     GNUNET_SCHEDULER_cancel (disconnect_task);
859     disconnect_task = NULL;
860   }
861
862   flags = GNUNET_CADET_OPTION_DEFAULT;
863   if (SPEED_REL == test)
864   {
865     test = SPEED;
866     flags |= GNUNET_CADET_OPTION_RELIABLE;
867   }
868   ch = GNUNET_CADET_channel_create (h1, NULL, p_id[1], 1, flags);
869
870   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
871                                                   &gather_stats_and_exit,
872                                                   (void *) __LINE__);
873   if (KEEPALIVE == test)
874     return; /* Don't send any data. */
875
876   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending data initializer...\n");
877   data_received = 0;
878   data_sent = 0;
879   ack_received = 0;
880   ack_sent = 0;
881   th = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
882                                            GNUNET_TIME_UNIT_FOREVER_REL,
883                                            size_payload + 1000,
884                                            &tmt_rdy, (void *) 0L);
885 }
886
887
888 /**
889  * Callback to be called when the requested peer information is available
890  *
891  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
892  * @param op the operation this callback corresponds to
893  * @param pinfo the result; will be NULL if the operation has failed
894  * @param emsg error message if the operation has failed;
895  *             NULL if the operation is successfull
896  */
897 static void
898 pi_cb (void *cls,
899        struct GNUNET_TESTBED_Operation *op,
900        const struct GNUNET_TESTBED_PeerInformation *pinfo,
901        const char *emsg)
902 {
903   long i = (long) cls;
904
905   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
906
907   if (NULL == pinfo || NULL != emsg)
908   {
909     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
910     abort_test (__LINE__);
911     return;
912   }
913   p_id[i] = pinfo->result.id;
914   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
915   p_ids++;
916   if (p_ids < 2)
917     return;
918   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
919   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
920                                             &do_test, NULL);
921 }
922
923 /**
924  * test main: start test when all peers are connected
925  *
926  * @param cls Closure.
927  * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
928  * @param num_peers Number of peers that are running.
929  * @param peers Array of peers.
930  * @param cadetes Handle to each of the CADETs of the peers.
931  */
932 static void
933 tmain (void *cls,
934        struct GNUNET_CADET_TEST_Context *ctx,
935        unsigned int num_peers,
936        struct GNUNET_TESTBED_Peer **peers,
937        struct GNUNET_CADET_Handle **cadets)
938 {
939   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
940   ok = 0;
941   test_ctx = ctx;
942   peers_running = num_peers;
943   GNUNET_assert (peers_running == peers_requested);
944   testbed_peers = peers;
945   h1 = cadets[0];
946   h2 = cadets[num_peers - 1];
947   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
948                                                   &disconnect_cadet_peers,
949                                                   (void *) __LINE__);
950   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
951   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
952                                                  GNUNET_TESTBED_PIT_IDENTITY,
953                                                  &pi_cb, (void *) 0L);
954   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
955                                                  GNUNET_TESTBED_PIT_IDENTITY,
956                                                  &pi_cb, (void *) 1L);
957   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
958 }
959
960
961 /**
962  * Main: start test
963  */
964 int
965 main (int argc, char *argv[])
966 {
967   initialized = GNUNET_NO;
968   static uint32_t ports[2];
969   const char *config_file;
970
971   GNUNET_log_setup ("test", "DEBUG", NULL);
972   config_file = "test_cadet.conf";
973
974   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
975
976   /* Find out requested size */
977   if (strstr (argv[0], "_2_") != NULL)
978   {
979     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
980     peers_requested = 2;
981   }
982   else if (strstr (argv[0], "_5_") != NULL)
983   {
984     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
985     peers_requested = 5;
986   }
987   else
988   {
989     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
990     peers_requested = 2;
991   }
992
993   /* Find out requested test */
994   if (strstr (argv[0], "_forward") != NULL)
995   {
996     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
997     test = FORWARD;
998     test_name = "unicast";
999     ok_goal = 4;
1000   }
1001   else if (strstr (argv[0], "_signal") != NULL)
1002   {
1003     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
1004     test = P2P_SIGNAL;
1005     test_name = "signal";
1006     ok_goal = 4;
1007   }
1008   else if (strstr (argv[0], "_speed_ack") != NULL)
1009   {
1010     /* Test is supposed to generate the following callbacks:
1011      * 1 incoming channel (@dest)
1012      * TOTAL_PACKETS received data packet (@dest)
1013      * TOTAL_PACKETS received data packet (@orig)
1014      * 1 received channel destroy (@dest)
1015      */
1016     ok_goal = TOTAL_PACKETS * 2 + 2;
1017     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1018     test = SPEED_ACK;
1019     test_name = "speed ack";
1020   }
1021   else if (strstr (argv[0], "_speed") != NULL)
1022   {
1023     /* Test is supposed to generate the following callbacks:
1024      * 1 incoming channel (@dest)
1025      * 1 initial packet (@dest)
1026      * TOTAL_PACKETS received data packet (@dest)
1027      * 1 received data packet (@orig)
1028      * 1 received channel destroy (@dest)
1029      */
1030     ok_goal = TOTAL_PACKETS + 4;
1031     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1032     if (strstr (argv[0], "_reliable") != NULL)
1033     {
1034       test = SPEED_REL;
1035       test_name = "speed reliable";
1036       config_file = "test_cadet_drop.conf";
1037     }
1038     else
1039     {
1040       test = SPEED;
1041       test_name = "speed";
1042     }
1043   }
1044   else if (strstr (argv[0], "_keepalive") != NULL)
1045   {
1046     test = KEEPALIVE;
1047     /* Test is supposed to generate the following callbacks:
1048      * 1 incoming channel (@dest)
1049      * [wait]
1050      * 1 received channel destroy (@dest)
1051      */
1052     ok_goal = 2;
1053   }
1054   else
1055   {
1056     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1057     test = SETUP;
1058     ok_goal = 0;
1059   }
1060
1061   if (strstr (argv[0], "backwards") != NULL)
1062   {
1063     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1064     test_backwards = GNUNET_YES;
1065     GNUNET_asprintf (&test_name, "backwards %s", test_name);
1066   }
1067
1068   p_ids = 0;
1069   ports[0] = 1;
1070   ports[1] = 0;
1071   GNUNET_CADET_TEST_run ("test_cadet_small",
1072                         config_file,
1073                         peers_requested,
1074                         &tmain,
1075                         NULL, /* tmain cls */
1076                         &incoming_channel,
1077                         &channel_cleaner,
1078                         handlers,
1079                         ports);
1080
1081   if (ok_goal > ok)
1082   {
1083     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1084                 "FAILED! (%d/%d)\n", ok, ok_goal);
1085     return 1;
1086   }
1087   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1088   return 0;
1089 }
1090
1091 /* end of test_cadet.c */