diff options
Diffstat (limited to 'src/stream/test_stream_local.c')
-rw-r--r-- | src/stream/test_stream_local.c | 176 |
1 files changed, 112 insertions, 64 deletions
diff --git a/src/stream/test_stream_local.c b/src/stream/test_stream_local.c index 2eeedeec5..2845a74c2 100644 --- a/src/stream/test_stream_local.c +++ b/src/stream/test_stream_local.c | |||
@@ -35,6 +35,11 @@ | |||
35 | #define VERBOSE 1 | 35 | #define VERBOSE 1 |
36 | 36 | ||
37 | /** | 37 | /** |
38 | * Number of peers | ||
39 | */ | ||
40 | #define NUM_PEERS 2 | ||
41 | |||
42 | /** | ||
38 | * Structure for holding peer's sockets and IO Handles | 43 | * Structure for holding peer's sockets and IO Handles |
39 | */ | 44 | */ |
40 | struct PeerData | 45 | struct PeerData |
@@ -65,43 +70,79 @@ struct PeerData | |||
65 | unsigned int bytes_read; | 70 | unsigned int bytes_read; |
66 | }; | 71 | }; |
67 | 72 | ||
68 | static struct GNUNET_OS_Process *arm_pid; | 73 | /** |
74 | * The current peer group | ||
75 | */ | ||
76 | static struct GNUNET_TESTING_PeerGroup *pg; | ||
77 | |||
78 | /** | ||
79 | * Peer 1 daemon | ||
80 | */ | ||
81 | static struct GNUNET_TESTING_Daemon *d1; | ||
82 | |||
83 | /** | ||
84 | * Peer 2 daemon | ||
85 | */ | ||
86 | static struct GNUNET_TESTING_Daemon *d2; | ||
87 | |||
69 | static struct PeerData peer1; | 88 | static struct PeerData peer1; |
70 | static struct PeerData peer2; | 89 | static struct PeerData peer2; |
71 | static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; | 90 | static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket; |
72 | static struct GNUNET_CONFIGURATION_Handle *config; | 91 | static struct GNUNET_CONFIGURATION_Handle *config; |
73 | 92 | ||
74 | static GNUNET_SCHEDULER_TaskIdentifier abort_task; | 93 | static GNUNET_SCHEDULER_TaskIdentifier abort_task; |
75 | static GNUNET_SCHEDULER_TaskIdentifier test_task; | ||
76 | static GNUNET_SCHEDULER_TaskIdentifier read_task; | 94 | static GNUNET_SCHEDULER_TaskIdentifier read_task; |
77 | 95 | ||
78 | static char *data = "ABCD"; | 96 | static char *data = "ABCD"; |
79 | static int result; | 97 | static int result; |
80 | 98 | ||
99 | |||
100 | /** | ||
101 | * Check whether peers successfully shut down. | ||
102 | */ | ||
103 | static void | ||
104 | shutdown_callback (void *cls, const char *emsg) | ||
105 | { | ||
106 | if (emsg != NULL) | ||
107 | { | ||
108 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
109 | "Shutdown of peers failed!\n"); | ||
110 | } | ||
111 | else | ||
112 | { | ||
113 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
114 | "All peers successfully shut down!\n"); | ||
115 | } | ||
116 | GNUNET_CONFIGURATION_destroy (config); | ||
117 | } | ||
118 | |||
119 | |||
81 | /** | 120 | /** |
82 | * Shutdown nicely | 121 | * Shutdown nicely |
83 | */ | 122 | */ |
84 | static void | 123 | static void |
85 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 124 | do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
86 | { | 125 | { |
87 | GNUNET_STREAM_close (peer1.socket); | 126 | if (NULL != peer1.socket) |
127 | GNUNET_STREAM_close (peer1.socket); | ||
88 | if (NULL != peer2.socket) | 128 | if (NULL != peer2.socket) |
89 | GNUNET_STREAM_close (peer2.socket); | 129 | GNUNET_STREAM_close (peer2.socket); |
130 | if (NULL != peer2_listen_socket) | ||
131 | GNUNET_STREAM_listen_close (peer2_listen_socket); | ||
132 | |||
90 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n"); | 133 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n"); |
91 | if (0 != abort_task) | 134 | if (0 != abort_task) |
92 | { | 135 | { |
93 | GNUNET_SCHEDULER_cancel (abort_task); | 136 | GNUNET_SCHEDULER_cancel (abort_task); |
94 | } | 137 | } |
95 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n"); | 138 | |
96 | if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) | ||
97 | { | ||
98 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
99 | } | ||
100 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n"); | 139 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n"); |
101 | /* Free the duplicated configuration */ | 140 | |
102 | GNUNET_CONFIGURATION_destroy (config); | 141 | GNUNET_TESTING_daemons_stop (pg, |
103 | GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); | 142 | GNUNET_TIME_relative_multiply |
104 | GNUNET_OS_process_close (arm_pid); | 143 | (GNUNET_TIME_UNIT_SECONDS, 5), |
144 | &shutdown_callback, | ||
145 | NULL); | ||
105 | } | 146 | } |
106 | 147 | ||
107 | 148 | ||
@@ -112,10 +153,6 @@ static void | |||
112 | do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 153 | do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
113 | { | 154 | { |
114 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n"); | 155 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n"); |
115 | if (0 != test_task) | ||
116 | { | ||
117 | GNUNET_SCHEDULER_cancel (test_task); | ||
118 | } | ||
119 | if (0 != read_task) | 156 | if (0 != read_task) |
120 | { | 157 | { |
121 | GNUNET_SCHEDULER_cancel (read_task); | 158 | GNUNET_SCHEDULER_cancel (read_task); |
@@ -317,7 +354,7 @@ stream_listen_cb (void *cls, | |||
317 | const struct GNUNET_PeerIdentity *initiator) | 354 | const struct GNUNET_PeerIdentity *initiator) |
318 | { | 355 | { |
319 | GNUNET_assert (NULL != socket); | 356 | GNUNET_assert (NULL != socket); |
320 | GNUNET_assert (NULL == initiator); /* Local peer=NULL? */ | 357 | GNUNET_assert (NULL != initiator); |
321 | GNUNET_assert (socket != peer1.socket); | 358 | GNUNET_assert (socket != peer1.socket); |
322 | 359 | ||
323 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 360 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
@@ -330,36 +367,47 @@ stream_listen_cb (void *cls, | |||
330 | 367 | ||
331 | 368 | ||
332 | /** | 369 | /** |
333 | * Testing function | 370 | * Callback to be called when testing peer group is ready |
334 | * | 371 | * |
335 | * @param cls NULL | 372 | * @param cls NULL |
336 | * @param tc the task context | 373 | * @param emsg NULL on success |
337 | */ | 374 | */ |
338 | static void | 375 | void |
339 | test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 376 | peergroup_ready (void *cls, const char *emsg) |
340 | { | 377 | { |
341 | struct GNUNET_PeerIdentity self; | 378 | if (NULL != emsg) |
342 | 379 | { | |
343 | test_task = GNUNET_SCHEDULER_NO_TASK; | 380 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
344 | /* Get our identity */ | 381 | "Starting peer group failed: %s\n", emsg); |
345 | GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config, | 382 | return; |
346 | &self)); | 383 | } |
384 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
385 | "Peer group is now read \n"); | ||
386 | |||
387 | GNUNET_assert (2 == GNUNET_TESTING_daemons_running (pg)); | ||
388 | |||
389 | d1 = GNUNET_TESTING_daemon_get (pg, 0); | ||
390 | GNUNET_assert (NULL != d1); | ||
391 | |||
392 | d2 = GNUNET_TESTING_daemon_get (pg, 1); | ||
393 | GNUNET_assert (NULL != d2); | ||
347 | 394 | ||
348 | peer2_listen_socket = GNUNET_STREAM_listen (config, | 395 | peer2_listen_socket = GNUNET_STREAM_listen (d2->cfg, |
349 | 10, /* App port */ | 396 | 10, /* App port */ |
350 | &stream_listen_cb, | 397 | &stream_listen_cb, |
351 | NULL); | 398 | NULL); |
352 | GNUNET_assert (NULL != peer2_listen_socket); | 399 | GNUNET_assert (NULL != peer2_listen_socket); |
353 | 400 | ||
354 | /* Connect to stream library */ | 401 | /* Connect to stream library */ |
355 | peer1.socket = GNUNET_STREAM_open (config, | 402 | peer1.socket = GNUNET_STREAM_open (d1->cfg, |
356 | &self, /* Null for local peer? */ | 403 | &d2->id, /* Null for local peer? */ |
357 | 10, /* App port */ | 404 | 10, /* App port */ |
358 | &stream_open_cb, | 405 | &stream_open_cb, |
359 | (void *) &peer1); | 406 | &peer1); |
360 | GNUNET_assert (NULL != peer1.socket); | 407 | GNUNET_assert (NULL != peer1.socket); |
361 | } | 408 | } |
362 | 409 | ||
410 | |||
363 | /** | 411 | /** |
364 | * Initialize framework and start test | 412 | * Initialize framework and start test |
365 | */ | 413 | */ |
@@ -367,30 +415,33 @@ static void | |||
367 | run (void *cls, char *const *args, const char *cfgfile, | 415 | run (void *cls, char *const *args, const char *cfgfile, |
368 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 416 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
369 | { | 417 | { |
370 | GNUNET_log_setup ("test_stream_local", | 418 | struct GNUNET_TESTING_Host *hosts; /* FIXME: free hosts (DLL) */ |
371 | #if VERBOSE | 419 | |
372 | "DEBUG", | 420 | /* GNUNET_log_setup ("test_stream_local", */ |
373 | #else | 421 | /* "DEBUG", */ |
374 | "WARNING", | 422 | /* NULL); */ |
375 | #endif | ||
376 | NULL); | ||
377 | /* Duplicate the configuration */ | ||
378 | config = GNUNET_CONFIGURATION_dup (cfg); | ||
379 | arm_pid = | ||
380 | GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm", | ||
381 | "gnunet-service-arm", | ||
382 | #if VERBOSE_ARM | ||
383 | "-L", "DEBUG", | ||
384 | #endif | ||
385 | "-c", "test_stream_local.conf", NULL); | ||
386 | |||
387 | abort_task = | ||
388 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
389 | (GNUNET_TIME_UNIT_SECONDS, 20), &do_abort, | ||
390 | NULL); | ||
391 | |||
392 | test_task = GNUNET_SCHEDULER_add_now (&test, NULL); | ||
393 | 423 | ||
424 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
425 | "Starting test\n"); | ||
426 | /* Duplicate the configuration */ | ||
427 | config = GNUNET_CONFIGURATION_dup (cfg); | ||
428 | |||
429 | hosts = GNUNET_TESTING_hosts_load (config); | ||
430 | |||
431 | pg = GNUNET_TESTING_peergroup_start (config, | ||
432 | 2, | ||
433 | GNUNET_TIME_relative_multiply | ||
434 | (GNUNET_TIME_UNIT_SECONDS, 3), | ||
435 | NULL, | ||
436 | &peergroup_ready, | ||
437 | NULL, | ||
438 | hosts); | ||
439 | GNUNET_assert (NULL != pg); | ||
440 | |||
441 | abort_task = | ||
442 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply | ||
443 | (GNUNET_TIME_UNIT_SECONDS, 40), &do_abort, | ||
444 | NULL); | ||
394 | } | 445 | } |
395 | 446 | ||
396 | /** | 447 | /** |
@@ -400,18 +451,15 @@ int main (int argc, char **argv) | |||
400 | { | 451 | { |
401 | int ret; | 452 | int ret; |
402 | 453 | ||
403 | char *const argv2[] = { "test-stream-local", | 454 | char *argv2[] = { "test-stream-local", |
404 | "-c", "test_stream_local.conf", | 455 | "-L", "DEBUG", |
405 | #if VERBOSE | 456 | "-c", "test_stream_local.conf", |
406 | "-L", "DEBUG", | 457 | NULL}; |
407 | #endif | ||
408 | NULL | ||
409 | }; | ||
410 | 458 | ||
411 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 459 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
412 | GNUNET_GETOPT_OPTION_END | 460 | GNUNET_GETOPT_OPTION_END |
413 | }; | 461 | }; |
414 | 462 | ||
415 | ret = | 463 | ret = |
416 | GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, | 464 | GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2, |
417 | "test-stream-local", "nohelp", options, &run, NULL); | 465 | "test-stream-local", "nohelp", options, &run, NULL); |
@@ -427,6 +475,6 @@ int main (int argc, char **argv) | |||
427 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); | 475 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n"); |
428 | return 1; | 476 | return 1; |
429 | } | 477 | } |
430 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n"); | 478 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test ok\n"); |
431 | return 0; | 479 | return 0; |
432 | } | 480 | } |