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