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