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