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_local.c
23 * @brief test cadet local: test of cadet channels with just one peer
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"
33 struct GNUNET_TESTING_Peer *me;
35 static struct GNUNET_CADET_Handle *cadet_peer_1;
37 static struct GNUNET_CADET_Handle *cadet_peer_2;
39 static struct GNUNET_CADET_Channel *ch;
41 static int result = GNUNET_OK;
43 static int got_data = GNUNET_NO;
45 static struct GNUNET_SCHEDULER_Task *abort_task;
47 static struct GNUNET_SCHEDULER_Task *connect_task;
49 static struct GNUNET_CADET_TransmitHandle *mth;
53 * Connect to other client and send data
55 * @param cls Closue (unused).
58 do_connect (void *cls);
65 do_shutdown (void *cls)
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutdown\n");
68 if (NULL != abort_task)
70 GNUNET_SCHEDULER_cancel (abort_task);
73 if (NULL != connect_task)
75 GNUNET_SCHEDULER_cancel (connect_task);
80 GNUNET_CADET_channel_destroy (ch);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 1\n");
84 if (NULL != cadet_peer_1)
86 GNUNET_CADET_disconnect (cadet_peer_1);
89 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnect client 2\n");
90 if (NULL != cadet_peer_2)
92 GNUNET_CADET_disconnect (cadet_peer_2);
99 * Something went wrong and timed out. Kill everything and set error flag
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ABORT\n");
105 result = GNUNET_SYSERR;
107 GNUNET_SCHEDULER_shutdown ();
112 * Function is called whenever a message is received.
114 * @param cls closure (set from GNUNET_CADET_connect)
115 * @param channel connection to the other end
116 * @param channel_ctx place to store local state associated with the channel
117 * @param message the actual message
119 * @return GNUNET_OK to keep the connection open,
120 * GNUNET_SYSERR to close it (signal serious error)
123 data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
125 const struct GNUNET_MessageHeader *message)
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Data callback! Shutting down.\n");
129 got_data = GNUNET_YES;
130 GNUNET_SCHEDULER_shutdown ();
131 GNUNET_CADET_receive_done (channel);
137 * Method called whenever another peer has added us to a channel
138 * the other peer initiated.
141 * @param channel new handle to the channel
142 * @param initiator peer that started the channel
143 * @param port port number
144 * @param options channel options
145 * @return initial channel context for the channel
146 * (can be NULL -- that's not an error)
149 inbound_channel (void *cls,
150 struct GNUNET_CADET_Channel *channel,
151 const struct GNUNET_PeerIdentity *initiator,
153 enum GNUNET_CADET_ChannelOption options)
155 long id = (long) cls;
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "received incoming channel on peer %d, port %u\n",
162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
164 result = GNUNET_SYSERR;
171 * Function called whenever an channel is destroyed. Should clean up
172 * any associated state.
174 * @param cls closure (set from GNUNET_CADET_connect)
175 * @param channel connection to the other end (henceforth invalid)
176 * @param channel_ctx place where local state associated
177 * with the channel is stored
180 channel_end (void *cls, const struct GNUNET_CADET_Channel *channel,
183 long id = (long) cls;
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "incoming channel closed at peer %ld\n",
190 GNUNET_CADET_notify_transmit_ready_cancel (mth);
193 if (GNUNET_NO == got_data)
195 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
196 GNUNET_TIME_UNIT_SECONDS,
204 * Handler array for traffic received on peer1
206 static struct GNUNET_CADET_MessageHandler handlers1[] = {
207 {&data_callback, 1, 0},
213 * Handler array for traffic received on peer2 (none expected)
215 static struct GNUNET_CADET_MessageHandler handlers2[] = {
216 {&data_callback, 1, 0},
222 * Data send callback: fillbuffer with test packet.
224 * @param cls Closure (unused).
225 * @param size Buffer size.
226 * @param buf Buffer to fill.
228 * @return size of test packet.
231 do_send (void *cls, size_t size, void *buf)
233 struct GNUNET_MessageHeader *m = buf;
239 result = GNUNET_SYSERR;
242 m->size = htons (sizeof (struct GNUNET_MessageHeader));
244 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
245 return sizeof (struct GNUNET_MessageHeader);
249 * Connect to other client and send data
251 * @param cls Closue (unused).
254 do_connect (void *cls)
256 struct GNUNET_PeerIdentity id;
259 GNUNET_TESTING_peer_get_identity (me, &id);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CONNECT BY PORT\n");
261 ch = GNUNET_CADET_channel_create (cadet_peer_1, NULL, &id, 1,
262 GNUNET_CADET_OPTION_DEFAULT);
263 mth = GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
264 GNUNET_TIME_UNIT_FOREVER_REL,
265 sizeof (struct GNUNET_MessageHeader),
271 * Initialize framework and start test
273 * @param cls Closure (unused).
274 * @param cfg Configuration handle.
275 * @param peer Testing peer handle.
279 const struct GNUNET_CONFIGURATION_Handle *cfg,
280 struct GNUNET_TESTING_Peer *peer)
282 static uint32_t ports[] = {1, 0};
285 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
287 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
288 (GNUNET_TIME_UNIT_SECONDS, 15),
291 cadet_peer_1 = GNUNET_CADET_connect (cfg, /* configuration */
292 (void *) 1L, /* cls */
293 NULL, /* inbound new hndlr */
294 &channel_end, /* channel end hndlr */
295 handlers1, /* traffic handlers */
296 NULL); /* ports offered */
298 cadet_peer_2 = GNUNET_CADET_connect (cfg, /* configuration */
299 (void *) 2L, /* cls */
300 &inbound_channel, /* inbound new hndlr */
301 &channel_end, /* channel end hndlr */
302 handlers2, /* traffic handlers */
303 ports); /* ports offered */
304 if (NULL == cadet_peer_1 || NULL == cadet_peer_2)
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 "Couldn't connect to cadet :(\n");
308 result = GNUNET_SYSERR;
309 GNUNET_SCHEDULER_shutdown ();
312 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
313 GNUNET_TIME_UNIT_SECONDS,
323 main (int argc, char *argv[])
325 if (0 != GNUNET_TESTING_peer_run ("test-cadet-local",
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "run failed\n");
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Final result: %d\n", result);
333 return (result == GNUNET_OK) ? 0 : 1;
336 /* end of test_cadet_local_1.c */