diff options
author | Sree Harsha Totakura <totakura@in.tum.de> | 2012-06-24 09:05:45 +0000 |
---|---|---|
committer | Sree Harsha Totakura <totakura@in.tum.de> | 2012-06-24 09:05:45 +0000 |
commit | 8285935ab9d48f6cb49837a872ab9df67f04690f (patch) | |
tree | 310e59493794f213dd568c292ca53eaa1469f71f /src/stream | |
parent | b253510019eb2b3334a797a2c09d86ae0b3e97f5 (diff) | |
download | gnunet-8285935ab9d48f6cb49837a872ab9df67f04690f.tar.gz gnunet-8285935ab9d48f6cb49837a872ab9df67f04690f.zip |
- removed test_stream_api.c
Diffstat (limited to 'src/stream')
-rw-r--r-- | src/stream/test_stream_api.c | 435 |
1 files changed, 0 insertions, 435 deletions
diff --git a/src/stream/test_stream_api.c b/src/stream/test_stream_api.c deleted file mode 100644 index e78973dcf..000000000 --- a/src/stream/test_stream_api.c +++ /dev/null | |||
@@ -1,435 +0,0 @@ | |||
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.c | ||
23 | * @brief Stream API testing between local peers | ||
24 | * @author Sree Harsha Totakura | ||
25 | */ | ||
26 | |||
27 | #include <string.h> | ||
28 | |||
29 | #include "platform.h" | ||
30 | #include "gnunet_util_lib.h" | ||
31 | #include "gnunet_mesh_service.h" | ||
32 | #include "gnunet_stream_lib.h" | ||
33 | #include "gnunet_testing_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 write handle | ||
49 | */ | ||
50 | struct GNUNET_STREAM_IOWriteHandle *io_write_handle; | ||
51 | |||
52 | /** | ||
53 | * Peer's io read handle | ||
54 | */ | ||
55 | struct GNUNET_STREAM_IOReadHandle *io_read_handle; | ||
56 | |||
57 | /** | ||
58 | * Bytes the peer has written | ||
59 | */ | ||
60 | unsigned int bytes_wrote; | ||
61 | |||
62 | /** | ||
63 | * Byte the peer has read | ||
64 | */ | ||
65 | unsigned int bytes_read; | ||
66 | }; | ||
67 | |||
68 | static struct GNUNET_OS_Process *arm_pid; | ||
69 | static struct PeerData peer1; | ||
70 | static struct PeerData peer2; | ||
71 | static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; | ||
72 | static struct GNUNET_CONFIGURATION_Handle *config; | ||
73 | |||
74 | static GNUNET_SCHEDULER_TaskIdentifier abort_task; | ||
75 | static GNUNET_SCHEDULER_TaskIdentifier test_task; | ||
76 | static GNUNET_SCHEDULER_TaskIdentifier read_task; | ||
77 | |||
78 | static char *data = "ABCD"; | ||
79 | static int result; | ||
80 | |||
81 | |||
82 | /** | ||
83 | * Shutdown nicely | ||
84 | */ | ||
85 | static void | ||
86 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
87 | { | ||
88 | GNUNET_STREAM_close (peer1.socket); | ||
89 | if (NULL != peer2.socket) | ||
90 | GNUNET_STREAM_close (peer2.socket); | ||
91 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n"); | ||
92 | if (0 != abort_task) | ||
93 | { | ||
94 | GNUNET_SCHEDULER_cancel (abort_task); | ||
95 | } | ||
96 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n"); | ||
97 | if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) | ||
98 | { | ||
99 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
100 | } | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n"); | ||
102 | /* Free the duplicated configuration */ | ||
103 | GNUNET_CONFIGURATION_destroy (config); | ||
104 | GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); | ||
105 | GNUNET_OS_process_destroy (arm_pid); | ||
106 | } | ||
107 | |||
108 | |||
109 | /** | ||
110 | * Something went wrong and timed out. Kill everything and set error flag | ||
111 | */ | ||
112 | static void | ||
113 | do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
114 | { | ||
115 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n"); | ||
116 | if (0 != test_task) | ||
117 | { | ||
118 | GNUNET_SCHEDULER_cancel (test_task); | ||
119 | } | ||
120 | if (0 != read_task) | ||
121 | { | ||
122 | GNUNET_SCHEDULER_cancel (read_task); | ||
123 | } | ||
124 | result = GNUNET_SYSERR; | ||
125 | abort_task = 0; | ||
126 | do_shutdown (cls, tc); | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * Signature for input processor | ||
131 | * | ||
132 | * @param cls the closure from GNUNET_STREAM_write/read | ||
133 | * @param status the status of the stream at the time this function is called | ||
134 | * @param data traffic from the other side | ||
135 | * @param size the number of bytes available in data read | ||
136 | * @return number of bytes of processed from 'data' (any data remaining should be | ||
137 | * given to the next time the read processor is called). | ||
138 | */ | ||
139 | static size_t | ||
140 | input_processor (void *cls, | ||
141 | enum GNUNET_STREAM_Status status, | ||
142 | const void *input_data, | ||
143 | size_t size); | ||
144 | |||
145 | |||
146 | /** | ||
147 | * The write completion function; called upon writing some data to stream or | ||
148 | * upon error | ||
149 | * | ||
150 | * @param cls the closure from GNUNET_STREAM_write/read | ||
151 | * @param status the status of the stream at the time this function is called | ||
152 | * @param size the number of bytes read or written | ||
153 | */ | ||
154 | static void | ||
155 | write_completion (void *cls, | ||
156 | enum GNUNET_STREAM_Status status, | ||
157 | size_t size) | ||
158 | { | ||
159 | struct PeerData *peer; | ||
160 | |||
161 | peer = (struct PeerData *) cls; | ||
162 | GNUNET_assert (GNUNET_STREAM_OK == status); | ||
163 | GNUNET_assert (size < strlen (data)); | ||
164 | peer->bytes_wrote += size; | ||
165 | |||
166 | if (peer->bytes_wrote < strlen(data)) /* Have more data to send */ | ||
167 | { | ||
168 | peer->io_write_handle = | ||
169 | GNUNET_STREAM_write (peer->socket, | ||
170 | (void *) data, | ||
171 | strlen(data) - peer->bytes_wrote, | ||
172 | GNUNET_TIME_relative_multiply | ||
173 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
174 | &write_completion, | ||
175 | cls); | ||
176 | GNUNET_assert (NULL != peer->io_write_handle); | ||
177 | } | ||
178 | else | ||
179 | { | ||
180 | if (&peer1 == peer) /* Peer1 has finished writing; should read now */ | ||
181 | { | ||
182 | peer->io_read_handle = | ||
183 | GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) | ||
184 | peer->socket, | ||
185 | GNUNET_TIME_relative_multiply | ||
186 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
187 | &input_processor, | ||
188 | cls); | ||
189 | GNUNET_assert (NULL!=peer->io_read_handle); | ||
190 | } | ||
191 | } | ||
192 | } | ||
193 | |||
194 | |||
195 | /** | ||
196 | * Function executed after stream has been established | ||
197 | * | ||
198 | * @param cls the closure from GNUNET_STREAM_open | ||
199 | * @param socket socket to use to communicate with the other side (read/write) | ||
200 | */ | ||
201 | static void | ||
202 | stream_open_cb (void *cls, | ||
203 | struct GNUNET_STREAM_Socket *socket) | ||
204 | { | ||
205 | struct PeerData *peer; | ||
206 | |||
207 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n"); | ||
208 | peer = (struct PeerData *) cls; | ||
209 | peer->bytes_wrote = 0; | ||
210 | GNUNET_assert (socket == peer1.socket); | ||
211 | GNUNET_assert (socket == peer->socket); | ||
212 | peer->io_write_handle = GNUNET_STREAM_write (peer->socket, /* socket */ | ||
213 | (void *) data, /* data */ | ||
214 | strlen(data), | ||
215 | GNUNET_TIME_relative_multiply | ||
216 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
217 | &write_completion, | ||
218 | cls); | ||
219 | GNUNET_assert (NULL != peer->io_write_handle); | ||
220 | } | ||
221 | |||
222 | |||
223 | /** | ||
224 | * Input processor | ||
225 | * | ||
226 | * @param cls the closure from GNUNET_STREAM_write/read | ||
227 | * @param status the status of the stream at the time this function is called | ||
228 | * @param data traffic from the other side | ||
229 | * @param size the number of bytes available in data read | ||
230 | * @return number of bytes of processed from 'data' (any data remaining should be | ||
231 | * given to the next time the read processor is called). | ||
232 | */ | ||
233 | static size_t | ||
234 | input_processor (void *cls, | ||
235 | enum GNUNET_STREAM_Status status, | ||
236 | const void *input_data, | ||
237 | size_t size) | ||
238 | { | ||
239 | struct PeerData *peer; | ||
240 | |||
241 | peer = (struct PeerData *) cls; | ||
242 | |||
243 | GNUNET_assert (GNUNET_STREAM_OK == status); | ||
244 | GNUNET_assert (size < strlen (data)); | ||
245 | GNUNET_assert (strncmp ((const char *) data + peer->bytes_read, | ||
246 | (const char *) input_data, | ||
247 | size)); | ||
248 | peer->bytes_read += size; | ||
249 | |||
250 | if (peer->bytes_read < strlen (data)) | ||
251 | { | ||
252 | peer->io_read_handle = GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) | ||
253 | peer->socket, | ||
254 | GNUNET_TIME_relative_multiply | ||
255 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
256 | &input_processor, | ||
257 | cls); | ||
258 | GNUNET_assert (NULL != peer->io_read_handle); | ||
259 | } | ||
260 | else | ||
261 | { | ||
262 | if (&peer2 == peer) /* Peer2 has completed reading; should write */ | ||
263 | { | ||
264 | peer->bytes_wrote = 0; | ||
265 | peer->io_write_handle = | ||
266 | GNUNET_STREAM_write ((struct GNUNET_STREAM_Socket *) | ||
267 | peer->socket, | ||
268 | (void *) data, | ||
269 | strlen(data), | ||
270 | GNUNET_TIME_relative_multiply | ||
271 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
272 | &write_completion, | ||
273 | cls); | ||
274 | } | ||
275 | else /* Peer1 has completed reading. End of tests */ | ||
276 | { | ||
277 | GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); | ||
278 | } | ||
279 | } | ||
280 | return size; | ||
281 | } | ||
282 | |||
283 | |||
284 | /** | ||
285 | * Scheduler call back; to be executed when a new stream is connected | ||
286 | * Called from listen connect for peer2 | ||
287 | */ | ||
288 | static void | ||
289 | stream_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
290 | { | ||
291 | read_task = GNUNET_SCHEDULER_NO_TASK; | ||
292 | GNUNET_assert (NULL != cls); | ||
293 | peer2.bytes_read = 0; | ||
294 | GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */ | ||
295 | peer2.io_read_handle = | ||
296 | GNUNET_STREAM_read ((struct GNUNET_STREAM_Socket *) cls, | ||
297 | GNUNET_TIME_relative_multiply | ||
298 | (GNUNET_TIME_UNIT_SECONDS, 5), | ||
299 | &input_processor, | ||
300 | (void *) &peer2); | ||
301 | GNUNET_assert (NULL != peer2.io_read_handle); | ||
302 | } | ||
303 | |||
304 | |||
305 | /** | ||
306 | * Functions of this type are called upon new stream connection from other peers | ||
307 | * | ||
308 | * @param cls the closure from GNUNET_STREAM_listen | ||
309 | * @param socket the socket representing the stream | ||
310 | * @param initiator the identity of the peer who wants to establish a stream | ||
311 | * with us | ||
312 | * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the | ||
313 | * stream (the socket will be invalid after the call) | ||
314 | */ | ||
315 | static int | ||
316 | stream_listen_cb (void *cls, | ||
317 | struct GNUNET_STREAM_Socket *socket, | ||
318 | const struct GNUNET_PeerIdentity *initiator) | ||
319 | { | ||
320 | GNUNET_assert (NULL != socket); | ||
321 | GNUNET_assert (NULL == initiator); /* Local peer=NULL? */ | ||
322 | GNUNET_assert (socket != peer1.socket); | ||
323 | |||
324 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
325 | "Peer connected: %s\n", GNUNET_i2s(initiator)); | ||
326 | |||
327 | peer2.socket = socket; | ||
328 | read_task = GNUNET_SCHEDULER_add_now (&stream_read, (void *) socket); | ||
329 | return GNUNET_OK; | ||
330 | } | ||
331 | |||
332 | |||
333 | /** | ||
334 | * Testing function | ||
335 | * | ||
336 | * @param cls NULL | ||
337 | * @param tc the task context | ||
338 | */ | ||
339 | static void | ||
340 | test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
341 | { | ||
342 | struct GNUNET_PeerIdentity self; | ||
343 | |||
344 | test_task = GNUNET_SCHEDULER_NO_TASK; | ||
345 | /* Get our identity */ | ||
346 | GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config, | ||
347 | &self)); | ||
348 | |||
349 | peer2_listen_socket = GNUNET_STREAM_listen (config, | ||
350 | 10, /* App port */ | ||
351 | &stream_listen_cb, | ||
352 | NULL, | ||
353 | GNUNET_STREAM_OPTION_END); | ||
354 | GNUNET_assert (NULL != peer2_listen_socket); | ||
355 | |||
356 | /* Connect to stream library */ | ||
357 | peer1.socket = GNUNET_STREAM_open (config, | ||
358 | &self, /* Null for local peer? */ | ||
359 | 10, /* App port */ | ||
360 | &stream_open_cb, | ||
361 | (void *) &peer1, | ||
362 | GNUNET_STREAM_OPTION_END); | ||
363 | GNUNET_assert (NULL != peer1.socket); | ||
364 | } | ||
365 | |||
366 | /** | ||
367 | * Initialize framework and start test | ||
368 | */ | ||
369 | static void | ||
370 | run (void *cls, char *const *args, const char *cfgfile, | ||
371 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
372 | { | ||
373 | GNUNET_log_setup ("test_stream_local", | ||
374 | #if VERBOSE | ||
375 | "DEBUG", | ||
376 | #else | ||
377 | "WARNING", | ||
378 | #endif | ||
379 | NULL); | ||
380 | /* Duplicate the configuration */ | ||
381 | config = GNUNET_CONFIGURATION_dup (cfg); | ||
382 | arm_pid = | ||
383 | GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm", | ||
384 | "gnunet-service-arm", | ||
385 | #if VERBOSE_ARM | ||
386 | "-L", "DEBUG", | ||
387 | #endif | ||
388 | "-c", "test_stream_local.conf", NULL); | ||
389 | |||
390 | abort_task = | ||
391 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
392 | (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort, | ||
393 | NULL); | ||
394 | |||
395 | test_task = GNUNET_SCHEDULER_add_now (&test, NULL); | ||
396 | |||
397 | } | ||
398 | |||
399 | /** | ||
400 | * Main function | ||
401 | */ | ||
402 | int main (int argc, char **argv) | ||
403 | { | ||
404 | int ret; | ||
405 | |||
406 | char *const argv2[] = { "test-stream-local", | ||
407 | "-c", "test_stream_local.conf", | ||
408 | #if VERBOSE | ||
409 | "-L", "DEBUG", | ||
410 | #endif | ||
411 | NULL | ||
412 | }; | ||
413 | |||
414 | struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
415 | GNUNET_GETOPT_OPTION_END | ||
416 | }; | ||
417 | |||
418 | ret = | ||
419 | GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, | ||
420 | "test-stream-local", "nohelp", options, &run, NULL); | ||
421 | |||
422 | if (GNUNET_OK != ret) | ||
423 | { | ||
424 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n", | ||
425 | ret); | ||
426 | return 1; | ||
427 | } | ||
428 | if (GNUNET_SYSERR == result) | ||
429 | { | ||
430 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); | ||
431 | return 1; | ||
432 | } | ||
433 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n"); | ||
434 | return 0; | ||
435 | } | ||