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