diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2012-01-16 17:17:57 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2012-01-16 17:17:57 +0000 |
commit | adda534763a0981d3be3de39b6006737cab98425 (patch) | |
tree | 62ea39ef76cd164389d3ecccd130d454e975bc61 /src | |
parent | 18a41e0fe392a136e89d7c5aed8a5a09cd48247b (diff) | |
download | gnunet-adda534763a0981d3be3de39b6006737cab98425.tar.gz gnunet-adda534763a0981d3be3de39b6006737cab98425.zip |
refined test cases
Diffstat (limited to 'src')
-rw-r--r-- | src/stream/test_stream_local.c | 245 | ||||
-rw-r--r-- | src/stream/test_stream_local_halfclose.c | 381 |
2 files changed, 499 insertions, 127 deletions
diff --git a/src/stream/test_stream_local.c b/src/stream/test_stream_local.c index 32e3bf2e4..27271faff 100644 --- a/src/stream/test_stream_local.c +++ b/src/stream/test_stream_local.c | |||
@@ -25,7 +25,6 @@ | |||
25 | */ | 25 | */ |
26 | 26 | ||
27 | #include <string.h> | 27 | #include <string.h> |
28 | #include <sys/socket.h> /* For SHUT_RD, SHUT_WR */ | ||
29 | 28 | ||
30 | #include "platform.h" | 29 | #include "platform.h" |
31 | #include "gnunet_util_lib.h" | 30 | #include "gnunet_util_lib.h" |
@@ -34,39 +33,52 @@ | |||
34 | 33 | ||
35 | #define VERBOSE 1 | 34 | #define VERBOSE 1 |
36 | 35 | ||
36 | /** | ||
37 | * Structure for holding peer's sockets and IO Handles | ||
38 | */ | ||
39 | struct PeerData | ||
40 | { | ||
41 | /** | ||
42 | * Peer's stream socket | ||
43 | */ | ||
44 | struct GNUNET_STREAM_Socket *socket; | ||
45 | |||
46 | /** | ||
47 | * Peer's io handle | ||
48 | */ | ||
49 | struct GNUNET_STREAM_IOHandle *io_handle; | ||
50 | |||
51 | /** | ||
52 | * Bytes the peer has written | ||
53 | */ | ||
54 | unsigned int bytes_wrote; | ||
55 | |||
56 | /** | ||
57 | * Byte the peer has read | ||
58 | */ | ||
59 | unsigned int bytes_read; | ||
60 | }; | ||
61 | |||
37 | static struct GNUNET_OS_Process *arm_pid; | 62 | static struct GNUNET_OS_Process *arm_pid; |
38 | static struct GNUNET_STREAM_Socket *peer1_socket; | 63 | static struct PeerData peer1; |
64 | static struct PeerData peer2; | ||
39 | static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; | 65 | static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; |
40 | static struct GNUNET_STREAM_Socket *peer2_socket; | ||
41 | 66 | ||
42 | static GNUNET_SCHEDULER_TaskIdentifier abort_task; | 67 | static GNUNET_SCHEDULER_TaskIdentifier abort_task; |
43 | static GNUNET_SCHEDULER_TaskIdentifier test_task; | 68 | static GNUNET_SCHEDULER_TaskIdentifier test_task; |
44 | static GNUNET_SCHEDULER_TaskIdentifier read_task; | 69 | static GNUNET_SCHEDULER_TaskIdentifier read_task; |
45 | 70 | ||
46 | static GNUNET_STREAM_IOHandle *peer1_IOHandle; | ||
47 | static GNUNET_STREAM_IOHandle *peer2_IOHandle; | ||
48 | |||
49 | static char *data = "ABCD"; | 71 | static char *data = "ABCD"; |
50 | static unsigned int data_pointer; | ||
51 | static unsigned int read_pointer; | ||
52 | static int result; | 72 | static int result; |
53 | 73 | ||
54 | static int peer1_write_pass; | ||
55 | static int peer2_read_pass; | ||
56 | static int peer1_half_closed_write_pass; | ||
57 | static int peer2_half_closed_read_pass; | ||
58 | static int peer1_write_shutdown_pass; | ||
59 | static int peer1_read_shutdown_pass; | ||
60 | |||
61 | |||
62 | /** | 74 | /** |
63 | * Shutdown nicely | 75 | * Shutdown nicely |
64 | */ | 76 | */ |
65 | static void | 77 | static void |
66 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 78 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
67 | { | 79 | { |
68 | GNUNET_STREAM_close (peer1_socket); | 80 | GNUNET_STREAM_close (peer1.socket); |
69 | GNUNET_STREAM_close (peer2_socket); | 81 | GNUNET_STREAM_close (peer2.socket); |
70 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n"); | 82 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n"); |
71 | if (0 != abort_task) | 83 | if (0 != abort_task) |
72 | { | 84 | { |
@@ -116,52 +128,37 @@ void write_completion (void *cls, | |||
116 | enum GNUNET_STREAM_Status status, | 128 | enum GNUNET_STREAM_Status status, |
117 | size_t size) | 129 | size_t size) |
118 | { | 130 | { |
131 | struct PeerData *peer; | ||
132 | |||
133 | peer = (struct PeerData *) cls; | ||
134 | GNUNET_assert (GNUNET_STREAM_OK == status); | ||
135 | GNUNET_assert (size < strlen (data)); | ||
136 | peer->bytes_wrote += size; | ||
119 | 137 | ||
120 | if (peer1_write_shutdown_pass) /* Called for peer2's write operation */ | 138 | if (peer->bytes_wrote < strlen(data)) /* Have more data to send */ |
121 | { | 139 | { |
122 | /* peer1 has shutdown reading */ | 140 | peer->io_handle = GNUNET_STREAM_write (peer->socket, |
123 | GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status); | 141 | (void *) data, |
124 | GNUNET_assert (0 == size); | 142 | strlen(data) - peer->bytes_wrote, |
125 | peer1_read_shutdown_pass = 1; | 143 | GNUNET_TIME_relative_multiply |
126 | 144 | (GNUNET_TIME_UNIT_SECONDS, 5), | |
127 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | 145 | &write_completion, |
146 | cls); | ||
147 | GNUNET_assert (NULL != peer->io_handle); | ||
128 | } | 148 | } |
129 | GNUNET_assert (GNUNET_STREAM_OK == status); | 149 | else |
130 | if (data_pointer + size != strlen(data)) /* Have more data to send */ | ||
131 | { | 150 | { |
132 | data_pointer += size; | 151 | if (&peer1 == peer) /* Peer1 has finished writing; should read now */ |
133 | peer1_IOHandle = GNUNET_STREAM_write (peer1_socket, | 152 | { |
134 | (void *) data, | 153 | peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) |
135 | strlen(data) - data_pointer, | 154 | peer->socket, |
136 | GNUNET_TIME_relative_multiply | 155 | GNUNET_TIME_relative_multiply |
137 | (GNUNET_TIME_UNIT_SECONDS, 5), | 156 | (GNUNET_TIME_UNIT_SECONDS, 5), |
138 | &write_completion, | 157 | &input_processor, |
139 | NULL); | 158 | cls); |
140 | GNUNET_assert (NULL != peer1_IOHandle); | 159 | GNUNET_assert (NULL!=peer->io_handle); |
160 | } | ||
141 | } | 161 | } |
142 | else{ | ||
143 | peer1_write_pass = 1; | ||
144 | /* If we are here and peer2_read_pass == 1 => we have send the data twice */ | ||
145 | if (peer2_read_pass) peer1_half_closed_write_pass = 1; | ||
146 | |||
147 | if (! peer1_half_closed_write_pass) | ||
148 | { | ||
149 | GNUNET_STREAM_shutdown (peer1_socket, SHUT_RD); | ||
150 | /* Half closed write */ | ||
151 | data_pointer = 0; | ||
152 | peer1_IOHandle = GNUNET_STREAM_write (peer1_socket, | ||
153 | (void *) data, | ||
154 | strlen(data), | ||
155 | GNUNET_TIME_relative_multiply | ||
156 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
157 | &write_completion, | ||
158 | NULL); | ||
159 | } | ||
160 | else | ||
161 | { | ||
162 | GNUNET_STREAM_shutdown (peer1_socket, SHUT_WR); | ||
163 | } | ||
164 | } | ||
165 | } | 162 | } |
166 | 163 | ||
167 | 164 | ||
@@ -173,20 +170,22 @@ void write_completion (void *cls, | |||
173 | */ | 170 | */ |
174 | static void | 171 | static void |
175 | stream_open_cb (void *cls, | 172 | stream_open_cb (void *cls, |
176 | struct GNUNET_STREAM_Socket | 173 | struct GNUNET_STREAM_Socket *socket) |
177 | *socket) | ||
178 | { | 174 | { |
179 | data_pointer = 0; | 175 | struct PeerData *peer; |
180 | GNUNET_assert (socket == peer1_socket); | 176 | |
181 | peer1_IOHandle = GNUNET_STREAM_write (socket, /* socket */ | 177 | peer = (struct PeerData *) cls; |
182 | (void *) data, /* data */ | 178 | peer->bytes_wrote = 0; |
183 | strlen(data), | 179 | GNUNET_assert (socket == peer1.socket); |
184 | GNUNET_TIME_relative_multiply | 180 | GNUNET_assert (socket == peer->socket); |
185 | (GNUNET_TIME_UNIT_SECONDS, 5), | 181 | peer->io_handle = GNUNET_STREAM_write (peer->socket, /* socket */ |
186 | &write_completion, | 182 | (void *) data, /* data */ |
187 | NULL); | 183 | strlen(data), |
188 | GNUNET_assert (NULL != peer1_IOHandle); | 184 | GNUNET_TIME_relative_multiply |
189 | 185 | (GNUNET_TIME_UNIT_SECONDS, 5), | |
186 | &write_completion, | ||
187 | cls); | ||
188 | GNUNET_assert (NULL != peer->io_handle); | ||
190 | } | 189 | } |
191 | 190 | ||
192 | 191 | ||
@@ -206,76 +205,67 @@ input_processor (void *cls, | |||
206 | const void *input_data, | 205 | const void *input_data, |
207 | size_t size) | 206 | size_t size) |
208 | { | 207 | { |
208 | struct PeerData *peer; | ||
209 | 209 | ||
210 | if (peer2_half_closed_read_pass) | 210 | peer = (struct PeerData *) cls; |
211 | { | ||
212 | GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status); | ||
213 | GNUNET_assert (0 == size); | ||
214 | peer1_write_shutdown_pass = 1; | ||
215 | /* Now this should result in STREAM_SHUTDOWN */ | ||
216 | peer2_IOHandle = GNUNET_STREAM_write (peer2_socket, | ||
217 | (void *) data, | ||
218 | strlen(data), | ||
219 | GNUNET_TIME_relative_multiply | ||
220 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
221 | &write_completion, | ||
222 | (void *) peer2_socket); | ||
223 | |||
224 | return 0; | ||
225 | } | ||
226 | 211 | ||
227 | GNUNET_assert (GNUNET_STERAM_OK == status); | 212 | GNUNET_assert (GNUNET_STERAM_OK == status); |
228 | GNUNET_assert (size < strlen (data)); | 213 | GNUNET_assert (size < strlen (data)); |
229 | GNUNET_assert (strncmp ((const char *) data, | 214 | GNUNET_assert (strncmp ((const char *) data + peer->bytes_read, |
230 | (const char *) input_data, | 215 | (const char *) input_data, |
231 | size)); | 216 | size)); |
232 | read_pointer += size; | 217 | peer->bytes_read += size; |
233 | 218 | ||
234 | if (read_pointer < strlen (data)) | 219 | if (peer->bytes_read < strlen (data)) |
235 | { | 220 | { |
236 | peer2_IOHandle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls, | 221 | peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) |
237 | GNUNET_TIME_relative_multiply | 222 | peer->socket, |
238 | (GNUNET_TIME_UNIT_SECONDS, 5), | 223 | GNUNET_TIME_relative_multiply |
239 | &input_processor, | 224 | (GNUNET_TIME_UNIT_SECONDS, 5), |
240 | NULL); | 225 | &input_processor, |
241 | GNUNET_assert (NULL != peer2_IOHandle); | 226 | cls); |
227 | GNUNET_assert (NULL != peer->io_handle); | ||
242 | } | 228 | } |
243 | else { | 229 | else |
244 | /* If we are here and peer2_read_pass => we have finished reading twice */ | 230 | { |
245 | if (peer1_write_pass && peer2_read_pass) peer2_half_closed_read_pass = 1; | 231 | if (&peer2 == peer) /* Peer2 has completed reading; should write */ |
246 | if (peer1_write_pass) peer2_read_pass = 1; | 232 | { |
247 | 233 | peer->bytes_wrote = 0; | |
248 | /* Half closed read */ | 234 | peer->io_handle = GNUNET_STREAM_write ((struct GNUNET_STREAM_Socket *) |
249 | read_pointer = 0; | 235 | peer->socket, |
250 | peer2_IOHandle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls, | 236 | (void *) data, |
251 | GNUNET_TIME_relative_multiply | 237 | strlen(data), |
252 | (GNUNET_TIME_UNIT_SECONDS, 5), | 238 | GNUNET_TIME_relative_multiply |
253 | &input_processor, | 239 | (GNUNET_TIME_UNIT_SECONDS, 5), |
254 | NULL); | 240 | &write_completion, |
255 | 241 | cls); | |
256 | 242 | } | |
257 | } | 243 | else /* Peer1 has completed reading. End of tests */ |
258 | 244 | { | |
245 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | ||
246 | } | ||
247 | } | ||
259 | return size; | 248 | return size; |
260 | } | 249 | } |
261 | 250 | ||
262 | 251 | ||
263 | /** | 252 | /** |
264 | * Scheduler call back; to be executed when a new stream is connected | 253 | * Scheduler call back; to be executed when a new stream is connected |
254 | * Called from listen connect for peer2 | ||
265 | */ | 255 | */ |
266 | static void | 256 | static void |
267 | stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 257 | stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
268 | { | 258 | { |
269 | read_task = GNUNET_SCHEDULER_NO_TASK; | 259 | read_task = GNUNET_SCHEDULER_NO_TASK; |
270 | GNUNET_assert (NULL != cls); | 260 | GNUNET_assert (NULL != cls); |
271 | read_pointer = 0; | 261 | peer2.bytes_read = 0; |
272 | GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */ | 262 | GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */ |
273 | peer2_IOHandle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls, | 263 | peer2.io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls, |
274 | GNUNET_TIME_relative_multiply | 264 | GNUNET_TIME_relative_multiply |
275 | (GNUNET_TIME_UNIT_SECONDS, 5), | 265 | (GNUNET_TIME_UNIT_SECONDS, 5), |
276 | &input_processor, | 266 | &input_processor, |
277 | NULL); | 267 | (void *) &peer2); |
278 | GNUNET_assert (NULL != peer2_IOHandle); | 268 | GNUNET_assert (NULL != peer2.io_handle); |
279 | } | 269 | } |
280 | 270 | ||
281 | 271 | ||
@@ -296,9 +286,9 @@ stream_listen_cb (void *cls, | |||
296 | { | 286 | { |
297 | GNUNET_assert (NULL != socket); | 287 | GNUNET_assert (NULL != socket); |
298 | GNUNET_assert (NULL == initiator); /* Local peer=NULL? */ | 288 | GNUNET_assert (NULL == initiator); /* Local peer=NULL? */ |
299 | GNUNET_assert (socket != peer1_socket); | 289 | GNUNET_assert (socket != peer1.socket); |
300 | 290 | ||
301 | peer2_socket = socket; | 291 | peer2.socket = socket; |
302 | read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket); | 292 | read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket); |
303 | return GNUNET_OK; | 293 | return GNUNET_OK; |
304 | } | 294 | } |
@@ -313,10 +303,11 @@ test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | |||
313 | test_task = GNUNET_SCHEDULER_NO_TASK; | 303 | test_task = GNUNET_SCHEDULER_NO_TASK; |
314 | 304 | ||
315 | /* Connect to stream library */ | 305 | /* Connect to stream library */ |
316 | peer1_socket = GNUNET_STREAM_open (NULL, /* Null for local peer? */ | 306 | peer1.socket = GNUNET_STREAM_open (NULL, /* Null for local peer? */ |
317 | 10, /* App port */ | 307 | 10, /* App port */ |
318 | open_cb); | 308 | &stream_open_cb, |
319 | GNUNET_assert (NULL != peer1_socket); | 309 | (void *) &peer1); |
310 | GNUNET_assert (NULL != peer1.socket); | ||
320 | peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */ | 311 | peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */ |
321 | &stream_listen_cb, | 312 | &stream_listen_cb, |
322 | NULL); | 313 | NULL); |
diff --git a/src/stream/test_stream_local_halfclose.c b/src/stream/test_stream_local_halfclose.c new file mode 100644 index 000000000..44ae5787a --- /dev/null +++ b/src/stream/test_stream_local_halfclose.c | |||
@@ -0,0 +1,381 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file stream/test_stream_local_halfclose.c | ||
23 | * @brief Stream API testing between local peers with half closed connections | ||
24 | * @author Sree Harsha Totakura | ||
25 | */ | ||
26 | |||
27 | #include <string.h> | ||
28 | #include <sys/socket.h> /* For SHUT_RD, SHUT_WR */ | ||
29 | |||
30 | #include "platform.h" | ||
31 | #include "gnunet_util_lib.h" | ||
32 | #include "gnunet_mesh_service.h" | ||
33 | #include "gnunet_stream_lib.h" | ||
34 | |||
35 | #define VERBOSE 1 | ||
36 | |||
37 | /** | ||
38 | * Structure for holding peer's sockets and IO Handles | ||
39 | */ | ||
40 | struct PeerData | ||
41 | { | ||
42 | /** | ||
43 | * Peer's stream socket | ||
44 | */ | ||
45 | struct GNUNET_STREAM_Socket *socket; | ||
46 | |||
47 | /** | ||
48 | * Peer's io handle | ||
49 | */ | ||
50 | struct GNUNET_STREAM_IOHandle *io_handle; | ||
51 | |||
52 | /** | ||
53 | * Bytes the peer has written | ||
54 | */ | ||
55 | unsigned int bytes_wrote; | ||
56 | |||
57 | /** | ||
58 | * Byte the peer has read | ||
59 | */ | ||
60 | unsigned int bytes_read; | ||
61 | }; | ||
62 | |||
63 | static struct GNUNET_OS_Process *arm_pid; | ||
64 | static struct PeerData peer1; | ||
65 | static struct PeerData peer2; | ||
66 | static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; | ||
67 | |||
68 | static GNUNET_SCHEDULER_TaskIdentifier abort_task; | ||
69 | static GNUNET_SCHEDULER_TaskIdentifier test_task; | ||
70 | static GNUNET_SCHEDULER_TaskIdentifier read_task; | ||
71 | |||
72 | static char *data = "ABCD"; | ||
73 | static int result; | ||
74 | |||
75 | /** | ||
76 | * Shutdown nicely | ||
77 | */ | ||
78 | static void | ||
79 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
80 | { | ||
81 | GNUNET_STREAM_close (peer1.socket); | ||
82 | GNUNET_STREAM_close (peer2.socket); | ||
83 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n"); | ||
84 | if (0 != abort_task) | ||
85 | { | ||
86 | GNUNET_SCHEDULER_cancel (abort_task); | ||
87 | } | ||
88 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n"); | ||
89 | if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) | ||
90 | { | ||
91 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
92 | } | ||
93 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n"); | ||
94 | GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); | ||
95 | GNUNET_OS_process_close (arm_pid); | ||
96 | } | ||
97 | |||
98 | |||
99 | /** | ||
100 | * Something went wrong and timed out. Kill everything and set error flag | ||
101 | */ | ||
102 | static void | ||
103 | do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
104 | { | ||
105 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n"); | ||
106 | if (0 != test_task) | ||
107 | { | ||
108 | GNUNET_SCHEDULER_cancel (test_task); | ||
109 | } | ||
110 | if (0 != read_task) | ||
111 | { | ||
112 | GNUNET_SCHEDULER_cancel (read_task); | ||
113 | } | ||
114 | result = GNUNET_SYSERR; | ||
115 | abort_task = 0; | ||
116 | do_shutdown (cls, tc); | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * The write completion function; called upon writing some data to stream or | ||
122 | * upon error | ||
123 | * | ||
124 | * @param cls the closure from GNUNET_STREAM_write/read | ||
125 | * @param status the status of the stream at the time this function is called | ||
126 | * @param size the number of bytes read or written | ||
127 | */ | ||
128 | void write_completion (void *cls, | ||
129 | enum GNUNET_STREAM_Status status, | ||
130 | size_t size) | ||
131 | { | ||
132 | struct PeerData *peer; | ||
133 | |||
134 | peer = (struct PeerData *) cls; | ||
135 | GNUNET_assert (GNUNET_STREAM_OK == status); | ||
136 | GNUNET_assert (size < strlen (data)); | ||
137 | peer->bytes_wrote += size; | ||
138 | |||
139 | if (peer->bytes_wrote < strlen(data)) /* Have more data to send */ | ||
140 | { | ||
141 | peer->io_handle = GNUNET_STREAM_write (peer->socket, | ||
142 | (void *) data, | ||
143 | strlen(data) - peer->bytes_wrote, | ||
144 | GNUNET_TIME_relative_multiply | ||
145 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
146 | &write_completion, | ||
147 | cls); | ||
148 | GNUNET_assert (NULL != peer->io_handle); | ||
149 | } | ||
150 | else | ||
151 | { | ||
152 | if (&peer1 == peer) /* Peer1 has finished writing; should read now */ | ||
153 | { | ||
154 | peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) | ||
155 | peer->socket, | ||
156 | GNUNET_TIME_relative_multiply | ||
157 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
158 | &input_processor, | ||
159 | cls); | ||
160 | GNUNET_assert (NULL!=peer->io_handle); | ||
161 | } | ||
162 | } | ||
163 | } | ||
164 | |||
165 | |||
166 | /** | ||
167 | * Function executed after stream has been established | ||
168 | * | ||
169 | * @param cls the closure from GNUNET_STREAM_open | ||
170 | * @param socket socket to use to communicate with the other side (read/write) | ||
171 | */ | ||
172 | static void | ||
173 | stream_open_cb (void *cls, | ||
174 | struct GNUNET_STREAM_Socket *socket) | ||
175 | { | ||
176 | struct PeerData *peer; | ||
177 | |||
178 | peer = (struct PeerData *) cls; | ||
179 | peer->bytes_wrote = 0; | ||
180 | GNUNET_assert (socket == peer1.socket); | ||
181 | GNUNET_assert (socket == peer->socket); | ||
182 | peer->io_handle = GNUNET_STREAM_write (peer->socket, /* socket */ | ||
183 | (void *) data, /* data */ | ||
184 | strlen(data), | ||
185 | GNUNET_TIME_relative_multiply | ||
186 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
187 | &write_completion, | ||
188 | cls); | ||
189 | GNUNET_assert (NULL != peer->io_handle); | ||
190 | } | ||
191 | |||
192 | |||
193 | /** | ||
194 | * Input processor | ||
195 | * | ||
196 | * @param cls the closure from GNUNET_STREAM_write/read | ||
197 | * @param status the status of the stream at the time this function is called | ||
198 | * @param data traffic from the other side | ||
199 | * @param size the number of bytes available in data read | ||
200 | * @return number of bytes of processed from 'data' (any data remaining should be | ||
201 | * given to the next time the read processor is called). | ||
202 | */ | ||
203 | static size_t | ||
204 | input_processor (void *cls, | ||
205 | enum GNUNET_STREAM_Status status, | ||
206 | const void *input_data, | ||
207 | size_t size) | ||
208 | { | ||
209 | struct PeerData *peer; | ||
210 | |||
211 | peer = (struct PeerData *) cls; | ||
212 | |||
213 | /* Peer1 is expected to read when it first finishes writing */ | ||
214 | if (peer == &peer1) | ||
215 | { | ||
216 | /* since p2 closed write */ | ||
217 | GNUNET_assert (GNUNET_STREAM_SHUTDOWN == status); | ||
218 | /* Test passed; shutdown now */ | ||
219 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | ||
220 | return 0; | ||
221 | } | ||
222 | |||
223 | GNUNET_assert (GNUNET_STERAM_OK == status); | ||
224 | GNUNET_assert (size < strlen (data)); | ||
225 | GNUNET_assert (strncmp ((const char *) data + peer->bytes_read, | ||
226 | (const char *) input_data, | ||
227 | size)); | ||
228 | peer->bytes_read += size; | ||
229 | |||
230 | if (peer->bytes_read < strlen (data)) | ||
231 | { | ||
232 | peer->io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) | ||
233 | peer->socket, | ||
234 | GNUNET_TIME_relative_multiply | ||
235 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
236 | &input_processor, | ||
237 | cls); | ||
238 | GNUNET_assert (NULL != peer->io_handle); | ||
239 | } | ||
240 | |||
241 | return size; | ||
242 | } | ||
243 | |||
244 | |||
245 | /** | ||
246 | * Scheduler call back; to be executed when a new stream is connected | ||
247 | * Called from listen connect for peer2 | ||
248 | */ | ||
249 | static void | ||
250 | stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
251 | { | ||
252 | read_task = GNUNET_SCHEDULER_NO_TASK; | ||
253 | GNUNET_assert (NULL != cls); | ||
254 | peer2.bytes_read = 0; | ||
255 | GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */ | ||
256 | /* Close the stream for writing */ | ||
257 | GNUNET_STREAM_shutdown ((struct GNUNET_STREAM_Socket *) cls, | ||
258 | SHUT_WR); | ||
259 | peer2.io_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls, | ||
260 | GNUNET_TIME_relative_multiply | ||
261 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
262 | &input_processor, | ||
263 | (void *) &peer2); | ||
264 | GNUNET_assert (NULL != peer2.io_handle); | ||
265 | } | ||
266 | |||
267 | |||
268 | /** | ||
269 | * Functions of this type are called upon new stream connection from other peers | ||
270 | * | ||
271 | * @param cls the closure from GNUNET_STREAM_listen | ||
272 | * @param socket the socket representing the stream | ||
273 | * @param initiator the identity of the peer who wants to establish a stream | ||
274 | * with us | ||
275 | * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the | ||
276 | * stream (the socket will be invalid after the call) | ||
277 | */ | ||
278 | static int | ||
279 | stream_listen_cb (void *cls, | ||
280 | struct GNUNET_STREAM_Socket *socket, | ||
281 | const struct GNUNET_PeerIdentity *initiator) | ||
282 | { | ||
283 | GNUNET_assert (NULL != socket); | ||
284 | GNUNET_assert (NULL == initiator); /* Local peer=NULL? */ | ||
285 | GNUNET_assert (socket != peer1.socket); | ||
286 | |||
287 | peer2.socket = socket; | ||
288 | read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket); | ||
289 | return GNUNET_OK; | ||
290 | } | ||
291 | |||
292 | |||
293 | /** | ||
294 | * Testing function | ||
295 | */ | ||
296 | static void | ||
297 | test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
298 | { | ||
299 | test_task = GNUNET_SCHEDULER_NO_TASK; | ||
300 | |||
301 | /* Connect to stream library */ | ||
302 | peer1.socket = GNUNET_STREAM_open (NULL, /* Null for local peer? */ | ||
303 | 10, /* App port */ | ||
304 | &stream_open_cb, | ||
305 | (void *) &peer1); | ||
306 | GNUNET_assert (NULL != peer1.socket); | ||
307 | peer2_listen_socket = GNUNET_STREAM_listen (10 /* App port */ | ||
308 | &stream_listen_cb, | ||
309 | NULL); | ||
310 | GNUNET_assert (NULL != peer2_listen_socket); | ||
311 | |||
312 | } | ||
313 | |||
314 | /** | ||
315 | * Initialize framework and start test | ||
316 | */ | ||
317 | static void | ||
318 | run (void *cls, char *const *args, const char *cfgfile, | ||
319 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
320 | { | ||
321 | GNUNET_log_setup ("test_stream_local", | ||
322 | #if VERBOSE | ||
323 | "DEBUG", | ||
324 | #else | ||
325 | "WARNING", | ||
326 | #endif | ||
327 | NULL); | ||
328 | arm_pid = | ||
329 | GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", | ||
330 | "gnunet-service-arm", | ||
331 | #if VERBOSE_ARM | ||
332 | "-L", "DEBUG", | ||
333 | #endif | ||
334 | "-c", "test_stream_local.conf", NULL); | ||
335 | |||
336 | abort_task = | ||
337 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
338 | (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort, | ||
339 | NULL); | ||
340 | |||
341 | test_task = GNUNET_SCHEDULER_add_now (&test, (void *) cfg); | ||
342 | |||
343 | } | ||
344 | |||
345 | /** | ||
346 | * Main function | ||
347 | */ | ||
348 | int main (int argc, char **argv) | ||
349 | { | ||
350 | int ret; | ||
351 | |||
352 | char *const argv2[] = { "test-stream-local", | ||
353 | "-c", "test_stream.conf", | ||
354 | #if VERBOSE | ||
355 | "-L", "DEBUG", | ||
356 | #endif | ||
357 | NULL | ||
358 | }; | ||
359 | |||
360 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
361 | GNUNET_GETOPT_OPTION_END | ||
362 | }; | ||
363 | |||
364 | ret = | ||
365 | GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, | ||
366 | "test-stream-local", "nohelp", options, &run, NULL); | ||
367 | |||
368 | if (GNUNET_OK != ret) | ||
369 | { | ||
370 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n", | ||
371 | ret); | ||
372 | return 1; | ||
373 | } | ||
374 | if (GNUNET_SYSERR == result) | ||
375 | { | ||
376 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); | ||
377 | return 1; | ||
378 | } | ||
379 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n"); | ||
380 | return 0; | ||
381 | } | ||