2 This file is part of GNUnet.
3 Copyright (C) 2011 GNUnet e.V.
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.
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.
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.
22 * @file cadet/test_cadet_single.c
23 * @brief test cadet single: test of cadet channels with just one client
24 * @author Bartlomiej Polot
28 #include "gnunet_util_lib.h"
29 #include "gnunet_dht_service.h"
30 #include "gnunet_testing_lib.h"
31 #include "gnunet_cadet_service.h"
34 #define DATA_SIZE 35000
36 struct GNUNET_TESTING_Peer *me;
38 static struct GNUNET_CADET_Handle *cadet;
40 static struct GNUNET_CADET_Channel *ch1;
42 static struct GNUNET_CADET_Channel *ch2;
46 static struct GNUNET_SCHEDULER_Task *abort_task;
48 static struct GNUNET_SCHEDULER_Task *connect_task;
50 static unsigned int repetition;
52 static struct GNUNET_CADET_TransmitHandle *nth;
55 /* forward declaration */
57 do_send (void *cls, size_t size, void *buf);
64 do_shutdown (void *cls)
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
69 GNUNET_CADET_notify_transmit_ready_cancel (nth);
72 if (NULL != abort_task)
74 GNUNET_SCHEDULER_cancel (abort_task);
77 if (NULL != connect_task)
79 GNUNET_SCHEDULER_cancel (connect_task);
84 GNUNET_CADET_channel_destroy (ch1);
87 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
91 GNUNET_CADET_disconnect (cadet);
102 * Something went wrong and timed out. Kill everything and set error flag
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
108 result = GNUNET_SYSERR;
110 GNUNET_SCHEDULER_shutdown ();
115 * Function is called whenever a message is received.
117 * @param cls closure (set from GNUNET_CADET_connect)
118 * @param channel connection to the other end
119 * @param channel_ctx place to store local state associated with the channel
120 * @param message the actual message
122 * @return GNUNET_OK to keep the connection open,
123 * GNUNET_SYSERR to close it (signal serious error)
126 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
128 const struct GNUNET_MessageHeader *message)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Data callback! Repetition %u/%u\n",
132 repetition, REPETITIONS);
133 repetition = repetition + 1;
134 if (repetition < REPETITIONS)
136 struct GNUNET_CADET_Channel *my_channel;
137 if (repetition % 2 == 0)
141 nth = GNUNET_CADET_notify_transmit_ready (my_channel, GNUNET_NO,
142 GNUNET_TIME_UNIT_FOREVER_REL,
143 sizeof (struct GNUNET_MessageHeader)
146 GNUNET_CADET_receive_done (channel);
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All data OK. Destroying channel.\n");
150 GNUNET_CADET_channel_destroy (ch1);
157 * Method called whenever another peer has added us to a channel
158 * the other peer initiated.
161 * @param channel new handle to the channel
162 * @param initiator peer that started the channel
163 * @param port port number
164 * @param options channel option flags
165 * @return initial channel context for the channel
166 * (can be NULL -- that's not an error)
169 inbound_channel (void *cls, struct GNUNET_CADET_Channel *channel,
170 const struct GNUNET_PeerIdentity *initiator,
171 const struct GNUNET_HashCode *port,
172 enum GNUNET_CADET_ChannelOption options)
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "received incoming channel on port %s\n",
183 * Function called whenever an inbound channel is destroyed. Should clean up
184 * any associated state.
186 * @param cls closure (set from GNUNET_CADET_connect)
187 * @param channel connection to the other end (henceforth invalid)
188 * @param channel_ctx place where local state associated
189 * with the channel is stored
192 channel_end (void *cls, const struct GNUNET_CADET_Channel *channel,
195 long id = (long) cls;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "incoming channel closed at peer %ld\n",
200 if ( (REPETITIONS == repetition) &&
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "everything fine! finishing!\n");
206 GNUNET_SCHEDULER_shutdown ();
216 * Handler array for traffic received on peer1
218 static struct GNUNET_CADET_MessageHandler handlers1[] = {
219 {&data_callback, 1, 0},
225 * Data send callback: fillbuffer with test packet.
227 * @param cls Closure (unused).
228 * @param size Buffer size.
229 * @param buf Buffer to fill.
231 * @return size of test packet.
234 do_send (void *cls, size_t size, void *buf)
236 struct GNUNET_MessageHeader *m = buf;
242 result = GNUNET_SYSERR;
245 m->size = htons (sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
247 memset (&m[1], 0, DATA_SIZE);
248 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader) + DATA_SIZE);
249 return sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
253 * Connect to other client and send data
255 * @param cls Closue (unused).
258 do_connect (void *cls)
260 struct GNUNET_PeerIdentity id;
261 size_t size = sizeof (struct GNUNET_MessageHeader) + DATA_SIZE;
264 GNUNET_TESTING_peer_get_identity (me, &id);
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
266 ch1 = GNUNET_CADET_channel_create (cadet, NULL, &id, GC_u2h (1),
267 GNUNET_CADET_OPTION_DEFAULT);
268 nth = GNUNET_CADET_notify_transmit_ready (ch1, GNUNET_NO,
269 GNUNET_TIME_UNIT_FOREVER_REL,
270 size, &do_send, NULL);
275 * Initialize framework and start test
277 * @param cls Closure (unused).
278 * @param cfg Configuration handle.
279 * @param peer Testing peer handle.
283 const struct GNUNET_CONFIGURATION_Handle *cfg,
284 struct GNUNET_TESTING_Peer *peer)
287 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
289 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
290 (GNUNET_TIME_UNIT_SECONDS, 15), &do_abort,
292 cadet = GNUNET_CADET_connect (cfg, /* configuration */
293 (void *) 1L, /* cls */
294 &channel_end, /* inbound end hndlr */
295 handlers1); /* traffic handlers */
296 GNUNET_CADET_open_port (cadet, GC_u2h (1), &inbound_channel, (void *) 1L);
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to cadet :(\n");
302 result = GNUNET_SYSERR;
306 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
316 main (int argc, char *argv[])
319 if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
327 return (result == GNUNET_OK) ? 0 : 1;
330 /* end of test_cadet_single.c */