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