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__);
38 * Structure for holding peer's sockets and IO Handles
43 * Peer's stream socket
45 struct GNUNET_STREAM_Socket *socket;
48 * Peer's io write handle
50 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
53 * Peer's io read handle
55 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
58 * Bytes the peer has written
60 unsigned int bytes_wrote;
63 * Byte the peer has read
65 unsigned int bytes_read;
68 static struct GNUNET_OS_Process *arm_pid;
69 static struct PeerData peer1;
70 static struct PeerData peer2;
71 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
72 static struct GNUNET_CONFIGURATION_Handle *config;
74 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
75 static GNUNET_SCHEDULER_TaskIdentifier test_task;
76 static GNUNET_SCHEDULER_TaskIdentifier read_task;
77 static GNUNET_SCHEDULER_TaskIdentifier write_task;
79 #define DATA_SIZE 65536 /* 64KB */
80 static uint32_t data[DATA_SIZE / 4]; /* 64KB array */
87 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
89 GNUNET_STREAM_close (peer1.socket);
90 if (NULL != peer2.socket)
91 GNUNET_STREAM_close (peer2.socket);
92 if (NULL != peer2_listen_socket)
93 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
97 GNUNET_SCHEDULER_cancel (abort_task);
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
100 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
102 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
105 /* Free the duplicated configuration */
106 GNUNET_CONFIGURATION_destroy (config);
107 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
108 GNUNET_OS_process_destroy (arm_pid);
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");
121 GNUNET_SCHEDULER_cancel (test_task);
125 GNUNET_SCHEDULER_cancel (read_task);
127 result = GNUNET_SYSERR;
129 do_shutdown (cls, tc);
134 * The write completion function; called upon writing some data to stream or
137 * @param cls the closure from GNUNET_STREAM_write/read
138 * @param status the status of the stream at the time this function is called
139 * @param size the number of bytes read or written
142 write_completion (void *cls,
143 enum GNUNET_STREAM_Status status,
146 struct PeerData *peer;
148 peer = (struct PeerData *) cls;
149 GNUNET_assert (GNUNET_STREAM_OK == status);
150 GNUNET_assert (size < DATA_SIZE);
151 peer->bytes_wrote += size;
153 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
155 peer->io_write_handle =
156 GNUNET_STREAM_write (peer->socket,
157 ((void *) data) + peer->bytes_wrote,
158 DATA_SIZE - peer->bytes_wrote,
159 GNUNET_TIME_relative_multiply
160 (GNUNET_TIME_UNIT_SECONDS, 5),
163 GNUNET_assert (NULL != peer->io_write_handle);
167 LOG (GNUNET_ERROR_TYPE_DEBUG, "Writing successfully finished\n");
169 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
175 * Task for calling STREAM_write with a chunk of random data
177 * @param cls the peer data entity
178 * @param tc the task context
181 stream_write_task (void *cls,
182 const struct GNUNET_SCHEDULER_TaskContext *tc)
184 struct PeerData *peer=cls;
187 write_task = GNUNET_SCHEDULER_NO_TASK;
188 for (count=0; count < DATA_SIZE / 4; count++)
190 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
193 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
194 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
197 GNUNET_TIME_relative_multiply
198 (GNUNET_TIME_UNIT_SECONDS, 10),
201 GNUNET_assert (NULL != peer->io_write_handle);
206 * Function executed after stream has been established
208 * @param cls the closure from GNUNET_STREAM_open
209 * @param socket socket to use to communicate with the other side (read/write)
212 stream_open_cb (void *cls,
213 struct GNUNET_STREAM_Socket *socket)
215 struct PeerData *peer;
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
218 peer = (struct PeerData *) cls;
219 peer->bytes_wrote = 0;
220 GNUNET_assert (socket == peer1.socket);
221 GNUNET_assert (socket == peer->socket);
222 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
227 * Scheduler call back; to be executed when a new stream is connected
228 * Called from listen connect for peer2
231 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
238 * @param status the status of the stream at the time this function is called
239 * @param data traffic from the other side
240 * @param size the number of bytes available in data read
241 * @return number of bytes of processed from 'data' (any data remaining should be
242 * given to the next time the read processor is called).
245 input_processor (void *cls,
246 enum GNUNET_STREAM_Status status,
247 const void *input_data,
250 struct PeerData *peer = cls;
252 GNUNET_assert (GNUNET_STREAM_OK == status);
253 GNUNET_assert (&peer2 == peer);
254 GNUNET_assert (size < DATA_SIZE);
255 GNUNET_assert (0 == memcmp (((void *)data ) + peer->bytes_read,
257 peer->bytes_read += size;
259 if (peer->bytes_read < DATA_SIZE)
261 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == read_task);
262 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
263 /* peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) */
265 /* GNUNET_TIME_relative_multiply */
266 /* (GNUNET_TIME_UNIT_SECONDS, 5), */
267 /* &input_processor, */
269 /* GNUNET_assert (NULL != peer->io_read_handle); */
273 /* Peer2 has completed reading*/
274 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reading finished successfully\n");
281 * Scheduler call back; to be executed when a new stream is connected
282 * Called from listen connect for peer2
285 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
287 struct PeerData *peer = cls;
289 read_task = GNUNET_SCHEDULER_NO_TASK;
290 GNUNET_assert (&peer2 == peer);
291 peer->io_read_handle =
292 GNUNET_STREAM_read (peer->socket,
293 GNUNET_TIME_relative_multiply
294 (GNUNET_TIME_UNIT_SECONDS, 10),
297 GNUNET_assert (NULL != peer->io_read_handle);
302 * Functions of this type are called upon new stream connection from other peers
304 * @param cls the closure from GNUNET_STREAM_listen
305 * @param socket the socket representing the stream
306 * @param initiator the identity of the peer who wants to establish a stream
308 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
309 * stream (the socket will be invalid after the call)
312 stream_listen_cb (void *cls,
313 struct GNUNET_STREAM_Socket *socket,
314 const struct GNUNET_PeerIdentity *initiator)
316 GNUNET_assert (NULL != socket);
317 GNUNET_assert (socket != peer1.socket);
319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
320 "Peer connected: %s\n", GNUNET_i2s(initiator));
322 peer2.socket = socket;
323 peer2.bytes_read = 0;
324 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
333 * @param tc the task context
336 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 struct GNUNET_PeerIdentity self;
340 test_task = GNUNET_SCHEDULER_NO_TASK;
341 /* Get our identity */
342 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
345 peer2_listen_socket = GNUNET_STREAM_listen (config,
349 GNUNET_STREAM_OPTION_END);
350 GNUNET_assert (NULL != peer2_listen_socket);
352 /* Connect to stream library */
353 peer1.socket = GNUNET_STREAM_open (config,
354 &self, /* Null for local peer? */
358 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
359 UINT32_MAX - GNUNET_CRYPTO_random_u32
360 (GNUNET_CRYPTO_QUALITY_WEAK, 64),
361 GNUNET_STREAM_OPTION_END);
362 GNUNET_assert (NULL != peer1.socket);
367 * Initialize framework and start test
370 run (void *cls, char *const *args, const char *cfgfile,
371 const struct GNUNET_CONFIGURATION_Handle *cfg)
373 /* Duplicate the configuration */
374 config = GNUNET_CONFIGURATION_dup (cfg);
376 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
377 "gnunet-service-arm",
381 "-c", "test_stream_local.conf", NULL);
384 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
385 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
388 test_task = GNUNET_SCHEDULER_add_now (&test, NULL);
394 int main (int argc, char **argv)
398 char *const argv2[] = { "test-stream-big",
399 "-c", "test_stream_local.conf",
404 struct GNUNET_GETOPT_CommandLineOption options[] = {
405 GNUNET_GETOPT_OPTION_END
409 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
410 "test-stream-big", "nohelp", options, &run, NULL);
412 if (GNUNET_OK != ret)
414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
418 if (GNUNET_SYSERR == result)
420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");