tests: Add tests for bind().
authorAndreas Schneider <asn@samba.org>
Wed, 21 May 2014 07:15:10 +0000 (09:15 +0200)
committerMichael Adam <obnox@samba.org>
Sat, 31 May 2014 10:30:53 +0000 (12:30 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
tests/CMakeLists.txt
tests/test_echo_tcp_bind.c [new file with mode: 0644]

index 6a5f97875e91809fb0f35742db65c37d8ea5b617..d3a4156e53d4cdb49556a00777bad56af848957b 100644 (file)
@@ -22,6 +22,7 @@ set(SWRAP_TESTS
     test_ioctl
     test_echo_tcp_socket
     test_echo_tcp_connect
+    test_echo_tcp_bind
     test_echo_tcp_socket_options
     test_echo_tcp_write_read
     test_echo_tcp_writev_readv
diff --git a/tests/test_echo_tcp_bind.c b/tests/test_echo_tcp_bind.c
new file mode 100644 (file)
index 0000000..6f811dc
--- /dev/null
@@ -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;
+}