16466ca0d694226c58a26beb35a79a0316cc14ae
[oweals/gnunet.git] / src / cadet / test_cadet.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file cadet/test_cadet.c
22  *
23  * @brief Test for the cadet service: retransmission of traffic.
24  */
25 #include <stdio.h>
26 #include "platform.h"
27 #include "cadet_test_lib.h"
28 #include "gnunet_cadet_service.h"
29 #include "gnunet_statistics_service.h"
30 #include <gauger.h>
31
32
33 /**
34  * How many messages to send
35  */
36 #define TOTAL_PACKETS 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 char *test_name;
68
69 /**
70  * Flag to send traffic leaf->root in speed tests to test BCK_ACK logic.
71  */
72 static int test_backwards = GNUNET_NO;
73
74 /**
75  * How many events have happened
76  */
77 static int ok;
78
79 /**
80  * Number of events expected to conclude the test successfully.
81  */
82 int ok_goal;
83
84 /**
85  * Size of each test packet
86  */
87 size_t size_payload = sizeof (struct GNUNET_MessageHeader) + sizeof (uint32_t);
88
89 /**
90  * Operation to get peer ids.
91  */
92 struct GNUNET_TESTBED_Operation *t_op[2];
93
94 /**
95  * Peer ids.
96  */
97 struct GNUNET_PeerIdentity *p_id[2];
98
99 /**
100  * Port ID
101  */
102 struct GNUNET_HashCode port;
103
104 /**
105  * Peer ids counter.
106  */
107 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, " KA sent: %u, KA received: %u\n",
351               ka_sent, ka_received);
352   if (KEEPALIVE == test && (ka_sent < 2 || ka_sent > ka_received + 1))
353     ok--;
354   GNUNET_TESTBED_operation_done (stats_op);
355
356   if (NULL != disconnect_task)
357     GNUNET_SCHEDULER_cancel (disconnect_task);
358   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
359                                               cls);
360 }
361
362
363 /**
364  * Process statistic values.
365  *
366  * @param cls closure (line number, unused)
367  * @param peer the peer the statistic belong to
368  * @param subsystem name of subsystem that created the statistic
369  * @param name the name of the datum
370  * @param value the current value
371  * @param is_persistent #GNUNET_YES if the value is persistent, #GNUNET_NO if not
372  * @return #GNUNET_OK to continue, #GNUNET_SYSERR to abort iteration
373  */
374 static int
375 stats_iterator (void *cls,
376                 const struct GNUNET_TESTBED_Peer *peer,
377                 const char *subsystem,
378                 const char *name,
379                 uint64_t value,
380                 int is_persistent)
381 {
382   static const char *s_sent = "# keepalives sent";
383   static const char *s_recv = "# keepalives received";
384   uint32_t i;
385
386   i = GNUNET_TESTBED_get_index (peer);
387   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
388               "STATS PEER %u - %s [%s]: %llu\n",
389               i,
390               subsystem,
391               name,
392               (unsigned long long) value);
393   if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
394     ka_sent = value;
395
396   if (0 == strncmp(s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
397     ka_received = value;
398
399   return GNUNET_OK;
400 }
401
402
403 /**
404  * Task to gather all statistics.
405  *
406  * @param cls Closure (NULL).
407  */
408 static void
409 gather_stats_and_exit (void *cls)
410 {
411   long l = (long) cls;
412
413   disconnect_task = NULL;
414   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
415               "gathering statistics from line %d\n",
416               (int) l);
417   if (NULL != ch)
418   {
419     if (NULL != th)
420     {
421       GNUNET_CADET_notify_transmit_ready_cancel (th);
422       th = NULL;
423     }
424     GNUNET_CADET_channel_destroy (ch);
425     ch = NULL;
426   }
427   stats_op = GNUNET_TESTBED_get_statistics (peers_running, testbed_peers,
428                                             "cadet", NULL,
429                                             stats_iterator, stats_cont, cls);
430 }
431
432
433
434 /**
435  * Abort test: schedule disconnect and shutdown immediately
436  *
437  * @param line Line in the code the abort is requested from (__LINE__).
438  */
439 static void
440 abort_test (long line)
441 {
442   if (disconnect_task != NULL)
443   {
444     GNUNET_SCHEDULER_cancel (disconnect_task);
445     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting test from %ld\n", line);
446     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
447                                                 (void *) line);
448   }
449 }
450
451 /**
452  * Transmit ready callback.
453  *
454  * @param cls Closure (message type).
455  * @param size Size of the tranmist buffer.
456  * @param buf Pointer to the beginning of the buffer.
457  *
458  * @return Number of bytes written to buf.
459  */
460 static size_t
461 tmt_rdy (void *cls, size_t size, void *buf);
462
463
464 /**
465  * Task to request a new data transmission.
466  *
467  * @param cls Closure (peer #).
468  */
469 static void
470 data_task (void *cls)
471 {
472   struct GNUNET_CADET_Channel *channel;
473   static struct GNUNET_CADET_TransmitHandle **pth;
474   long src;
475
476   data_job = NULL;
477   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Data task\n");
478   if (GNUNET_YES == test_backwards)
479   {
480     channel = incoming_ch;
481     pth = &incoming_th;
482     src = peers_requested - 1;
483   }
484   else
485   {
486     channel = ch;
487     pth = &th;
488     src = 0;
489   }
490
491   GNUNET_assert (NULL != channel);
492   GNUNET_assert (NULL == *pth);
493
494   *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
495                                              GNUNET_TIME_UNIT_FOREVER_REL,
496                                              size_payload + data_sent,
497                                              &tmt_rdy, (void *) src);
498   if (NULL == *pth)
499   {
500     unsigned long i = (unsigned long) cls;
501
502     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Retransmission\n");
503     if (0 == i)
504     {
505       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "  in 1 ms\n");
506       data_job = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
507                                                &data_task, (void *) 1L);
508     }
509     else
510     {
511       i++;
512       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
513                   "in %llu ms\n",
514                   (unsigned long long) i);
515       data_job = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
516                                                                               i),
517                                                &data_task, (void *) i);
518     }
519   }
520 }
521
522
523 /**
524  * Transmit ready callback
525  *
526  * @param cls Closure (peer # which is sending the data).
527  * @param size Size of the buffer we have.
528  * @param buf Buffer to copy data to.
529  */
530 static size_t
531 tmt_rdy (void *cls, size_t size, void *buf)
532 {
533   struct GNUNET_MessageHeader *msg = buf;
534   size_t msg_size;
535   uint32_t *data;
536   long id = (long) cls;
537   unsigned int counter;
538
539   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540               "tmt_rdy on %ld, filling buffer\n",
541               id);
542   if (0 == id)
543     th = NULL;
544   else if ((peers_requested - 1) == id)
545     incoming_th = NULL;
546   else
547     GNUNET_assert (0);
548   counter = get_expected_target () == id ? ack_sent : data_sent;
549   msg_size = size_payload + counter;
550   GNUNET_assert (msg_size > sizeof (struct GNUNET_MessageHeader));
551   if ( (size < msg_size) ||
552        (NULL == buf) )
553   {
554     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555                 "size %u, buf %p, data_sent %u, ack_received %u\n",
556                 (unsigned int) size,
557                 buf,
558                 data_sent,
559                 ack_received);
560     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ok %u, ok goal %u\n", ok, ok_goal);
561     GNUNET_break (ok >= ok_goal - 2);
562
563     return 0;
564   }
565   msg->size = htons (msg_size);
566   msg->type = htons (1);
567   data = (uint32_t *) &msg[1];
568   *data = htonl (counter);
569   if (GNUNET_NO == initialized)
570   {
571     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572                 "sending initializer\n");
573     msg_size = size_payload + 1000;
574     msg->size = htons (msg_size);
575   if (SPEED_ACK == test)
576       data_sent++;
577   }
578   else if ( (SPEED == test) ||
579             (SPEED_ACK == test) )
580   {
581     if (get_expected_target() == id)
582       ack_sent++;
583     else
584       data_sent++;
585     counter++;
586     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
587                 " Sent message %u size %u\n",
588                 counter,
589                 (unsigned int) msg_size);
590     if ( (data_sent < TOTAL_PACKETS) &&
591          (SPEED == test) )
592     {
593       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
594                   " Scheduling message %d\n",
595                   counter + 1);
596       data_job = GNUNET_SCHEDULER_add_now (&data_task, NULL);
597     }
598   }
599
600   return msg_size;
601 }
602
603
604 /**
605  * Function is called whenever a message is received.
606  *
607  * @param cls closure (set from GNUNET_CADET_connect(), peer number)
608  * @param channel connection to the other end
609  * @param channel_ctx place to store local state associated with the channel
610  * @param message the actual message
611  * @return #GNUNET_OK to keep the connection open,
612  *         #GNUNET_SYSERR to close it (signal serious error)
613  */
614 static int
615 data_callback (void *cls,
616                struct GNUNET_CADET_Channel *channel,
617                void **channel_ctx,
618                const struct GNUNET_MessageHeader *message)
619 {
620   struct GNUNET_CADET_TransmitHandle **pth;
621   long client = (long) cls;
622   long expected_target_client;
623   uint32_t *data;
624   uint32_t payload;
625   unsigned int counter;
626
627   ok++;
628   counter = get_expected_target () == client ? data_received : ack_received;
629
630   GNUNET_CADET_receive_done (channel);
631
632   if ((ok % 10) == 0)
633   {
634     if (NULL != disconnect_task)
635     {
636       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " reschedule timeout\n");
637       GNUNET_SCHEDULER_cancel (disconnect_task);
638       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
639                                                       &gather_stats_and_exit,
640                                                       (void *) __LINE__);
641     }
642   }
643
644   switch (client)
645   {
646   case 0L:
647     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
648     GNUNET_assert (channel == ch);
649     pth = &th;
650     break;
651   case 1L:
652   case 4L:
653     GNUNET_assert (client == peers_requested - 1);
654     GNUNET_assert (channel == incoming_ch);
655     pth = &incoming_th;
656     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Leaf client %ld got a message.\n",
657                 client);
658     break;
659   default:
660     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Client %ld not valid.\n", client);
661     GNUNET_assert (0);
662   }
663   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
664   data = (uint32_t *) &message[1];
665   payload = ntohl (*data);
666   if (payload == counter)
667   {
668     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload as expected: %u\n", payload);
669   }
670   else
671   {
672     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " payload %u, expected: %u\n",
673                 payload, counter);
674   }
675   expected_target_client = get_expected_target ();
676
677   if (GNUNET_NO == initialized)
678   {
679     initialized = GNUNET_YES;
680     start_time = GNUNET_TIME_absolute_get ();
681     if (SPEED == test)
682     {
683       GNUNET_assert (peers_requested - 1 == client);
684       data_job = GNUNET_SCHEDULER_add_now (&data_task, NULL);
685       return GNUNET_OK;
686     }
687   }
688
689   counter++;
690   if (client == expected_target_client) /* Normally 4 */
691   {
692     data_received++;
693     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
694     if (SPEED != test || (ok_goal - 2) == ok)
695     {
696       /* Send ACK */
697       GNUNET_assert (NULL == *pth);
698       *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
699                                                  GNUNET_TIME_UNIT_FOREVER_REL,
700                                                  size_payload + ack_sent,
701                                                  &tmt_rdy, (void *) client);
702       return GNUNET_OK;
703     }
704     else
705     {
706       if (data_received < TOTAL_PACKETS)
707         return GNUNET_OK;
708     }
709   }
710   else /* Normally 0 */
711   {
712     if (SPEED_ACK == test || SPEED == test)
713     {
714       ack_received++;
715       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", ack_received);
716       /* send more data */
717       GNUNET_assert (NULL == *pth);
718       *pth = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
719                                                  GNUNET_TIME_UNIT_FOREVER_REL,
720                                                  size_payload + data_sent,
721                                                  &tmt_rdy, (void *) client);
722       if (ack_received < TOTAL_PACKETS && SPEED != test)
723         return GNUNET_OK;
724       if (ok == 2 && SPEED == test)
725         return GNUNET_OK;
726       show_end_data();
727     }
728     if (test == P2P_SIGNAL)
729     {
730       if (NULL != incoming_th)
731       {
732         GNUNET_CADET_notify_transmit_ready_cancel (incoming_th);
733         incoming_th = NULL;
734       }
735       GNUNET_CADET_channel_destroy (incoming_ch);
736       incoming_ch = NULL;
737     }
738     else
739     {
740       if (NULL != th)
741       {
742         GNUNET_CADET_notify_transmit_ready_cancel (th);
743         th = NULL;
744       }
745       GNUNET_CADET_channel_destroy (ch);
746       ch = NULL;
747     }
748   }
749
750   return GNUNET_OK;
751 }
752
753
754 /**
755  * Data handlers for every message type of CADET's payload.
756  * {callback_function, message_type, size_expected}
757  */
758 static struct GNUNET_CADET_MessageHandler handlers[] = {
759   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
760   {NULL, 0, 0}
761 };
762
763
764 /**
765  * Method called whenever another peer has added us to a channel
766  * the other peer initiated.
767  *
768  * @param cls Closure.
769  * @param channel New handle to the channel.
770  * @param initiator Peer that started the channel.
771  * @param port Port this channel is connected to.
772  * @param options channel option flags
773  * @return Initial channel context for the channel
774  *         (can be NULL -- that's not an error).
775  */
776 static void *
777 incoming_channel (void *cls,
778                   struct GNUNET_CADET_Channel *channel,
779                   const struct GNUNET_PeerIdentity *initiator,
780                   const struct GNUNET_HashCode *port,
781                   enum GNUNET_CADET_ChannelOption options)
782 {
783   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
784               "Incoming channel from %s to peer %d:%s\n",
785               GNUNET_i2s (initiator),
786               (int) (long) cls, GNUNET_h2s (port));
787   ok++;
788   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
789   if ((long) cls == peers_requested - 1)
790     incoming_ch = channel;
791   else
792   {
793     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
794                 "Incoming channel for unknown client %lu\n", (long) cls);
795     GNUNET_break(0);
796   }
797   if (NULL != disconnect_task)
798   {
799     GNUNET_SCHEDULER_cancel (disconnect_task);
800     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
801                                                     &gather_stats_and_exit,
802                                                     (void *) __LINE__);
803   }
804
805   return NULL;
806 }
807
808
809 /**
810  * Function called whenever an inbound channel is destroyed.  Should clean up
811  * any associated state.
812  *
813  * @param cls closure (set from GNUNET_CADET_connect, peer number)
814  * @param channel connection to the other end (henceforth invalid)
815  * @param channel_ctx place where local state associated
816  *                   with the channel is stored
817  */
818 static void
819 channel_cleaner (void *cls,
820                  const struct GNUNET_CADET_Channel *channel,
821                  void *channel_ctx)
822 {
823   long i = (long) cls;
824
825   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
826               "Incoming channel disconnected at peer %ld\n",
827               i);
828   if (peers_running - 1 == i)
829   {
830     ok++;
831     GNUNET_break (channel == incoming_ch);
832     incoming_ch = NULL;
833   }
834   else if (0L == i)
835   {
836     if (P2P_SIGNAL == test)
837     {
838       ok++;
839     }
840     GNUNET_break (channel == ch);
841     ch = NULL;
842   }
843   else
844     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
845                 "Unknown peer! %d\n",
846                 (int) i);
847   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
848
849   if (NULL != disconnect_task)
850   {
851     GNUNET_SCHEDULER_cancel (disconnect_task);
852     disconnect_task = GNUNET_SCHEDULER_add_now (&gather_stats_and_exit,
853                                                 (void *) __LINE__);
854   }
855 }
856
857
858 /**
859  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
860  *
861  * Testcase continues when the root receives confirmation of connected peers,
862  * on callback function ch.
863  *
864  * @param cls Closure (unused).
865  */
866 static void
867 do_test (void *cls)
868 {
869   enum GNUNET_CADET_ChannelOption flags;
870
871   test_task = NULL;
872   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873               "do_test\n");
874   if (NULL != disconnect_task)
875   {
876     GNUNET_SCHEDULER_cancel (disconnect_task);
877     disconnect_task = NULL;
878   }
879
880   flags = GNUNET_CADET_OPTION_DEFAULT;
881   if (SPEED_REL == test)
882   {
883     test = SPEED;
884     flags |= GNUNET_CADET_OPTION_RELIABLE;
885   }
886
887   ch = GNUNET_CADET_channel_create (h1,
888                                     NULL,
889                                     p_id[1],
890                                     &port,
891                                     flags);
892
893   disconnect_task
894     = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
895                                     &gather_stats_and_exit,
896                                     (void *) __LINE__);
897   if (KEEPALIVE == test)
898     return; /* Don't send any data. */
899
900   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901               "Sending data initializer...\n");
902   data_received = 0;
903   data_sent = 0;
904   ack_received = 0;
905   ack_sent = 0;
906   th = GNUNET_CADET_notify_transmit_ready (ch,
907                                            GNUNET_NO,
908                                            GNUNET_TIME_UNIT_FOREVER_REL,
909                                            size_payload + 1000,
910                                            &tmt_rdy, (void *) 0L);
911 }
912
913
914 /**
915  * Callback to be called when the requested peer information is available
916  *
917  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
918  * @param op the operation this callback corresponds to
919  * @param pinfo the result; will be NULL if the operation has failed
920  * @param emsg error message if the operation has failed;
921  *             NULL if the operation is successfull
922  */
923 static void
924 pi_cb (void *cls,
925        struct GNUNET_TESTBED_Operation *op,
926        const struct GNUNET_TESTBED_PeerInformation *pinfo,
927        const char *emsg)
928 {
929   long i = (long) cls;
930
931   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932               "id callback for %ld\n", i);
933
934   if ( (NULL == pinfo) ||
935        (NULL != emsg) )
936   {
937     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
938                 "pi_cb: %s\n", emsg);
939     abort_test (__LINE__);
940     return;
941   }
942   p_id[i] = pinfo->result.id;
943   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944               "  id: %s\n", GNUNET_i2s (p_id[i]));
945   p_ids++;
946   if (p_ids < 2)
947     return;
948   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949               "Got all IDs, starting test\n");
950   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
951                                             &do_test,
952                                             NULL);
953 }
954
955
956 /**
957  * test main: start test when all peers are connected
958  *
959  * @param cls Closure.
960  * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
961  * @param num_peers Number of peers that are running.
962  * @param peers Array of peers.
963  * @param cadetes Handle to each of the CADETs of the peers.
964  */
965 static void
966 tmain (void *cls,
967        struct GNUNET_CADET_TEST_Context *ctx,
968        unsigned int num_peers,
969        struct GNUNET_TESTBED_Peer **peers,
970        struct GNUNET_CADET_Handle **cadets)
971 {
972   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
973   ok = 0;
974   test_ctx = ctx;
975   peers_running = num_peers;
976   GNUNET_assert (peers_running == peers_requested);
977   testbed_peers = peers;
978   h1 = cadets[0];
979   h2 = cadets[num_peers - 1];
980   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
981                                                   &disconnect_cadet_peers,
982                                                   (void *) __LINE__);
983   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
984   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
985                                                  GNUNET_TESTBED_PIT_IDENTITY,
986                                                  &pi_cb, (void *) 0L);
987   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
988                                                  GNUNET_TESTBED_PIT_IDENTITY,
989                                                  &pi_cb, (void *) 1L);
990   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
991 }
992
993
994 /**
995  * Main: start test
996  */
997 int
998 main (int argc, char *argv[])
999 {
1000   initialized = GNUNET_NO;
1001   static const struct GNUNET_HashCode *ports[2];
1002   const char *config_file;
1003   char port_id[] = "test port";
1004   GNUNET_CRYPTO_hash (port_id, sizeof (port_id), &port);
1005
1006   GNUNET_log_setup ("test", "DEBUG", NULL);
1007   config_file = "test_cadet.conf";
1008
1009   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
1010
1011   /* Find out requested size */
1012   if (strstr (argv[0], "_2_") != NULL)
1013   {
1014     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
1015     peers_requested = 2;
1016   }
1017   else if (strstr (argv[0], "_5_") != NULL)
1018   {
1019     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
1020     peers_requested = 5;
1021   }
1022   else
1023   {
1024     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
1025     peers_requested = 2;
1026   }
1027
1028   /* Find out requested test */
1029   if (strstr (argv[0], "_forward") != NULL)
1030   {
1031     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
1032     test = FORWARD;
1033     test_name = "unicast";
1034     ok_goal = 4;
1035   }
1036   else if (strstr (argv[0], "_signal") != NULL)
1037   {
1038     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
1039     test = P2P_SIGNAL;
1040     test_name = "signal";
1041     ok_goal = 4;
1042   }
1043   else if (strstr (argv[0], "_speed_ack") != NULL)
1044   {
1045     /* Test is supposed to generate the following callbacks:
1046      * 1 incoming channel (@dest)
1047      * TOTAL_PACKETS received data packet (@dest)
1048      * TOTAL_PACKETS received data packet (@orig)
1049      * 1 received channel destroy (@dest)
1050      */
1051     ok_goal = TOTAL_PACKETS * 2 + 2;
1052     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
1053     test = SPEED_ACK;
1054     test_name = "speed ack";
1055   }
1056   else if (strstr (argv[0], "_speed") != NULL)
1057   {
1058     /* Test is supposed to generate the following callbacks:
1059      * 1 incoming channel (@dest)
1060      * 1 initial packet (@dest)
1061      * TOTAL_PACKETS received data packet (@dest)
1062      * 1 received data packet (@orig)
1063      * 1 received channel destroy (@dest)
1064      */
1065     ok_goal = TOTAL_PACKETS + 4;
1066     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
1067     if (strstr (argv[0], "_reliable") != NULL)
1068     {
1069       test = SPEED_REL;
1070       test_name = "speed reliable";
1071       config_file = "test_cadet_drop.conf";
1072     }
1073     else
1074     {
1075       test = SPEED;
1076       test_name = "speed";
1077     }
1078   }
1079   else if (strstr (argv[0], "_keepalive") != NULL)
1080   {
1081     test = KEEPALIVE;
1082     /* Test is supposed to generate the following callbacks:
1083      * 1 incoming channel (@dest)
1084      * [wait]
1085      * 1 received channel destroy (@dest)
1086      */
1087     ok_goal = 2;
1088   }
1089   else
1090   {
1091     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
1092     test = SETUP;
1093     ok_goal = 0;
1094   }
1095
1096   if (strstr (argv[0], "backwards") != NULL)
1097   {
1098     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
1099     test_backwards = GNUNET_YES;
1100     GNUNET_asprintf (&test_name, "backwards %s", test_name);
1101   }
1102
1103   p_ids = 0;
1104   ports[0] = &port;
1105   ports[1] = NULL;
1106   GNUNET_CADET_TEST_run ("test_cadet_small",
1107                         config_file,
1108                         peers_requested,
1109                         &tmain,
1110                         NULL, /* tmain cls */
1111                         &incoming_channel,
1112                         &channel_cleaner,
1113                         handlers,
1114                         ports);
1115
1116   if (ok_goal > ok)
1117   {
1118     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1119                 "FAILED! (%d/%d)\n", ok, ok_goal);
1120     return 1;
1121   }
1122   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
1123   return 0;
1124 }
1125
1126 /* end of test_cadet.c */