diff options
author | Andreas Schneider <asn@samba.org> | 2014-05-21 09:15:10 +0200 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2014-05-31 12:30:53 +0200 |
commit | b124fcb2dfb6193f198bd47891c3286e527bd530 (patch) | |
tree | 5a37bbaf67b34180fae0a66e9b627d91b119072a /tests/test_echo_tcp_bind.c | |
parent | 45eb7c9070fdd99b63fe01d31e26616d8f047f71 (diff) | |
download | socket_wrapper-b124fcb2dfb6193f198bd47891c3286e527bd530.tar.gz socket_wrapper-b124fcb2dfb6193f198bd47891c3286e527bd530.tar.xz socket_wrapper-b124fcb2dfb6193f198bd47891c3286e527bd530.zip |
tests: Add tests for bind().
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
Diffstat (limited to 'tests/test_echo_tcp_bind.c')
-rw-r--r-- | tests/test_echo_tcp_bind.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/tests/test_echo_tcp_bind.c b/tests/test_echo_tcp_bind.c new file mode 100644 index 0000000..6f811dc --- /dev/null +++ b/tests/test_echo_tcp_bind.c @@ -0,0 +1,110 @@ +#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_bind_ipv4(void **state) +{ + struct sockaddr_in sin; + socklen_t slen = sizeof(struct sockaddr_in); + int rc; + int s; + + (void) state; /* unused */ + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + assert_return_code(s, errno); + + ZERO_STRUCT(sin); + sin.sin_family = AF_INET; + + rc = inet_pton(AF_INET, "127.0.0.20", &sin.sin_addr); + assert_int_equal(rc, 1); + + rc = bind(s, (struct sockaddr *)&sin, slen); + assert_return_code(rc, errno); + + ZERO_STRUCT(sin); + sin.sin_family = AF_INET; + sin.sin_port = htons(torture_server_port()); + rc = inet_pton(AF_INET, torture_server_address(AF_INET), &sin.sin_addr); + assert_int_equal(rc, 1); + + rc = connect(s, (struct sockaddr *)&sin, slen); + assert_return_code(rc, errno); + + close(s); +} + +#ifdef HAVE_IPV6 +static void test_bind_on_ipv6_sock(void **state) +{ + struct sockaddr_in sin; + socklen_t slen = sizeof(struct sockaddr_in); + int rc; + int s; + + (void) state; /* unused */ + + s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + assert_return_code(s, errno); + + ZERO_STRUCT(sin); + sin.sin_family = AF_INET; + + rc = inet_pton(AF_INET, "127.0.0.20", &sin.sin_addr); + assert_int_equal(rc, 1); + + rc = bind(s, (struct sockaddr *)&sin, slen); + assert_int_equal(rc, -1); + assert_int_equal(errno, EAFNOSUPPORT); + + close(s); +} +#endif /* HAVE_IPV6 */ + +int main(void) { + int rc; + + const UnitTest tests[] = { + unit_test_setup_teardown(test_bind_ipv4, + setup_echo_srv_tcp_ipv4, + teardown), +#ifdef HAVE_IPV6 + unit_test_setup_teardown(test_bind_on_ipv6_sock, + setup_echo_srv_tcp_ipv6, + teardown), +#endif /* HAVE_IPV6 */ + }; + + rc = run_tests(tests); + + return rc; +} |