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.h"
34 #define LOG(kind, ...) \
35 GNUNET_log (kind, __VA_ARGS__);
37 #define TIME_REL_SECS(sec) \
38 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
41 * Structure for holding peer's sockets and IO Handles
46 * Peer's stream socket
48 struct GNUNET_STREAM_Socket *socket;
51 * Peer's io write handle
53 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
56 * Peer's io read handle
58 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
61 * Bytes the peer has written
63 unsigned int bytes_wrote;
66 * Byte the peer has read
68 unsigned int bytes_read;
71 static struct GNUNET_OS_Process *arm_pid;
72 static struct PeerData peer1;
73 static struct PeerData peer2;
74 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
75 static struct GNUNET_CONFIGURATION_Handle *config;
77 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
78 static GNUNET_SCHEDULER_TaskIdentifier test_task;
79 static GNUNET_SCHEDULER_TaskIdentifier read_task;
80 static GNUNET_SCHEDULER_TaskIdentifier write_task;
82 #define DATA_SIZE 65536 /* 64KB */
83 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
90 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 GNUNET_STREAM_close (peer1.socket);
93 if (NULL != peer2.socket)
94 GNUNET_STREAM_close (peer2.socket);
95 if (NULL != peer2_listen_socket)
96 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
100 GNUNET_SCHEDULER_cancel (abort_task);
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
103 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
105 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
108 /* Free the duplicated configuration */
109 GNUNET_CONFIGURATION_destroy (config);
110 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
111 GNUNET_OS_process_destroy (arm_pid);
116 * Something went wrong and timed out. Kill everything and set error flag
119 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
124 GNUNET_SCHEDULER_cancel (test_task);
128 GNUNET_SCHEDULER_cancel (read_task);
130 result = GNUNET_SYSERR;
132 do_shutdown (cls, tc);
137 * The write completion function; called upon writing some data to stream or
140 * @param cls the closure from GNUNET_STREAM_write/read
141 * @param status the status of the stream at the time this function is called
142 * @param size the number of bytes read or written
145 write_completion (void *cls,
146 enum GNUNET_STREAM_Status status,
149 struct PeerData *peer;
151 peer = (struct PeerData *) cls;
152 GNUNET_assert (GNUNET_STREAM_OK == status);
153 GNUNET_assert (size < DATA_SIZE);
154 peer->bytes_wrote += size;
156 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
158 peer->io_write_handle =
159 GNUNET_STREAM_write (peer->socket,
160 ((void *) data) + peer->bytes_wrote,
161 DATA_SIZE - peer->bytes_wrote,
162 GNUNET_TIME_relative_multiply
163 (GNUNET_TIME_UNIT_SECONDS, 5),
166 GNUNET_assert (NULL != peer->io_write_handle);
170 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
172 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
178 * Task for calling STREAM_write with a chunk of random data
180 * @param cls the peer data entity
181 * @param tc the task context
184 stream_write_task (void *cls,
185 const struct GNUNET_SCHEDULER_TaskContext *tc)
187 struct PeerData *peer=cls;
190 write_task = GNUNET_SCHEDULER_NO_TASK;
191 for (count=0; count < DATA_SIZE / 4; count++)
193 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
197 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
200 GNUNET_TIME_relative_multiply
201 (GNUNET_TIME_UNIT_SECONDS, 10),
204 GNUNET_assert (NULL != peer->io_write_handle);
209 * Function executed after stream has been established
211 * @param cls the closure from GNUNET_STREAM_open
212 * @param socket socket to use to communicate with the other side (read/write)
215 stream_open_cb (void *cls,
216 struct GNUNET_STREAM_Socket *socket)
218 struct PeerData *peer;
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
221 peer = (struct PeerData *) cls;
222 peer->bytes_wrote = 0;
223 GNUNET_assert (socket == peer1.socket);
224 GNUNET_assert (socket == peer->socket);
225 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
230 * Scheduler call back; to be executed when a new stream is connected
231 * Called from listen connect for peer2
234 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
241 * @param status the status of the stream at the time this function is called
242 * @param data traffic from the other side
243 * @param size the number of bytes available in data read
244 * @return number of bytes of processed from 'data' (any data remaining should be
245 * given to the next time the read processor is called).
248 input_processor (void *cls,
249 enum GNUNET_STREAM_Status status,
250 const void *input_data,
253 struct PeerData *peer = cls;
255 GNUNET_assert (GNUNET_STREAM_OK == status);
256 GNUNET_assert (&peer2 == peer);
257 GNUNET_assert (size < DATA_SIZE);
258 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
260 peer->bytes_read += size;
262 if (peer->bytes_read < DATA_SIZE)
264 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
265 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
266 /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
268 /* GNUNET_TIME_relative_multiply */
269 /* (GNUNET_TIME_UNIT_SECONDS, 5), */
270 /* &input_processor, */
272 /* GNUNET_assert (NULL != peer->io_read_handle); */
276 /* Peer2 has completed reading*/
277 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
284 * Scheduler call back; to be executed when a new stream is connected
285 * Called from listen connect for peer2
288 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct PeerData *peer = cls;
292 read_task = GNUNET_SCHEDULER_NO_TASK;
293 GNUNET_assert (&peer2 == peer);
294 peer->io_read_handle =
295 GNUNET_STREAM_read (peer->socket,
296 GNUNET_TIME_relative_multiply
297 (GNUNET_TIME_UNIT_SECONDS, 10),
300 GNUNET_assert (NULL != peer->io_read_handle);
305 * Functions of this type are called upon new stream connection from other peers
307 * @param cls the closure from GNUNET_STREAM_listen
308 * @param socket the socket representing the stream
309 * @param initiator the identity of the peer who wants to establish a stream
311 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
312 * stream (the socket will be invalid after the call)
315 stream_listen_cb (void *cls,
316 struct GNUNET_STREAM_Socket *socket,
317 const struct GNUNET_PeerIdentity *initiator)
319 GNUNET_assert (NULL != socket);
320 GNUNET_assert (socket != peer1.socket);
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Peer connected: %s\n", GNUNET_i2s(initiator));
325 peer2.socket = socket;
326 peer2.bytes_read = 0;
327 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
333 * Task for connecting the peer to stream as client
335 * @param cls PeerData
336 * @param tc the TaskContext
339 stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct PeerData *peer = cls;
342 struct GNUNET_PeerIdentity self;
344 GNUNET_assert (&peer1 == peer);
345 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
347 /* Connect to stream */
348 peer->socket = GNUNET_STREAM_open (config,
349 &self, /* Null for local peer? */
353 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
354 UINT32_MAX - GNUNET_CRYPTO_random_u32
355 (GNUNET_CRYPTO_QUALITY_WEAK, 64),
356 GNUNET_STREAM_OPTION_END);
357 GNUNET_assert (NULL != peer->socket);
365 * @param tc the task context
368 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
370 struct GNUNET_PeerIdentity self;
372 test_task = GNUNET_SCHEDULER_NO_TASK;
373 /* Get our identity */
374 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
377 peer2_listen_socket = GNUNET_STREAM_listen (config,
381 GNUNET_STREAM_OPTION_END);
382 GNUNET_assert (NULL != peer2_listen_socket);
383 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1);
388 * Initialize framework and start test
391 run (void *cls, char *const *args, const char *cfgfile,
392 const struct GNUNET_CONFIGURATION_Handle *cfg)
394 /* Duplicate the configuration */
395 config = GNUNET_CONFIGURATION_dup (cfg);
397 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
398 "gnunet-service-arm",
402 "-c", "test_stream_local.conf", NULL);
405 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
406 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
408 test_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(3), &test, NULL);
414 int main (int argc, char **argv)
418 char *const argv2[] = { "test-stream-big",
419 "-c", "test_stream_local.conf",
424 struct GNUNET_GETOPT_CommandLineOption options[] = {
425 GNUNET_GETOPT_OPTION_END
429 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
430 "test-stream-big", "nohelp", options, &run, NULL);
432 if (GNUNET_OK != ret)
434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
438 if (GNUNET_SYSERR == result)
440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");