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_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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
95 GNUNET_SCHEDULER_cancel (abort_task);
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
98 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
100 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
103 /* Free the duplicated configuration */
104 GNUNET_CONFIGURATION_destroy (config);
105 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
106 GNUNET_OS_process_destroy (arm_pid);
111 * Something went wrong and timed out. Kill everything and set error flag
114 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
119 GNUNET_SCHEDULER_cancel (test_task);
123 GNUNET_SCHEDULER_cancel (read_task);
125 result = GNUNET_SYSERR;
127 do_shutdown (cls, tc);
132 * The write completion function; called upon writing some data to stream or
135 * @param cls the closure from GNUNET_STREAM_write/read
136 * @param status the status of the stream at the time this function is called
137 * @param size the number of bytes read or written
140 write_completion (void *cls,
141 enum GNUNET_STREAM_Status status,
144 struct PeerData *peer;
146 peer = (struct PeerData *) cls;
147 GNUNET_assert (GNUNET_STREAM_OK == status);
148 GNUNET_assert (size < DATA_SIZE);
149 peer->bytes_wrote += size;
151 if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
153 peer->io_write_handle =
154 GNUNET_STREAM_write (peer->socket,
156 DATA_SIZE - peer->bytes_wrote,
157 GNUNET_TIME_relative_multiply
158 (GNUNET_TIME_UNIT_SECONDS, 5),
161 GNUNET_assert (NULL != peer->io_write_handle);
165 LOG (GNUNET_ERROR_TYPE_DEBUG,
166 "Finished writing the data; waiting for the other peer to finish"
173 * Task for calling STREAM_write with a chunk of random data
175 * @param cls the peer data entity
176 * @param tc the task context
179 stream_write_task (void *cls,
180 const struct GNUNET_SCHEDULER_TaskContext *tc)
182 struct PeerData *peer=cls;
185 write_task = GNUNET_SCHEDULER_NO_TASK;
186 for (count=0; count < DATA_SIZE / 4; count++)
188 data[count]=GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
191 LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
192 peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
195 GNUNET_TIME_relative_multiply
196 (GNUNET_TIME_UNIT_SECONDS, 10),
199 GNUNET_assert (NULL != peer->io_write_handle);
204 * Function executed after stream has been established
206 * @param cls the closure from GNUNET_STREAM_open
207 * @param socket socket to use to communicate with the other side (read/write)
210 stream_open_cb (void *cls,
211 struct GNUNET_STREAM_Socket *socket)
213 struct PeerData *peer;
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
216 peer = (struct PeerData *) cls;
217 peer->bytes_wrote = 0;
218 GNUNET_assert (socket == peer1.socket);
219 GNUNET_assert (socket == peer->socket);
220 write_task = GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
227 * @param cls the closure from GNUNET_STREAM_write/read
228 * @param status the status of the stream at the time this function is called
229 * @param data traffic from the other side
230 * @param size the number of bytes available in data read
231 * @return number of bytes of processed from 'data' (any data remaining should be
232 * given to the next time the read processor is called).
235 input_processor (void *cls,
236 enum GNUNET_STREAM_Status status,
237 const void *input_data,
240 struct PeerData *peer = cls;
242 GNUNET_assert (GNUNET_STREAM_OK == status);
243 GNUNET_assert (size < DATA_SIZE);
244 GNUNET_assert (memcmp (data + peer->bytes_read,
247 peer->bytes_read += size;
249 if (peer->bytes_read < DATA_SIZE)
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 /* Peer2 has completed reading*/
263 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
270 * Scheduler call back; to be executed when a new stream is connected
271 * Called from listen connect for peer2
274 stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
276 struct GNUNET_STREAM_Socket *socket = cls;
278 read_task = GNUNET_SCHEDULER_NO_TASK;
279 GNUNET_assert (socket == peer2.socket);
280 peer2.bytes_read = 0;
281 GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
282 peer2.io_read_handle =
283 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
284 GNUNET_TIME_relative_multiply
285 (GNUNET_TIME_UNIT_SECONDS, 10),
288 GNUNET_assert (NULL != peer2.io_read_handle);
293 * Functions of this type are called upon new stream connection from other peers
295 * @param cls the closure from GNUNET_STREAM_listen
296 * @param socket the socket representing the stream
297 * @param initiator the identity of the peer who wants to establish a stream
299 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
300 * stream (the socket will be invalid after the call)
303 stream_listen_cb (void *cls,
304 struct GNUNET_STREAM_Socket *socket,
305 const struct GNUNET_PeerIdentity *initiator)
307 GNUNET_assert (NULL != socket);
308 GNUNET_assert (socket != peer1.socket);
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Peer connected: %s\n", GNUNET_i2s(initiator));
313 peer2.socket = socket;
314 read_task = GNUNET_SCHEDULER_add_now (&stream_read_task, socket);
323 * @param tc the task context
326 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
328 struct GNUNET_PeerIdentity self;
330 test_task = GNUNET_SCHEDULER_NO_TASK;
331 /* Get our identity */
332 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
335 peer2_listen_socket = GNUNET_STREAM_listen (config,
339 GNUNET_assert (NULL != peer2_listen_socket);
341 /* Connect to stream library */
342 peer1.socket = GNUNET_STREAM_open (config,
343 &self, /* Null for local peer? */
347 GNUNET_assert (NULL != peer1.socket);
351 * Initialize framework and start test
354 run (void *cls, char *const *args, const char *cfgfile,
355 const struct GNUNET_CONFIGURATION_Handle *cfg)
357 /* Duplicate the configuration */
358 config = GNUNET_CONFIGURATION_dup (cfg);
360 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
361 "gnunet-service-arm",
365 "-c", "test_stream_local.conf", NULL);
368 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
369 (GNUNET_TIME_UNIT_SECONDS, 30), &do_abort,
372 test_task = GNUNET_SCHEDULER_add_now (&test, NULL);
378 int main (int argc, char **argv)
382 char *const argv2[] = { "test-stream-big",
383 "-c", "test_stream_local.conf",
388 struct GNUNET_GETOPT_CommandLineOption options[] = {
389 GNUNET_GETOPT_OPTION_END
393 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
394 "test-stream-big", "nohelp", options, &run, NULL);
396 if (GNUNET_OK != ret)
398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
402 if (GNUNET_SYSERR == result)
404 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");