aboutsummaryrefslogtreecommitdiff
path: root/src/stream/test_stream_local.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stream/test_stream_local.c')
-rw-r--r--src/stream/test_stream_local.c153
1 files changed, 34 insertions, 119 deletions
diff --git a/src/stream/test_stream_local.c b/src/stream/test_stream_local.c
index bf0584015..e660d77d7 100644
--- a/src/stream/test_stream_local.c
+++ b/src/stream/test_stream_local.c
@@ -30,10 +30,11 @@
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31#include "gnunet_mesh_service.h" 31#include "gnunet_mesh_service.h"
32#include "gnunet_stream_lib.h" 32#include "gnunet_stream_lib.h"
33#include "gnunet_testing_lib.h" 33#include "gnunet_testing_lib-new.h"
34
35#define VERBOSE 1
36 34
35/**
36 * Relative seconds shorthand
37 */
37#define TIME_REL_SECS(sec) \ 38#define TIME_REL_SECS(sec) \
38 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec) 39 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
39 40
@@ -69,15 +70,14 @@ struct PeerData
69 unsigned int bytes_read; 70 unsigned int bytes_read;
70}; 71};
71 72
72static struct GNUNET_OS_Process *arm_pid;
73static struct PeerData peer1; 73static struct PeerData peer1;
74static struct PeerData peer2; 74static struct PeerData peer2;
75static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; 75static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
76static struct GNUNET_CONFIGURATION_Handle *config_peer1; 76static const struct GNUNET_CONFIGURATION_Handle *config;
77static struct GNUNET_CONFIGURATION_Handle *config_peer2; 77static struct GNUNET_TESTING_Peer *self;
78static struct GNUNET_PeerIdentity self_id;
78 79
79static GNUNET_SCHEDULER_TaskIdentifier abort_task; 80static GNUNET_SCHEDULER_TaskIdentifier abort_task;
80static GNUNET_SCHEDULER_TaskIdentifier test_task;
81 81
82static char *data = "ABCD"; 82static char *data = "ABCD";
83static int result; 83static int result;
@@ -85,6 +85,7 @@ static int result;
85static int writing_success; 85static int writing_success;
86static int reading_success; 86static int reading_success;
87 87
88
88/** 89/**
89 * Input processor 90 * Input processor
90 * 91 *
@@ -121,6 +122,7 @@ stream_read_task (void *cls,
121 GNUNET_assert (NULL != peer->io_read_handle); 122 GNUNET_assert (NULL != peer->io_read_handle);
122} 123}
123 124
125
124/** 126/**
125 * The write completion function; called upon writing some data to stream or 127 * The write completion function; called upon writing some data to stream or
126 * upon error 128 * upon error
@@ -159,6 +161,7 @@ stream_write_task (void *cls,
159 GNUNET_assert (NULL != peer->io_write_handle); 161 GNUNET_assert (NULL != peer->io_write_handle);
160 } 162 }
161 163
164
162/** 165/**
163 * Shutdown nicely 166 * Shutdown nicely
164 */ 167 */
@@ -175,17 +178,6 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
175 { 178 {
176 GNUNET_SCHEDULER_cancel (abort_task); 179 GNUNET_SCHEDULER_cancel (abort_task);
177 } 180 }
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
179 if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
180 {
181 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
182 }
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
184 /* Free the duplicated configuration */
185 GNUNET_CONFIGURATION_destroy (config_peer1);
186 GNUNET_CONFIGURATION_destroy (config_peer2);
187 GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
188 GNUNET_OS_process_destroy (arm_pid);
189} 181}
190 182
191 183
@@ -196,11 +188,6 @@ static void
196do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 188do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
197{ 189{
198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n"); 190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
199 if (0 != test_task)
200 {
201 GNUNET_SCHEDULER_cancel (test_task);
202 }
203
204 result = GNUNET_SYSERR; 191 result = GNUNET_SYSERR;
205 abort_task = 0; 192 abort_task = 0;
206 do_shutdown (cls, tc); 193 do_shutdown (cls, tc);
@@ -225,7 +212,6 @@ write_completion (void *cls,
225 GNUNET_assert (GNUNET_STREAM_OK == status); 212 GNUNET_assert (GNUNET_STREAM_OK == status);
226 GNUNET_assert (size <= strlen (data)); 213 GNUNET_assert (size <= strlen (data));
227 peer->bytes_wrote += size; 214 peer->bytes_wrote += size;
228
229 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */ 215 if (peer->bytes_wrote < strlen(data)) /* Have more data to send */
230 { 216 {
231 GNUNET_SCHEDULER_add_now (&stream_write_task, peer); 217 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
@@ -234,8 +220,7 @@ write_completion (void *cls,
234 { 220 {
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Writing completed\n"); 222 "Writing completed\n");
237 223 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
238 if (&peer1 == peer) /* Peer1 has finished writing; should read now */
239 { 224 {
240 peer->bytes_read = 0; 225 peer->bytes_read = 0;
241 GNUNET_SCHEDULER_add_now (&stream_read_task, peer); 226 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
@@ -265,7 +250,6 @@ stream_open_cb (void *cls,
265 GNUNET_assert (&peer1 == peer); 250 GNUNET_assert (&peer1 == peer);
266 GNUNET_assert (socket == peer1.socket); 251 GNUNET_assert (socket == peer1.socket);
267 GNUNET_assert (socket == peer->socket); 252 GNUNET_assert (socket == peer->socket);
268
269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n"); 253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stream established from peer1\n");
270 peer->bytes_wrote = 0; 254 peer->bytes_wrote = 0;
271 GNUNET_SCHEDULER_add_now (&stream_write_task, peer); 255 GNUNET_SCHEDULER_add_now (&stream_write_task, peer);
@@ -295,8 +279,7 @@ input_processor (void *cls,
295 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read, 279 GNUNET_assert (0 == strncmp ((const char *) data + peer->bytes_read,
296 (const char *) input_data, 280 (const char *) input_data,
297 size)); 281 size));
298 peer->bytes_read += size; 282 peer->bytes_read += size;
299
300 if (peer->bytes_read < strlen (data)) 283 if (peer->bytes_read < strlen (data))
301 { 284 {
302 GNUNET_SCHEDULER_add_now (&stream_read_task, peer); 285 GNUNET_SCHEDULER_add_now (&stream_read_task, peer);
@@ -335,22 +318,15 @@ stream_listen_cb (void *cls,
335 const struct GNUNET_PeerIdentity *initiator) 318 const struct GNUNET_PeerIdentity *initiator)
336{ 319{
337 struct PeerData *peer=cls; 320 struct PeerData *peer=cls;
338 struct GNUNET_PeerIdentity self;
339 321
340 GNUNET_assert (NULL != socket); 322 GNUNET_assert (NULL != socket);
341 GNUNET_assert (socket != peer1.socket); 323 GNUNET_assert (socket != peer1.socket);
342 GNUNET_assert (&peer2 == peer); 324 GNUNET_assert (&peer2 == peer);
343 325 GNUNET_assert (0 == memcmp (&self_id,
344 /* Get our identity */
345 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config_peer1,
346 &self));
347 GNUNET_assert (0 == memcmp (&self,
348 initiator, 326 initiator,
349 sizeof (struct GNUNET_PeerIdentity))); 327 sizeof (struct GNUNET_PeerIdentity)));
350
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Peer connected: %s\n", GNUNET_i2s(initiator)); 329 "Peer connected: %s\n", GNUNET_i2s(initiator));
353
354 peer->socket = socket; 330 peer->socket = socket;
355 peer->bytes_read = 0; 331 peer->bytes_read = 0;
356 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2); 332 GNUNET_SCHEDULER_add_now (&stream_read_task, &peer2);
@@ -368,14 +344,11 @@ static void
368stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 344stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
369{ 345{
370 struct PeerData *peer = cls; 346 struct PeerData *peer = cls;
371 struct GNUNET_PeerIdentity self;
372 347
373 GNUNET_assert (&peer1 == peer); 348 GNUNET_assert (&peer1 == peer);
374 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config_peer1,
375 &self));
376 /* Connect to stream library */ 349 /* Connect to stream library */
377 peer->socket = GNUNET_STREAM_open (config_peer1, 350 peer->socket = GNUNET_STREAM_open (config,
378 &self, /* Null for local peer? */ 351 &self_id,
379 10, /* App port */ 352 10, /* App port */
380 &stream_open_cb, 353 &stream_open_cb,
381 &peer1, 354 &peer1,
@@ -385,61 +358,27 @@ stream_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
385 358
386 359
387/** 360/**
388 * Testing function 361 * Initialize framework and start test
389 *
390 * @param cls NULL
391 * @param tc the task context
392 */ 362 */
393static void 363static void
394test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) 364run (void *cls,
365 const struct GNUNET_CONFIGURATION_Handle *cfg,
366 struct GNUNET_TESTING_Peer *peer)
395{ 367{
396 struct GNUNET_PeerIdentity self; 368 config = cfg;
397 369 self = peer;
398 test_task = GNUNET_SCHEDULER_NO_TASK; 370 GNUNET_TESTING_peer_get_identity (peer, &self_id);
399 /* Get our identity */ 371 peer2_listen_socket = GNUNET_STREAM_listen (config,
400 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config_peer1,
401 &self));
402
403 peer2_listen_socket = GNUNET_STREAM_listen (config_peer2,
404 10, /* App port */ 372 10, /* App port */
405 &stream_listen_cb, 373 &stream_listen_cb,
406 &peer2, 374 &peer2,
407 GNUNET_STREAM_OPTION_END); 375 GNUNET_STREAM_OPTION_END);
408 GNUNET_assert (NULL != peer2_listen_socket); 376 GNUNET_assert (NULL != peer2_listen_socket);
409 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1); 377 GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &stream_connect, &peer1);
410} 378 abort_task =
411 379 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
412/** 380 (GNUNET_TIME_UNIT_SECONDS, 30), &do_abort,
413 * Initialize framework and start test 381 NULL);
414 */
415static void
416run (void *cls, char *const *args, const char *cfgfile,
417 const struct GNUNET_CONFIGURATION_Handle *cfg)
418{
419 GNUNET_log_setup ("test_stream_local",
420#if VERBOSE
421 "DEBUG",
422#else
423 "WARNING",
424#endif
425 NULL);
426 /* Duplicate the configuration */
427 config_peer1 = GNUNET_CONFIGURATION_dup (cfg);
428 config_peer2 = GNUNET_CONFIGURATION_dup (cfg);
429 arm_pid =
430 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
431 "gnunet-service-arm",
432#if VERBOSE_ARM
433 "-L", "DEBUG",
434#endif
435 "-c", "test_stream_local.conf", NULL);
436
437 abort_task =
438 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
439 (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
440 NULL);
441
442 test_task = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS(2), &test, NULL);
443} 382}
444 383
445/** 384/**
@@ -447,35 +386,11 @@ run (void *cls, char *const *args, const char *cfgfile,
447 */ 386 */
448int main (int argc, char **argv) 387int main (int argc, char **argv)
449{ 388{
450 int ret; 389 if (0 != GNUNET_TESTING_peer_run ("test_stream_local",
451 390 "test_stream_local.conf",
452 char *const argv2[] = { "test-stream-local", 391 &run, NULL))
453 "-c", "test_stream_local.conf",
454#if VERBOSE
455 "-L", "DEBUG",
456#endif
457 NULL
458 };
459
460 struct GNUNET_GETOPT_CommandLineOption options[] = {
461 GNUNET_GETOPT_OPTION_END
462 };
463
464 ret =
465 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
466 "test-stream-local", "nohelp", options, &run, NULL);
467
468 if (GNUNET_OK != ret)
469 {
470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
471 ret);
472 return 1; 392 return 1;
473 } 393 return (GNUNET_SYSERR == result) ? 1 : 0;
474 if (GNUNET_SYSERR == result)
475 {
476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
477 return 1;
478 }
479 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n");
480 return 0;
481} 394}
395
396/* end of test_stream_local.c */