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_2peers.c
23 * @brief Stream API testing between 2 peers using testing API
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;
63 * Peer's shutdown handle
65 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
70 struct GNUNET_PeerIdentity our_id;
73 * Bytes the peer has written
75 unsigned int bytes_wrote;
78 * Byte the peer has read
80 unsigned int bytes_read;
84 * The current peer group
86 static struct GNUNET_TESTING_PeerGroup *pg;
91 static struct GNUNET_TESTING_Daemon *d1;
96 static struct GNUNET_TESTING_Daemon *d2;
98 static struct PeerData peer1;
99 static struct PeerData peer2;
100 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
101 static struct GNUNET_CONFIGURATION_Handle *config;
103 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
104 static GNUNET_SCHEDULER_TaskIdentifier read_task;
106 static char *data = "ABCD";
109 static int writing_success;
110 static int reading_success;
114 * Check whether peers successfully shut down.
117 peergroup_shutdown_callback (void *cls, const char *emsg)
121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
122 "Shutdown of peers failed!\n");
126 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
127 "All peers successfully shut down!\n");
129 GNUNET_CONFIGURATION_destroy (config);
134 * Close sockets and stop testing deamons nicely
137 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 if (NULL != peer1.socket)
140 GNUNET_STREAM_close (peer1.socket);
141 if (NULL != peer2.socket)
142 GNUNET_STREAM_close (peer2.socket);
143 if (NULL != peer2_listen_socket)
144 GNUNET_STREAM_listen_close (peer2_listen_socket);
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
149 GNUNET_SCHEDULER_cancel (abort_task);
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
154 GNUNET_TESTING_daemons_stop (pg,
155 GNUNET_TIME_relative_multiply
156 (GNUNET_TIME_UNIT_SECONDS, 5),
157 &peergroup_shutdown_callback,
163 * Completion callback for shutdown
165 * @param cls the closure from GNUNET_STREAM_shutdown call
166 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
170 shutdown_completion (void *cls,
173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174 "STREAM shutdown successful\n");
175 GNUNET_SCHEDULER_add_now (&do_close,
182 * Shutdown sockets gracefully
185 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket,
189 &shutdown_completion,
195 * Something went wrong and timed out. Kill everything and set error flag
198 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
203 GNUNET_SCHEDULER_cancel (read_task);
205 result = GNUNET_SYSERR;
211 * Signature for input processor
213 * @param cls the closure from GNUNET_STREAM_write/read
214 * @param status the status of the stream at the time this function is called
215 * @param data traffic from the other side
216 * @param size the number of bytes available in data read
217 * @return number of bytes of processed from 'data' (any data remaining should be
218 * given to the next time the read processor is called).
221 input_processor (void *cls,
222 enum GNUNET_STREAM_Status status,
223 const void *input_data,
228 * The write completion function; called upon writing some data to stream or
231 * @param cls the closure from GNUNET_STREAM_write/read
232 * @param status the status of the stream at the time this function is called
233 * @param size the number of bytes read or written
236 write_completion (void *cls,
237 enum GNUNET_STREAM_Status status,
240 struct PeerData *peer = cls;
242 GNUNET_assert (GNUNET_STREAM_OK == status);
243 GNUNET_assert (size <= strlen (data));
244 peer->bytes_wrote += size;
246 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
248 peer->io_write_handle =
249 GNUNET_STREAM_write (peer->socket,
251 strlen(data) - peer->bytes_wrote,
252 GNUNET_TIME_relative_multiply
253 (GNUNET_TIME_UNIT_SECONDS, 5),
256 GNUNET_assert (NULL != peer->io_write_handle);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Writing completed\n");
263 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
265 peer->io_read_handle =
266 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
268 GNUNET_TIME_relative_multiply
269 (GNUNET_TIME_UNIT_SECONDS, 5),
272 GNUNET_assert (NULL!=peer->io_read_handle);
276 writing_success = GNUNET_YES;
277 if (GNUNET_YES == reading_success)
278 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
285 * Function executed after stream has been established
287 * @param cls the closure from GNUNET_STREAM_open
288 * @param socket socket to use to communicate with the other side (read/write)
291 stream_open_cb (void *cls,
292 struct GNUNET_STREAM_Socket *socket)
294 struct PeerData *peer;
296 GNUNET_assert (socket == peer1.socket);
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
298 "%s: Stream established from peer1\n",
299 GNUNET_i2s (&peer1.our_id));
300 peer = (struct PeerData *) cls;
301 peer->bytes_wrote = 0;
302 GNUNET_assert (socket == peer1.socket);
303 GNUNET_assert (socket == peer->socket);
304 peer->io_write_handle = GNUNET_STREAM_write (peer->socket, /* socket */
305 (void *) data, /* data */
307 GNUNET_TIME_relative_multiply
308 (GNUNET_TIME_UNIT_SECONDS, 5),
311 GNUNET_assert (NULL != peer->io_write_handle);
318 * @param cls the closure from GNUNET_STREAM_write/read
319 * @param status the status of the stream at the time this function is called
320 * @param data traffic from the other side
321 * @param size the number of bytes available in data read
322 * @return number of bytes of processed from 'data' (any data remaining should be
323 * given to the next time the read processor is called).
326 input_processor (void *cls,
327 enum GNUNET_STREAM_Status status,
328 const void *input_data,
331 struct PeerData *peer;
333 peer = (struct PeerData *) cls;
335 if (GNUNET_STREAM_TIMEOUT == status)
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Read operation timedout - reading again!\n");
339 GNUNET_assert (0 == size);
340 peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
342 GNUNET_TIME_relative_multiply
343 (GNUNET_TIME_UNIT_SECONDS, 5),
346 GNUNET_assert (NULL != peer->io_read_handle);
350 GNUNET_assert (GNUNET_STREAM_OK == status);
351 GNUNET_assert (size <= strlen (data));
352 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
353 (const char *) input_data,
355 peer->bytes_read += size;
357 if (peer->bytes_read < strlen (data))
359 peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *)
361 GNUNET_TIME_relative_multiply
362 (GNUNET_TIME_UNIT_SECONDS, 5),
365 GNUNET_assert (NULL != peer->io_read_handle);
369 if (&peer2 == peer) /* Peer2 has completed reading; should write */
371 peer->bytes_wrote = 0;
372 peer->io_write_handle =
373 GNUNET_STREAM_write (peer->socket,
376 GNUNET_TIME_relative_multiply
377 (GNUNET_TIME_UNIT_SECONDS, 5),
381 else /* Peer1 has completed reading. End of tests */
383 reading_success = GNUNET_YES;
384 if (GNUNET_YES == writing_success)
385 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
393 * Scheduler call back; to be executed when a new stream is connected
394 * Called from listen connect for peer2
397 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
399 read_task = GNUNET_SCHEDULER_NO_TASK;
400 GNUNET_assert (NULL != cls);
401 peer2.bytes_read = 0;
402 peer2.io_read_handle =
403 GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls,
404 GNUNET_TIME_relative_multiply
405 (GNUNET_TIME_UNIT_SECONDS, 5),
408 GNUNET_assert (NULL != peer2.io_read_handle);
413 * Functions of this type are called upon new stream connection from other peers
415 * @param cls the closure from GNUNET_STREAM_listen
416 * @param socket the socket representing the stream
417 * @param initiator the identity of the peer who wants to establish a stream
419 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
420 * stream (the socket will be invalid after the call)
423 stream_listen_cb (void *cls,
424 struct GNUNET_STREAM_Socket *socket,
425 const struct GNUNET_PeerIdentity *initiator)
427 GNUNET_assert (NULL != socket);
428 GNUNET_assert (NULL != initiator);
429 GNUNET_assert (socket != peer1.socket);
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "%s: Peer connected: %s\n",
433 GNUNET_i2s (&peer2.our_id),
434 GNUNET_i2s(initiator));
436 peer2.socket = socket;
437 /* FIXME: reading should be done right now instead of a scheduled call */
438 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
444 * Callback to be called when testing peer group is ready
447 * @param emsg NULL on success
450 peergroup_ready (void *cls, const char *emsg)
454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
455 "Starting peer group failed: %s\n", emsg);
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "Peer group is now ready\n");
461 GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg));
463 d1 = GNUNET_TESTING_daemon_get (pg, 0);
464 GNUNET_assert (NULL != d1);
466 d2 = GNUNET_TESTING_daemon_get (pg, 1);
467 GNUNET_assert (NULL != d2);
469 GNUNET_TESTING_get_peer_identity (d1->cfg,
471 GNUNET_TESTING_get_peer_identity (d2->cfg,
473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 GNUNET_i2s (&peer1.our_id),
476 GNUNET_i2s (&d1->id));
477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 GNUNET_i2s (&peer2.our_id),
480 GNUNET_i2s (&d2->id));
482 peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg,
486 GNUNET_assert (NULL != peer2_listen_socket);
488 /* Connect to stream library */
489 peer1.socket = GNUNET_STREAM_open (d1->cfg,
490 &d2->id, /* Null for local peer? */
494 GNUNET_assert (NULL != peer1.socket);
499 * Initialize framework and start test
502 run (void *cls, char *const *args, const char *cfgfile,
503 const struct GNUNET_CONFIGURATION_Handle *cfg)
505 struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */
507 /* GNUNET_log_setup ("test_stream_local", */
511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 /* Duplicate the configuration */
514 config = GNUNET_CONFIGURATION_dup (cfg);
516 hosts = GNUNET_TESTING_hosts_load (config);
518 pg = GNUNET_TESTING_peergroup_start (config,
520 GNUNET_TIME_relative_multiply
521 (GNUNET_TIME_UNIT_SECONDS, 3),
526 GNUNET_assert (NULL != pg);
529 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
530 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
537 int main (int argc, char **argv)
541 char *argv2[] = { "test-stream-local",
543 "-c", "test_stream_local.conf",
546 struct GNUNET_GETOPT_CommandLineOption options[] = {
547 GNUNET_GETOPT_OPTION_END
551 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
552 "test-stream-local", "nohelp", options, &run, NULL);
554 if (GNUNET_OK != ret)
556 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
560 if (GNUNET_SYSERR == result)
562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
565 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");