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_testbed_service.h"
32 #include "gnunet_mesh_service.h"
33 #include "gnunet_stream_lib.h"
42 #define TIME_REL_SECS(sec) \
43 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
46 * Structure for holding peer's sockets and IO Handles
51 * The testbed peer handle corresponding to this peer
53 struct GNUNET_TESTBED_Peer *peer;
56 * Peer's stream socket
58 struct GNUNET_STREAM_Socket *socket;
61 * Peer's io write handle
63 struct GNUNET_STREAM_IOWriteHandle *io_write_handle;
66 * Peer's io read handle
68 struct GNUNET_STREAM_IOReadHandle *io_read_handle;
71 * Peer's shutdown handle
73 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
76 * Testbed operation handle specific for this peer
78 struct GNUNET_TESTBED_Operation *op;
83 struct GNUNET_PeerIdentity our_id;
86 * Bytes the peer has written
88 unsigned int bytes_wrote;
91 * Byte the peer has read
93 unsigned int bytes_read;
96 * GNUNET_YES if the peer has successfully completed the current test
101 * The shutdown operation that has to be used by the stream_shutdown_task
103 int shutdown_operation;
108 * Enumeration for various tests that are to be passed in the same order as
114 * Peer1 writing; Peer2 reading
119 * Peer1 write shutdown; Peer2 should get an error when it tries to read;
121 PEER1_WRITE_SHUTDOWN,
124 * Peer1 reads; Peer2 writes (connection is halfclosed)
126 PEER1_HALFCLOSE_READ,
129 * Peer1 attempts to write; Should fail with stream already shutdown error
131 PEER1_HALFCLOSE_WRITE_FAIL,
134 * Peer1 read shutdown; Peer2 should get stream shutdown error during write
139 * All tests successfully finished
146 * Different states in test setup
156 * Get the identity of peer 1
161 * Get the identity of peer 2
166 * Connect to stream service of peer 2
168 PEER2_STREAM_CONNECT,
171 * Connect to stream service of peer 1
179 * Peer1 writes first and then calls for SHUT_WR
180 * Peer2 reads first and then calls for SHUT_RD
181 * Attempt to write again by Peer1 should be rejected
182 * Attempt to read again by Peer2 should be rejected
183 * Peer1 then reads from Peer2 which writes
185 static struct PeerData peer1;
186 static struct PeerData peer2;
189 * Task for aborting the test case if it takes too long
191 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
194 * Task for reading from stream
196 static GNUNET_SCHEDULER_TaskIdentifier read_task;
198 static char *data = "ABCD";
201 * Handle to testbed operation
203 struct GNUNET_TESTBED_Operation *op;
206 * Final testing result
211 * Current running test
213 enum Test current_test;
216 * State is test setup
218 enum SetupState setup_state;
224 * @param cls the closure from GNUNET_STREAM_write/read
225 * @param status the status of the stream at the time this function is called
226 * @param data traffic from the other side
227 * @param size the number of bytes available in data read
228 * @return number of bytes of processed from 'data' (any data remaining should be
229 * given to the next time the read processor is called).
232 input_processor (void *cls,
233 enum GNUNET_STREAM_Status status,
234 const void *input_data,
239 * The transition function; responsible for the transitions among tests
246 * Task for calling STREAM_read
248 * @param cls the peer data entity
249 * @param tc the task context
252 stream_read_task (void *cls,
253 const struct GNUNET_SCHEDULER_TaskContext *tc)
255 struct PeerData *peer = cls;
257 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
258 GNUNET_TIME_relative_multiply
259 (GNUNET_TIME_UNIT_SECONDS, 5),
262 switch (current_test)
264 case PEER1_WRITE_SHUTDOWN:
265 GNUNET_assert (&peer2 == peer);
266 GNUNET_assert (NULL == peer->io_read_handle);
267 transition (); /* to PEER1_HALFCLOSE_READ */
270 GNUNET_assert (NULL != peer->io_read_handle);
276 * The write completion function; called upon writing some data to stream or
279 * @param cls the closure from GNUNET_STREAM_write/read
280 * @param status the status of the stream at the time this function is called
281 * @param size the number of bytes read or written
284 write_completion (void *cls,
285 enum GNUNET_STREAM_Status status,
290 * Task for calling STREAM_write
292 * @param cls the peer data entity
293 * @param tc the task context
296 stream_write_task (void *cls,
297 const struct GNUNET_SCHEDULER_TaskContext *tc)
299 struct PeerData *peer = cls;
301 peer->io_write_handle =
302 GNUNET_STREAM_write (peer->socket,
304 strlen(data) - peer->bytes_wrote,
305 GNUNET_TIME_relative_multiply
306 (GNUNET_TIME_UNIT_SECONDS, 5),
309 switch (current_test)
311 case PEER1_HALFCLOSE_WRITE_FAIL:
312 GNUNET_assert (&peer1 == peer);
313 GNUNET_assert (NULL == peer->io_write_handle);
314 transition(); /* To PEER1_READ_SHUTDOWN */
316 case PEER1_READ_SHUTDOWN:
317 GNUNET_assert (&peer2 == peer);
318 GNUNET_assert (NULL == peer->io_write_handle);
319 transition (); /* To SUCCESS */
322 GNUNET_assert (NULL != peer->io_write_handle);
328 * Close sockets and stop testing deamons nicely
331 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
333 if (NULL != peer2.socket)
334 GNUNET_STREAM_close (peer2.socket);
335 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
336 GNUNET_SCHEDULER_cancel (abort_task);
337 if (NULL != peer2.op)
338 GNUNET_TESTBED_operation_done (peer2.op);
340 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
345 * Completion callback for shutdown
347 * @param cls the closure from GNUNET_STREAM_shutdown call
348 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
352 shutdown_completion (void *cls,
355 switch (current_test)
359 case PEER1_WRITE_SHUTDOWN:
360 GNUNET_assert (cls == &peer1);
361 GNUNET_assert (SHUT_WR == operation);
362 peer1.test_ok = GNUNET_YES;
363 /* Peer2 should read with error */
364 peer2.bytes_read = 0;
365 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
367 case PEER1_READ_SHUTDOWN:
368 peer1.test_ok = GNUNET_YES;
369 peer2.bytes_wrote = 0;
370 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
372 case PEER1_HALFCLOSE_READ:
373 case PEER1_HALFCLOSE_WRITE_FAIL:
375 GNUNET_assert (0); /* We shouldn't reach here */
381 * Task for calling STREAM_shutdown
383 * @param cls the peer entity
384 * @param tc the TaskContext
387 stream_shutdown_task (void *cls,
388 const struct GNUNET_SCHEDULER_TaskContext *tc)
390 struct PeerData *peer = cls;
392 peer->shutdown_handle = GNUNET_STREAM_shutdown (peer->socket,
393 peer->shutdown_operation,
394 &shutdown_completion,
396 GNUNET_assert (NULL != peer->shutdown_handle);
401 * Something went wrong and timed out. Kill everything and set error flag
404 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
409 GNUNET_SCHEDULER_cancel (read_task);
411 result = GNUNET_SYSERR;
418 * The transition function; responsible for the transitions among tests
423 if ((GNUNET_YES == peer1.test_ok) && (GNUNET_YES == peer2.test_ok))
425 peer1.test_ok = GNUNET_NO;
426 peer2.test_ok = GNUNET_NO;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "TEST %d SUCCESSFULL\n", current_test);
429 switch (current_test)
432 current_test = PEER1_WRITE_SHUTDOWN;
433 /* Peer1 should shutdown writing */
434 peer1.shutdown_operation = SHUT_WR;
435 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
437 case PEER1_WRITE_SHUTDOWN:
438 current_test = PEER1_HALFCLOSE_READ;
439 /* Peer2 should be able to write successfully */
440 peer2.bytes_wrote = 0;
441 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
443 /* Peer1 should be able to read successfully */
444 peer1.bytes_read = 0;
445 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
447 case PEER1_HALFCLOSE_READ:
448 current_test = PEER1_HALFCLOSE_WRITE_FAIL;
449 peer1.bytes_wrote = 0;
450 peer2.bytes_read = 0;
451 peer2.test_ok = GNUNET_YES;
452 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer1);
454 case PEER1_HALFCLOSE_WRITE_FAIL:
455 current_test = PEER1_READ_SHUTDOWN;
456 peer1.shutdown_operation = SHUT_RD;
457 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
459 case PEER1_READ_SHUTDOWN:
460 current_test = SUCCESS;
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "All tests successful\n");
463 GNUNET_SCHEDULER_add_now (&do_close, NULL);
466 GNUNET_assert (0); /* We shouldn't reach here */
473 * The write completion function; called upon writing some data to stream or
476 * @param cls the closure from GNUNET_STREAM_write/read
477 * @param status the status of the stream at the time this function is called
478 * @param size the number of bytes read or written
481 write_completion (void *cls,
482 enum GNUNET_STREAM_Status status,
485 struct PeerData *peer = cls;
487 switch (current_test)
490 case PEER1_HALFCLOSE_READ:
492 GNUNET_assert (GNUNET_STREAM_OK == status);
493 GNUNET_assert (size <= strlen (data));
494 peer->bytes_wrote += size;
496 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
498 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Writing completed\n");
507 peer1.test_ok = GNUNET_YES;
508 transition (); /* to PEER1_WRITE_SHUTDOWN */
510 else /* This will happen during PEER1_HALFCLOSE_READ */
512 peer2.test_ok = GNUNET_YES;
513 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
517 case PEER1_HALFCLOSE_WRITE_FAIL:
518 GNUNET_assert (peer == &peer1);
519 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
520 GNUNET_assert (0 == size);
521 peer1.test_ok = GNUNET_YES;
523 case PEER1_READ_SHUTDOWN:
524 GNUNET_assert (peer == &peer2);
525 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
526 GNUNET_assert (0 == size);
527 peer2.test_ok = GNUNET_YES;
529 case PEER1_WRITE_SHUTDOWN:
531 GNUNET_assert (0); /* We shouldn't reach here */
537 * Function executed after stream has been established
539 * @param cls the closure from GNUNET_STREAM_open
540 * @param socket socket to use to communicate with the other side (read/write)
543 stream_open_cb (void *cls,
544 struct GNUNET_STREAM_Socket *socket)
546 struct PeerData *peer;
548 GNUNET_assert (socket == peer1.socket);
549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
550 "%s: Stream established from peer1\n",
551 GNUNET_i2s (&peer1.our_id));
552 peer = (struct PeerData *) cls;
553 peer->bytes_wrote = 0;
554 GNUNET_assert (socket == peer1.socket);
555 GNUNET_assert (socket == peer->socket);
556 peer1.test_ok = GNUNET_NO;
557 peer2.test_ok = GNUNET_NO;
558 current_test = PEER1_WRITE;
559 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
566 * @param cls the closure from GNUNET_STREAM_write/read
567 * @param status the status of the stream at the time this function is called
568 * @param data traffic from the other side
569 * @param size the number of bytes available in data read
570 * @return number of bytes of processed from 'data' (any data remaining should be
571 * given to the next time the read processor is called).
574 input_processor (void *cls,
575 enum GNUNET_STREAM_Status status,
576 const void *input_data,
579 struct PeerData *peer;
581 peer = (struct PeerData *) cls;
583 switch (current_test)
586 case PEER1_HALFCLOSE_READ:
587 if (GNUNET_STREAM_TIMEOUT == status)
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Read operation timedout - reading again!\n");
591 GNUNET_assert (0 == size);
592 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
596 GNUNET_assert (GNUNET_STREAM_OK == status);
597 GNUNET_assert (size <= strlen (data));
598 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
599 (const char *) input_data,
601 peer->bytes_read += size;
603 if (peer->bytes_read < strlen (data))
605 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
609 if (&peer2 == peer) /* Peer2 has completed reading; should write */
611 peer2.test_ok = GNUNET_YES;
612 transition (); /* Transition to PEER1_WRITE_SHUTDOWN */
614 else /* Peer1 has completed reading. End of tests */
616 peer1.test_ok = GNUNET_YES;
617 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
621 case PEER1_WRITE_SHUTDOWN:
622 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
623 peer2.test_ok = GNUNET_YES;
625 case PEER1_HALFCLOSE_WRITE_FAIL:
626 case PEER1_READ_SHUTDOWN:
628 GNUNET_assert (0); /* We shouldn't reach here */
636 * Scheduler call back; to be executed when a new stream is connected
637 * Called from listen connect for peer2
640 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
642 read_task = GNUNET_SCHEDULER_NO_TASK;
643 GNUNET_assert (NULL != cls);
644 peer2.bytes_read = 0;
645 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
650 * Functions of this type are called upon new stream connection from other peers
652 * @param cls the closure from GNUNET_STREAM_listen
653 * @param socket the socket representing the stream
654 * @param initiator the identity of the peer who wants to establish a stream
656 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
657 * stream (the socket will be invalid after the call)
660 stream_listen_cb (void *cls,
661 struct GNUNET_STREAM_Socket *socket,
662 const struct GNUNET_PeerIdentity *initiator)
664 GNUNET_assert (NULL != socket);
665 GNUNET_assert (NULL != initiator);
666 GNUNET_assert (socket != peer1.socket);
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
669 "%s: Peer connected: %s\n",
670 GNUNET_i2s (&peer2.our_id),
671 GNUNET_i2s(initiator));
673 peer2.socket = socket;
674 /* FIXME: reading should be done right now instead of a scheduled call */
675 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
681 * Listen success callback; connects a peer to stream as client
684 stream_connect (void);
688 * Adapter function called to destroy a connection to
692 * @param op_result service handle returned from the connect adapter
695 stream_da (void *cls, void *op_result)
697 struct GNUNET_STREAM_ListenSocket *lsocket;
702 GNUNET_STREAM_listen_close (lsocket);
703 if (NULL != peer1.op)
704 GNUNET_TESTBED_operation_done (peer1.op);
706 GNUNET_SCHEDULER_shutdown ();
711 GNUNET_assert (op_result == peer1.socket);
712 GNUNET_STREAM_close (peer1.socket);
713 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
721 * Adapter function called to establish a connection to
725 * @param cfg configuration of the peer to connect to; will be available until
726 * GNUNET_TESTBED_operation_done() is called on the operation returned
727 * from GNUNET_TESTBED_service_connect()
728 * @return service handle to return in 'op_result', NULL on error
731 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
733 struct GNUNET_STREAM_ListenSocket *lsocket;
737 case PEER2_STREAM_CONNECT:
738 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
739 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
740 &stream_connect, GNUNET_STREAM_OPTION_END);
741 GNUNET_assert (NULL != lsocket);
743 case PEER1_STREAM_CONNECT:
744 peer1.socket = GNUNET_STREAM_open (cfg, &peer2.our_id, 10, &stream_open_cb,
745 &peer1, GNUNET_STREAM_OPTION_END);
746 GNUNET_assert (NULL != peer1.socket);
755 * Listen success callback; connects a peer to stream as client
758 stream_connect (void)
760 GNUNET_assert (PEER2_STREAM_CONNECT == setup_state);
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
762 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
764 stream_ca, stream_da, &peer1);
765 setup_state = PEER1_STREAM_CONNECT;
770 * Callback to be called when the requested peer information is available
772 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
773 * @param op the operation this callback corresponds to
774 * @param pinfo the result; will be NULL if the operation has failed
775 * @param emsg error message if the operation has failed; will be NULL if the
776 * operation is successfull
779 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
780 const struct GNUNET_TESTBED_PeerInformation *pinfo,
783 GNUNET_assert (NULL == emsg);
784 GNUNET_assert (op == op_);
787 case PEER1_GET_IDENTITY:
788 memcpy (&peer1.our_id, pinfo->result.id,
789 sizeof (struct GNUNET_PeerIdentity));
790 GNUNET_TESTBED_operation_done (op);
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
793 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
794 GNUNET_TESTBED_PIT_IDENTITY,
796 setup_state = PEER2_GET_IDENTITY;
798 case PEER2_GET_IDENTITY:
799 memcpy (&peer2.our_id, pinfo->result.id,
800 sizeof (struct GNUNET_PeerIdentity));
801 GNUNET_TESTBED_operation_done (op);
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
804 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
806 stream_ca, stream_da, &peer2);
807 setup_state = PEER2_STREAM_CONNECT;
816 * Controller event callback
819 * @param event the controller event
822 controller_event_cb (void *cls,
823 const struct GNUNET_TESTBED_EventInformation *event)
827 case GNUNET_TESTBED_ET_CONNECT:
828 GNUNET_assert (INIT == setup_state);
829 GNUNET_TESTBED_operation_done (op);
830 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
831 GNUNET_TESTBED_PIT_IDENTITY,
833 setup_state = PEER1_GET_IDENTITY;
835 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
838 case PEER1_STREAM_CONNECT:
839 case PEER2_STREAM_CONNECT:
840 GNUNET_assert (NULL == event->details.operation_finished.emsg);
853 * Signature of a main function for a testcase.
856 * @param num_peers number of peers in 'peers'
857 * @param peers handle to peers run in the testbed
860 test_master (void *cls, unsigned int num_peers,
861 struct GNUNET_TESTBED_Peer **peers)
863 GNUNET_assert (NULL != peers);
864 GNUNET_assert (NULL != peers[0]);
865 GNUNET_assert (NULL != peers[1]);
866 peer1.peer = peers[0];
867 peer2.peer = peers[1];
868 op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL, peer2.peer, peer1.peer);
871 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
872 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
880 int main (int argc, char **argv)
886 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
887 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
888 GNUNET_TESTBED_test_run ("test_stream_2peers_halfclose",
889 "test_stream_local.conf", NUM_PEERS, event_mask,
890 &controller_event_cb, NULL, &test_master, NULL);
891 if (GNUNET_SYSERR == result)