Adapting verify successor code to use trail id
[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 100
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_INFO, "Retransmission\n");
341     if (0 == i)
342     {
343       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "  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_INFO, "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_DEBUG, " 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,
455                 "Leaf client %li got a message.\n",
456                 client);
457     break;
458   default:
459     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Client %li not valid.\n", client);
460     GNUNET_abort ();
461   }
462   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
463   data = (uint32_t *) &message[1];
464   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " payload: (%u)\n", ntohl (*data));
465   if (SPEED == test && GNUNET_YES == test_backwards)
466   {
467     expected_target_client = 0L;
468   }
469   else
470   {
471     expected_target_client = peers_requested - 1;
472   }
473
474   if (GNUNET_NO == initialized)
475   {
476     initialized = GNUNET_YES;
477     start_time = GNUNET_TIME_absolute_get ();
478     if (SPEED == test)
479     {
480       GNUNET_assert (peers_requested - 1 == client);
481       GNUNET_SCHEDULER_add_now (&data_task, NULL);
482       return GNUNET_OK;
483     }
484   }
485
486   if (client == expected_target_client) // Normally 4
487   {
488     data_received++;
489     GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received data %u\n", data_received);
490     if (SPEED != test || (ok_goal - 2) == ok)
491     {
492       GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
493                                           GNUNET_TIME_UNIT_FOREVER_REL,
494                                           size_payload + data_sent,
495                                           &tmt_rdy, (void *) 1L);
496       return GNUNET_OK;
497     }
498     else
499     {
500       if (data_received < TOTAL_PACKETS)
501         return GNUNET_OK;
502     }
503   }
504   else // Normally 0
505   {
506     if (test == SPEED_ACK || test == SPEED)
507     {
508       data_ack++;
509       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", data_ack);
510       GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
511                                           GNUNET_TIME_UNIT_FOREVER_REL,
512                                           size_payload + data_sent,
513                                           &tmt_rdy, (void *) 1L);
514       if (data_ack < TOTAL_PACKETS && SPEED != test)
515         return GNUNET_OK;
516       if (ok == 2 && SPEED == test)
517         return GNUNET_OK;
518       show_end_data();
519     }
520     if (test == P2P_SIGNAL)
521     {
522       GNUNET_CADET_channel_destroy (incoming_ch);
523       incoming_ch = NULL;
524     }
525     else
526     {
527       GNUNET_CADET_channel_destroy (ch);
528       ch = NULL;
529     }
530   }
531
532   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
533   {
534     GNUNET_SCHEDULER_cancel (disconnect_task);
535     disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
536                                                     &disconnect_cadet_peers,
537                                                     (void *) __LINE__);
538   }
539
540   return GNUNET_OK;
541 }
542
543
544 /**
545  * Stats callback. Finish the stats testbed operation and when all stats have
546  * been iterated, shutdown the test.
547  *
548  * @param cls closure
549  * @param op the operation that has been finished
550  * @param emsg error message in case the operation has failed; will be NULL if
551  *          operation has executed successfully.
552  */
553 static void
554 stats_cont (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
555 {
556   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stats_cont for peer %u\n", cls);
557   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " sent: %u, received: %u\n",
558               ka_sent, ka_received);
559   if (ka_sent < 2 || ka_sent > ka_received + 1)
560     ok--;
561   GNUNET_TESTBED_operation_done (stats_op);
562
563   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
564     GNUNET_SCHEDULER_cancel (disconnect_task);
565   disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
566                                               (void *) __LINE__);
567
568 }
569
570
571 /**
572  * Process statistic values.
573  *
574  * @param cls closure
575  * @param peer the peer the statistic belong to
576  * @param subsystem name of subsystem that created the statistic
577  * @param name the name of the datum
578  * @param value the current value
579  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
580  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
581  */
582 static int
583 stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
584                 const char *subsystem, const char *name,
585                 uint64_t value, int is_persistent)
586 {
587   static const char *s_sent = "# keepalives sent";
588   static const char *s_recv = "# keepalives received";
589   uint32_t i;
590
591   i = GNUNET_TESTBED_get_index (peer);
592   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  %u - %s [%s]: %llu\n",
593               i, subsystem, name, value);
594   if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
595     ka_sent = value;
596
597   if (0 == strncmp(s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
598     ka_received = value;
599
600   return GNUNET_OK;
601 }
602
603
604 /**
605  * Task check that keepalives were sent and received.
606  *
607  * @param cls Closure (NULL).
608  * @param tc Task Context.
609  */
610 static void
611 check_keepalives (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
612 {
613   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
614     return;
615
616   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
617   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "check keepalives\n");
618   GNUNET_CADET_channel_destroy (ch);
619   stats_op = GNUNET_TESTBED_get_statistics (peers_running, testbed_peers,
620                                             "cadet", NULL,
621                                             stats_iterator, stats_cont, NULL);
622 }
623
624
625 /**
626  * Handlers, for diverse services
627  */
628 static struct GNUNET_CADET_MessageHandler handlers[] = {
629   {&data_callback, 1, sizeof (struct GNUNET_MessageHeader)},
630   {NULL, 0, 0}
631 };
632
633
634 /**
635  * Method called whenever another peer has added us to a channel
636  * the other peer initiated.
637  *
638  * @param cls Closure.
639  * @param channel New handle to the channel.
640  * @param initiator Peer that started the channel.
641  * @param port Port this channel is connected to.
642  * @param options channel option flags
643  * @return Initial channel context for the channel
644  *         (can be NULL -- that's not an error).
645  */
646 static void *
647 incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
648                  const struct GNUNET_PeerIdentity *initiator,
649                  uint32_t port, enum GNUNET_CADET_ChannelOption options)
650 {
651   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
652               "Incoming channel from %s to peer %d\n",
653               GNUNET_i2s (initiator), (long) cls);
654   ok++;
655   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
656   if ((long) cls == peers_requested - 1)
657     incoming_ch = channel;
658   else
659   {
660     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
661                 "Incoming channel for unknown client %lu\n", (long) cls);
662     GNUNET_break(0);
663   }
664   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
665   {
666     GNUNET_SCHEDULER_cancel (disconnect_task);
667     if (KEEPALIVE == test)
668     {
669       struct GNUNET_TIME_Relative delay;
670       delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS , 5);
671       disconnect_task =
672         GNUNET_SCHEDULER_add_delayed (delay, &check_keepalives, NULL);
673     }
674     else
675       disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
676                                                       &disconnect_cadet_peers,
677                                                       (void *) __LINE__);
678   }
679
680   return NULL;
681 }
682
683 /**
684  * Function called whenever an inbound channel is destroyed.  Should clean up
685  * any associated state.
686  *
687  * @param cls closure (set from GNUNET_CADET_connect)
688  * @param channel connection to the other end (henceforth invalid)
689  * @param channel_ctx place where local state associated
690  *                   with the channel is stored
691  */
692 static void
693 channel_cleaner (void *cls, const struct GNUNET_CADET_Channel *channel,
694                  void *channel_ctx)
695 {
696   long i = (long) cls;
697
698   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
699               "Incoming channel disconnected at peer %ld\n", i);
700   if (peers_running - 1 == i)
701   {
702     ok++;
703     GNUNET_break (channel == incoming_ch);
704     incoming_ch = NULL;
705   }
706   else if (0L == i)
707   {
708     if (P2P_SIGNAL == test)
709     {
710       ok ++;
711     }
712     GNUNET_break (channel == ch);
713     ch = NULL;
714   }
715   else
716     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
717                 "Unknown peer! %d\n", i);
718   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
719
720   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
721   {
722     GNUNET_SCHEDULER_cancel (disconnect_task);
723     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_cadet_peers,
724                                                 (void *) __LINE__);
725   }
726
727   return;
728 }
729
730
731 /**
732  * START THE TESTCASE ITSELF, AS WE ARE CONNECTED TO THE CADET SERVICES.
733  *
734  * Testcase continues when the root receives confirmation of connected peers,
735  * on callback funtion ch.
736  *
737  * @param cls Closure (unsued).
738  * @param tc Task Context.
739  */
740 static void
741 do_test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
742 {
743   enum GNUNET_CADET_ChannelOption flags;
744
745   if ((GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason) != 0)
746     return;
747
748   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test_task\n");
749
750   if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
751   {
752     GNUNET_SCHEDULER_cancel (disconnect_task);
753   }
754
755   flags = GNUNET_CADET_OPTION_DEFAULT;
756   if (SPEED_REL == test)
757   {
758     test = SPEED;
759     flags |= GNUNET_CADET_OPTION_RELIABLE;
760   }
761   ch = GNUNET_CADET_channel_create (h1, NULL, p_id[1], 1, flags);
762
763   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
764                                                   &disconnect_cadet_peers,
765                                                   (void *) __LINE__);
766   if (KEEPALIVE == test)
767     return; /* Don't send any data. */
768
769   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770               "Sending data initializer...\n");
771   data_ack = 0;
772   data_received = 0;
773   data_sent = 0;
774   GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
775                                      GNUNET_TIME_UNIT_FOREVER_REL,
776                                      size_payload, &tmt_rdy, (void *) 1L);
777 }
778
779 /**
780  * Callback to be called when the requested peer information is available
781  *
782  * @param cls the closure from GNUNET_TESTBED_peer_get_information()
783  * @param op the operation this callback corresponds to
784  * @param pinfo the result; will be NULL if the operation has failed
785  * @param emsg error message if the operation has failed;
786  *             NULL if the operation is successfull
787  */
788 static void
789 pi_cb (void *cls,
790        struct GNUNET_TESTBED_Operation *op,
791        const struct GNUNET_TESTBED_PeerInformation *pinfo,
792        const char *emsg)
793 {
794   long i = (long) cls;
795
796   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "id callback for %ld\n", i);
797
798   if (NULL == pinfo || NULL != emsg)
799   {
800     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "pi_cb: %s\n", emsg);
801     abort_test (__LINE__);
802     return;
803   }
804   p_id[i] = pinfo->result.id;
805   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  id: %s\n", GNUNET_i2s (p_id[i]));
806   p_ids++;
807   if (p_ids < 2)
808     return;
809   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got all IDs, starting test\n");
810   test_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
811                                             &do_test, NULL);
812 }
813
814 /**
815  * test main: start test when all peers are connected
816  *
817  * @param cls Closure.
818  * @param ctx Argument to give to GNUNET_CADET_TEST_cleanup on test end.
819  * @param num_peers Number of peers that are running.
820  * @param peers Array of peers.
821  * @param cadetes Handle to each of the CADETs of the peers.
822  */
823 static void
824 tmain (void *cls,
825        struct GNUNET_CADET_TEST_Context *ctx,
826        unsigned int num_peers,
827        struct GNUNET_TESTBED_Peer **peers,
828        struct GNUNET_CADET_Handle **cadets)
829 {
830   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
831   ok = 0;
832   test_ctx = ctx;
833   peers_running = num_peers;
834   GNUNET_assert (peers_running == peers_requested);
835   testbed_peers = peers;
836   h1 = cadets[0];
837   h2 = cadets[num_peers - 1];
838   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
839                                                   &disconnect_cadet_peers,
840                                                   (void *) __LINE__);
841   shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
842                                                   &shutdown_task, NULL);
843   t_op[0] = GNUNET_TESTBED_peer_get_information (peers[0],
844                                                  GNUNET_TESTBED_PIT_IDENTITY,
845                                                  &pi_cb, (void *) 0L);
846   t_op[1] = GNUNET_TESTBED_peer_get_information (peers[num_peers - 1],
847                                                  GNUNET_TESTBED_PIT_IDENTITY,
848                                                  &pi_cb, (void *) 1L);
849   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "requested peer ids\n");
850 }
851
852
853 /**
854  * Main: start test
855  */
856 int
857 main (int argc, char *argv[])
858 {
859   initialized = GNUNET_NO;
860   static uint32_t ports[2];
861   const char *config_file;
862
863   GNUNET_log_setup ("test", "DEBUG", NULL);
864   config_file = "test_cadet.conf";
865
866   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
867
868   /* Find out requested size */
869   if (strstr (argv[0], "_2_") != NULL)
870   {
871     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
872     peers_requested = 2;
873   }
874   else if (strstr (argv[0], "_5_") != NULL)
875   {
876     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
877     peers_requested = 5;
878   }
879   else
880   {
881     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
882     peers_requested = 2;
883   }
884
885   /* Find out requested test */
886   if (strstr (argv[0], "_forward") != NULL)
887   {
888     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
889     test = FORWARD;
890     test_name = "unicast";
891     ok_goal = 4;
892   }
893   else if (strstr (argv[0], "_signal") != NULL)
894   {
895     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
896     test = P2P_SIGNAL;
897     test_name = "signal";
898     ok_goal = 4;
899   }
900   else if (strstr (argv[0], "_speed_ack") != NULL)
901   {
902     /* Test is supposed to generate the following callbacks:
903      * 1 incoming channel (@dest)
904      * TOTAL_PACKETS received data packet (@dest)
905      * TOTAL_PACKETS received data packet (@orig)
906      * 1 received channel destroy (@dest)
907      */
908     ok_goal = TOTAL_PACKETS * 2 + 2;
909     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED_ACK\n");
910     test = SPEED_ACK;
911     test_name = "speed ack";
912   }
913   else if (strstr (argv[0], "_speed") != NULL)
914   {
915     /* Test is supposed to generate the following callbacks:
916      * 1 incoming channel (@dest)
917      * 1 initial packet (@dest)
918      * TOTAL_PACKETS received data packet (@dest)
919      * 1 received data packet (@orig)
920      * 1 received channel destroy (@dest)
921      */
922     ok_goal = TOTAL_PACKETS + 4;
923     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SPEED\n");
924     if (strstr (argv[0], "_reliable") != NULL)
925     {
926       test = SPEED_REL;
927       test_name = "speed reliable";
928       config_file = "test_cadet_drop.conf";
929     }
930     else
931     {
932       test = SPEED;
933       test_name = "speed";
934     }
935   }
936   else if (strstr (argv[0], "_keepalive") != NULL)
937   {
938     test = KEEPALIVE;
939     /* Test is supposed to generate the following callbacks:
940      * 1 incoming channel (@dest)
941      * [wait]
942      * 1 received channel destroy (@dest)
943      */
944     ok_goal = 2;
945   }
946   else
947   {
948     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "UNKNOWN\n");
949     test = SETUP;
950     ok_goal = 0;
951   }
952
953   if (strstr (argv[0], "backwards") != NULL)
954   {
955     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "BACKWARDS (LEAF TO ROOT)\n");
956     test_backwards = GNUNET_YES;
957     GNUNET_asprintf (&test_name, "backwards %s", test_name);
958   }
959
960   p_ids = 0;
961   ports[0] = 1;
962   ports[1] = 0;
963   GNUNET_CADET_TEST_run ("test_cadet_small",
964                         config_file,
965                         peers_requested,
966                         &tmain,
967                         NULL, /* tmain cls */
968                         &incoming_channel,
969                         &channel_cleaner,
970                         handlers,
971                         ports);
972
973   if (ok_goal > ok)
974   {
975     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
976                 "FAILED! (%d/%d)\n", ok, ok_goal);
977     return 1;
978   }
979   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "success\n");
980   return 0;
981 }
982
983 /* end of test_cadet.c */
984