diff options
| author | Andreas Schneider <asn@cryptomilk.org> | 2013-12-10 11:03:12 +0100 |
|---|---|---|
| committer | Andreas Schneider <asn@cryptomilk.org> | 2013-12-10 14:23:26 +0100 |
| commit | 1e12eb0b3e4f974c33a2b628d0857e137fce8cfa (patch) | |
| tree | 3ebe5ec1939f831479e684b2c75bec8d142c1b09 | |
| parent | 78cc4fb76d14d4df313e412bf148312eea2c706b (diff) | |
| download | socket_wrapper-1e12eb0b3e4f974c33a2b628d0857e137fce8cfa.tar.gz socket_wrapper-1e12eb0b3e4f974c33a2b628d0857e137fce8cfa.tar.xz socket_wrapper-1e12eb0b3e4f974c33a2b628d0857e137fce8cfa.zip | |
tests: Add a torture library with helpers.
| -rw-r--r-- | tests/CMakeLists.txt | 12 | ||||
| -rw-r--r-- | tests/torture.c | 94 | ||||
| -rw-r--r-- | tests/torture.h | 19 |
3 files changed, 121 insertions, 4 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 49cd62a..1154561 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,13 +6,21 @@ include_directories( ${CMOCKA_INCLUDE_DIR} ) +set(TORTURE_LIBRARY torture) + +# RFC862 echo server add_executable(echo_srv echo_srv.c) -add_cmocka_test(testsuite testsuite.c ${CMOCKA_LIBRARY} ${SWRAP_REQUIRED_LIBRARIES}) -add_cmocka_test(test_echo_udp_sendto_recvfrom test_echo_udp_sendto_recvfrom.c ${CMOCKA_LIBRARY} ${SWRAP_REQUIRED_LIBRARIES}) +add_library(${TORTURE_LIBRARY} STATIC torture.c) +target_link_libraries(${TORTURE_LIBRARY} + ${CMOCKA_LIBRARY} + ${SWRAP_REQUIRED_LIBRARIES}) set(SWRAP_TESTS testsuite test_echo_udp_sendto_recvfrom) + foreach(_SWRAP_TEST ${SWRAP_TESTS}) + add_cmocka_test(${_SWRAP_TEST} ${_SWRAP_TEST}.c ${TORTURE_LIBRARY}) + if (OSX) set_property( TEST diff --git a/tests/torture.c b/tests/torture.c new file mode 100644 index 0000000..b1a0f8c --- /dev/null +++ b/tests/torture.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) Andreas Schneider 2013 <asn@samba.org> + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the author nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "config.h" + +#include "torture.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define TORTURE_SOCKET_DIR "/tmp/test_socket_wrapper_XXXXXX" +#define TORTURE_ECHO_SRV_PIDFILE "echo_srv.pid" +#define TORTURE_ECHO_SRV_IPV4 "127.0.0.10" +#define TORTURE_ECHO_SRV_IPV6 "::10" + +void torture_setup_socket_dir(void **state) +{ + struct torture_state *s; + const char *p; + size_t len; + + s = malloc(sizeof(struct torture_state)); + assert_non_null(s); + + s->socket_dir = strdup(TORTURE_SOCKET_DIR); + assert_non_null(s->socket_dir); + + p = mkdtemp(s->socket_dir); + assert_non_null(p); + + len = strlen(p) + 1 + strlen(TORTURE_ECHO_SRV_PIDFILE) + 1; + + s->srv_pidfile = malloc(len); + assert_non_null(s->srv_pidfile); + + snprintf(s->srv_pidfile, len, "%s/%s", p, TORTURE_ECHO_SRV_PIDFILE); + + setenv("SOCKET_WRAPPER_DIR", p, 1); + setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "21", 1); + + *state = s; +} + +void torture_setup_echo_srv_udp_ipv4(void **state) +{ + struct torture_state *s; + char start_echo_srv[1024] = {0}; + int rc; + + torture_setup_socket_dir(state); + + s = *state; + + snprintf(start_echo_srv, sizeof(start_echo_srv), + "%s/tests/echo_srv -b %s -D -u --pid %s", + BINARYDIR, TORTURE_ECHO_SRV_IPV4, s->srv_pidfile); + + rc = system(start_echo_srv); + assert_int_equal(rc, 0); + + sleep(1); +} diff --git a/tests/torture.h b/tests/torture.h index 2af5aca..2bc0cf7 100644 --- a/tests/torture.h +++ b/tests/torture.h @@ -31,15 +31,30 @@ * SUCH DAMAGE. */ -#include "config.h" - #ifndef _TORTURE_H #define _TORTURE_H +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + #include <string.h> +#define TORTURE_ECHO_SRV_PORT 7 + +struct torture_state { + char *socket_dir; + char *srv_pidfile; +}; + #ifndef ZERO_STRUCT #define ZERO_STRUCT(x) memset((char *)&(x), 0, sizeof(x)) #endif +void torture_setup_socket_dir(void **state); +void torture_setup_echo_srv_udp_ipv4(void **state); + #endif /* _TORTURE_H */ |
