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 GNUNET_TESTBED_operation_done (peer2.op);
342 * Completion callback for shutdown
344 * @param cls the closure from GNUNET_STREAM_shutdown call
345 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
349 shutdown_completion (void *cls,
352 switch (current_test)
356 case PEER1_WRITE_SHUTDOWN:
357 GNUNET_assert (cls == &peer1);
358 GNUNET_assert (SHUT_WR == operation);
359 peer1.test_ok = GNUNET_YES;
360 /* Peer2 should read with error */
361 peer2.bytes_read = 0;
362 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
364 case PEER1_READ_SHUTDOWN:
365 peer1.test_ok = GNUNET_YES;
366 peer2.bytes_wrote = 0;
367 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
369 case PEER1_HALFCLOSE_READ:
370 case PEER1_HALFCLOSE_WRITE_FAIL:
372 GNUNET_assert (0); /* We shouldn't reach here */
378 * Task for calling STREAM_shutdown
380 * @param cls the peer entity
381 * @param tc the TaskContext
384 stream_shutdown_task (void *cls,
385 const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct PeerData *peer = cls;
389 peer->shutdown_handle = GNUNET_STREAM_shutdown (peer->socket,
390 peer->shutdown_operation,
391 &shutdown_completion,
393 GNUNET_assert (NULL != peer->shutdown_handle);
398 * Something went wrong and timed out. Kill everything and set error flag
401 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
406 GNUNET_SCHEDULER_cancel (read_task);
408 result = GNUNET_SYSERR;
415 * The transition function; responsible for the transitions among tests
420 if ((GNUNET_YES == peer1.test_ok) && (GNUNET_YES == peer2.test_ok))
422 peer1.test_ok = GNUNET_NO;
423 peer2.test_ok = GNUNET_NO;
424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
425 "TEST %d SUCCESSFULL\n", current_test);
426 switch (current_test)
429 current_test = PEER1_WRITE_SHUTDOWN;
430 /* Peer1 should shutdown writing */
431 peer1.shutdown_operation = SHUT_WR;
432 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
434 case PEER1_WRITE_SHUTDOWN:
435 current_test = PEER1_HALFCLOSE_READ;
436 /* Peer2 should be able to write successfully */
437 peer2.bytes_wrote = 0;
438 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer2);
440 /* Peer1 should be able to read successfully */
441 peer1.bytes_read = 0;
442 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer1);
444 case PEER1_HALFCLOSE_READ:
445 current_test = PEER1_HALFCLOSE_WRITE_FAIL;
446 peer1.bytes_wrote = 0;
447 peer2.bytes_read = 0;
448 peer2.test_ok = GNUNET_YES;
449 GNUNET_SCHEDULER_add_now (&stream_write_task, &peer1);
451 case PEER1_HALFCLOSE_WRITE_FAIL:
452 current_test = PEER1_READ_SHUTDOWN;
453 peer1.shutdown_operation = SHUT_RD;
454 GNUNET_SCHEDULER_add_now (&stream_shutdown_task, &peer1);
456 case PEER1_READ_SHUTDOWN:
457 current_test = SUCCESS;
458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459 "All tests successful\n");
460 GNUNET_SCHEDULER_add_now (&do_close, NULL);
463 GNUNET_assert (0); /* We shouldn't reach here */
470 * The write completion function; called upon writing some data to stream or
473 * @param cls the closure from GNUNET_STREAM_write/read
474 * @param status the status of the stream at the time this function is called
475 * @param size the number of bytes read or written
478 write_completion (void *cls,
479 enum GNUNET_STREAM_Status status,
482 struct PeerData *peer = cls;
484 switch (current_test)
487 case PEER1_HALFCLOSE_READ:
489 GNUNET_assert (GNUNET_STREAM_OK == status);
490 GNUNET_assert (size <= strlen (data));
491 peer->bytes_wrote += size;
493 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
495 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
500 "Writing completed\n");
504 peer1.test_ok = GNUNET_YES;
505 transition (); /* to PEER1_WRITE_SHUTDOWN */
507 else /* This will happen during PEER1_HALFCLOSE_READ */
509 peer2.test_ok = GNUNET_YES;
510 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
514 case PEER1_HALFCLOSE_WRITE_FAIL:
515 GNUNET_assert (peer == &peer1);
516 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
517 GNUNET_assert (0 == size);
518 peer1.test_ok = GNUNET_YES;
520 case PEER1_READ_SHUTDOWN:
521 GNUNET_assert (peer == &peer2);
522 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
523 GNUNET_assert (0 == size);
524 peer2.test_ok = GNUNET_YES;
526 case PEER1_WRITE_SHUTDOWN:
528 GNUNET_assert (0); /* We shouldn't reach here */
534 * Function executed after stream has been established
536 * @param cls the closure from GNUNET_STREAM_open
537 * @param socket socket to use to communicate with the other side (read/write)
540 stream_open_cb (void *cls,
541 struct GNUNET_STREAM_Socket *socket)
543 struct PeerData *peer;
545 GNUNET_assert (socket == peer1.socket);
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "%s: Stream established from peer1\n",
548 GNUNET_i2s (&peer1.our_id));
549 peer = (struct PeerData *) cls;
550 peer->bytes_wrote = 0;
551 GNUNET_assert (socket == peer1.socket);
552 GNUNET_assert (socket == peer->socket);
553 peer1.test_ok = GNUNET_NO;
554 peer2.test_ok = GNUNET_NO;
555 current_test = PEER1_WRITE;
556 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
563 * @param cls the closure from GNUNET_STREAM_write/read
564 * @param status the status of the stream at the time this function is called
565 * @param data traffic from the other side
566 * @param size the number of bytes available in data read
567 * @return number of bytes of processed from 'data' (any data remaining should be
568 * given to the next time the read processor is called).
571 input_processor (void *cls,
572 enum GNUNET_STREAM_Status status,
573 const void *input_data,
576 struct PeerData *peer;
578 peer = (struct PeerData *) cls;
580 switch (current_test)
583 case PEER1_HALFCLOSE_READ:
584 if (GNUNET_STREAM_TIMEOUT == status)
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Read operation timedout - reading again!\n");
588 GNUNET_assert (0 == size);
589 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
593 GNUNET_assert (GNUNET_STREAM_OK == status);
594 GNUNET_assert (size <= strlen (data));
595 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
596 (const char *) input_data,
598 peer->bytes_read += size;
600 if (peer->bytes_read < strlen (data))
602 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
606 if (&peer2 == peer) /* Peer2 has completed reading; should write */
608 peer2.test_ok = GNUNET_YES;
609 transition (); /* Transition to PEER1_WRITE_SHUTDOWN */
611 else /* Peer1 has completed reading. End of tests */
613 peer1.test_ok = GNUNET_YES;
614 transition (); /* to PEER1_HALFCLOSE_WRITE_FAIL */
618 case PEER1_WRITE_SHUTDOWN:
619 GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status);
620 peer2.test_ok = GNUNET_YES;
622 case PEER1_HALFCLOSE_WRITE_FAIL:
623 case PEER1_READ_SHUTDOWN:
625 GNUNET_assert (0); /* We shouldn't reach here */
633 * Scheduler call back; to be executed when a new stream is connected
634 * Called from listen connect for peer2
637 stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
639 read_task = GNUNET_SCHEDULER_NO_TASK;
640 GNUNET_assert (NULL != cls);
641 peer2.bytes_read = 0;
642 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
647 * Functions of this type are called upon new stream connection from other peers
649 * @param cls the closure from GNUNET_STREAM_listen
650 * @param socket the socket representing the stream
651 * @param initiator the identity of the peer who wants to establish a stream
653 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
654 * stream (the socket will be invalid after the call)
657 stream_listen_cb (void *cls,
658 struct GNUNET_STREAM_Socket *socket,
659 const struct GNUNET_PeerIdentity *initiator)
661 GNUNET_assert (NULL != socket);
662 GNUNET_assert (NULL != initiator);
663 GNUNET_assert (socket != peer1.socket);
665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
666 "%s: Peer connected: %s\n",
667 GNUNET_i2s (&peer2.our_id),
668 GNUNET_i2s(initiator));
670 peer2.socket = socket;
671 /* FIXME: reading should be done right now instead of a scheduled call */
672 read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket);
678 * Listen success callback; connects a peer to stream as client
681 stream_connect (void);
685 * Adapter function called to destroy a connection to
689 * @param op_result service handle returned from the connect adapter
692 stream_da (void *cls, void *op_result)
694 struct GNUNET_STREAM_ListenSocket *lsocket;
699 GNUNET_STREAM_listen_close (lsocket);
700 GNUNET_TESTBED_operation_done (peer1.op);
705 GNUNET_assert (op_result == peer1.socket);
706 GNUNET_STREAM_close (peer1.socket);
707 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
715 * Adapter function called to establish a connection to
719 * @param cfg configuration of the peer to connect to; will be available until
720 * GNUNET_TESTBED_operation_done() is called on the operation returned
721 * from GNUNET_TESTBED_service_connect()
722 * @return service handle to return in 'op_result', NULL on error
725 stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
727 struct GNUNET_STREAM_ListenSocket *lsocket;
731 case PEER2_STREAM_CONNECT:
732 lsocket = GNUNET_STREAM_listen (cfg, 10, &stream_listen_cb, NULL,
733 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
734 &stream_connect, GNUNET_STREAM_OPTION_END);
735 GNUNET_assert (NULL != lsocket);
737 case PEER1_STREAM_CONNECT:
738 peer1.socket = GNUNET_STREAM_open (cfg, &peer2.our_id, 10, &stream_open_cb,
739 &peer1, GNUNET_STREAM_OPTION_END);
740 GNUNET_assert (NULL != peer1.socket);
749 * Listen success callback; connects a peer to stream as client
752 stream_connect (void)
754 GNUNET_assert (PEER2_STREAM_CONNECT == setup_state);
755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream listen open successful\n");
756 peer1.op = GNUNET_TESTBED_service_connect (&peer1, peer1.peer, "stream",
758 stream_ca, stream_da, &peer1);
759 setup_state = PEER1_STREAM_CONNECT;
764 * Callback to be called when the requested peer information is available
766 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
767 * @param op the operation this callback corresponds to
768 * @param pinfo the result; will be NULL if the operation has failed
769 * @param emsg error message if the operation has failed; will be NULL if the
770 * operation is successfull
773 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
774 const struct GNUNET_TESTBED_PeerInformation *pinfo,
777 GNUNET_assert (NULL == emsg);
778 GNUNET_assert (op == op_);
781 case PEER1_GET_IDENTITY:
782 memcpy (&peer1.our_id, pinfo->result.id,
783 sizeof (struct GNUNET_PeerIdentity));
784 GNUNET_TESTBED_operation_done (op);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
787 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
788 GNUNET_TESTBED_PIT_IDENTITY,
790 setup_state = PEER2_GET_IDENTITY;
792 case PEER2_GET_IDENTITY:
793 memcpy (&peer2.our_id, pinfo->result.id,
794 sizeof (struct GNUNET_PeerIdentity));
795 GNUNET_TESTBED_operation_done (op);
796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 2 id: %s\n", GNUNET_i2s
798 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
800 stream_ca, stream_da, &peer2);
801 setup_state = PEER2_STREAM_CONNECT;
810 * Controller event callback
813 * @param event the controller event
816 controller_event_cb (void *cls,
817 const struct GNUNET_TESTBED_EventInformation *event)
821 case GNUNET_TESTBED_ET_CONNECT:
822 GNUNET_assert (INIT == setup_state);
823 GNUNET_TESTBED_operation_done (op);
824 op = GNUNET_TESTBED_peer_get_information (peer1.peer,
825 GNUNET_TESTBED_PIT_IDENTITY,
827 setup_state = PEER1_GET_IDENTITY;
829 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
832 case PEER1_STREAM_CONNECT:
833 case PEER2_STREAM_CONNECT:
834 GNUNET_assert (NULL == event->details.operation_finished.emsg);
847 * Signature of a main function for a testcase.
850 * @param num_peers number of peers in 'peers'
851 * @param peers handle to peers run in the testbed
854 test_master (void *cls, unsigned int num_peers,
855 struct GNUNET_TESTBED_Peer **peers)
857 GNUNET_assert (NULL != peers);
858 GNUNET_assert (NULL != peers[0]);
859 GNUNET_assert (NULL != peers[1]);
860 peer1.peer = peers[0];
861 peer2.peer = peers[1];
862 op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL, peer2.peer, peer1.peer);
865 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
866 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
874 int main (int argc, char **argv)
880 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
881 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
882 GNUNET_TESTBED_test_run ("test_stream_2peers_halfclose",
883 "test_stream_local.conf", NUM_PEERS, event_mask,
884 &controller_event_cb, NULL, &test_master, NULL);
885 if (GNUNET_SYSERR == result)