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";
84 * @param cls the closure from GNUNET_STREAM_write/read
85 * @param status the status of the stream at the time this function is called
86 * @param data traffic from the other side
87 * @param size the number of bytes available in data read
88 * @return number of bytes of processed from 'data' (any data remaining should be
89 * given to the next time the read processor is called).
92 input_processor (void *cls,
93 enum GNUNET_STREAM_Status status,
94 const void *input_data,
98 * Task for calling STREAM_read
100 * @param cls the peer data entity
101 * @param tc the task context
104 stream_read_task (void *cls,
105 const struct GNUNET_SCHEDULER_TaskContext *tc)
107 struct PeerData *peer = cls;
109 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
110 GNUNET_TIME_relative_multiply
111 (GNUNET_TIME_UNIT_SECONDS, 5),
114 GNUNET_assert (NULL != peer->io_read_handle);
118 * The write completion function; called upon writing some data to stream or
121 * @param cls the closure from GNUNET_STREAM_write/read
122 * @param status the status of the stream at the time this function is called
123 * @param size the number of bytes read or written
126 write_completion (void *cls,
127 enum GNUNET_STREAM_Status status,
132 * Task for calling STREAM_write
134 * @param cls the peer data entity
135 * @param tc the task context
138 stream_write_task (void *cls,
139 const struct GNUNET_SCHEDULER_TaskContext *tc)
141 struct PeerData *peer = cls;
143 peer->io_write_handle =
144 GNUNET_STREAM_write (peer->socket,
146 strlen(data) - peer->bytes_wrote,
147 GNUNET_TIME_relative_multiply
148 (GNUNET_TIME_UNIT_SECONDS, 5),
152 GNUNET_assert (NULL != peer->io_write_handle);
159 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 GNUNET_STREAM_close (peer1.socket);
162 if (NULL != peer2.socket)
163 GNUNET_STREAM_close (peer2.socket);
164 if (NULL != peer2_listen_socket)
165 GNUNET_STREAM_listen_close (peer2_listen_socket);
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
169 GNUNET_SCHEDULER_cancel (abort_task);
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
172 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
174 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
177 /* Free the duplicated configuration */
178 GNUNET_CONFIGURATION_destroy (config);
179 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
180 GNUNET_OS_process_close (arm_pid);
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");
193 GNUNET_SCHEDULER_cancel (test_task);
197 GNUNET_SCHEDULER_cancel (read_task);
199 result = GNUNET_SYSERR;
201 do_shutdown (cls, tc);
205 * Signature for input processor
207 * @param cls the closure from GNUNET_STREAM_write/read
208 * @param status the status of the stream at the time this function is called
209 * @param data traffic from the other side
210 * @param size the number of bytes available in data read
211 * @return number of bytes of processed from 'data' (any data remaining should be
212 * given to the next time the read processor is called).
215 input_processor (void *cls,
216 enum GNUNET_STREAM_Status status,
217 const void *input_data,
222 * The write completion function; called upon writing some data to stream or
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 size the number of bytes read or written
230 write_completion (void *cls,
231 enum GNUNET_STREAM_Status status,
234 struct PeerData *peer=cls;;
236 GNUNET_assert (GNUNET_STREAM_OK == status);
237 GNUNET_assert (size < strlen (data));
238 peer->bytes_wrote += size;
240 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
242 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
246 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
248 peer->bytes_read = 0;
249 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
256 * Function executed after stream has been established
258 * @param cls the closure from GNUNET_STREAM_open
259 * @param socket socket to use to communicate with the other side (read/write)
262 stream_open_cb (void *cls,
263 struct GNUNET_STREAM_Socket *socket)
265 struct PeerData *peer=cls;
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
268 peer->bytes_wrote = 0;
269 GNUNET_assert (socket == peer1.socket);
270 GNUNET_assert (socket == peer->socket);
271 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
278 * @param cls the closure from GNUNET_STREAM_write/read
279 * @param status the status of the stream at the time this function is called
280 * @param data traffic from the other side
281 * @param size the number of bytes available in data read
282 * @return number of bytes of processed from 'data' (any data remaining should be
283 * given to the next time the read processor is called).
286 input_processor (void *cls,
287 enum GNUNET_STREAM_Status status,
288 const void *input_data,
291 struct PeerData *peer = cls;
293 GNUNET_assert (GNUNET_STREAM_OK == status);
294 GNUNET_assert (size < strlen (data));
295 GNUNET_assert (strncmp ((const char *) data + peer->bytes_read,
296 (const char *) input_data,
298 peer->bytes_read += size;
300 if (peer->bytes_read < strlen (data))
302 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
306 if (&peer2 == peer) /* Peer2 has completed reading; should write */
308 peer->bytes_wrote = 0;
309 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
311 else /* Peer1 has completed reading. End of tests */
313 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
321 * Scheduler call back; to be executed when a new stream is connected
322 * Called from listen connect for peer2
325 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
327 read_task = GNUNET_SCHEDULER_NO_TASK;
328 GNUNET_assert (NULL != cls);
329 peer2.bytes_read = 0;
330 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
335 * Functions of this type are called upon new stream connection from other peers
337 * @param cls the closure from GNUNET_STREAM_listen
338 * @param socket the socket representing the stream
339 * @param initiator the identity of the peer who wants to establish a stream
341 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
342 * stream (the socket will be invalid after the call)
345 stream_listen_cb (void *cls,
346 struct GNUNET_STREAM_Socket *socket,
347 const struct GNUNET_PeerIdentity *initiator)
349 struct GNUNET_PeerIdentity self;
351 GNUNET_assert (NULL != socket);
352 GNUNET_assert (socket != peer1.socket);
354 /* Get our identity */
355 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
357 GNUNET_assert (0 == memcmp (&self,
359 sizeof (struct GNUNET_PeerIdentity)));
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Peer connected: %s\n", GNUNET_i2s(initiator));
364 peer2.socket = socket;
365 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
374 * @param tc the task context
377 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
379 struct GNUNET_PeerIdentity self;
381 test_task = GNUNET_SCHEDULER_NO_TASK;
382 /* Get our identity */
383 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
386 peer2_listen_socket = GNUNET_STREAM_listen (config,
390 GNUNET_assert (NULL != peer2_listen_socket);
392 /* Connect to stream library */
393 peer1.socket = GNUNET_STREAM_open (config,
394 &self, /* Null for local peer? */
398 GNUNET_assert (NULL != peer1.socket);
402 * Initialize framework and start test
405 run (void *cls, char *const *args, const char *cfgfile,
406 const struct GNUNET_CONFIGURATION_Handle *cfg)
408 GNUNET_log_setup ("test_stream_local",
415 /* Duplicate the configuration */
416 config = GNUNET_CONFIGURATION_dup (cfg);
418 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
419 "gnunet-service-arm",
423 "-c", "test_stream_local.conf", NULL);
426 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
427 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
430 test_task = GNUNET_SCHEDULER_add_now (&test, NULL);
437 int main (int argc, char **argv)
441 char *const argv2[] = { "test-stream-local",
442 "-c", "test_stream_local.conf",
449 struct GNUNET_GETOPT_CommandLineOption options[] = {
450 GNUNET_GETOPT_OPTION_END
454 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
455 "test-stream-local", "nohelp", options, &run, NULL);
457 if (GNUNET_OK != ret)
459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
463 if (GNUNET_SYSERR == result)
465 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");