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