diff options
author | Andreas Schneider <asn@samba.org> | 2013-12-17 14:48:38 +0100 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2013-12-17 14:48:38 +0100 |
commit | 3c3b7f69d283fc6407a146b412df00517e500b01 (patch) | |
tree | ae767a3a9c23efb1fdfcb06b60228f31b4069ef5 /tests | |
parent | 0d87b9e18aa400844c18639e575d95b2f98266d5 (diff) | |
download | socket_wrapper-3c3b7f69d283fc6407a146b412df00517e500b01.tar.gz socket_wrapper-3c3b7f69d283fc6407a146b412df00517e500b01.tar.xz socket_wrapper-3c3b7f69d283fc6407a146b412df00517e500b01.zip |
tests: Add test_echo_tcp_write_read.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/test_echo_tcp_write_read.c | 140 |
2 files changed, 141 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ec5f238..63b0046 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,7 +17,7 @@ target_link_libraries(${TORTURE_LIBRARY} ${CMOCKA_LIBRARY} ${SWRAP_REQUIRED_LIBRARIES}) -set(SWRAP_TESTS test_ioctl test_echo_udp_sendto_recvfrom test_echo_udp_send_recv) +set(SWRAP_TESTS test_ioctl test_echo_udp_sendto_recvfrom test_echo_udp_send_recv test_echo_tcp_write_read) foreach(_SWRAP_TEST ${SWRAP_TESTS}) add_cmocka_test(${_SWRAP_TEST} ${_SWRAP_TEST}.c ${TORTURE_LIBRARY}) diff --git a/tests/test_echo_tcp_write_read.c b/tests/test_echo_tcp_write_read.c new file mode 100644 index 0000000..389e400 --- /dev/null +++ b/tests/test_echo_tcp_write_read.c @@ -0,0 +1,140 @@ +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#include "config.h" +#include "torture.h" + +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> + +static void setup_echo_srv_tcp_ipv4(void **state) +{ + torture_setup_echo_srv_tcp_ipv4(state); +} + +static void setup_echo_srv_tcp_ipv6(void **state) +{ + torture_setup_echo_srv_tcp_ipv6(state); +} + +static void teardown(void **state) +{ + torture_teardown_echo_srv(state); +} + +static void test_write_read_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 */ + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + assert_int_not_equal(s, -1); + + ZERO_STRUCT(sin); + sin.sin_family = AF_INET; + sin.sin_port = htons(TORTURE_ECHO_SRV_PORT); + + rc = inet_pton(sin.sin_family, TORTURE_ECHO_SRV_IPV4, &sin.sin_addr); + assert_int_equal(rc, 1); + + rc = connect(s, (struct sockaddr *)&sin, slen); + assert_int_equal(rc, 0); + + for (i = 0; i < 10; i++) { + char send_buf[64] = {0}; + char recv_buf[64] = {0}; + + snprintf(send_buf, sizeof(send_buf), "packet.%d", i); + + ret = write(s, + send_buf, + sizeof(send_buf)); + assert_int_not_equal(ret, -1); + + ret = read(s, + recv_buf, + sizeof(recv_buf)); + assert_int_not_equal(ret, -1); + + assert_memory_equal(send_buf, recv_buf, sizeof(send_buf)); + } + + close(s); +} + +#ifdef HAVE_IPV6 +static void test_write_read_ipv6(void **state) +{ + struct sockaddr_in6 sin6; + socklen_t slen = sizeof(struct sockaddr_in6); + ssize_t ret; + int rc; + int i; + int s; + + (void) state; /* unused */ + + s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + assert_int_not_equal(s, -1); + + ZERO_STRUCT(sin6); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons(TORTURE_ECHO_SRV_PORT); + + rc = inet_pton(AF_INET6, TORTURE_ECHO_SRV_IPV6, &sin6.sin6_addr); + assert_int_equal(rc, 1); + + rc = connect(s, (struct sockaddr *)&sin6, slen); + assert_int_equal(rc, 0); + + for (i = 0; i < 10; i++) { + char send_buf[64] = {0}; + char recv_buf[64] = {0}; + + snprintf(send_buf, sizeof(send_buf), "packet.%d", i); + + ret = write(s, + send_buf, + sizeof(send_buf)); + assert_int_not_equal(ret, -1); + + ret = read(s, + recv_buf, + sizeof(recv_buf)); + assert_int_not_equal(ret, -1); + + assert_memory_equal(send_buf, recv_buf, sizeof(send_buf)); + } + + close(s); +} +#endif + +int main(void) { + int rc; + + const UnitTest tests[] = { + unit_test_setup_teardown(test_write_read_ipv4, setup_echo_srv_tcp_ipv4, teardown), +#ifdef HAVE_IPV6 + unit_test_setup_teardown(test_write_read_ipv6, setup_echo_srv_tcp_ipv6, teardown), +#endif + }; + + rc = run_tests(tests); + + return rc; +} |