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_local.c
23 * @brief Stream API testing between local peers
24 * @author Sree Harsha Totakura
30 #include "gnunet_util_lib.h"
31 #include "gnunet_mesh_service.h"
32 #include "gnunet_stream_lib.h"
33 #include "gnunet_testing_lib-new.h"
36 * Relative seconds shorthand
38 #define TIME_REL_SECS(sec) \
39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
43 * Structure for holding peer's sockets and IO Handles
48 * Peer's stream socket
50 struct GNUNET_STREAM_Socket *socket;
53 * Peer's io write handle
55 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
58 * Peer's io read handle
60 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
63 * Bytes the peer has written
65 unsigned int bytes_wrote;
68 * Byte the peer has read
70 unsigned int bytes_read;
73 static struct PeerData peer1;
74 static struct PeerData peer2;
75 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
76 static const struct GNUNET_CONFIGURATION_Handle *config;
77 static struct GNUNET_TESTING_Peer *self;
78 static struct GNUNET_PeerIdentity self_id;
80 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
82 static char *data = "ABCD";
85 static int writing_success;
86 static int reading_success;
92 * @param cls the closure from GNUNET_STREAM_write/read
93 * @param status the status of the stream at the time this function is called
94 * @param data traffic from the other side
95 * @param size the number of bytes available in data read
96 * @return number of bytes of processed from 'data' (any data remaining should be
97 * given to the next time the read processor is called).
100 input_processor (void *cls,
101 enum GNUNET_STREAM_Status status,
102 const void *input_data,
106 * Task for calling STREAM_read
108 * @param cls the peer data entity
109 * @param tc the task context
112 stream_read_task (void *cls,
113 const struct GNUNET_SCHEDULER_TaskContext *tc)
115 struct PeerData *peer = cls;
117 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
118 GNUNET_TIME_relative_multiply
119 (GNUNET_TIME_UNIT_SECONDS, 5),
122 GNUNET_assert (NULL != peer->io_read_handle);
127 * The write completion function; called upon writing some data to stream or
130 * @param cls the closure from GNUNET_STREAM_write/read
131 * @param status the status of the stream at the time this function is called
132 * @param size the number of bytes read or written
135 write_completion (void *cls,
136 enum GNUNET_STREAM_Status status,
141 * Task for calling STREAM_write
143 * @param cls the peer data entity
144 * @param tc the task context
147 stream_write_task (void *cls,
148 const struct GNUNET_SCHEDULER_TaskContext *tc)
150 struct PeerData *peer = cls;
152 peer->io_write_handle =
153 GNUNET_STREAM_write (peer->socket,
155 strlen(data) - peer->bytes_wrote,
156 GNUNET_TIME_relative_multiply
157 (GNUNET_TIME_UNIT_SECONDS, 5),
161 GNUNET_assert (NULL != peer->io_write_handle);
169 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 GNUNET_STREAM_close (peer1.socket);
172 if (NULL != peer2.socket)
173 GNUNET_STREAM_close (peer2.socket);
174 if (NULL != peer2_listen_socket)
175 GNUNET_STREAM_listen_close (peer2_listen_socket);
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
179 GNUNET_SCHEDULER_cancel (abort_task);
185 * Something went wrong and timed out. Kill everything and set error flag
188 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
191 result = GNUNET_SYSERR;
193 do_shutdown (cls, tc);
198 * The write completion function; called upon writing some data to stream or
201 * @param cls the closure from GNUNET_STREAM_write/read
202 * @param status the status of the stream at the time this function is called
203 * @param size the number of bytes read or written
206 write_completion (void *cls,
207 enum GNUNET_STREAM_Status status,
210 struct PeerData *peer=cls;
212 GNUNET_assert (GNUNET_STREAM_OK == status);
213 GNUNET_assert (size <= strlen (data));
214 peer->bytes_wrote += size;
215 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
217 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Writing completed\n");
223 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
225 peer->bytes_read = 0;
226 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
230 writing_success = GNUNET_YES;
231 if (GNUNET_YES == reading_success)
232 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
239 * Function executed after stream has been established
241 * @param cls the closure from GNUNET_STREAM_open
242 * @param socket socket to use to communicate with the other side (read/write)
245 stream_open_cb (void *cls,
246 struct GNUNET_STREAM_Socket *socket)
248 struct PeerData *peer=cls;
250 GNUNET_assert (&peer1 == peer);
251 GNUNET_assert (socket == peer1.socket);
252 GNUNET_assert (socket == peer->socket);
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
254 peer->bytes_wrote = 0;
255 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
262 * @param cls the closure from GNUNET_STREAM_write/read
263 * @param status the status of the stream at the time this function is called
264 * @param data traffic from the other side
265 * @param size the number of bytes available in data read
266 * @return number of bytes of processed from 'data' (any data remaining should be
267 * given to the next time the read processor is called).
270 input_processor (void *cls,
271 enum GNUNET_STREAM_Status status,
272 const void *input_data,
275 struct PeerData *peer = cls;
277 GNUNET_assert (GNUNET_STREAM_OK == status);
278 GNUNET_assert (size <= strlen (data));
279 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
280 (const char *) input_data,
282 peer->bytes_read += size;
283 if (peer->bytes_read < strlen (data))
285 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
289 if (&peer2 == peer) /* Peer2 has completed reading; should write */
291 peer->bytes_wrote = 0;
292 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
294 else /* Peer1 has completed reading. End of tests */
296 reading_success = GNUNET_YES;
297 if (GNUNET_YES == writing_success)
298 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
306 * Functions of this type are called upon new stream connection from other peers
308 * @param cls the PeerData of peer2
309 * @param socket the socket representing the stream
310 * @param initiator the identity of the peer who wants to establish a stream
312 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
313 * stream (the socket will be invalid after the call)
316 stream_listen_cb (void *cls,
317 struct GNUNET_STREAM_Socket *socket,
318 const struct GNUNET_PeerIdentity *initiator)
320 struct PeerData *peer=cls;
322 GNUNET_assert (NULL != socket);
323 GNUNET_assert (socket != peer1.socket);
324 GNUNET_assert (&peer2 == peer);
325 GNUNET_assert (0 == memcmp (&self_id,
327 sizeof (struct GNUNET_PeerIdentity)));
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Peer connected: %s\n", GNUNET_i2s(initiator));
330 peer->socket = socket;
331 peer->bytes_read = 0;
332 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
338 * Task for connecting the peer to stream as client
340 * @param cls PeerData
341 * @param tc the TaskContext
344 stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 struct PeerData *peer = cls;
348 GNUNET_assert (&peer1 == peer);
349 /* Connect to stream library */
350 peer->socket = GNUNET_STREAM_open (config,
355 GNUNET_STREAM_OPTION_END);
356 GNUNET_assert (NULL != peer1.socket);
361 * Initialize framework and start test
365 const struct GNUNET_CONFIGURATION_Handle *cfg,
366 struct GNUNET_TESTING_Peer *peer)
370 GNUNET_TESTING_peer_get_identity (peer, &self_id);
371 peer2_listen_socket = GNUNET_STREAM_listen (config,
375 GNUNET_STREAM_OPTION_END);
376 GNUNET_assert (NULL != peer2_listen_socket);
377 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1);
379 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
380 (GNUNET_TIME_UNIT_SECONDS, 30), &do_abort,
387 int main (int argc, char **argv)
389 if (0 != GNUNET_TESTING_peer_run ("test_stream_local",
390 "test_stream_local.conf",
393 return (GNUNET_SYSERR == result) ? 1 : 0;
396 /* end of test_stream_local.c */