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