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
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_mesh_service.h"
30 #include "gnunet_stream_lib.h"
37 #define TIME_REL_SECS(sec) \
38 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
41 * Structure for holding peer's sockets and IO Handles
46 * The testbed peer handle corresponding to this peer
48 struct GNUNET_TESTBED_Peer *peer;
51 * Peer's stream socket
53 struct GNUNET_STREAM_Socket *socket;
56 * Peer's io write handle
58 struct GNUNET_STREAM_WriteHandle *io_write_handle;
61 * Peer's io read handle
63 struct GNUNET_STREAM_ReadHandle *io_read_handle;
66 * Peer's shutdown handle
68 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
71 * Testbed operation handle specific for this peer
73 struct GNUNET_TESTBED_Operation *op;
78 struct GNUNET_PeerIdentity our_id;
81 * Bytes the peer has written
83 unsigned int bytes_wrote;
86 * Byte the peer has read
88 unsigned int bytes_read;
91 * GNUNET_YES if the peer has successfully completed the current test
96 * The shutdown operation that has to be used by the stream_shutdown_task
98 int shutdown_operation;
103 * Enumeration for various tests that are to be passed in the same order as
109 * Peer1 writing; Peer2 reading
114 * Peer1 write shutdown; Peer2 should get an error when it tries to read;
116 PEER1_WRITE_SHUTDOWN,
119 * Peer1 reads; Peer2 writes (connection is halfclosed)
121 PEER1_HALFCLOSE_READ,
124 * Peer1 attempts to write; Should fail with stream already shutdown error
126 PEER1_HALFCLOSE_WRITE_FAIL,
129 * Peer1 read shutdown; Peer2 should get stream shutdown error during write
134 * All tests successfully finished
141 * Different states in test setup
146 * Get the identity of peer 1
151 * Get the identity of peer 2
156 * Connect to stream service of peer 2
158 PEER2_STREAM_CONNECT,
161 * Connect to stream service of peer 1
169 * Peer1 writes first and then calls for SHUT_WR
170 * Peer2 reads first and then calls for SHUT_RD
171 * Attempt to write again by Peer1 should be rejected
172 * Attempt to read again by Peer2 should be rejected
173 * Peer1 then reads from Peer2 which writes
175 static struct PeerData peer1;
176 static struct PeerData peer2;
179 * Task for aborting the test case if it takes too long
181 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
184 * Task for reading from stream
186 static GNUNET_SCHEDULER_TaskIdentifier read_task;
188 static char *data = "ABCD";
191 * Handle to testbed operation
193 struct GNUNET_TESTBED_Operation *op;
196 * Final testing result
201 * Current running test
203 enum Test current_test;
206 * State is test setup
208 enum SetupState setup_state;
214 * @param cls the closure from GNUNET_STREAM_write/read
215 * @param status the status of the stream at the time this function is called
216 * @param data traffic from the other side
217 * @param size the number of bytes available in data read
218 * @return number of bytes of processed from 'data' (any data remaining should be
219 * given to the next time the read processor is called).
222 input_processor (void *cls,
223 enum GNUNET_STREAM_Status status,
224 const void *input_data,
229 * The transition function; responsible for the transitions among tests
236 * Task for calling STREAM_read
238 * @param cls the peer data entity
239 * @param tc the task context
242 stream_read_task (void *cls,
243 const struct GNUNET_SCHEDULER_TaskContext *tc)
245 struct PeerData *peer = cls;
247 peer->io_read_handle = GNUNET_STREAM_read (peer->socket,
248 GNUNET_TIME_relative_multiply
249 (GNUNET_TIME_UNIT_SECONDS, 5),
252 switch (current_test)
254 case PEER1_WRITE_SHUTDOWN:
255 GNUNET_assert (&peer2 == peer);
256 GNUNET_assert (NULL == peer->io_read_handle);
257 peer2.test_ok = GNUNET_YES;
258 transition (); /* to PEER1_HALFCLOSE_READ */
261 GNUNET_assert (NULL != peer->io_read_handle);
267 * The write completion function; called upon writing some data to stream or
270 * @param cls the closure from GNUNET_STREAM_write/read
271 * @param status the status of the stream at the time this function is called
272 * @param size the number of bytes read or written
275 write_completion (void *cls,
276 enum GNUNET_STREAM_Status status,
281 * Task for calling STREAM_write
283 * @param cls the peer data entity
284 * @param tc the task context
287 stream_write_task (void *cls,
288 const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct PeerData *peer = cls;
292 peer->io_write_handle =
293 GNUNET_STREAM_write (peer->socket,
295 strlen(data) - peer->bytes_wrote,
296 GNUNET_TIME_relative_multiply
297 (GNUNET_TIME_UNIT_SECONDS, 5),
300 switch (current_test)
302 case PEER1_HALFCLOSE_WRITE_FAIL:
303 GNUNET_assert (&peer1 == peer);
304 GNUNET_assert (NULL == peer->io_write_handle);
305 transition(); /* To PEER1_READ_SHUTDOWN */
307 case PEER1_READ_SHUTDOWN:
308 GNUNET_assert (&peer2 == peer);
309 GNUNET_assert (NULL == peer->io_write_handle);
310 transition (); /* To SUCCESS */
313 GNUNET_assert (NULL != peer->io_write_handle);
319 * Close sockets and stop testing deamons nicely
322 do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
324 if (NULL != peer2.socket)
325 GNUNET_STREAM_close (peer2.socket);
326 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
327 GNUNET_SCHEDULER_cancel (abort_task);
328 if (NULL != peer2.op)
329 GNUNET_TESTBED_operation_done (peer2.op);
331 GNUNET_SCHEDULER_shutdown (); /* For shutting down testbed */
336 * Completion callback for shutdown
338 * @param cls the closure from GNUNET_STREAM_shutdown call
339 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
343 shutdown_completion (void *cls,
346 switch (current_test)
350 case PEER1_WRITE_SHUTDOWN:
351 GNUNET_assert (cls == &peer1);
352 GNUNET_assert (SHUT_WR == operation);
353 peer1.test_ok = GNUNET_YES;
354 /* Peer2 should read with error */
355 peer2.bytes_read = 0;
356 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
358 case PEER1_READ_SHUTDOWN:
359 peer1.test_ok = GNUNET_YES;
360 peer2.bytes_wrote = 0;
361 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
363 case PEER1_HALFCLOSE_READ:
364 case PEER1_HALFCLOSE_WRITE_FAIL:
366 GNUNET_assert (0); /* We shouldn't reach here */
372 * Task for calling STREAM_shutdown
374 * @param cls the peer entity
375 * @param tc the TaskContext
378 stream_shutdown_task (void *cls,
379 const struct GNUNET_SCHEDULER_TaskContext *tc)
381 struct PeerData *peer = cls;
383 peer->shutdown_handle = GNUNET_STREAM_shutdown (peer->socket,
384 peer->shutdown_operation,
385 &shutdown_completion,
387 GNUNET_assert (NULL != peer->shutdown_handle);
392 * Something went wrong and timed out. Kill everything and set error flag
395 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
400 GNUNET_SCHEDULER_cancel (read_task);
402 result = GNUNET_SYSERR;
409 * The transition function; responsible for the transitions among tests
414 if ((GNUNET_YES == peer1.test_ok) && (GNUNET_YES == peer2.test_ok))
416 peer1.test_ok = GNUNET_NO;
417 peer2.test_ok = GNUNET_NO;
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "TEST %d SUCCESSFULL\n", current_test);
420 switch (current_test)
423 current_test = PEER1_WRITE_SHUTDOWN;
424 /* Peer1 should shutdown writing */
425 peer1.shutdown_operation = SHUT_WR;
426 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
428 case PEER1_WRITE_SHUTDOWN:
429 current_test = PEER1_HALFCLOSE_READ;
430 /* Peer2 should be able to write successfully */
431 peer2.bytes_wrote = 0;
432 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
434 /* Peer1 should be able to read successfully */
435 peer1.bytes_read = 0;
436 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
438 case PEER1_HALFCLOSE_READ:
439 current_test = PEER1_HALFCLOSE_WRITE_FAIL;
440 peer1.bytes_wrote = 0;
441 peer2.bytes_read = 0;
442 peer2.test_ok = GNUNET_YES;
443 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer1);
445 case PEER1_HALFCLOSE_WRITE_FAIL:
446 current_test = PEER1_READ_SHUTDOWN;
447 peer1.shutdown_operation = SHUT_RD;
448 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
450 case PEER1_READ_SHUTDOWN:
451 current_test = SUCCESS;
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "All tests successful\n");
454 GNUNET_SCHEDULER_add_now (&do_close, NULL);
457 GNUNET_assert (0); /* We shouldn't reach here */
464 * The write completion function; called upon writing some data to stream or
467 * @param cls the closure from GNUNET_STREAM_write/read
468 * @param status the status of the stream at the time this function is called
469 * @param size the number of bytes read or written
472 write_completion (void *cls,
473 enum GNUNET_STREAM_Status status,
476 struct PeerData *peer = cls;
478 switch (current_test)
481 case PEER1_HALFCLOSE_READ:
483 GNUNET_assert (GNUNET_STREAM_OK == status);
484 GNUNET_assert (size <= strlen (data));
485 peer->bytes_wrote += size;
487 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
489 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Writing completed\n");
498 peer1.test_ok = GNUNET_YES;
499 transition (); /* to PEER1_WRITE_SHUTDOWN */
501 else /* This will happen during PEER1_HALFCLOSE_READ */
503 peer2.test_ok = GNUNET_YES;
504 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
508 case PEER1_HALFCLOSE_WRITE_FAIL:
509 GNUNET_assert (peer == &peer1);
510 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
511 GNUNET_assert (0 == size);
512 peer1.test_ok = GNUNET_YES;
514 case PEER1_READ_SHUTDOWN:
515 GNUNET_assert (peer == &peer2);
516 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
517 GNUNET_assert (0 == size);
518 peer2.test_ok = GNUNET_YES;
520 case PEER1_WRITE_SHUTDOWN:
522 GNUNET_assert (0); /* We shouldn't reach here */
528 * Function executed after stream has been established
530 * @param cls the closure from GNUNET_STREAM_open
531 * @param socket socket to use to communicate with the other side (read/write)
534 stream_open_cb (void *cls,
535 struct GNUNET_STREAM_Socket *socket)
537 struct PeerData *peer;
539 GNUNET_assert (socket == peer1.socket);
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "%s: Stream established from peer1\n",
542 GNUNET_i2s (&peer1.our_id));
543 peer = (struct PeerData *) cls;
544 peer->bytes_wrote = 0;
545 GNUNET_assert (socket == peer1.socket);
546 GNUNET_assert (socket == peer->socket);
547 peer1.test_ok = GNUNET_NO;
548 peer2.test_ok = GNUNET_NO;
549 current_test = PEER1_WRITE;
550 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
557 * @param cls the closure from GNUNET_STREAM_write/read
558 * @param status the status of the stream at the time this function is called
559 * @param data traffic from the other side
560 * @param size the number of bytes available in data read
561 * @return number of bytes of processed from 'data' (any data remaining should be
562 * given to the next time the read processor is called).
565 input_processor (void *cls,
566 enum GNUNET_STREAM_Status status,
567 const void *input_data,
570 struct PeerData *peer;
572 peer = (struct PeerData *) cls;
574 switch (current_test)
577 case PEER1_HALFCLOSE_READ:
578 if (GNUNET_STREAM_TIMEOUT == status)
580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
581 "Read operation timedout - reading again!\n");
582 GNUNET_assert (0 == size);
583 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
587 GNUNET_assert (GNUNET_STREAM_OK == status);
588 GNUNET_assert (size <= strlen (data));
589 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
590 (const char *) input_data,
592 peer->bytes_read += size;
594 if (peer->bytes_read < strlen (data))
596 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
600 if (&peer2 == peer) /* Peer2 has completed reading; should write */
602 peer2.test_ok = GNUNET_YES;
603 transition (); /* Transition to PEER1_WRITE_SHUTDOWN */
605 else /* Peer1 has completed reading. End of tests */
607 peer1.test_ok = GNUNET_YES;
608 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
612 case PEER1_WRITE_SHUTDOWN:
613 GNUNET_assert (0); /* This callback will not be called when stream
616 case PEER1_HALFCLOSE_WRITE_FAIL:
617 case PEER1_READ_SHUTDOWN:
619 GNUNET_assert (0); /* We shouldn't reach here */
627 * Scheduler call back; to be executed when a new stream is connected
628 * Called from listen connect for peer2
631 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
633 read_task = GNUNET_SCHEDULER_NO_TASK;
634 GNUNET_assert (NULL != cls);
635 peer2.bytes_read = 0;
636 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
641 * Functions of this type are called upon new stream connection from other peers
643 * @param cls the closure from GNUNET_STREAM_listen
644 * @param socket the socket representing the stream
645 * @param initiator the identity of the peer who wants to establish a stream
647 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
648 * stream (the socket will be invalid after the call)
651 stream_listen_cb (void *cls,
652 struct GNUNET_STREAM_Socket *socket,
653 const struct GNUNET_PeerIdentity *initiator)
655 if ((NULL == socket) || (NULL == initiator))
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
658 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
659 GNUNET_SCHEDULER_cancel (abort_task);
660 abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
663 GNUNET_assert (socket != peer1.socket);
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
665 "%s: Peer connected: %s\n",
666 GNUNET_i2s (&peer2.our_id),
667 GNUNET_i2s(initiator));
668 peer2.socket = socket;
669 /* FIXME: reading should be done right now instead of a scheduled call */
670 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
676 * Listen success callback; connects a peer to stream as client
679 stream_connect (void);
683 * Adapter function called to destroy a connection to
687 * @param op_result service handle returned from the connect adapter
690 stream_da (void *cls, void *op_result)
692 struct GNUNET_STREAM_ListenSocket *lsocket;
697 GNUNET_STREAM_listen_close (lsocket);
698 if (NULL != peer1.op)
699 GNUNET_TESTBED_operation_done (peer1.op);
701 GNUNET_SCHEDULER_shutdown ();
706 GNUNET_assert (op_result == peer1.socket);
707 GNUNET_STREAM_close (peer1.socket);
708 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
716 * Adapter function called to establish a connection to
720 * @param cfg configuration of the peer to connect to; will be available until
721 * GNUNET_TESTBED_operation_done() is called on the operation returned
722 * from GNUNET_TESTBED_service_connect()
723 * @return service handle to return in 'op_result', NULL on error
726 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
728 struct GNUNET_STREAM_ListenSocket *lsocket;
732 case PEER2_STREAM_CONNECT:
733 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
734 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
735 &stream_connect, GNUNET_STREAM_OPTION_END);
736 GNUNET_assert (NULL != lsocket);
738 case PEER1_STREAM_CONNECT:
739 peer1.socket = GNUNET_STREAM_open (cfg, &peer2.our_id, 10, &stream_open_cb,
740 &peer1, GNUNET_STREAM_OPTION_END);
741 GNUNET_assert (NULL != peer1.socket);
750 * Listen success callback; connects a peer to stream as client
753 stream_connect (void)
755 GNUNET_assert (PEER2_STREAM_CONNECT == setup_state);
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
757 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
759 stream_ca, stream_da, &peer1);
760 setup_state = PEER1_STREAM_CONNECT;
765 * Callback to be called when the requested peer information is available
767 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
768 * @param op the operation this callback corresponds to
769 * @param pinfo the result; will be NULL if the operation has failed
770 * @param emsg error message if the operation has failed; will be NULL if the
771 * operation is successfull
774 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
775 const struct GNUNET_TESTBED_PeerInformation *pinfo,
778 GNUNET_assert (NULL == emsg);
779 GNUNET_assert (op == op_);
782 case PEER1_GET_IDENTITY:
783 memcpy (&peer1.our_id, pinfo->result.id,
784 sizeof (struct GNUNET_PeerIdentity));
785 GNUNET_TESTBED_operation_done (op);
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
788 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
789 GNUNET_TESTBED_PIT_IDENTITY,
791 setup_state = PEER2_GET_IDENTITY;
793 case PEER2_GET_IDENTITY:
794 memcpy (&peer2.our_id, pinfo->result.id,
795 sizeof (struct GNUNET_PeerIdentity));
796 GNUNET_TESTBED_operation_done (op);
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
799 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
801 stream_ca, stream_da, &peer2);
802 setup_state = PEER2_STREAM_CONNECT;
811 * Controller event callback
814 * @param event the controller event
817 controller_event_cb (void *cls,
818 const struct GNUNET_TESTBED_EventInformation *event)
822 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
825 case PEER1_STREAM_CONNECT:
826 case PEER2_STREAM_CONNECT:
827 GNUNET_assert (NULL == event->details.operation_finished.emsg);
840 * Signature of a main function for a testcase.
843 * @param num_peers number of peers in 'peers'
844 * @param peers handle to peers run in the testbed
847 test_master (void *cls, unsigned int num_peers,
848 struct GNUNET_TESTBED_Peer **peers)
850 GNUNET_assert (NULL != peers);
851 GNUNET_assert (NULL != peers[0]);
852 GNUNET_assert (NULL != peers[1]);
853 peer1.peer = peers[0];
854 peer2.peer = peers[1];
855 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
856 GNUNET_TESTBED_PIT_IDENTITY,
858 setup_state = PEER1_GET_IDENTITY;
860 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
861 (GNUNET_TIME_UNIT_SECONDS, 1000), &do_abort,
869 int main (int argc, char **argv)
875 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
876 (void) GNUNET_TESTBED_test_run ("test_stream_2peers_halfclose",
877 "test_stream_local.conf", NUM_PEERS,
879 &controller_event_cb, NULL, &test_master,
881 if (GNUNET_SYSERR == result)