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;
105 static char *data = "ABCD";
108 static int writing_success;
109 static int reading_success;
115 * @param cls the closure from GNUNET_STREAM_write/read
116 * @param status the status of the stream at the time this function is called
117 * @param data traffic from the other side
118 * @param size the number of bytes available in data read
119 * @return number of bytes of processed from 'data' (any data remaining should be
120 * given to the next time the read processor is called).
123 input_processor (void *cls,
124 enum GNUNET_STREAM_Status status,
125 const void *input_data,
129 * Task for calling STREAM_read
131 * @param cls the peer data entity
132 * @param tc the task context
135 stream_read_task (void *cls,
136 const struct GNUNET_SCHEDULER_TaskContext *tc)
138 struct PeerData *peer = cls;
140 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
141 GNUNET_TIME_relative_multiply
142 (GNUNET_TIME_UNIT_SECONDS, 5),
145 GNUNET_assert (NULL != peer->io_read_handle);
149 * The write completion function; called upon writing some data to stream or
152 * @param cls the closure from GNUNET_STREAM_write/read
153 * @param status the status of the stream at the time this function is called
154 * @param size the number of bytes read or written
157 write_completion (void *cls,
158 enum GNUNET_STREAM_Status status,
163 * Task for calling STREAM_write
165 * @param cls the peer data entity
166 * @param tc the task context
169 stream_write_task (void *cls,
170 const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct PeerData *peer = cls;
174 peer->io_write_handle =
175 GNUNET_STREAM_write (peer->socket,
177 strlen(data) - peer->bytes_wrote,
178 GNUNET_TIME_relative_multiply
179 (GNUNET_TIME_UNIT_SECONDS, 5),
183 GNUNET_assert (NULL != peer->io_write_handle);
188 * Check whether peers successfully shut down.
191 peergroup_shutdown_callback (void *cls, const char *emsg)
195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
196 "Shutdown of peers failed!\n");
200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
201 "All peers successfully shut down!\n");
203 GNUNET_CONFIGURATION_destroy (config);
208 * Close sockets and stop testing deamons nicely
211 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
213 if (NULL != peer1.socket)
214 GNUNET_STREAM_close (peer1.socket);
215 if (NULL != peer2.socket)
216 GNUNET_STREAM_close (peer2.socket);
217 if (NULL != peer2_listen_socket)
218 GNUNET_STREAM_listen_close (peer2_listen_socket);
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
223 GNUNET_SCHEDULER_cancel (abort_task);
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
228 GNUNET_TESTING_daemons_stop (pg,
229 GNUNET_TIME_relative_multiply
230 (GNUNET_TIME_UNIT_SECONDS, 5),
231 &peergroup_shutdown_callback,
237 * Completion callback for shutdown
239 * @param cls the closure from GNUNET_STREAM_shutdown call
240 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
244 shutdown_completion (void *cls,
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "STREAM shutdown successful\n");
249 GNUNET_SCHEDULER_add_now (&do_close,
256 * Shutdown sockets gracefully
259 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
261 peer1.shutdown_handle = GNUNET_STREAM_shutdown (peer1.socket,
263 &shutdown_completion,
269 * Something went wrong and timed out. Kill everything and set error flag
272 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
275 result = GNUNET_SYSERR;
282 * The write completion function; called upon writing some data to stream or
285 * @param cls the closure from GNUNET_STREAM_write/read
286 * @param status the status of the stream at the time this function is called
287 * @param size the number of bytes read or written
290 write_completion (void *cls,
291 enum GNUNET_STREAM_Status status,
294 struct PeerData *peer=cls;
296 GNUNET_assert (GNUNET_STREAM_OK == status);
297 GNUNET_assert (size <= strlen (data));
298 peer->bytes_wrote += size;
300 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
302 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Writing completed\n");
309 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
311 peer->bytes_read = 0;
312 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
316 writing_success = GNUNET_YES;
317 if (GNUNET_YES == reading_success)
318 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
325 * Function executed after stream has been established
327 * @param cls the closure from GNUNET_STREAM_open
328 * @param socket socket to use to communicate with the other side (read/write)
331 stream_open_cb (void *cls,
332 struct GNUNET_STREAM_Socket *socket)
334 struct PeerData *peer=cls;
336 GNUNET_assert (&peer1 == peer);
337 GNUNET_assert (socket == peer1.socket);
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "%s: Stream established from peer1\n",
340 GNUNET_i2s (&peer1.our_id));
341 peer->bytes_wrote = 0;
342 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
349 * @param cls the closure from GNUNET_STREAM_write/read
350 * @param status the status of the stream at the time this function is called
351 * @param data traffic from the other side
352 * @param size the number of bytes available in data read
353 * @return number of bytes of processed from 'data' (any data remaining should be
354 * given to the next time the read processor is called).
357 input_processor (void *cls,
358 enum GNUNET_STREAM_Status status,
359 const void *input_data,
362 struct PeerData *peer;
364 peer = (struct PeerData *) cls;
366 if (GNUNET_STREAM_TIMEOUT == status)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Read operation timedout - reading again!\n");
370 GNUNET_assert (0 == size);
371 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
375 GNUNET_assert (GNUNET_STREAM_OK == status);
376 GNUNET_assert (size <= strlen (data));
377 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
378 (const char *) input_data,
380 peer->bytes_read += size;
382 if (peer->bytes_read < strlen (data))
384 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
388 if (&peer2 == peer) /* Peer2 has completed reading; should write */
390 peer->bytes_wrote = 0;
391 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
393 else /* Peer1 has completed reading. End of tests */
395 reading_success = GNUNET_YES;
396 if (GNUNET_YES == writing_success)
397 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
405 * Functions of this type are called upon new stream connection from other peers
407 * @param cls the closure from GNUNET_STREAM_listen
408 * @param socket the socket representing the stream
409 * @param initiator the identity of the peer who wants to establish a stream
411 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
412 * stream (the socket will be invalid after the call)
415 stream_listen_cb (void *cls,
416 struct GNUNET_STREAM_Socket *socket,
417 const struct GNUNET_PeerIdentity *initiator)
419 GNUNET_assert (NULL != socket);
420 GNUNET_assert (NULL != initiator);
421 GNUNET_assert (socket != peer1.socket);
423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
424 "%s: Peer connected: %s\n",
425 GNUNET_i2s (&peer2.our_id),
426 GNUNET_i2s(initiator));
428 peer2.socket = socket;
429 peer2.bytes_read = 0;
430 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
436 * Callback to be called when testing peer group is ready
439 * @param emsg NULL on success
442 peergroup_ready (void *cls, const char *emsg)
446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
447 "Starting peer group failed: %s\n", emsg);
450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
451 "Peer group is now ready\n");
453 GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg));
455 d1 = GNUNET_TESTING_daemon_get (pg, 0);
456 GNUNET_assert (NULL != d1);
458 d2 = GNUNET_TESTING_daemon_get (pg, 1);
459 GNUNET_assert (NULL != d2);
461 GNUNET_TESTING_get_peer_identity (d1->cfg,
463 GNUNET_TESTING_get_peer_identity (d2->cfg,
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
467 GNUNET_i2s (&peer1.our_id),
468 GNUNET_i2s (&d1->id));
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 GNUNET_i2s (&peer2.our_id),
472 GNUNET_i2s (&d2->id));
474 peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg,
478 GNUNET_assert (NULL != peer2_listen_socket);
480 /* Connect to stream library */
481 peer1.socket = GNUNET_STREAM_open (d1->cfg,
482 &d2->id, /* Null for local peer? */
486 GNUNET_STREAM_OPTION_END);
487 GNUNET_assert (NULL != peer1.socket);
492 * Initialize framework and start test
495 run (void *cls, char *const *args, const char *cfgfile,
496 const struct GNUNET_CONFIGURATION_Handle *cfg)
498 struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */
500 GNUNET_log_setup ("test_stream_2peers",
504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 /* Duplicate the configuration */
507 config = GNUNET_CONFIGURATION_dup (cfg);
509 hosts = GNUNET_TESTING_hosts_load (config);
511 pg = GNUNET_TESTING_peergroup_start (config,
513 GNUNET_TIME_relative_multiply
514 (GNUNET_TIME_UNIT_SECONDS, 3),
519 GNUNET_assert (NULL != pg);
522 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
523 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
530 int main (int argc, char **argv)
534 char *argv2[] = { "test-stream-2peers",
536 "-c", "test_stream_local.conf",
539 struct GNUNET_GETOPT_CommandLineOption options[] = {
540 GNUNET_GETOPT_OPTION_END
544 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
545 "test-stream-2peers", "nohelp", options, &run, NULL);
547 if (GNUNET_OK != ret)
549 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
553 if (GNUNET_SYSERR == result)
555 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
558 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");