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"
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;
65 struct GNUNET_PeerIdentity our_id;
68 * Bytes the peer has written
70 unsigned int bytes_wrote;
73 * Byte the peer has read
75 unsigned int bytes_read;
79 * The current peer group
81 static struct GNUNET_TESTING_PeerGroup *pg;
86 static struct GNUNET_TESTING_Daemon *d1;
91 static struct GNUNET_TESTING_Daemon *d2;
93 static struct PeerData peer1;
94 static struct PeerData peer2;
95 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
96 static struct GNUNET_CONFIGURATION_Handle *config;
98 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
99 static GNUNET_SCHEDULER_TaskIdentifier read_task;
101 static char *data = "ABCD";
106 * Check whether peers successfully shut down.
109 shutdown_callback (void *cls, const char *emsg)
113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
114 "Shutdown of peers failed!\n");
118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
119 "All peers successfully shut down!\n");
121 GNUNET_CONFIGURATION_destroy (config);
129 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
131 if (NULL != peer1.socket)
132 GNUNET_STREAM_close (peer1.socket);
133 if (NULL != peer2.socket)
134 GNUNET_STREAM_close (peer2.socket);
135 if (NULL != peer2_listen_socket)
136 GNUNET_STREAM_listen_close (peer2_listen_socket);
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
141 GNUNET_SCHEDULER_cancel (abort_task);
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
146 GNUNET_TESTING_daemons_stop (pg,
147 GNUNET_TIME_relative_multiply
148 (GNUNET_TIME_UNIT_SECONDS, 5),
155 * Something went wrong and timed out. Kill everything and set error flag
158 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
163 GNUNET_SCHEDULER_cancel (read_task);
165 result = GNUNET_SYSERR;
167 do_shutdown (cls, tc);
171 * Signature for input processor
173 * @param cls the closure from GNUNET_STREAM_write/read
174 * @param status the status of the stream at the time this function is called
175 * @param data traffic from the other side
176 * @param size the number of bytes available in data read
177 * @return number of bytes of processed from 'data' (any data remaining should be
178 * given to the next time the read processor is called).
181 input_processor (void *cls,
182 enum GNUNET_STREAM_Status status,
183 const void *input_data,
188 * The write completion function; called upon writing some data to stream or
191 * @param cls the closure from GNUNET_STREAM_write/read
192 * @param status the status of the stream at the time this function is called
193 * @param size the number of bytes read or written
196 write_completion (void *cls,
197 enum GNUNET_STREAM_Status status,
200 struct PeerData *peer;
202 peer = (struct PeerData *) cls;
203 GNUNET_assert (GNUNET_STREAM_OK == status);
204 GNUNET_assert (size <= strlen (data));
205 peer->bytes_wrote += size;
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "Writing completed\n");
210 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
212 peer->io_write_handle =
213 GNUNET_STREAM_write (peer->socket,
215 strlen(data) - peer->bytes_wrote,
216 GNUNET_TIME_relative_multiply
217 (GNUNET_TIME_UNIT_SECONDS, 5),
220 GNUNET_assert (NULL != peer->io_write_handle);
224 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
226 peer->io_read_handle =
227 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
229 GNUNET_TIME_relative_multiply
230 (GNUNET_TIME_UNIT_SECONDS, 5),
233 GNUNET_assert (NULL!=peer->io_read_handle);
240 * Function executed after stream has been established
242 * @param cls the closure from GNUNET_STREAM_open
243 * @param socket socket to use to communicate with the other side (read/write)
246 stream_open_cb (void *cls,
247 struct GNUNET_STREAM_Socket *socket)
249 struct PeerData *peer;
251 GNUNET_assert (socket == peer1.socket);
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "%s: Stream established from peer1\n",
254 GNUNET_i2s (&peer1.our_id));
255 peer = (struct PeerData *) cls;
256 peer->bytes_wrote = 0;
257 GNUNET_assert (socket == peer1.socket);
258 GNUNET_assert (socket == peer->socket);
259 peer->io_write_handle = GNUNET_STREAM_write (peer->socket, /* socket */
260 (void *) data, /* data */
262 GNUNET_TIME_relative_multiply
263 (GNUNET_TIME_UNIT_SECONDS, 5),
266 GNUNET_assert (NULL != peer->io_write_handle);
273 * @param cls the closure from GNUNET_STREAM_write/read
274 * @param status the status of the stream at the time this function is called
275 * @param data traffic from the other side
276 * @param size the number of bytes available in data read
277 * @return number of bytes of processed from 'data' (any data remaining should be
278 * given to the next time the read processor is called).
281 input_processor (void *cls,
282 enum GNUNET_STREAM_Status status,
283 const void *input_data,
286 struct PeerData *peer;
288 peer = (struct PeerData *) cls;
290 GNUNET_assert (GNUNET_STREAM_OK == status);
291 GNUNET_assert (size < strlen (data));
292 GNUNET_assert (strncmp ((const char *) data + peer->bytes_read,
293 (const char *) input_data,
295 peer->bytes_read += size;
297 if (peer->bytes_read < strlen (data))
299 peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
301 GNUNET_TIME_relative_multiply
302 (GNUNET_TIME_UNIT_SECONDS, 5),
305 GNUNET_assert (NULL != peer->io_read_handle);
309 if (&peer2 == peer) /* Peer2 has completed reading; should write */
311 peer->bytes_wrote = 0;
312 peer->io_write_handle =
313 GNUNET_STREAM_write (peer->socket,
316 GNUNET_TIME_relative_multiply
317 (GNUNET_TIME_UNIT_SECONDS, 5),
321 else /* Peer1 has completed reading. End of tests */
323 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
331 * Scheduler call back; to be executed when a new stream is connected
332 * Called from listen connect for peer2
335 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 read_task = GNUNET_SCHEDULER_NO_TASK;
338 GNUNET_assert (NULL != cls);
339 peer2.bytes_read = 0;
340 peer2.io_read_handle =
341 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
342 GNUNET_TIME_relative_multiply
343 (GNUNET_TIME_UNIT_SECONDS, 5),
346 GNUNET_assert (NULL != peer2.io_read_handle);
351 * Functions of this type are called upon new stream connection from other peers
353 * @param cls the closure from GNUNET_STREAM_listen
354 * @param socket the socket representing the stream
355 * @param initiator the identity of the peer who wants to establish a stream
357 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
358 * stream (the socket will be invalid after the call)
361 stream_listen_cb (void *cls,
362 struct GNUNET_STREAM_Socket *socket,
363 const struct GNUNET_PeerIdentity *initiator)
365 GNUNET_assert (NULL != socket);
366 GNUNET_assert (NULL != initiator);
367 GNUNET_assert (socket != peer1.socket);
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "%s: Peer connected: %s\n",
371 GNUNET_i2s (&peer2.our_id),
372 GNUNET_i2s(initiator));
374 peer2.socket = socket;
375 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
381 * Callback to be called when testing peer group is ready
384 * @param emsg NULL on success
387 peergroup_ready (void *cls, const char *emsg)
391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
392 "Starting peer group failed: %s\n", emsg);
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
396 "Peer group is now ready\n");
398 GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg));
400 d1 = GNUNET_TESTING_daemon_get (pg, 0);
401 GNUNET_assert (NULL != d1);
403 d2 = GNUNET_TESTING_daemon_get (pg, 1);
404 GNUNET_assert (NULL != d2);
406 GNUNET_TESTING_get_peer_identity (d1->cfg,
408 GNUNET_TESTING_get_peer_identity (d2->cfg,
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412 GNUNET_i2s (&peer1.our_id),
413 GNUNET_i2s (&d1->id));
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
416 GNUNET_i2s (&peer2.our_id),
417 GNUNET_i2s (&d2->id));
419 peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg,
423 GNUNET_assert (NULL != peer2_listen_socket);
425 /* Connect to stream library */
426 peer1.socket = GNUNET_STREAM_open (d1->cfg,
427 &d2->id, /* Null for local peer? */
431 GNUNET_assert (NULL != peer1.socket);
436 * Initialize framework and start test
439 run (void *cls, char *const *args, const char *cfgfile,
440 const struct GNUNET_CONFIGURATION_Handle *cfg)
442 struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */
444 /* GNUNET_log_setup ("test_stream_local", */
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450 /* Duplicate the configuration */
451 config = GNUNET_CONFIGURATION_dup (cfg);
453 hosts = GNUNET_TESTING_hosts_load (config);
455 pg = GNUNET_TESTING_peergroup_start (config,
457 GNUNET_TIME_relative_multiply
458 (GNUNET_TIME_UNIT_SECONDS, 3),
463 GNUNET_assert (NULL != pg);
466 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
467 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
474 int main (int argc, char **argv)
478 char *argv2[] = { "test-stream-local",
480 "-c", "test_stream_local.conf",
483 struct GNUNET_GETOPT_CommandLineOption options[] = {
484 GNUNET_GETOPT_OPTION_END
488 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
489 "test-stream-local", "nohelp", options, &run, NULL);
491 if (GNUNET_OK != ret)
493 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
497 if (GNUNET_SYSERR == result)
499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
502 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");