2 This file is part of GNUnet.
3 (C) 2011, 2012 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-new.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_IOWriteHandle *io_write_handle;
62 * Peer's io read handle
64 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
67 * Bytes the peer has written
69 unsigned int bytes_wrote;
72 * Byte the peer has read
74 unsigned int bytes_read;
77 static struct PeerData peer1;
78 static struct PeerData peer2;
79 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
80 static const struct GNUNET_CONFIGURATION_Handle *config;
81 static struct GNUNET_TESTING_Peer *self;
82 static struct GNUNET_PeerIdentity self_id;
84 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
85 static GNUNET_SCHEDULER_TaskIdentifier read_task;
86 static GNUNET_SCHEDULER_TaskIdentifier write_task;
88 #define DATA_SIZE 65536 /* 64KB */
89 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
96 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 GNUNET_STREAM_close (peer1.socket);
99 if (NULL != peer2.socket)
100 GNUNET_STREAM_close (peer2.socket);
101 if (NULL != peer2_listen_socket)
102 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
106 GNUNET_SCHEDULER_cancel (abort_task);
112 * Something went wrong and timed out. Kill everything and set error flag
115 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
118 if (GNUNET_SCHEDULER_NO_TASK != read_task)
120 GNUNET_SCHEDULER_cancel (read_task);
122 result = GNUNET_SYSERR;
124 do_shutdown (cls, tc);
129 * The write completion function; called upon writing some data to stream or
132 * @param cls the closure from GNUNET_STREAM_write/read
133 * @param status the status of the stream at the time this function is called
134 * @param size the number of bytes read or written
137 write_completion (void *cls,
138 enum GNUNET_STREAM_Status status,
141 struct PeerData *peer;
143 peer = (struct PeerData *) cls;
144 GNUNET_assert (GNUNET_STREAM_OK == status);
145 GNUNET_assert (size < DATA_SIZE);
146 peer->bytes_wrote += size;
148 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
150 peer->io_write_handle =
151 GNUNET_STREAM_write (peer->socket,
152 ((void *) data) + peer->bytes_wrote,
153 DATA_SIZE - peer->bytes_wrote,
154 GNUNET_TIME_relative_multiply
155 (GNUNET_TIME_UNIT_SECONDS, 5),
158 GNUNET_assert (NULL != peer->io_write_handle);
162 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
164 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
170 * Task for calling STREAM_write with a chunk of random data
172 * @param cls the peer data entity
173 * @param tc the task context
176 stream_write_task (void *cls,
177 const struct GNUNET_SCHEDULER_TaskContext *tc)
179 struct PeerData *peer=cls;
182 write_task = GNUNET_SCHEDULER_NO_TASK;
183 for (count=0; count < DATA_SIZE / 4; count++)
185 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
188 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
189 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
192 GNUNET_TIME_relative_multiply
193 (GNUNET_TIME_UNIT_SECONDS, 10),
196 GNUNET_assert (NULL != peer->io_write_handle);
201 * Function executed after stream has been established
203 * @param cls the closure from GNUNET_STREAM_open
204 * @param socket socket to use to communicate with the other side (read/write)
207 stream_open_cb (void *cls,
208 struct GNUNET_STREAM_Socket *socket)
210 struct PeerData *peer;
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
213 peer = (struct PeerData *) cls;
214 peer->bytes_wrote = 0;
215 GNUNET_assert (socket == peer1.socket);
216 GNUNET_assert (socket == peer->socket);
217 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
222 * Scheduler call back; to be executed when a new stream is connected
223 * Called from listen connect for peer2
226 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
233 * @param status the status of the stream at the time this function is called
234 * @param data traffic from the other side
235 * @param size the number of bytes available in data read
236 * @return number of bytes of processed from 'data' (any data remaining should be
237 * given to the next time the read processor is called).
240 input_processor (void *cls,
241 enum GNUNET_STREAM_Status status,
242 const void *input_data,
245 struct PeerData *peer = cls;
247 GNUNET_assert (GNUNET_STREAM_OK == status);
248 GNUNET_assert (&peer2 == peer);
249 GNUNET_assert (size < DATA_SIZE);
250 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
252 peer->bytes_read += size;
254 if (peer->bytes_read < DATA_SIZE)
256 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
257 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
261 /* Peer2 has completed reading*/
262 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
269 * Scheduler call back; to be executed when a new stream is connected
270 * Called from listen connect for peer2
273 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
275 struct PeerData *peer = cls;
277 read_task = GNUNET_SCHEDULER_NO_TASK;
278 GNUNET_assert (&peer2 == peer);
279 peer->io_read_handle =
280 GNUNET_STREAM_read (peer->socket,
281 GNUNET_TIME_relative_multiply
282 (GNUNET_TIME_UNIT_SECONDS, 10),
285 GNUNET_assert (NULL != peer->io_read_handle);
290 * Functions of this type are called upon new stream connection from other peers
292 * @param cls the closure from GNUNET_STREAM_listen
293 * @param socket the socket representing the stream
294 * @param initiator the identity of the peer who wants to establish a stream
296 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
297 * stream (the socket will be invalid after the call)
300 stream_listen_cb (void *cls,
301 struct GNUNET_STREAM_Socket *socket,
302 const struct GNUNET_PeerIdentity *initiator)
304 GNUNET_assert (NULL != socket);
305 GNUNET_assert (socket != peer1.socket);
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Peer connected: %s\n", GNUNET_i2s(initiator));
308 peer2.socket = socket;
309 peer2.bytes_read = 0;
310 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
316 * Task for connecting the peer to stream as client
318 * @param cls PeerData
319 * @param tc the TaskContext
322 stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 struct PeerData *peer = cls;
326 GNUNET_assert (&peer1 == peer);
327 /* Connect to stream */
328 peer->socket = GNUNET_STREAM_open (config,
329 &self_id, /* Null for local peer? */
333 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
334 UINT32_MAX - GNUNET_CRYPTO_random_u32
335 (GNUNET_CRYPTO_QUALITY_WEAK, 64),
336 GNUNET_STREAM_OPTION_END);
337 GNUNET_assert (NULL != peer->socket);
342 * Initialize framework and start test
346 const struct GNUNET_CONFIGURATION_Handle *cfg,
347 struct GNUNET_TESTING_Peer *peer)
351 (void) GNUNET_TESTING_peer_get_identity (peer, &self_id);
352 peer2_listen_socket = GNUNET_STREAM_listen (config,
356 GNUNET_STREAM_OPTION_END);
357 GNUNET_assert (NULL != peer2_listen_socket);
358 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1);
360 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
361 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
369 int main (int argc, char **argv)
371 if (0 != GNUNET_TESTING_peer_run ("test_stream_sequence_wraparound",
372 "test_stream_local.conf",
375 return (GNUNET_SYSERR == result) ? 1 : 0;
378 /* end of test_stream_sequence_wraparound.c */