aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/stream/Makefile.am2
-rw-r--r--src/stream/test_stream_2peers_halfclose.c455
2 files changed, 272 insertions, 185 deletions
diff --git a/src/stream/Makefile.am b/src/stream/Makefile.am
index 472b5949f..6c9e82dea 100644
--- a/src/stream/Makefile.am
+++ b/src/stream/Makefile.am
@@ -51,7 +51,7 @@ test_stream_2peers_halfclose_SOURCES = \
51test_stream_2peers_halfclose_LDADD = \ 51test_stream_2peers_halfclose_LDADD = \
52 $(top_builddir)/src/stream/libgnunetstream.la \ 52 $(top_builddir)/src/stream/libgnunetstream.la \
53 $(top_builddir)/src/util/libgnunetutil.la \ 53 $(top_builddir)/src/util/libgnunetutil.la \
54 $(top_builddir)/src/testing_old/libgnunettesting_old.la 54 $(top_builddir)/src/testbed/libgnunettestbed.la
55 55
56test_stream_local_SOURCES = \ 56test_stream_local_SOURCES = \
57 test_stream_local.c 57 test_stream_local.c
diff --git a/src/stream/test_stream_2peers_halfclose.c b/src/stream/test_stream_2peers_halfclose.c
index 8de758ea0..e089a22a1 100644
--- a/src/stream/test_stream_2peers_halfclose.c
+++ b/src/stream/test_stream_2peers_halfclose.c
@@ -28,10 +28,9 @@
28 28
29#include "platform.h" 29#include "platform.h"
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31#include "gnunet_testbed_service.h"
31#include "gnunet_mesh_service.h" 32#include "gnunet_mesh_service.h"
32#include "gnunet_stream_lib.h" 33#include "gnunet_stream_lib.h"
33#include "gnunet_testing_lib.h"
34#include "gnunet_scheduler_lib.h"
35 34
36#define VERBOSE 1 35#define VERBOSE 1
37 36
@@ -49,6 +48,11 @@
49struct PeerData 48struct PeerData
50{ 49{
51 /** 50 /**
51 * The testbed peer handle corresponding to this peer
52 */
53 struct GNUNET_TESTBED_Peer *peer;
54
55 /**
52 * Peer's stream socket 56 * Peer's stream socket
53 */ 57 */
54 struct GNUNET_STREAM_Socket *socket; 58 struct GNUNET_STREAM_Socket *socket;
@@ -69,6 +73,11 @@ struct PeerData
69 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle; 73 struct GNUNET_STREAM_ShutdownHandle *shutdown_handle;
70 74
71 /** 75 /**
76 * Testbed operation handle specific for this peer
77 */
78 struct GNUNET_TESTBED_Operation *op;
79
80 /**
72 * Our Peer id 81 * Our Peer id
73 */ 82 */
74 struct GNUNET_PeerIdentity our_id; 83 struct GNUNET_PeerIdentity our_id;
@@ -94,20 +103,76 @@ struct PeerData
94 int shutdown_operation; 103 int shutdown_operation;
95}; 104};
96 105
97/**
98 * The current peer group
99 */
100static struct GNUNET_TESTING_PeerGroup *pg;
101 106
102/** 107/**
103 * Peer 1 daemon 108 * Enumeration for various tests that are to be passed in the same order as
109 * below
104 */ 110 */
105static struct GNUNET_TESTING_Daemon *d1; 111enum Test
112{
113 /**
114 * Peer1 writing; Peer2 reading
115 */
116 PEER1_WRITE,
117
118 /**
119 * Peer1 write shutdown; Peer2 should get an error when it tries to read;
120 */
121 PEER1_WRITE_SHUTDOWN,
122
123 /**
124 * Peer1 reads; Peer2 writes (connection is halfclosed)
125 */
126 PEER1_HALFCLOSE_READ,
127
128 /**
129 * Peer1 attempts to write; Should fail with stream already shutdown error
130 */
131 PEER1_HALFCLOSE_WRITE_FAIL,
132
133 /**
134 * Peer1 read shutdown; Peer2 should get stream shutdown error during write
135 */
136 PEER1_READ_SHUTDOWN,
137
138 /**
139 * All tests successfully finished
140 */
141 SUCCESS
142};
143
106 144
107/** 145/**
108 * Peer 2 daemon 146 * Different states in test setup
109 */ 147 */
110static struct GNUNET_TESTING_Daemon *d2; 148enum SetupState
149{
150 /**
151 * The initial state
152 */
153 INIT,
154
155 /**
156 * Get the identity of peer 1
157 */
158 PEER1_GET_IDENTITY,
159
160 /**
161 * Get the identity of peer 2
162 */
163 PEER2_GET_IDENTITY,
164
165 /**
166 * Connect to stream service of peer 2
167 */
168 PEER2_STREAM_CONNECT,
169
170 /**
171 * Connect to stream service of peer 1
172 */
173 PEER1_STREAM_CONNECT
174
175};
111 176
112 177
113/** 178/**
@@ -119,51 +184,28 @@ static struct GNUNET_TESTING_Daemon *d2;
119 */ 184 */
120static struct PeerData peer1; 185static struct PeerData peer1;
121static struct PeerData peer2; 186static struct PeerData peer2;
122static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
123static struct GNUNET_CONFIGURATION_Handle *config;
124 187
188/**
189 * Task for aborting the test case if it takes too long
190 */
125static GNUNET_SCHEDULER_TaskIdentifier abort_task; 191static GNUNET_SCHEDULER_TaskIdentifier abort_task;
192
193/**
194 * Task for reading from stream
195 */
126static GNUNET_SCHEDULER_TaskIdentifier read_task; 196static GNUNET_SCHEDULER_TaskIdentifier read_task;
127 197
128static char *data = "ABCD"; 198static char *data = "ABCD";
129static int result;
130 199
131/** 200/**
132 * Enumeration for various tests that are to be passed in the same order as 201 * Handle to testbed operation
133 * below
134 */ 202 */
135enum Test 203struct GNUNET_TESTBED_Operation *op;
136 { 204
137 /** 205/**
138 * Peer1 writing; Peer2 reading 206 * Final testing result
139 */ 207 */
140 PEER1_WRITE, 208static int result;
141
142 /**
143 * Peer1 write shutdown; Peer2 should get an error when it tries to read;
144 */
145 PEER1_WRITE_SHUTDOWN,
146
147 /**
148 * Peer1 reads; Peer2 writes (connection is halfclosed)
149 */
150 PEER1_HALFCLOSE_READ,
151
152 /**
153 * Peer1 attempts to write; Should fail with stream already shutdown error
154 */
155 PEER1_HALFCLOSE_WRITE_FAIL,
156
157 /**
158 * Peer1 read shutdown; Peer2 should get stream shutdown error during write
159 */
160 PEER1_READ_SHUTDOWN,
161
162 /**
163 * All tests successfully finished
164 */
165 SUCCESS
166 };
167 209
168/** 210/**
169 * Current running test 211 * Current running test
@@ -171,6 +213,12 @@ enum Test
171enum Test current_test; 213enum Test current_test;
172 214
173/** 215/**
216 * State is test setup
217 */
218enum SetupState setup_state;
219
220
221/**
174 * Input processor 222 * Input processor
175 * 223 *
176 * @param cls the closure from GNUNET_STREAM_write/read 224 * @param cls the closure from GNUNET_STREAM_write/read
@@ -277,51 +325,16 @@ stream_write_task (void *cls,
277 325
278 326
279/** 327/**
280 * Check whether peers successfully shut down.
281 */
282static void
283peergroup_shutdown_callback (void *cls, const char *emsg)
284{
285 if (emsg != NULL)
286 {
287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
288 "Shutdown of peers failed!\n");
289 }
290 else
291 {
292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
293 "All peers successfully shut down!\n");
294 }
295 GNUNET_CONFIGURATION_destroy (config);
296}
297
298
299/**
300 * Close sockets and stop testing deamons nicely 328 * Close sockets and stop testing deamons nicely
301 */ 329 */
302static void 330static void
303do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 331do_close (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
304{ 332{
305 if (NULL != peer1.socket)
306 GNUNET_STREAM_close (peer1.socket);
307 if (NULL != peer2.socket) 333 if (NULL != peer2.socket)
308 GNUNET_STREAM_close (peer2.socket); 334 GNUNET_STREAM_close (peer2.socket);
309 if (NULL != peer2_listen_socket) 335 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
310 GNUNET_STREAM_listen_close (peer2_listen_socket);
311
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
313 if (0 != abort_task)
314 {
315 GNUNET_SCHEDULER_cancel (abort_task); 336 GNUNET_SCHEDULER_cancel (abort_task);
316 } 337 GNUNET_TESTBED_operation_done (peer2.op);
317
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
319
320 GNUNET_TESTING_daemons_stop (pg,
321 GNUNET_TIME_relative_multiply
322 (GNUNET_TIME_UNIT_SECONDS, 5),
323 &peergroup_shutdown_callback,
324 NULL);
325} 338}
326 339
327 340
@@ -665,137 +678,211 @@ stream_listen_cb (void *cls,
665 * Listen success callback; connects a peer to stream as client 678 * Listen success callback; connects a peer to stream as client
666 */ 679 */
667static void 680static void
681stream_connect (void);
682
683
684/**
685 * Adapter function called to destroy a connection to
686 * a service.
687 *
688 * @param cls closure
689 * @param op_result service handle returned from the connect adapter
690 */
691static void
692stream_da (void *cls, void *op_result)
693{
694 struct GNUNET_STREAM_ListenSocket *lsocket;
695
696 if (&peer2 == cls)
697 {
698 lsocket = op_result;
699 GNUNET_STREAM_listen_close (lsocket);
700 GNUNET_TESTBED_operation_done (peer1.op);
701 return;
702 }
703 if (&peer1 == cls)
704 {
705 GNUNET_assert (op_result == peer1.socket);
706 GNUNET_STREAM_close (peer1.socket);
707 GNUNET_SCHEDULER_shutdown (); /* Exit point of the test */
708 return;
709 }
710 GNUNET_assert (0);
711}
712
713
714/**
715 * Adapter function called to establish a connection to
716 * a service.
717 *
718 * @param cls closure
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
723 */
724static void *
725stream_ca (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
726{
727 struct GNUNET_STREAM_ListenSocket *lsocket;
728
729 switch (setup_state)
730 {
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);
736 return 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);
741 return peer1.socket;
742 default:
743 GNUNET_assert (0);
744 }
745}
746
747
748/**
749 * Listen success callback; connects a peer to stream as client
750 */
751static void
668stream_connect (void) 752stream_connect (void)
753{
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",
757 NULL, NULL,
758 stream_ca, stream_da, &peer1);
759 setup_state = PEER1_STREAM_CONNECT;
760}
761
762
763/**
764 * Callback to be called when the requested peer information is available
765 *
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
771 */
772static void
773peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
774 const struct GNUNET_TESTBED_PeerInformation *pinfo,
775 const char *emsg)
669{ 776{
670 /* Connect to stream library */ 777 GNUNET_assert (NULL == emsg);
671 peer1.socket = GNUNET_STREAM_open (d1->cfg, 778 GNUNET_assert (op == op_);
672 &d2->id, /* Null for local peer? */ 779 switch (setup_state)
673 10, /* App port */ 780 {
674 &stream_open_cb, 781 case PEER1_GET_IDENTITY:
675 &peer1, 782 memcpy (&peer1.our_id, pinfo->result.id,
676 GNUNET_STREAM_OPTION_END); 783 sizeof (struct GNUNET_PeerIdentity));
677 GNUNET_assert (NULL != peer1.socket); 784 GNUNET_TESTBED_operation_done (op);
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 id: %s\n", GNUNET_i2s
786 (&peer1.our_id));
787 op = GNUNET_TESTBED_peer_get_information (peer2.peer,
788 GNUNET_TESTBED_PIT_IDENTITY,
789 &peerinfo_cb, NULL);
790 setup_state = PEER2_GET_IDENTITY;
791 break;
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
797 (&peer2.our_id));
798 peer2.op = GNUNET_TESTBED_service_connect (&peer2, peer2.peer, "stream",
799 NULL, NULL,
800 stream_ca, stream_da, &peer2);
801 setup_state = PEER2_STREAM_CONNECT;
802 break;
803 default:
804 GNUNET_assert (0);
805 }
678} 806}
679 807
680 808
681/** 809/**
682 * Callback to be called when testing peer group is ready 810 * Controller event callback
683 * 811 *
684 * @param cls NULL 812 * @param cls NULL
685 * @param emsg NULL on success 813 * @param event the controller event
686 */ 814 */
687void 815static void
688peergroup_ready (void *cls, const char *emsg) 816controller_event_cb (void *cls,
817 const struct GNUNET_TESTBED_EventInformation *event)
689{ 818{
690 if (NULL != emsg) 819 switch (event->type)
820 {
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,
826 &peerinfo_cb, NULL);
827 setup_state = PEER1_GET_IDENTITY;
828 break;
829 case GNUNET_TESTBED_ET_OPERATION_FINISHED:
830 switch (setup_state)
691 { 831 {
692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 832 case PEER1_STREAM_CONNECT:
693 "Starting peer group failed: %s\n", emsg); 833 case PEER2_STREAM_CONNECT:
694 return; 834 GNUNET_assert (NULL == event->details.operation_finished.emsg);
835 break;
836 default:
837 GNUNET_assert (0);
695 } 838 }
696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 839 break;
697 "Peer group is now ready\n"); 840 default:
698 841 GNUNET_assert (0);
699 GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg)); 842 }
700
701 d1 = GNUNET_TESTING_daemon_get (pg, 0);
702 GNUNET_assert (NULL != d1);
703
704 d2 = GNUNET_TESTING_daemon_get (pg, 1);
705 GNUNET_assert (NULL != d2);
706
707 GNUNET_TESTING_get_peer_identity (d1->cfg,
708 &peer1.our_id);
709 GNUNET_TESTING_get_peer_identity (d2->cfg,
710 &peer2.our_id);
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "%s : %s\n",
713 GNUNET_i2s (&peer1.our_id),
714 GNUNET_i2s (&d1->id));
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "%s : %s\n",
717 GNUNET_i2s (&peer2.our_id),
718 GNUNET_i2s (&d2->id));
719
720 peer2_listen_socket =
721 GNUNET_STREAM_listen (d2->cfg,
722 10, /* App port */
723 &stream_listen_cb,
724 NULL,
725 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
726 &stream_connect,
727 GNUNET_STREAM_OPTION_END);
728 GNUNET_assert (NULL != peer2_listen_socket);
729} 843}
730 844
731 845
732/** 846/**
733 * Initialize framework and start test 847 * Signature of a main function for a testcase.
848 *
849 * @param cls closure
850 * @param num_peers number of peers in 'peers'
851 * @param peers handle to peers run in the testbed
734 */ 852 */
735static void 853static void
736run (void *cls, char *const *args, const char *cfgfile, 854test_master (void *cls, unsigned int num_peers,
737 const struct GNUNET_CONFIGURATION_Handle *cfg) 855 struct GNUNET_TESTBED_Peer **peers)
738{ 856{
739 struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */ 857 GNUNET_assert (NULL != peers);
740 858 GNUNET_assert (NULL != peers[0]);
741 /* GNUNET_log_setup ("test_stream_local", */ 859 GNUNET_assert (NULL != peers[1]);
742 /* "DEBUG", */ 860 peer1.peer = peers[0];
743 /* NULL); */ 861 peer2.peer = peers[1];
744 862 op = GNUNET_TESTBED_overlay_connect (NULL, NULL, NULL, peer2.peer, peer1.peer);
745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 863 setup_state = INIT;
746 "Starting test\n");
747 /* Duplicate the configuration */
748 config = GNUNET_CONFIGURATION_dup (cfg);
749
750 hosts = GNUNET_TESTING_hosts_load (config);
751
752 pg = GNUNET_TESTING_peergroup_start (config,
753 2,
754 GNUNET_TIME_relative_multiply
755 (GNUNET_TIME_UNIT_SECONDS, 3),
756 NULL,
757 &peergroup_ready,
758 NULL,
759 hosts);
760 GNUNET_assert (NULL != pg);
761
762 abort_task = 864 abort_task =
763 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply 865 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
764 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort, 866 (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort,
765 NULL); 867 NULL);
766} 868}
767 869
870
768/** 871/**
769 * Main function 872 * Main function
770 */ 873 */
771int main (int argc, char **argv) 874int main (int argc, char **argv)
772{ 875{
773 int ret; 876 uint64_t event_mask;
774 877
775 char *argv2[] = { "test-stream-2peers-halfclose", 878 result = GNUNET_NO;
776 "-L", "DEBUG", 879 event_mask = 0;
777 "-c", "test_stream_local.conf", 880 event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
778 NULL}; 881 event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
779 882 GNUNET_TESTBED_test_run ("test_stream_2peers_halfclose",
780 struct GNUNET_GETOPT_CommandLineOption options[] = { 883 "test_stream_local.conf", NUM_PEERS, event_mask,
781 GNUNET_GETOPT_OPTION_END 884 &controller_event_cb, NULL, &test_master, NULL);
782 };
783
784 ret =
785 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
786 "test-stream-2peers-halfclose", "nohelp", options, &run, NULL);
787
788 if (GNUNET_OK != ret)
789 {
790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
791 ret);
792 return 1;
793 }
794 if (GNUNET_SYSERR == result) 885 if (GNUNET_SYSERR == result)
795 {
796 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
797 return 1; 886 return 1;
798 }
799 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");
800 return 0; 887 return 0;
801} 888}