From: Martin Willi Date: Thu, 27 Jun 2013 09:21:58 +0000 (+0200) Subject: unit-tests: add sync/async tests for UNIX/TCP streams and services X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7f24f3948f2b11324ddee61d5fd892999874a6b;p=thirdparty%2Fstrongswan.git unit-tests: add sync/async tests for UNIX/TCP streams and services --- diff --git a/src/libstrongswan/tests/Makefile.am b/src/libstrongswan/tests/Makefile.am index 0a28a6fc48..ca17c847f9 100644 --- a/src/libstrongswan/tests/Makefile.am +++ b/src/libstrongswan/tests/Makefile.am @@ -7,7 +7,7 @@ test_runner_SOURCES = \ test_linked_list.c test_enumerator.c test_linked_list_enumerator.c \ test_bio_reader.c test_bio_writer.c test_chunk.c test_enum.c test_hashtable.c \ test_identification.c test_threading.c test_utils.c test_vectors.c \ - test_watcher.c test_ecdsa.c test_rsa.c + test_watcher.c test_stream.c test_ecdsa.c test_rsa.c test_runner_CFLAGS = \ -I$(top_srcdir)/src/libstrongswan \ diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c index f3a2d57407..edcc15f7a8 100644 --- a/src/libstrongswan/tests/test_runner.c +++ b/src/libstrongswan/tests/test_runner.c @@ -83,6 +83,7 @@ int main() srunner_add_suite(sr, utils_suite_create()); srunner_add_suite(sr, vectors_suite_create()); srunner_add_suite(sr, watcher_suite_create()); + srunner_add_suite(sr, stream_suite_create()); if (lib->plugins->has_feature(lib->plugins, PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA))) { diff --git a/src/libstrongswan/tests/test_runner.h b/src/libstrongswan/tests/test_runner.h index 7a8f1c3abe..ee9c14ae2a 100644 --- a/src/libstrongswan/tests/test_runner.h +++ b/src/libstrongswan/tests/test_runner.h @@ -33,5 +33,6 @@ Suite *vectors_suite_create(); Suite *ecdsa_suite_create(); Suite *rsa_suite_create(); Suite *watcher_suite_create(); +Suite *stream_suite_create(); #endif /** TEST_RUNNER_H_ */ diff --git a/src/libstrongswan/tests/test_stream.c b/src/libstrongswan/tests/test_stream.c new file mode 100644 index 0000000000..f0d225c4ec --- /dev/null +++ b/src/libstrongswan/tests/test_stream.c @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2013 Martin Willi + * Copyright (C) 2013 revosec AG + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "test_suite.h" + +#include + +static char* services[] = { + "unix:///tmp/strongswan-test-service.sck", + "tcp://127.0.0.1:7766", + "tcp://[::1]:7766", +}; + +static char msg[] = "testmessage"; +static int msglen = 12; + +static bool servicing(void *data, stream_t *stream) +{ + char buf[64]; + ssize_t len; + + ck_assert(streq((char*)data, "test")); + len = stream->read(stream, buf, sizeof(buf), TRUE); + ck_assert_int_eq(len, msglen); + len = stream->write(stream, buf, len, TRUE); + ck_assert_int_eq(len, msglen); + + return FALSE; +} + +START_TEST(test_sync) +{ + char buf[64]; + stream_service_t *service; + stream_t *stream; + ssize_t len; + + lib->processor->set_threads(lib->processor, 8); + + service = lib->streams->create_service(lib->streams, services[_i], 1); + ck_assert(service != NULL); + service->on_accept(service, servicing, "test", JOB_PRIO_HIGH, 1); + + stream = lib->streams->connect(lib->streams, services[_i]); + ck_assert(stream != NULL); + len = stream->write(stream, msg, msglen, TRUE); + ck_assert_int_eq(len, msglen); + len = stream->read(stream, buf, sizeof(buf), TRUE); + ck_assert_int_eq(len, msglen); + ck_assert(streq(buf, msg)); + stream->destroy(stream); + + service->destroy(service); +} +END_TEST + +static bool on_write(void *data, stream_t *stream) +{ + ssize_t len; + + ck_assert(streq((char*)data, "test-write")); + len = stream->write(stream, msg, msglen, TRUE); + ck_assert_int_eq(len, msglen); + return FALSE; +} + +static bool read_done = FALSE; + +static bool on_read(void *data, stream_t *stream) +{ + ssize_t len; + char buf[64]; + + ck_assert(streq((char*)data, "test-read")); + len = stream->read(stream, buf, sizeof(buf), TRUE); + ck_assert_int_eq(len, msglen); + ck_assert(streq(buf, msg)); + read_done = TRUE; + return FALSE; +} + + +START_TEST(test_async) +{ + stream_service_t *service; + stream_t *stream; + + lib->processor->set_threads(lib->processor, 8); + + service = lib->streams->create_service(lib->streams, services[_i], 1); + ck_assert(service != NULL); + service->on_accept(service, servicing, "test", JOB_PRIO_HIGH, 0); + + stream = lib->streams->connect(lib->streams, services[_i]); + ck_assert(stream != NULL); + stream->on_write(stream, (stream_cb_t)on_write, "test-write"); + stream->on_read(stream, (stream_cb_t)on_read, "test-read"); + + while (!read_done) + { + usleep(1000); + } + stream->destroy(stream); + + service->destroy(service); +} +END_TEST + +Suite *stream_suite_create() +{ + Suite *s; + TCase *tc; + + s = suite_create("stream"); + + tc = tcase_create("sync"); + tcase_add_loop_test(tc, test_sync, 0, countof(services)); + suite_add_tcase(s, tc); + + tc = tcase_create("async"); + tcase_add_loop_test(tc, test_async, 0, countof(services)); + suite_add_tcase(s, tc); + + return s; +}