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_halfclose.c
23 * @brief Testcases for Stream API halfclosed connections between 2 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"
34 #include "gnunet_scheduler_lib.h"
44 * Structure for holding peer's sockets and IO Handles
49 * Peer's stream socket
51 struct GNUNET_STREAM_Socket *socket;
54 * Peer's io write handle
56 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
59 * Peer's io read handle
61 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
64 * Peer's shutdown handle
66 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
71 struct GNUNET_PeerIdentity our_id;
74 * Bytes the peer has written
76 unsigned int bytes_wrote;
79 * Byte the peer has read
81 unsigned int bytes_read;
84 * GNUNET_YES if the peer has successfully completed the current test
89 * The shutdown operation that has to be used by the stream_shutdown_task
91 int shutdown_operation;
95 * The current peer group
97 static struct GNUNET_TESTING_PeerGroup *pg;
102 static struct GNUNET_TESTING_Daemon *d1;
107 static struct GNUNET_TESTING_Daemon *d2;
111 * Peer1 writes first and then calls for SHUT_WR
112 * Peer2 reads first and then calls for SHUT_RD
113 * Attempt to write again by Peer1 should be rejected
114 * Attempt to read again by Peer2 should be rejected
115 * Peer1 then reads from Peer2 which writes
117 static struct PeerData peer1;
118 static struct PeerData peer2;
119 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
120 static struct GNUNET_CONFIGURATION_Handle *config;
122 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
123 static GNUNET_SCHEDULER_TaskIdentifier read_task;
125 static char *data = "ABCD";
129 * Enumeration for various tests that are to be passed in the same order as
135 * Peer1 writing; Peer2 reading
140 * Peer1 write shutdown; Peer2 should get an error when it tries to read;
142 PEER1_WRITE_SHUTDOWN,
145 * Peer1 reads; Peer2 writes (connection is halfclosed)
147 PEER1_HALFCLOSE_READ,
150 * Peer1 attempts to write; Should fail with stream already shutdown error
152 PEER1_HALFCLOSE_WRITE_FAIL,
155 * Peer1 read shutdown; Peer2 should get stream shutdown error during write
160 * All tests successfully finished
166 * Current running test
168 enum Test current_test;
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 transition function; responsible for the transitions among tests
195 * Task for calling STREAM_read
197 * @param cls the peer data entity
198 * @param tc the task context
201 stream_read_task (void *cls,
202 const struct GNUNET_SCHEDULER_TaskContext *tc)
204 struct PeerData *peer = cls;
206 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
207 GNUNET_TIME_relative_multiply
208 (GNUNET_TIME_UNIT_SECONDS, 5),
211 switch (current_test)
213 case PEER1_WRITE_SHUTDOWN:
214 GNUNET_assert (&peer2 == peer);
215 GNUNET_assert (NULL == peer->io_read_handle);
216 transition (); /* to PEER1_HALFCLOSE_READ */
219 GNUNET_assert (NULL != peer->io_read_handle);
225 * The write completion function; called upon writing some data to stream or
228 * @param cls the closure from GNUNET_STREAM_write/read
229 * @param status the status of the stream at the time this function is called
230 * @param size the number of bytes read or written
233 write_completion (void *cls,
234 enum GNUNET_STREAM_Status status,
239 * Task for calling STREAM_write
241 * @param cls the peer data entity
242 * @param tc the task context
245 stream_write_task (void *cls,
246 const struct GNUNET_SCHEDULER_TaskContext *tc)
248 struct PeerData *peer = cls;
250 peer->io_write_handle =
251 GNUNET_STREAM_write (peer->socket,
253 strlen(data) - peer->bytes_wrote,
254 GNUNET_TIME_relative_multiply
255 (GNUNET_TIME_UNIT_SECONDS, 5),
258 switch (current_test)
260 case PEER1_HALFCLOSE_WRITE_FAIL:
261 GNUNET_assert (&peer1 == peer);
262 GNUNET_assert (NULL == peer->io_write_handle);
263 transition(); /* To PEER1_READ_SHUTDOWN */
265 case PEER1_READ_SHUTDOWN:
266 GNUNET_assert (&peer2 == peer);
267 GNUNET_assert (NULL == peer->io_write_handle);
268 transition (); /* To SUCCESS */
271 GNUNET_assert (NULL != peer->io_write_handle);
277 * Check whether peers successfully shut down.
280 peergroup_shutdown_callback (void *cls, const char *emsg)
284 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
285 "Shutdown of peers failed!\n");
289 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
290 "All peers successfully shut down!\n");
292 GNUNET_CONFIGURATION_destroy (config);
297 * Close sockets and stop testing deamons nicely
300 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
302 if (NULL != peer1.socket)
303 GNUNET_STREAM_close (peer1.socket);
304 if (NULL != peer2.socket)
305 GNUNET_STREAM_close (peer2.socket);
306 if (NULL != peer2_listen_socket)
307 GNUNET_STREAM_listen_close (peer2_listen_socket);
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
312 GNUNET_SCHEDULER_cancel (abort_task);
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
317 GNUNET_TESTING_daemons_stop (pg,
318 GNUNET_TIME_relative_multiply
319 (GNUNET_TIME_UNIT_SECONDS, 5),
320 &peergroup_shutdown_callback,
326 * Completion callback for shutdown
328 * @param cls the closure from GNUNET_STREAM_shutdown call
329 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
333 shutdown_completion (void *cls,
336 switch (current_test)
340 case PEER1_WRITE_SHUTDOWN:
341 peer1.test_ok = GNUNET_YES;
342 /* Peer2 should read with error */
343 peer2.bytes_read = 0;
344 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
346 case PEER1_READ_SHUTDOWN:
347 peer1.test_ok = GNUNET_YES;
348 peer2.bytes_wrote = 0;
349 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
351 case PEER1_HALFCLOSE_READ:
352 case PEER1_HALFCLOSE_WRITE_FAIL:
354 GNUNET_assert (0); /* We shouldn't reach here */
360 * Task for calling STREAM_shutdown
362 * @param cls the peer entity
363 * @param tc the TaskContext
366 stream_shutdown_task (void *cls,
367 const struct GNUNET_SCHEDULER_TaskContext *tc)
369 struct PeerData *peer = cls;
371 peer->shutdown_handle = GNUNET_STREAM_shutdown (peer->socket,
372 peer->shutdown_operation,
373 &shutdown_completion,
375 GNUNET_assert (NULL != peer->shutdown_handle);
380 * Something went wrong and timed out. Kill everything and set error flag
383 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
388 GNUNET_SCHEDULER_cancel (read_task);
390 result = GNUNET_SYSERR;
397 * The transition function; responsible for the transitions among tests
402 if ((GNUNET_YES == peer1.test_ok) && (GNUNET_YES == peer2.test_ok))
404 peer1.test_ok = GNUNET_NO;
405 peer2.test_ok = GNUNET_NO;
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "TEST %d SUCCESSFULL\n", current_test);
408 switch (current_test)
411 current_test = PEER1_WRITE_SHUTDOWN;
412 /* Peer1 should shutdown writing */
413 peer1.shutdown_operation = SHUT_WR;
414 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
416 case PEER1_WRITE_SHUTDOWN:
417 current_test = PEER1_HALFCLOSE_READ;
418 /* Peer2 should be able to write successfully */
419 peer2.bytes_wrote = 0;
420 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
422 /* Peer1 should be able to read successfully */
423 peer1.bytes_read = 0;
424 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
426 case PEER1_HALFCLOSE_READ:
427 current_test = PEER1_HALFCLOSE_WRITE_FAIL;
428 peer1.bytes_wrote = 0;
429 peer2.bytes_read = 0;
430 peer2.test_ok = GNUNET_YES;
431 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer1);
433 case PEER1_HALFCLOSE_WRITE_FAIL:
434 current_test = PEER1_READ_SHUTDOWN;
435 peer1.shutdown_operation = SHUT_RD;
436 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
438 case PEER1_READ_SHUTDOWN:
439 current_test = SUCCESS;
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "All tests successful\n");
442 GNUNET_SCHEDULER_add_now (&do_close, NULL);
445 GNUNET_assert (0); /* We shouldn't reach here */
452 * The write completion function; called upon writing some data to stream or
455 * @param cls the closure from GNUNET_STREAM_write/read
456 * @param status the status of the stream at the time this function is called
457 * @param size the number of bytes read or written
460 write_completion (void *cls,
461 enum GNUNET_STREAM_Status status,
464 struct PeerData *peer = cls;
466 switch (current_test)
469 case PEER1_HALFCLOSE_READ:
471 GNUNET_assert (GNUNET_STREAM_OK == status);
472 GNUNET_assert (size <= strlen (data));
473 peer->bytes_wrote += size;
475 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
477 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482 "Writing completed\n");
486 peer1.test_ok = GNUNET_YES;
487 transition (); /* to PEER1_WRITE_SHUTDOWN */
489 else /* This will happen during PEER1_HALFCLOSE_READ */
491 peer2.test_ok = GNUNET_YES;
492 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
496 case PEER1_HALFCLOSE_WRITE_FAIL:
497 GNUNET_assert (peer == &peer1);
498 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
499 GNUNET_assert (0 == size);
500 peer1.test_ok = GNUNET_YES;
502 case PEER1_READ_SHUTDOWN:
503 GNUNET_assert (peer == &peer2);
504 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
505 GNUNET_assert (0 == size);
506 peer2.test_ok = GNUNET_YES;
508 case PEER1_WRITE_SHUTDOWN:
510 GNUNET_assert (0); /* We shouldn't reach here */
516 * Function executed after stream has been established
518 * @param cls the closure from GNUNET_STREAM_open
519 * @param socket socket to use to communicate with the other side (read/write)
522 stream_open_cb (void *cls,
523 struct GNUNET_STREAM_Socket *socket)
525 struct PeerData *peer;
527 GNUNET_assert (socket == peer1.socket);
528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
529 "%s: Stream established from peer1\n",
530 GNUNET_i2s (&peer1.our_id));
531 peer = (struct PeerData *) cls;
532 peer->bytes_wrote = 0;
533 GNUNET_assert (socket == peer1.socket);
534 GNUNET_assert (socket == peer->socket);
535 peer1.test_ok = GNUNET_NO;
536 peer2.test_ok = GNUNET_NO;
537 current_test = PEER1_WRITE;
538 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
545 * @param cls the closure from GNUNET_STREAM_write/read
546 * @param status the status of the stream at the time this function is called
547 * @param data traffic from the other side
548 * @param size the number of bytes available in data read
549 * @return number of bytes of processed from 'data' (any data remaining should be
550 * given to the next time the read processor is called).
553 input_processor (void *cls,
554 enum GNUNET_STREAM_Status status,
555 const void *input_data,
558 struct PeerData *peer;
560 peer = (struct PeerData *) cls;
562 switch (current_test)
565 case PEER1_HALFCLOSE_READ:
566 if (GNUNET_STREAM_TIMEOUT == status)
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Read operation timedout - reading again!\n");
570 GNUNET_assert (0 == size);
571 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
575 GNUNET_assert (GNUNET_STREAM_OK == status);
576 GNUNET_assert (size <= strlen (data));
577 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
578 (const char *) input_data,
580 peer->bytes_read += size;
582 if (peer->bytes_read < strlen (data))
584 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
588 if (&peer2 == peer) /* Peer2 has completed reading; should write */
590 peer2.test_ok = GNUNET_YES;
591 transition (); /* Transition to PEER1_WRITE_SHUTDOWN */
593 else /* Peer1 has completed reading. End of tests */
595 peer1.test_ok = GNUNET_YES;
596 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
600 case PEER1_WRITE_SHUTDOWN:
601 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
602 peer2.test_ok = GNUNET_YES;
604 case PEER1_HALFCLOSE_WRITE_FAIL:
605 case PEER1_READ_SHUTDOWN:
607 GNUNET_assert (0); /* We shouldn't reach here */
615 * Scheduler call back; to be executed when a new stream is connected
616 * Called from listen connect for peer2
619 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
621 read_task = GNUNET_SCHEDULER_NO_TASK;
622 GNUNET_assert (NULL != cls);
623 peer2.bytes_read = 0;
624 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
629 * Functions of this type are called upon new stream connection from other peers
631 * @param cls the closure from GNUNET_STREAM_listen
632 * @param socket the socket representing the stream
633 * @param initiator the identity of the peer who wants to establish a stream
635 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
636 * stream (the socket will be invalid after the call)
639 stream_listen_cb (void *cls,
640 struct GNUNET_STREAM_Socket *socket,
641 const struct GNUNET_PeerIdentity *initiator)
643 GNUNET_assert (NULL != socket);
644 GNUNET_assert (NULL != initiator);
645 GNUNET_assert (socket != peer1.socket);
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648 "%s: Peer connected: %s\n",
649 GNUNET_i2s (&peer2.our_id),
650 GNUNET_i2s(initiator));
652 peer2.socket = socket;
653 /* FIXME: reading should be done right now instead of a scheduled call */
654 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
660 * Callback to be called when testing peer group is ready
663 * @param emsg NULL on success
666 peergroup_ready (void *cls, const char *emsg)
670 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
671 "Starting peer group failed: %s\n", emsg);
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Peer group is now ready\n");
677 GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg));
679 d1 = GNUNET_TESTING_daemon_get (pg, 0);
680 GNUNET_assert (NULL != d1);
682 d2 = GNUNET_TESTING_daemon_get (pg, 1);
683 GNUNET_assert (NULL != d2);
685 GNUNET_TESTING_get_peer_identity (d1->cfg,
687 GNUNET_TESTING_get_peer_identity (d2->cfg,
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
691 GNUNET_i2s (&peer1.our_id),
692 GNUNET_i2s (&d1->id));
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
695 GNUNET_i2s (&peer2.our_id),
696 GNUNET_i2s (&d2->id));
698 peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg,
702 GNUNET_assert (NULL != peer2_listen_socket);
704 /* Connect to stream library */
705 peer1.socket = GNUNET_STREAM_open (d1->cfg,
706 &d2->id, /* Null for local peer? */
710 GNUNET_assert (NULL != peer1.socket);
715 * Initialize framework and start test
718 run (void *cls, char *const *args, const char *cfgfile,
719 const struct GNUNET_CONFIGURATION_Handle *cfg)
721 struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */
723 /* GNUNET_log_setup ("test_stream_local", */
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729 /* Duplicate the configuration */
730 config = GNUNET_CONFIGURATION_dup (cfg);
732 hosts = GNUNET_TESTING_hosts_load (config);
734 pg = GNUNET_TESTING_peergroup_start (config,
736 GNUNET_TIME_relative_multiply
737 (GNUNET_TIME_UNIT_SECONDS, 3),
742 GNUNET_assert (NULL != pg);
745 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
746 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
753 int main (int argc, char **argv)
757 char *argv2[] = { "test-stream-local",
759 "-c", "test_stream_local.conf",
762 struct GNUNET_GETOPT_CommandLineOption options[] = {
763 GNUNET_GETOPT_OPTION_END
767 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
768 "test-stream-local", "nohelp", options, &run, NULL);
770 if (GNUNET_OK != ret)
772 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
776 if (GNUNET_SYSERR == result)
778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
781 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");