Blame SOURCES/0034-TEST-Unit-test-for-create_pipe_fd.patch

905b4d
From 92cf06af1c5ee8be33722e76c79a3c290b9f67c8 Mon Sep 17 00:00:00 2001
905b4d
From: Jakub Hrozek <jhrozek@redhat.com>
905b4d
Date: Fri, 17 Oct 2014 16:44:05 +0200
905b4d
Subject: [PATCH 34/46] TEST: Unit test for create_pipe_fd
905b4d
905b4d
Reviewed-by: Pavel Reichl <preichl@redhat.com>
905b4d
Reviewed-by: Simo Sorce <simo@redhat.com>
905b4d
(cherry picked from commit 4a5cced91df68a85ef0b30de8efe104c8a0aab7a)
905b4d
---
905b4d
 src/tests/cwrap/test_responder_common.c | 91 +++++++++++++++++++++++++++++++++
905b4d
 1 file changed, 91 insertions(+)
905b4d
905b4d
diff --git a/src/tests/cwrap/test_responder_common.c b/src/tests/cwrap/test_responder_common.c
905b4d
index 23dcf753f184cdecaf39c73c6e9be0e23e6df968..7e3f2e025a0dce15cc93e560a42bb566eff9fb30 100644
905b4d
--- a/src/tests/cwrap/test_responder_common.c
905b4d
+++ b/src/tests/cwrap/test_responder_common.c
905b4d
@@ -23,6 +23,7 @@
905b4d
 #include <sys/types.h>
905b4d
 #include <sys/stat.h>
905b4d
 #include <fcntl.h>
905b4d
+#include <talloc.h>
905b4d
 
905b4d
 #include <popt.h>
905b4d
 #include "util/util.h"
905b4d
@@ -105,6 +106,93 @@ void test_csv_to_uid_list_neg(void **state)
905b4d
     talloc_free(tmp_ctx);
905b4d
 }
905b4d
 
905b4d
+struct create_pipe_ctx {
905b4d
+    int fd;
905b4d
+    const char *sock_name;
905b4d
+};
905b4d
+
905b4d
+void test_create_pipe_fd_setup(void **state)
905b4d
+{
905b4d
+    struct create_pipe_ctx *ctx;
905b4d
+
905b4d
+    ctx = talloc(global_talloc_context, struct create_pipe_ctx);
905b4d
+    assert_non_null(ctx);
905b4d
+    ctx->fd = -1;
905b4d
+
905b4d
+    *state = ctx;
905b4d
+}
905b4d
+
905b4d
+void check_sock_properties(struct create_pipe_ctx *ctx, mode_t mode)
905b4d
+{
905b4d
+    int ret;
905b4d
+    int optval;
905b4d
+    socklen_t optlen;
905b4d
+    struct stat sbuf;
905b4d
+
905b4d
+    /* Check existence of the file and the permissions */
905b4d
+    ret = stat(ctx->sock_name, &sbuf);
905b4d
+    assert_int_equal(ret, 0);
905b4d
+    assert_true(S_ISSOCK(sbuf.st_mode));
905b4d
+    assert_true((sbuf.st_mode & ~S_IFMT) == mode);
905b4d
+
905b4d
+    /* Check it's a UNIX socket */
905b4d
+    optlen = sizeof(optval);
905b4d
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_DOMAIN, &optval, &optlen);
905b4d
+    assert_int_equal(ret, 0);
905b4d
+    assert_int_equal(optval, AF_UNIX);
905b4d
+
905b4d
+    optlen = sizeof(optval);
905b4d
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_TYPE, &optval, &optlen);
905b4d
+    assert_int_equal(ret, 0);
905b4d
+    assert_int_equal(optval, SOCK_STREAM);
905b4d
+
905b4d
+    /* Make sure this is a listening socket */
905b4d
+    optlen = sizeof(optval);
905b4d
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_ACCEPTCONN, &optval, &optlen);
905b4d
+    assert_int_equal(ret, 0);
905b4d
+    assert_int_equal(optval, 1);
905b4d
+
905b4d
+    /* Check the right protocol */
905b4d
+    optlen = sizeof(optval);
905b4d
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_PROTOCOL, &optval, &optlen);
905b4d
+    assert_int_equal(ret, 0);
905b4d
+    assert_int_equal(optval, 0);
905b4d
+
905b4d
+}
905b4d
+
905b4d
+void test_create_pipe_fd(void **state)
905b4d
+{
905b4d
+    int ret;
905b4d
+    struct create_pipe_ctx *ctx;
905b4d
+
905b4d
+    ctx = talloc_get_type(*state, struct create_pipe_ctx);
905b4d
+
905b4d
+    ctx->sock_name = __FUNCTION__;
905b4d
+
905b4d
+    ret = create_pipe_fd(ctx->sock_name, &ctx->fd, 0111);
905b4d
+    assert_int_equal(ret, EOK);
905b4d
+    assert_int_not_equal(ctx->fd, -1);
905b4d
+    check_sock_properties(ctx, 0666);
905b4d
+
905b4d
+    /* Make sure we can overwrite an existing socket */
905b4d
+    ret = create_pipe_fd(ctx->sock_name, &ctx->fd, 0000);
905b4d
+    assert_int_equal(ret, EOK);
905b4d
+    assert_int_not_equal(ctx->fd, -1);
905b4d
+    check_sock_properties(ctx, 0777);
905b4d
+}
905b4d
+
905b4d
+void test_create_pipe_fd_teardown(void **state)
905b4d
+{
905b4d
+    struct create_pipe_ctx *ctx;
905b4d
+
905b4d
+    ctx = talloc_get_type(*state, struct create_pipe_ctx);
905b4d
+
905b4d
+    if (ctx->fd != -1) {
905b4d
+        unlink(ctx->sock_name);
905b4d
+        close(ctx->fd);
905b4d
+    }
905b4d
+}
905b4d
+
905b4d
 int main(int argc, const char *argv[])
905b4d
 {
905b4d
     poptContext pc;
905b4d
@@ -119,6 +207,9 @@ int main(int argc, const char *argv[])
905b4d
         unit_test(test_uid_csv_to_uid_list),
905b4d
         unit_test(test_name_csv_to_uid_list),
905b4d
         unit_test(test_csv_to_uid_list_neg),
905b4d
+        unit_test_setup_teardown(test_create_pipe_fd,
905b4d
+                                 test_create_pipe_fd_setup,
905b4d
+                                 test_create_pipe_fd_teardown)
905b4d
     };
905b4d
 
905b4d
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
905b4d
-- 
905b4d
1.9.3
905b4d