The branch, master has been updated via 76c7df9 tests: Add test_sendto_recvfrom_ipv6(). via fe78b28 torture: Add torture_setup_echo_srv_udp_ipv6(). via 88aeb65 torture_teardown_echo_srv via 0c4a784 tests: Use torture helpers in test_echo_udp_sendto_recvfrom. via c6c0669 tests: Add teardown functions to torture lib. via 1e12eb0 tests: Add a torture library with helpers. via 78cc4fb tests: Fix the help output of the echo server from 2b53d42 tests: Add test_echo_udp_sendto_recvfrom.
http://gitweb.samba.org/?p=socket_wrapper.git;a=shortlog;h=master - Log ----------------------------------------------------------------- commit 76c7df9fceb992e3d9de2f29d9dfb508f60df0de Author: Andreas Schneider <a...@cryptomilk.org> Date: Tue Dec 10 11:46:03 2013 +0100 tests: Add test_sendto_recvfrom_ipv6(). commit fe78b28f689201617bc8312378f3c7f59239eb1b Author: Andreas Schneider <a...@cryptomilk.org> Date: Tue Dec 10 11:38:08 2013 +0100 torture: Add torture_setup_echo_srv_udp_ipv6(). commit 88aeb652d94f7f951ca9cd41322202f7f02e8acb Author: Andreas Schneider <a...@cryptomilk.org> Date: Tue Dec 10 11:37:44 2013 +0100 torture_teardown_echo_srv commit 0c4a784047a6a8eda69d31e6db49a5904836e2de Author: Andreas Schneider <a...@cryptomilk.org> Date: Tue Dec 10 11:19:34 2013 +0100 tests: Use torture helpers in test_echo_udp_sendto_recvfrom. commit c6c0669a2a9c4141693a7cf1d8f5f35ee9bda7ac Author: Andreas Schneider <a...@cryptomilk.org> Date: Tue Dec 10 11:14:04 2013 +0100 tests: Add teardown functions to torture lib. commit 1e12eb0b3e4f974c33a2b628d0857e137fce8cfa Author: Andreas Schneider <a...@cryptomilk.org> Date: Tue Dec 10 11:03:12 2013 +0100 tests: Add a torture library with helpers. commit 78cc4fb76d14d4df313e412bf148312eea2c706b Author: Jakub Hrozek <jhro...@redhat.com> Date: Tue Dec 10 10:57:24 2013 +0100 tests: Fix the help output of the echo server ----------------------------------------------------------------------- Summary of changes: tests/CMakeLists.txt | 12 ++- tests/echo_srv.c | 11 ++- tests/test_echo_udp_sendto_recvfrom.c | 175 +++++++++++---------------------- tests/torture.c | 170 ++++++++++++++++++++++++++++++++ tests/torture.h | 26 +++++- 5 files changed, 272 insertions(+), 122 deletions(-) create mode 100644 tests/torture.c Changeset truncated at 500 lines: 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/echo_srv.c b/tests/echo_srv.c index 13e2bc3..32a03da 100644 --- a/tests/echo_srv.c +++ b/tests/echo_srv.c @@ -296,8 +296,15 @@ int main(int argc, char **argv) opts.daemon = true; break; default: /* '?' */ - fprintf(stderr, "Usage: %s [-p port] [-u] [-t] [--pid pidfile]\n", - argv[0]); + fprintf(stderr, "Usage: %s [-p port] [-u] [-t] [-b bind_addr] " \ + "[-D] [--pid pidfile]\n" + "-t makes the server listen on TCP\n" + "-u makes the server listen on UDP\n" + "-D tells the server to become a deamon and " \ + "write a PIDfile\n" + "The default port is 7, the default PIDfile is " \ + "echo_srv.pid in the current directory\n", + argv[0]); ret = 1; goto done; } diff --git a/tests/test_echo_udp_sendto_recvfrom.c b/tests/test_echo_udp_sendto_recvfrom.c index 9596e5e..7139656 100644 --- a/tests/test_echo_udp_sendto_recvfrom.c +++ b/tests/test_echo_udp_sendto_recvfrom.c @@ -11,138 +11,77 @@ #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> -#include <string.h> #include <stdlib.h> #include <stdio.h> -#include <signal.h> - -#include <sys/stat.h> -#include <fcntl.h> - -#include <unistd.h> - -#define ECHO_SRV_IP "127.0.0.10" -#define ECHO_SRV_PORT 7 - -#define ECHO_SRV_PIDFILE "echo_srv.pid" - -struct test_opts { - char *socket_wrapper_dir; - char *pidfile; -}; - -static void setup(void **state) +static void setup_echo_srv_udp_ipv4(void **state) { - char test_tmpdir[256]; - struct test_opts *o; - size_t len; - const char *p; - - o = malloc(sizeof(struct test_opts)); - assert_non_null(o); - - snprintf(test_tmpdir, sizeof(test_tmpdir), "/tmp/test_socket_wrapper_XXXXXX"); - - p = mkdtemp(test_tmpdir); - assert_non_null(p); - - o->socket_wrapper_dir = strdup(p); - assert_non_null(o->socket_wrapper_dir); - - len = strlen(p) + 1 + strlen(ECHO_SRV_PIDFILE) + 1; - - o->pidfile = malloc(len); - assert_non_null(o->pidfile); - - snprintf(o->pidfile, len, "%s/%s", p, ECHO_SRV_PIDFILE); - - setenv("SOCKET_WRAPPER_DIR", p, 1); - setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "21", 1); - - *state = o; + torture_setup_echo_srv_udp_ipv4(state); } -static void setup_echo_srv_udp(void **state) +static void setup_echo_srv_udp_ipv6(void **state) { - struct test_opts *o; - char start_echo_srv[1024] = {0}; - int rc; - - setup(state); - o = *state; - - snprintf(start_echo_srv, sizeof(start_echo_srv), - "%s/tests/echo_srv -b %s -D -u --pid %s", - BINARYDIR, ECHO_SRV_IP, o->pidfile); - - rc = system(start_echo_srv); - assert_int_equal(rc, 0); - - sleep(1); + torture_setup_echo_srv_udp_ipv6(state); } static void teardown(void **state) { - struct test_opts *o = *state; - char remove_cmd[1024] = {0}; + torture_teardown_echo_srv(state); +} + +static void test_sendto_recvfrom_ipv4(void **state) +{ + struct sockaddr_in sin; + socklen_t slen = sizeof(struct sockaddr_in); + ssize_t ret; int rc; + int i; + int s; (void) state; /* unused */ - snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", o->socket_wrapper_dir); - - rc = system(remove_cmd); - if (rc < 0) { - fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno)); - } - - free(o->socket_wrapper_dir); - free(o->pidfile); - free(o); -} + s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + assert_int_not_equal(s, -1); -static void teardown_echo_srv_udp(void **state) -{ - struct test_opts *o = *state; - char buf[8] = {0}; - long int tmp; - ssize_t rc; - pid_t pid; - int fd; - - /* read the pidfile */ - fd = open(o->pidfile, O_RDONLY); - if (fd < 0) { - goto done; - } + ZERO_STRUCT(sin); + sin.sin_family = AF_INET; + sin.sin_port = htons(TORTURE_ECHO_SRV_PORT); - rc = read(fd, buf, sizeof(buf)); - close(fd); - if (rc <= 0) { - goto done; - } + rc = inet_aton(TORTURE_ECHO_SRV_IPV4, &sin.sin_addr); + assert_int_equal(rc, 1); - buf[sizeof(buf) - 1] = '\0'; + for (i = 0; i < 10; i++) { + char send_buf[64] = {0}; + char recv_buf[64] = {0}; + struct sockaddr_in cli_in; + socklen_t clen; - tmp = strtol(buf, NULL, 10); - if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) { - goto done; - } + snprintf(send_buf, sizeof(send_buf), "packet.%d", i); - pid = (pid_t)(tmp & 0xFFFF); + ret = sendto(s, + send_buf, + sizeof(send_buf), + 0, + (struct sockaddr *)(void *)&sin, + slen); + assert_int_not_equal(ret, -1); - /* kill daemon */ - kill(pid, SIGTERM); + ret = recvfrom(s, + recv_buf, + sizeof(recv_buf), + 0, + (struct sockaddr *)&cli_in, + &clen); -done: - teardown(state); + assert_memory_equal(send_buf, recv_buf, sizeof(send_buf)); + } } -static void test_sendto_recvfrom_ipv4(void **state) +#ifdef HAVE_IPV6 +static void test_sendto_recvfrom_ipv6(void **state) { - struct sockaddr_in sin; - socklen_t slen = sizeof(struct sockaddr_in); + struct sockaddr_in6 sin6; + socklen_t slen = sizeof(struct sockaddr_in6); ssize_t ret; int rc; int i; @@ -150,20 +89,20 @@ static void test_sendto_recvfrom_ipv4(void **state) (void) state; /* unused */ - s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); assert_int_not_equal(s, -1); - ZERO_STRUCT(sin); - sin.sin_family = AF_INET; - sin.sin_port = htons(ECHO_SRV_PORT); + ZERO_STRUCT(sin6); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons(TORTURE_ECHO_SRV_PORT); - rc = inet_aton(ECHO_SRV_IP, &sin.sin_addr); + rc = inet_pton(AF_INET6, TORTURE_ECHO_SRV_IPV6, &sin6.sin6_addr); assert_int_equal(rc, 1); for (i = 0; i < 10; i++) { char send_buf[64] = {0}; char recv_buf[64] = {0}; - struct sockaddr_in cli_in; + struct sockaddr_in6 cli_in6; socklen_t clen; snprintf(send_buf, sizeof(send_buf), "packet.%d", i); @@ -172,7 +111,7 @@ static void test_sendto_recvfrom_ipv4(void **state) send_buf, sizeof(send_buf), 0, - (struct sockaddr *)(void *)&sin, + (struct sockaddr *)(void *)&sin6, slen); assert_int_not_equal(ret, -1); @@ -180,18 +119,22 @@ static void test_sendto_recvfrom_ipv4(void **state) recv_buf, sizeof(recv_buf), 0, - (struct sockaddr *)&cli_in, + (struct sockaddr *)&cli_in6, &clen); assert_memory_equal(send_buf, recv_buf, sizeof(send_buf)); } } +#endif int main(void) { int rc; const UnitTest tests[] = { - unit_test_setup_teardown(test_sendto_recvfrom_ipv4, setup_echo_srv_udp, teardown_echo_srv_udp), + unit_test_setup_teardown(test_sendto_recvfrom_ipv4, setup_echo_srv_udp_ipv4, teardown), +#ifdef HAVE_IPV6 + unit_test_setup_teardown(test_sendto_recvfrom_ipv6, setup_echo_srv_udp_ipv6, teardown), +#endif }; rc = run_tests(tests); diff --git a/tests/torture.c b/tests/torture.c new file mode 100644 index 0000000..e2bab8c --- /dev/null +++ b/tests/torture.c @@ -0,0 +1,170 @@ +/* + * Copyright (C) Andreas Schneider 2013 <a...@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 <errno.h> + +#include <sys/types.h> +#include <sys/stat.h> +#include <signal.h> +#include <fcntl.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" + +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; +} + +static void torture_setup_echo_srv_udp_ip(void **state, const char *ip) +{ + 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, ip, s->srv_pidfile); + + rc = system(start_echo_srv); + assert_int_equal(rc, 0); + + sleep(1); +} + +void torture_setup_echo_srv_udp_ipv4(void **state) +{ + torture_setup_echo_srv_udp_ip(state, TORTURE_ECHO_SRV_IPV4); +} + +void torture_setup_echo_srv_udp_ipv6(void **state) +{ + torture_setup_echo_srv_udp_ip(state, TORTURE_ECHO_SRV_IPV6); +} + +void torture_teardown_socket_dir(void **state) +{ + struct torture_state *s = *state; + char remove_cmd[1024] = {0}; + int rc; + + snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", s->socket_dir); + + rc = system(remove_cmd); + if (rc < 0) { + fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno)); + } + + free(s->socket_dir); + free(s->srv_pidfile); + free(s); +} + +void torture_teardown_echo_srv(void **state) +{ + struct torture_state *s = *state; + char buf[8] = {0}; + long int tmp; + ssize_t rc; + pid_t pid; + int fd; + + /* read the pidfile */ + fd = open(s->srv_pidfile, O_RDONLY); + if (fd < 0) { + goto done; + } + + rc = read(fd, buf, sizeof(buf)); + close(fd); + if (rc <= 0) { + goto done; + } + + buf[sizeof(buf) - 1] = '\0'; + + tmp = strtol(buf, NULL, 10); + if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) { + goto done; + } + + pid = (pid_t)(tmp & 0xFFFF); + + /* Make sure the daemon goes away! */ + kill(pid, SIGTERM); + + kill(pid, 0); + if (rc == 0) { + fprintf(stderr, + "WARNING the echo server is still running!\n"); + } + +done: + torture_teardown_socket_dir(state); +} diff --git a/tests/torture.h b/tests/torture.h index 2af5aca..be2556f 100644 --- a/tests/torture.h +++ b/tests/torture.h @@ -31,15 +31,37 @@ * 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_IPV4 "127.0.0.10" +/* socket wrapper IPv6 prefix FD00::5357:5Fxx */ -- Socket Wrapper Repository