- fix short new testcases: send_test_message initizalize data for forward and signal...
[oweals/gnunet.git] / src / cadet / test_cadet_single.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2011 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file cadet/test_cadet_single.c
23  * @brief test cadet single: test of cadet channels with just one client
24  * @author Bartlomiej Polot
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_cadet_service.h"
32
33 #define REPETITIONS 5
34 #define DATA_SIZE 35000
35
36 struct GNUNET_TESTING_Peer *me;
37
38 static struct GNUNET_CADET_Handle *cadet;
39
40 static struct GNUNET_CADET_Channel *ch1;
41
42 static struct GNUNET_CADET_Channel *ch2;
43
44 static int result;
45
46 static struct GNUNET_SCHEDULER_Task *abort_task;
47
48 static struct GNUNET_SCHEDULER_Task *connect_task;
49
50 static unsigned int repetition;
51
52 static struct GNUNET_CADET_TransmitHandle *nth;
53
54 static struct GNUNET_CADET_Port *port;
55
56
57 /* forward declaration */
58 static size_t
59 do_send (void *cls, size_t size, void *buf);
60
61
62 /**
63  * Shutdown nicely
64  */
65 static void
66 do_shutdown (void *cls)
67 {
68   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
69               "shutdown\n");
70   if (NULL != port)
71   {
72     GNUNET_CADET_close_port (port);
73     port = NULL;
74   }
75   if (NULL != nth)
76   {
77     GNUNET_CADET_notify_transmit_ready_cancel (nth);
78     nth = NULL;
79   }
80   if (NULL != abort_task)
81   {
82     GNUNET_SCHEDULER_cancel (abort_task);
83     abort_task = NULL;
84   }
85   if (NULL != connect_task)
86   {
87     GNUNET_SCHEDULER_cancel (connect_task);
88     connect_task = NULL;
89   }
90   if (NULL != ch1)
91   {
92     GNUNET_CADET_channel_destroy (ch1);
93     ch1 = NULL;
94   }
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96               "Disconnect clients\n");
97   if (NULL != cadet)
98   {
99     GNUNET_CADET_disconnect (cadet);
100     cadet = NULL;
101   }
102   else
103   {
104     GNUNET_break (0);
105   }
106 }
107
108
109 /**
110  * Something went wrong and timed out. Kill everything and set error flag
111  */
112 static void
113 do_abort (void *cls)
114 {
115   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
116   result = GNUNET_SYSERR;
117   abort_task = NULL;
118   GNUNET_SCHEDULER_shutdown ();
119 }
120
121
122 /**
123  * Function is called whenever a message is received.
124  *
125  * @param cls closure (set from GNUNET_CADET_connect)
126  * @param channel connection to the other end
127  * @param channel_ctx place to store local state associated with the channel
128  * @param message the actual message
129  * @return #GNUNET_OK to keep the connection open,
130  *         #GNUNET_SYSERR to close it (signal serious error)
131  */
132 static int
133 data_callback (void *cls,
134                struct GNUNET_CADET_Channel *channel,
135                void **channel_ctx,
136                const struct GNUNET_MessageHeader *message)
137 {
138   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139               "Data callback! Repetition %u/%u\n",
140               repetition, REPETITIONS);
141   repetition++;
142   if (repetition < REPETITIONS)
143   {
144     struct GNUNET_CADET_Channel *my_channel;
145     if (0 == repetition % 2)
146       my_channel = ch1;
147     else
148       my_channel = ch2;
149     nth = GNUNET_CADET_notify_transmit_ready (my_channel,
150                                               GNUNET_NO,
151                                               GNUNET_TIME_UNIT_FOREVER_REL,
152                                               sizeof (struct GNUNET_MessageHeader)
153                                               + DATA_SIZE,
154                                               &do_send, NULL);
155     GNUNET_CADET_receive_done (channel);
156     return GNUNET_OK;
157   }
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159               "All data OK. Destroying channel.\n");
160   GNUNET_assert (NULL == nth);
161   GNUNET_CADET_channel_destroy (ch1);
162   ch1 = NULL;
163   return GNUNET_OK;
164 }
165
166
167 /**
168  * Method called whenever another peer has added us to a channel
169  * the other peer initiated.
170  *
171  * @param cls closure
172  * @param channel new handle to the channel
173  * @param initiator peer that started the channel
174  * @param port port number
175  * @param options channel option flags
176  * @return initial channel context for the channel
177  *         (can be NULL -- that's not an error)
178  */
179 static void *
180 inbound_channel (void *cls,
181                  struct GNUNET_CADET_Channel *channel,
182                  const struct GNUNET_PeerIdentity *initiator,
183                  const struct GNUNET_HashCode *port,
184                  enum GNUNET_CADET_ChannelOption options)
185 {
186   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187               "received incoming channel on port %s\n",
188               GNUNET_h2s (port));
189   ch2 = channel;
190   return NULL;
191 }
192
193
194 /**
195  * Function called whenever an inbound channel is destroyed.  Should clean up
196  * any associated state.
197  *
198  * @param cls closure (set from GNUNET_CADET_connect)
199  * @param channel connection to the other end (henceforth invalid)
200  * @param channel_ctx place where local state associated
201  *                   with the channel is stored
202  */
203 static void
204 channel_end (void *cls,
205              const struct GNUNET_CADET_Channel *channel,
206              void *channel_ctx)
207 {
208   long id = (long) cls;
209
210   nth = NULL;
211   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212               "incoming channel closed at peer %ld\n",
213               id);
214   if ( (REPETITIONS == repetition) &&
215        (channel == ch2) )
216   {
217     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218                 "everything fine! finishing!\n");
219     result = GNUNET_OK;
220     GNUNET_SCHEDULER_shutdown ();
221   }
222   if (channel == ch2)
223     ch2 = NULL;
224   if (channel == ch1)
225     ch1 = NULL;
226 }
227
228
229 /**
230  * Handler array for traffic received on peer1
231  */
232 static struct GNUNET_CADET_MessageHandler handlers1[] = {
233   {&data_callback, 1, 0},
234   {NULL, 0, 0}
235 };
236
237
238 /**
239  * Data send callback: fillbuffer with test packet.
240  *
241  * @param cls Closure (unused).
242  * @param size Buffer size.
243  * @param buf Buffer to fill.
244  * @return size of test packet.
245  */
246 static size_t
247 do_send (void *cls, size_t size, void *buf)
248 {
249   struct GNUNET_MessageHeader *m = buf;
250
251   nth = NULL;
252   if (NULL == buf)
253   {
254     GNUNET_break (0);
255     result = GNUNET_SYSERR;
256     return 0;
257   }
258   m->size = htons (sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
259   m->type = htons (1);
260   memset (&m[1], 0, DATA_SIZE);
261   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
262   return sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
263 }
264
265 /**
266  * Connect to other client and send data
267  *
268  * @param cls Closue (unused).
269  */
270 static void
271 do_connect (void *cls)
272 {
273   struct GNUNET_PeerIdentity id;
274   size_t size = sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
275
276   connect_task = NULL;
277   GNUNET_TESTING_peer_get_identity (me, &id);
278   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
279   ch1 = GNUNET_CADET_channel_create (cadet, NULL, &id, GC_u2h (1),
280                                      GNUNET_CADET_OPTION_DEFAULT);
281   nth = GNUNET_CADET_notify_transmit_ready (ch1,
282                                             GNUNET_NO,
283                                             GNUNET_TIME_UNIT_FOREVER_REL,
284                                             size,
285                                             &do_send,
286                                             NULL);
287 }
288
289
290 /**
291  * Initialize framework and start test
292  *
293  * @param cls Closure (unused).
294  * @param cfg Configuration handle.
295  * @param peer Testing peer handle.
296  */
297 static void
298 run (void *cls,
299      const struct GNUNET_CONFIGURATION_Handle *cfg,
300      struct GNUNET_TESTING_Peer *peer)
301 {
302   me = peer;
303   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
304   abort_task =
305       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
306                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
307                                     NULL);
308   cadet = GNUNET_CADET_connect (cfg,       /* configuration */
309                               (void *) 1L,     /* cls */
310                               &channel_end,      /* inbound end hndlr */
311                               handlers1); /* traffic handlers */
312   port = GNUNET_CADET_open_port (cadet,
313                                  GC_u2h (1),
314                                  &inbound_channel,
315                                  (void *) 1L);
316
317
318   if (NULL == cadet)
319   {
320     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321                 "Couldn't connect to cadet :(\n");
322     result = GNUNET_SYSERR;
323     return;
324   }
325   connect_task
326     = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
327                                     &do_connect,
328                                     NULL);
329 }
330
331
332 /**
333  * Main
334  */
335 int
336 main (int argc,
337       char *argv[])
338 {
339   result = GNUNET_NO;
340   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
341                                     "test_cadet.conf",
342                                     &run, NULL))
343   {
344     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345                 "run failed\n");
346     return 2;
347   }
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               "Final result: %d\n",
350               result);
351   return (result == GNUNET_OK) ? 0 : 1;
352 }
353
354 /* end of test_cadet_single.c */