tests: Add test that getsockname is correct after socket().
authorAndreas Schneider <asn@samba.org>
Tue, 27 May 2014 18:53:51 +0000 (20:53 +0200)
committerMichael Adam <obnox@samba.org>
Sat, 31 May 2014 10:30:45 +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_socket.c [new file with mode: 0644]

index 287539b46f4fef2d986242d49e0325b1fc780b4c..6a5f97875e91809fb0f35742db65c37d8ea5b617 100644 (file)
@@ -20,6 +20,7 @@ target_link_libraries(${TORTURE_LIBRARY}
 
 set(SWRAP_TESTS
     test_ioctl
+    test_echo_tcp_socket
     test_echo_tcp_connect
     test_echo_tcp_socket_options
     test_echo_tcp_write_read
diff --git a/tests/test_echo_tcp_socket.c b/tests/test_echo_tcp_socket.c
new file mode 100644 (file)
index 0000000..04f15aa
--- /dev/null
@@ -0,0 +1,69 @@
+#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 test_socket_getsockname(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_int_not_equal(s, -1);
+
+       ZERO_STRUCT(sin);
+       rc = getsockname(s, (struct sockaddr *)&sin, &slen);
+       assert_return_code(rc, errno);
+       assert_int_equal(sin.sin_family, AF_INET);
+}
+
+#ifdef HAVE_IPV6
+static void test_socket_getsockname6(void **state)
+{
+       struct sockaddr_in6 sin6;
+       socklen_t slen = sizeof(struct sockaddr_in6);
+       int rc;
+       int s;
+
+       (void) state; /* unused */
+
+       s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
+       assert_int_not_equal(s, -1);
+
+       ZERO_STRUCT(sin6);
+       rc = getsockname(s, (struct sockaddr *)&sin6, &slen);
+       assert_return_code(rc, errno);
+       assert_int_equal(sin6.sin6_family, AF_INET6);
+}
+#endif
+
+int main(void) {
+       int rc;
+
+       const UnitTest tests[] = {
+               unit_test(test_socket_getsockname),
+#ifdef HAVE_IPV6
+               unit_test(test_socket_getsockname6),
+#endif
+       };
+
+       rc = run_tests(tests);
+
+       return rc;
+}