2 This file is part of GNUnet.
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file stream/test_stream_sequence_wraparound.c
23 * @brief test cases for sequence wrap around situations during data transfer
24 * @author Sree Harsha Totakura
30 #include "gnunet_util_lib.h"
31 #include "gnunet_stream_lib.h"
32 #include "gnunet_testing_lib.h"
35 * Generic logging shorthand
37 #define LOG(kind, ...) \
38 GNUNET_log (kind, __VA_ARGS__);
41 * Relative seconds shorthand
43 #define TIME_REL_SECS(sec) \
44 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
47 * Structure for holding peer's sockets and IO Handles
52 * Peer's stream socket
54 struct GNUNET_STREAM_Socket *socket;
57 * Peer's io write handle
59 struct GNUNET_STREAM_WriteHandle *io_write_handle;
62 * Peer's io read handle
64 struct GNUNET_STREAM_ReadHandle *io_read_handle;
67 * Peer's shutdown handle
69 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
72 * Bytes the peer has written
74 unsigned int bytes_wrote;
77 * Byte the peer has read
79 unsigned int bytes_read;
82 static struct PeerData peer1;
83 static struct PeerData peer2;
84 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
85 static const struct GNUNET_CONFIGURATION_Handle *config;
86 static struct GNUNET_TESTING_Peer *self;
87 static struct GNUNET_PeerIdentity self_id;
89 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
90 static GNUNET_SCHEDULER_TaskIdentifier read_task;
91 static GNUNET_SCHEDULER_TaskIdentifier write_task;
93 #define DATA_SIZE 65536 /* 64KB */
94 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
101 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
103 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
104 GNUNET_SCHEDULER_cancel (abort_task);
105 if (NULL != peer1.socket)
106 GNUNET_STREAM_close (peer1.socket);
107 if (NULL != peer2.socket)
108 GNUNET_STREAM_close (peer2.socket);
109 if (NULL != peer2_listen_socket)
110 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
115 * Something went wrong and timed out. Kill everything and set error flag
118 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
121 if (GNUNET_SCHEDULER_NO_TASK != read_task)
123 GNUNET_SCHEDULER_cancel (read_task);
125 result = GNUNET_SYSERR;
126 abort_task = GNUNET_SCHEDULER_NO_TASK;
132 * Completion callback for shutdown
134 * @param cls the closure from GNUNET_STREAM_shutdown call
135 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
139 shutdown_completion (void *cls,
142 static int shutdowns;
144 if (++shutdowns == 1)
146 peer1.shutdown_handle = NULL;
147 peer2.shutdown_handle = GNUNET_STREAM_shutdown (peer2.socket, SHUT_RDWR,
148 &shutdown_completion, cls);
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STREAM shutdown successful\n");
152 GNUNET_SCHEDULER_add_now (&do_close, cls);
157 * Shutdown sockets gracefully
160 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
163 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket, SHUT_RDWR,
164 &shutdown_completion, cls);
169 * The write completion function; called upon writing some data to stream or
172 * @param cls the closure from GNUNET_STREAM_write/read
173 * @param status the status of the stream at the time this function is called
174 * @param size the number of bytes read or written
177 write_completion (void *cls,
178 enum GNUNET_STREAM_Status status,
181 struct PeerData *peer;
183 peer = (struct PeerData *) cls;
184 GNUNET_assert (GNUNET_STREAM_OK == status);
185 GNUNET_assert (size <= DATA_SIZE);
186 peer->bytes_wrote += size;
188 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
190 peer->io_write_handle =
191 GNUNET_STREAM_write (peer->socket,
192 ((void *) data) + peer->bytes_wrote,
193 DATA_SIZE - peer->bytes_wrote,
194 GNUNET_TIME_relative_multiply
195 (GNUNET_TIME_UNIT_SECONDS, 5),
198 GNUNET_assert (NULL != peer->io_write_handle);
202 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
204 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
210 * Task for calling STREAM_write with a chunk of random data
212 * @param cls the peer data entity
213 * @param tc the task context
216 stream_write_task (void *cls,
217 const struct GNUNET_SCHEDULER_TaskContext *tc)
219 struct PeerData *peer=cls;
222 write_task = GNUNET_SCHEDULER_NO_TASK;
223 for (count=0; count < DATA_SIZE / 4; count++)
225 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
229 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
232 GNUNET_TIME_relative_multiply
233 (GNUNET_TIME_UNIT_SECONDS, 10),
236 GNUNET_assert (NULL != peer->io_write_handle);
241 * Function executed after stream has been established
243 * @param cls the closure from GNUNET_STREAM_open
244 * @param socket socket to use to communicate with the other side (read/write)
247 stream_open_cb (void *cls,
248 struct GNUNET_STREAM_Socket *socket)
250 struct PeerData *peer;
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
253 peer = (struct PeerData *) cls;
254 peer->bytes_wrote = 0;
255 GNUNET_assert (socket == peer1.socket);
256 GNUNET_assert (socket == peer->socket);
257 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
262 * Scheduler call back; to be executed when a new stream is connected
263 * Called from listen connect for peer2
266 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
273 * @param status the status of the stream at the time this function is called
274 * @param data traffic from the other side
275 * @param size the number of bytes available in data read
276 * @return number of bytes of processed from 'data' (any data remaining should be
277 * given to the next time the read processor is called).
280 input_processor (void *cls,
281 enum GNUNET_STREAM_Status status,
282 const void *input_data,
285 struct PeerData *peer = cls;
287 GNUNET_assert (GNUNET_STREAM_OK == status);
288 GNUNET_assert (&peer2 == peer);
289 GNUNET_assert (size < DATA_SIZE);
290 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
292 peer->bytes_read += size;
294 if (peer->bytes_read < DATA_SIZE)
296 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
297 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
301 /* Peer2 has completed reading*/
302 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
309 * Scheduler call back; to be executed when a new stream is connected
310 * Called from listen connect for peer2
313 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 struct PeerData *peer = cls;
317 read_task = GNUNET_SCHEDULER_NO_TASK;
318 GNUNET_assert (&peer2 == peer);
319 peer->io_read_handle =
320 GNUNET_STREAM_read (peer->socket,
321 GNUNET_TIME_relative_multiply
322 (GNUNET_TIME_UNIT_SECONDS, 10),
325 GNUNET_assert (NULL != peer->io_read_handle);
330 * Functions of this type are called upon new stream connection from other peers
332 * @param cls the closure from GNUNET_STREAM_listen
333 * @param socket the socket representing the stream
334 * @param initiator the identity of the peer who wants to establish a stream
336 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
337 * stream (the socket will be invalid after the call)
340 stream_listen_cb (void *cls,
341 struct GNUNET_STREAM_Socket *socket,
342 const struct GNUNET_PeerIdentity *initiator)
344 if ((NULL == socket) || (NULL == initiator))
346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
347 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
348 GNUNET_SCHEDULER_cancel (abort_task);
349 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
352 GNUNET_assert (NULL != socket);
353 GNUNET_assert (socket != peer1.socket);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Peer connected: %s\n", GNUNET_i2s(initiator));
356 peer2.socket = socket;
357 peer2.bytes_read = 0;
358 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
364 * Listen success callback; connects a peer to stream as client
367 stream_connect (void)
370 GNUNET_STREAM_open (config,
371 &self_id, /* Null for local peer? */
375 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
376 UINT32_MAX - GNUNET_CRYPTO_random_u32
377 (GNUNET_CRYPTO_QUALITY_WEAK, 64),
378 GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
379 GNUNET_STREAM_OPTION_END);
380 GNUNET_assert (NULL != peer1.socket);
385 * Initialize framework and start test
389 const struct GNUNET_CONFIGURATION_Handle *cfg,
390 struct GNUNET_TESTING_Peer *peer)
394 (void) GNUNET_TESTING_peer_get_identity (peer, &self_id);
395 peer2_listen_socket =
396 GNUNET_STREAM_listen (config,
400 GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
402 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
404 GNUNET_STREAM_OPTION_END);
405 GNUNET_assert (NULL != peer2_listen_socket);
407 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
408 (GNUNET_TIME_UNIT_SECONDS, 100), &do_abort,
416 int main (int argc, char **argv)
418 if (0 != GNUNET_TESTING_peer_run ("test_stream_sequence_wraparound",
419 "test_stream_local.conf",
422 return (GNUNET_SYSERR == result) ? 1 : 0;
425 /* end of test_stream_sequence_wraparound.c */