-skeletons for transport-ng
[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
53 /* forward declaration */
54 static size_t
55 do_send (void *cls, size_t size, void *buf);
56
57
58 /**
59  * Shutdown nicely
60  */
61 static void
62 do_shutdown (void *cls)
63 {
64   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
65   if (NULL != abort_task)
66   {
67     GNUNET_SCHEDULER_cancel (abort_task);
68     abort_task = NULL;
69   }
70   if (NULL != connect_task)
71   {
72     GNUNET_SCHEDULER_cancel (connect_task);
73     connect_task = NULL;
74   }
75   if (NULL != ch1)
76   {
77     GNUNET_CADET_channel_destroy (ch1);
78     ch1 = NULL;
79   }
80   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
81   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
82   if (NULL != cadet)
83   {
84     GNUNET_CADET_disconnect (cadet);
85     cadet = NULL;
86   }
87   else
88   {
89     GNUNET_break (0);
90   }
91 }
92
93
94 /**
95  * Something went wrong and timed out. Kill everything and set error flag
96  */
97 static void
98 do_abort (void *cls)
99 {
100   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
101   result = GNUNET_SYSERR;
102   abort_task = NULL;
103   GNUNET_SCHEDULER_shutdown ();
104 }
105
106
107 /**
108  * Function is called whenever a message is received.
109  *
110  * @param cls closure (set from GNUNET_CADET_connect)
111  * @param channel connection to the other end
112  * @param channel_ctx place to store local state associated with the channel
113  * @param message the actual message
114  *
115  * @return GNUNET_OK to keep the connection open,
116  *         GNUNET_SYSERR to close it (signal serious error)
117  */
118 static int
119 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
120                void **channel_ctx,
121                const struct GNUNET_MessageHeader *message)
122 {
123   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124               "Data callback! Repetition %u/%u\n",
125               repetition, REPETITIONS);
126   repetition = repetition + 1;
127   if (repetition < REPETITIONS)
128   {
129     struct GNUNET_CADET_Channel *my_channel;
130     if (repetition % 2 == 0)
131       my_channel = ch1;
132     else
133       my_channel = ch2;
134     GNUNET_CADET_notify_transmit_ready (my_channel, GNUNET_NO,
135                                        GNUNET_TIME_UNIT_FOREVER_REL,
136                                        sizeof (struct GNUNET_MessageHeader)
137                                        + DATA_SIZE,
138                                        &do_send, NULL);
139     GNUNET_CADET_receive_done (channel);
140     return GNUNET_OK;
141   }
142   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All data OK. Destroying channel.\n");
143   GNUNET_CADET_channel_destroy (ch1);
144   ch1 = NULL;
145   return GNUNET_OK;
146 }
147
148
149 /**
150  * Method called whenever another peer has added us to a channel
151  * the other peer initiated.
152  *
153  * @param cls closure
154  * @param channel new handle to the channel
155  * @param initiator peer that started the channel
156  * @param port port number
157  * @param options channel option flags
158  * @return initial channel context for the channel
159  *         (can be NULL -- that's not an error)
160  */
161 static void *
162 inbound_channel (void *cls, struct GNUNET_CADET_Channel *channel,
163                 const struct GNUNET_PeerIdentity *initiator,
164                 uint32_t port, enum GNUNET_CADET_ChannelOption options)
165 {
166   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167               "received incoming channel on port %u\n",
168               port);
169   ch2 = channel;
170   return NULL;
171 }
172
173
174 /**
175  * Function called whenever an inbound channel is destroyed.  Should clean up
176  * any associated state.
177  *
178  * @param cls closure (set from GNUNET_CADET_connect)
179  * @param channel connection to the other end (henceforth invalid)
180  * @param channel_ctx place where local state associated
181  *                   with the channel is stored
182  */
183 static void
184 channel_end (void *cls, const struct GNUNET_CADET_Channel *channel,
185              void *channel_ctx)
186 {
187   long id = (long) cls;
188
189   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190               "incoming channel closed at peer %ld\n",
191               id);
192   if (REPETITIONS == repetition && channel == ch2)
193   {
194     ch2 = NULL;
195     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "everything fine! finishing!\n");
196     result = GNUNET_OK;
197     GNUNET_SCHEDULER_shutdown ();
198   }
199 }
200
201
202 /**
203  * Handler array for traffic received on peer1
204  */
205 static struct GNUNET_CADET_MessageHandler handlers1[] = {
206   {&data_callback, 1, 0},
207   {NULL, 0, 0}
208 };
209
210
211 /**
212  * Data send callback: fillbuffer with test packet.
213  *
214  * @param cls Closure (unused).
215  * @param size Buffer size.
216  * @param buf Buffer to fill.
217  *
218  * @return size of test packet.
219  */
220 static size_t
221 do_send (void *cls, size_t size, void *buf)
222 {
223   struct GNUNET_MessageHeader *m = buf;
224
225   if (NULL == buf)
226   {
227     GNUNET_break (0);
228     result = GNUNET_SYSERR;
229     return 0;
230   }
231   m->size = htons (sizeof (struct GNUNET_MessageHeader));
232   m->type = htons (1);
233   memset (&m[1], 0, DATA_SIZE);
234   GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
235   return sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
236 }
237
238 /**
239  * Connect to other client and send data
240  *
241  * @param cls Closue (unused).
242  */
243 static void
244 do_connect (void *cls)
245 {
246   struct GNUNET_PeerIdentity id;
247   size_t size = sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
248
249   connect_task = NULL;
250   GNUNET_TESTING_peer_get_identity (me, &id);
251   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
252   ch1 = GNUNET_CADET_channel_create (cadet, NULL, &id, 1,
253                                     GNUNET_CADET_OPTION_DEFAULT);
254   GNUNET_CADET_notify_transmit_ready (ch1, GNUNET_NO,
255                                      GNUNET_TIME_UNIT_FOREVER_REL,
256                                      size, &do_send, NULL);
257 }
258
259
260 /**
261  * Initialize framework and start test
262  *
263  * @param cls Closure (unused).
264  * @param cfg Configuration handle.
265  * @param peer Testing peer handle.
266  */
267 static void
268 run (void *cls,
269      const struct GNUNET_CONFIGURATION_Handle *cfg,
270      struct GNUNET_TESTING_Peer *peer)
271 {
272   static uint32_t ports[] = {1, 0};
273
274   me = peer;
275   GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
276   abort_task =
277       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
278                                     (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
279                                     NULL);
280   cadet = GNUNET_CADET_connect (cfg,       /* configuration */
281                               (void *) 1L,     /* cls */
282                               &inbound_channel,   /* inbound new hndlr */
283                               &channel_end,      /* inbound end hndlr */
284                               handlers1, /* traffic handlers */
285                               ports);     /* ports offered */
286
287   if (NULL == cadet)
288   {
289     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to cadet :(\n");
290     result = GNUNET_SYSERR;
291     return;
292   }
293   connect_task
294     = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
295                                     &do_connect,
296                                     NULL);
297 }
298
299
300 /**
301  * Main
302  */
303 int
304 main (int argc, char *argv[])
305 {
306   result = GNUNET_NO;
307   if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
308                                     "test_cadet.conf",
309                                     &run, NULL))
310   {
311     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
312     return 2;
313   }
314   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
315   return (result == GNUNET_OK) ? 0 : 1;
316 }
317
318 /* end of test_cadet_single.c */