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.h"
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;
78 static char *data = "ABCD";
85 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 GNUNET_STREAM_close (peer1.socket);
88 if (NULL != peer2.socket)
89 GNUNET_STREAM_close (peer2.socket);
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
93 GNUNET_SCHEDULER_cancel (abort_task);
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
96 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
98 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
101 /* Free the duplicated configuration */
102 GNUNET_CONFIGURATION_destroy (config);
103 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
104 GNUNET_OS_process_close (arm_pid);
109 * Something went wrong and timed out. Kill everything and set error flag
112 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
117 GNUNET_SCHEDULER_cancel (test_task);
121 GNUNET_SCHEDULER_cancel (read_task);
123 result = GNUNET_SYSERR;
125 do_shutdown (cls, tc);
129 * Signature for input processor
131 * @param cls the closure from GNUNET_STREAM_write/read
132 * @param status the status of the stream at the time this function is called
133 * @param data traffic from the other side
134 * @param size the number of bytes available in data read
135 * @return number of bytes of processed from 'data' (any data remaining should be
136 * given to the next time the read processor is called).
139 input_processor (void *cls,
140 enum GNUNET_STREAM_Status status,
141 const void *input_data,
146 * The write completion function; called upon writing some data to stream or
149 * @param cls the closure from GNUNET_STREAM_write/read
150 * @param status the status of the stream at the time this function is called
151 * @param size the number of bytes read or written
154 write_completion (void *cls,
155 enum GNUNET_STREAM_Status status,
158 struct PeerData *peer;
160 peer = (struct PeerData *) cls;
161 GNUNET_assert (GNUNET_STREAM_OK == status);
162 GNUNET_assert (size < strlen (data));
163 peer->bytes_wrote += size;
165 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
167 peer->io_write_handle =
168 GNUNET_STREAM_write (peer->socket,
170 strlen(data) - peer->bytes_wrote,
171 GNUNET_TIME_relative_multiply
172 (GNUNET_TIME_UNIT_SECONDS, 5),
175 GNUNET_assert (NULL != peer->io_write_handle);
179 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
181 peer->io_read_handle =
182 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
184 GNUNET_TIME_relative_multiply
185 (GNUNET_TIME_UNIT_SECONDS, 5),
188 GNUNET_assert (NULL!=peer->io_read_handle);
195 * Function executed after stream has been established
197 * @param cls the closure from GNUNET_STREAM_open
198 * @param socket socket to use to communicate with the other side (read/write)
201 stream_open_cb (void *cls,
202 struct GNUNET_STREAM_Socket *socket)
204 struct PeerData *peer;
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
207 peer = (struct PeerData *) cls;
208 peer->bytes_wrote = 0;
209 GNUNET_assert (socket == peer1.socket);
210 GNUNET_assert (socket == peer->socket);
211 peer->io_write_handle = GNUNET_STREAM_write (peer->socket, /* socket */
212 (void *) data, /* data */
214 GNUNET_TIME_relative_multiply
215 (GNUNET_TIME_UNIT_SECONDS, 5),
218 GNUNET_assert (NULL != peer->io_write_handle);
225 * @param cls the closure from GNUNET_STREAM_write/read
226 * @param status the status of the stream at the time this function is called
227 * @param data traffic from the other side
228 * @param size the number of bytes available in data read
229 * @return number of bytes of processed from 'data' (any data remaining should be
230 * given to the next time the read processor is called).
233 input_processor (void *cls,
234 enum GNUNET_STREAM_Status status,
235 const void *input_data,
238 struct PeerData *peer;
240 peer = (struct PeerData *) cls;
242 GNUNET_assert (GNUNET_STREAM_OK == status);
243 GNUNET_assert (size < strlen (data));
244 GNUNET_assert (strncmp ((const char *) data + peer->bytes_read,
245 (const char *) input_data,
247 peer->bytes_read += size;
249 if (peer->bytes_read < strlen (data))
251 peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
253 GNUNET_TIME_relative_multiply
254 (GNUNET_TIME_UNIT_SECONDS, 5),
257 GNUNET_assert (NULL != peer->io_read_handle);
261 if (&peer2 == peer) /* Peer2 has completed reading; should write */
263 peer->bytes_wrote = 0;
264 peer->io_write_handle =
265 GNUNET_STREAM_write ((struct GNUNET_STREAM_Socket *)
269 GNUNET_TIME_relative_multiply
270 (GNUNET_TIME_UNIT_SECONDS, 5),
274 else /* Peer1 has completed reading. End of tests */
276 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
284 * Scheduler call back; to be executed when a new stream is connected
285 * Called from listen connect for peer2
288 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 read_task = GNUNET_SCHEDULER_NO_TASK;
291 GNUNET_assert (NULL != cls);
292 peer2.bytes_read = 0;
293 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
294 peer2.io_read_handle =
295 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
296 GNUNET_TIME_relative_multiply
297 (GNUNET_TIME_UNIT_SECONDS, 5),
300 GNUNET_assert (NULL != peer2.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 (NULL == initiator); /* Local peer=NULL? */
321 GNUNET_assert (socket != peer1.socket);
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Peer connected: %s\n", GNUNET_i2s(initiator));
326 peer2.socket = socket;
327 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
336 * @param tc the task context
339 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
341 struct GNUNET_PeerIdentity self;
343 test_task = GNUNET_SCHEDULER_NO_TASK;
344 /* Get our identity */
345 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
348 peer2_listen_socket = GNUNET_STREAM_listen (config,
352 GNUNET_assert (NULL != peer2_listen_socket);
354 /* Connect to stream library */
355 peer1.socket = GNUNET_STREAM_open (config,
356 &self, /* Null for local peer? */
360 GNUNET_assert (NULL != peer1.socket);
364 * Initialize framework and start test
367 run (void *cls, char *const *args, const char *cfgfile,
368 const struct GNUNET_CONFIGURATION_Handle *cfg)
370 GNUNET_log_setup ("test_stream_local",
377 /* Duplicate the configuration */
378 config = GNUNET_CONFIGURATION_dup (cfg);
380 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
381 "gnunet-service-arm",
385 "-c", "test_stream_local.conf", NULL);
388 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
389 (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort,
392 test_task = GNUNET_SCHEDULER_add_now (&test, NULL);
399 int main (int argc, char **argv)
403 char *const argv2[] = { "test-stream-local",
404 "-c", "test_stream_local.conf",
411 struct GNUNET_GETOPT_CommandLineOption options[] = {
412 GNUNET_GETOPT_OPTION_END
416 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
417 "test-stream-local", "nohelp", options, &run, NULL);
419 if (GNUNET_OK != ret)
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
425 if (GNUNET_SYSERR == result)
427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");