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 if (NULL != peer1.socket)
99 GNUNET_STREAM_close (peer1.socket);
100 if (NULL != peer2.socket)
101 GNUNET_STREAM_close (peer2.socket);
102 if (NULL != peer2_listen_socket)
103 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
107 GNUNET_SCHEDULER_cancel (abort_task);
113 * Something went wrong and timed out. Kill everything and set error flag
116 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
119 if (GNUNET_SCHEDULER_NO_TASK != read_task)
121 GNUNET_SCHEDULER_cancel (read_task);
123 result = GNUNET_SYSERR;
125 do_shutdown (cls, tc);
130 * The write completion function; called upon writing some data to stream or
133 * @param cls the closure from GNUNET_STREAM_write/read
134 * @param status the status of the stream at the time this function is called
135 * @param size the number of bytes read or written
138 write_completion (void *cls,
139 enum GNUNET_STREAM_Status status,
142 struct PeerData *peer;
144 peer = (struct PeerData *) cls;
145 GNUNET_assert (GNUNET_STREAM_OK == status);
146 GNUNET_assert (size <= DATA_SIZE);
147 peer->bytes_wrote += size;
149 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
151 peer->io_write_handle =
152 GNUNET_STREAM_write (peer->socket,
153 ((void *) data) + peer->bytes_wrote,
154 DATA_SIZE - peer->bytes_wrote,
155 GNUNET_TIME_relative_multiply
156 (GNUNET_TIME_UNIT_SECONDS, 5),
159 GNUNET_assert (NULL != peer->io_write_handle);
163 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
165 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
171 * Task for calling STREAM_write with a chunk of random data
173 * @param cls the peer data entity
174 * @param tc the task context
177 stream_write_task (void *cls,
178 const struct GNUNET_SCHEDULER_TaskContext *tc)
180 struct PeerData *peer=cls;
183 write_task = GNUNET_SCHEDULER_NO_TASK;
184 for (count=0; count < DATA_SIZE / 4; count++)
186 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
189 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
190 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
193 GNUNET_TIME_relative_multiply
194 (GNUNET_TIME_UNIT_SECONDS, 10),
197 GNUNET_assert (NULL != peer->io_write_handle);
202 * Function executed after stream has been established
204 * @param cls the closure from GNUNET_STREAM_open
205 * @param socket socket to use to communicate with the other side (read/write)
208 stream_open_cb (void *cls,
209 struct GNUNET_STREAM_Socket *socket)
211 struct PeerData *peer;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
214 peer = (struct PeerData *) cls;
215 peer->bytes_wrote = 0;
216 GNUNET_assert (socket == peer1.socket);
217 GNUNET_assert (socket == peer->socket);
218 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
223 * Scheduler call back; to be executed when a new stream is connected
224 * Called from listen connect for peer2
227 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
234 * @param status the status of the stream at the time this function is called
235 * @param data traffic from the other side
236 * @param size the number of bytes available in data read
237 * @return number of bytes of processed from 'data' (any data remaining should be
238 * given to the next time the read processor is called).
241 input_processor (void *cls,
242 enum GNUNET_STREAM_Status status,
243 const void *input_data,
246 struct PeerData *peer = cls;
248 GNUNET_assert (GNUNET_STREAM_OK == status);
249 GNUNET_assert (&peer2 == peer);
250 GNUNET_assert (size < DATA_SIZE);
251 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
253 peer->bytes_read += size;
255 if (peer->bytes_read < DATA_SIZE)
257 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
258 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
262 /* Peer2 has completed reading*/
263 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
270 * Scheduler call back; to be executed when a new stream is connected
271 * Called from listen connect for peer2
274 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
276 struct PeerData *peer = cls;
278 read_task = GNUNET_SCHEDULER_NO_TASK;
279 GNUNET_assert (&peer2 == peer);
280 peer->io_read_handle =
281 GNUNET_STREAM_read (peer->socket,
282 GNUNET_TIME_relative_multiply
283 (GNUNET_TIME_UNIT_SECONDS, 10),
286 GNUNET_assert (NULL != peer->io_read_handle);
291 * Functions of this type are called upon new stream connection from other peers
293 * @param cls the closure from GNUNET_STREAM_listen
294 * @param socket the socket representing the stream
295 * @param initiator the identity of the peer who wants to establish a stream
297 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
298 * stream (the socket will be invalid after the call)
301 stream_listen_cb (void *cls,
302 struct GNUNET_STREAM_Socket *socket,
303 const struct GNUNET_PeerIdentity *initiator)
305 if ((NULL == socket) || (NULL == initiator))
307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
308 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
309 GNUNET_SCHEDULER_cancel (abort_task);
310 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
313 GNUNET_assert (NULL != socket);
314 GNUNET_assert (socket != peer1.socket);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Peer connected: %s\n", GNUNET_i2s(initiator));
317 peer2.socket = socket;
318 peer2.bytes_read = 0;
319 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
325 * Listen success callback; connects a peer to stream as client
328 stream_connect (void)
331 GNUNET_STREAM_open (config,
332 &self_id, /* Null for local peer? */
336 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
337 UINT32_MAX - GNUNET_CRYPTO_random_u32
338 (GNUNET_CRYPTO_QUALITY_WEAK, 64),
339 GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
340 GNUNET_STREAM_OPTION_END);
341 GNUNET_assert (NULL != peer1.socket);
346 * Initialize framework and start test
350 const struct GNUNET_CONFIGURATION_Handle *cfg,
351 struct GNUNET_TESTING_Peer *peer)
355 (void) GNUNET_TESTING_peer_get_identity (peer, &self_id);
356 peer2_listen_socket =
357 GNUNET_STREAM_listen (config,
361 GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
363 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
365 GNUNET_STREAM_OPTION_END);
366 GNUNET_assert (NULL != peer2_listen_socket);
368 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
369 (GNUNET_TIME_UNIT_SECONDS, 100), &do_abort,
377 int main (int argc, char **argv)
379 if (0 != GNUNET_TESTING_peer_run ("test_stream_sequence_wraparound",
380 "test_stream_local.conf",
383 return (GNUNET_SYSERR == result) ? 1 : 0;
386 /* end of test_stream_sequence_wraparound.c */