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_halfclose.c
23 * @brief Stream API testing between local peers with half closed connections
24 * @author Sree Harsha Totakura
28 #include <sys/socket.h> /* For SHUT_RD, SHUT_WR */
31 #include "gnunet_util_lib.h"
32 #include "gnunet_mesh_service.h"
33 #include "gnunet_stream_lib.h"
38 * Structure for holding peer's sockets and IO Handles
43 * Peer's stream socket
45 struct GNUNET_STREAM_Socket *socket;
50 struct GNUNET_STREAM_IOHandle *io_handle;
53 * Bytes the peer has written
55 unsigned int bytes_wrote;
58 * Byte the peer has read
60 unsigned int bytes_read;
63 static struct GNUNET_OS_Process *arm_pid;
64 static struct PeerData peer1;
65 static struct PeerData peer2;
66 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
68 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
69 static GNUNET_SCHEDULER_TaskIdentifier test_task;
70 static GNUNET_SCHEDULER_TaskIdentifier read_task;
72 static char *data = "ABCD";
79 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 GNUNET_STREAM_close (peer1.socket);
82 GNUNET_STREAM_close (peer2.socket);
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
86 GNUNET_SCHEDULER_cancel (abort_task);
88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
89 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
91 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
94 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
95 GNUNET_OS_process_close (arm_pid);
100 * Something went wrong and timed out. Kill everything and set error flag
103 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
108 GNUNET_SCHEDULER_cancel (test_task);
112 GNUNET_SCHEDULER_cancel (read_task);
114 result = GNUNET_SYSERR;
116 do_shutdown (cls, tc);
121 * The write completion function; called upon writing some data to stream or
124 * @param cls the closure from GNUNET_STREAM_write/read
125 * @param status the status of the stream at the time this function is called
126 * @param size the number of bytes read or written
128 void write_completion (void *cls,
129 enum GNUNET_STREAM_Status status,
132 struct PeerData *peer;
134 peer = (struct PeerData *) cls;
135 GNUNET_assert (GNUNET_STREAM_OK == status);
136 GNUNET_assert (size < strlen (data));
137 peer->bytes_wrote += size;
139 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
141 peer->io_handle = GNUNET_STREAM_write (peer->socket,
143 strlen(data) - peer->bytes_wrote,
144 GNUNET_TIME_relative_multiply
145 (GNUNET_TIME_UNIT_SECONDS, 5),
148 GNUNET_assert (NULL != peer->io_handle);
152 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
154 peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
156 GNUNET_TIME_relative_multiply
157 (GNUNET_TIME_UNIT_SECONDS, 5),
160 GNUNET_assert (NULL!=peer->io_handle);
167 * Function executed after stream has been established
169 * @param cls the closure from GNUNET_STREAM_open
170 * @param socket socket to use to communicate with the other side (read/write)
173 stream_open_cb (void *cls,
174 struct GNUNET_STREAM_Socket *socket)
176 struct PeerData *peer;
178 peer = (struct PeerData *) cls;
179 peer->bytes_wrote = 0;
180 GNUNET_assert (socket == peer1.socket);
181 GNUNET_assert (socket == peer->socket);
182 peer->io_handle = GNUNET_STREAM_write (peer->socket, /* socket */
183 (void *) data, /* data */
185 GNUNET_TIME_relative_multiply
186 (GNUNET_TIME_UNIT_SECONDS, 5),
189 GNUNET_assert (NULL != peer->io_handle);
196 * @param cls the closure from GNUNET_STREAM_write/read
197 * @param status the status of the stream at the time this function is called
198 * @param data traffic from the other side
199 * @param size the number of bytes available in data read
200 * @return number of bytes of processed from 'data' (any data remaining should be
201 * given to the next time the read processor is called).
204 input_processor (void *cls,
205 enum GNUNET_STREAM_Status status,
206 const void *input_data,
209 struct PeerData *peer;
211 peer = (struct PeerData *) cls;
213 /* Peer1 is expected to read when it first finishes writing */
216 /* since p2 closed write */
217 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
218 /* Test passed; shutdown now */
219 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
223 GNUNET_assert (GNUNET_STERAM_OK == status);
224 GNUNET_assert (size < strlen (data));
225 GNUNET_assert (strncmp ((const char *) data + peer->bytes_read,
226 (const char *) input_data,
228 peer->bytes_read += size;
230 if (peer->bytes_read < strlen (data))
232 peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
234 GNUNET_TIME_relative_multiply
235 (GNUNET_TIME_UNIT_SECONDS, 5),
238 GNUNET_assert (NULL != peer->io_handle);
246 * Scheduler call back; to be executed when a new stream is connected
247 * Called from listen connect for peer2
250 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
252 read_task = GNUNET_SCHEDULER_NO_TASK;
253 GNUNET_assert (NULL != cls);
254 peer2.bytes_read = 0;
255 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
256 /* Close the stream for writing */
257 GNUNET_STREAM_shutdown ((struct GNUNET_STREAM_Socket *) cls,
259 peer2.io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
260 GNUNET_TIME_relative_multiply
261 (GNUNET_TIME_UNIT_SECONDS, 5),
264 GNUNET_assert (NULL != peer2.io_handle);
269 * Functions of this type are called upon new stream connection from other peers
271 * @param cls the closure from GNUNET_STREAM_listen
272 * @param socket the socket representing the stream
273 * @param initiator the identity of the peer who wants to establish a stream
275 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
276 * stream (the socket will be invalid after the call)
279 stream_listen_cb (void *cls,
280 struct GNUNET_STREAM_Socket *socket,
281 const struct GNUNET_PeerIdentity *initiator)
283 GNUNET_assert (NULL != socket);
284 GNUNET_assert (NULL == initiator); /* Local peer=NULL? */
285 GNUNET_assert (socket != peer1.socket);
287 peer2.socket = socket;
288 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
297 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
299 test_task = GNUNET_SCHEDULER_NO_TASK;
301 /* Connect to stream library */
302 peer1.socket = GNUNET_STREAM_open (NULL, /* Null for local peer? */
306 GNUNET_assert (NULL != peer1.socket);
307 peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */
310 GNUNET_assert (NULL != peer2_listen_socket);
315 * Initialize framework and start test
318 run (void *cls, char *const *args, const char *cfgfile,
319 const struct GNUNET_CONFIGURATION_Handle *cfg)
321 GNUNET_log_setup ("test_stream_local",
329 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
330 "gnunet-service-arm",
334 "-c", "test_stream_local.conf", NULL);
337 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
338 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
341 test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg);
348 int main (int argc, char **argv)
352 char *const argv2[] = { "test-stream-local",
353 "-c", "test_stream.conf",
360 struct GNUNET_GETOPT_CommandLineOption options[] = {
361 GNUNET_GETOPT_OPTION_END
365 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
366 "test-stream-local", "nohelp", options, &run, NULL);
368 if (GNUNET_OK != ret)
370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
374 if (GNUNET_SYSERR == result)
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");