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