dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone
Blob Blame History Raw
From 92cf06af1c5ee8be33722e76c79a3c290b9f67c8 Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Fri, 17 Oct 2014 16:44:05 +0200
Subject: [PATCH 34/46] TEST: Unit test for create_pipe_fd

Reviewed-by: Pavel Reichl <preichl@redhat.com>
Reviewed-by: Simo Sorce <simo@redhat.com>
(cherry picked from commit 4a5cced91df68a85ef0b30de8efe104c8a0aab7a)
---
 src/tests/cwrap/test_responder_common.c | 91 +++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)

diff --git a/src/tests/cwrap/test_responder_common.c b/src/tests/cwrap/test_responder_common.c
index 23dcf753f184cdecaf39c73c6e9be0e23e6df968..7e3f2e025a0dce15cc93e560a42bb566eff9fb30 100644
--- a/src/tests/cwrap/test_responder_common.c
+++ b/src/tests/cwrap/test_responder_common.c
@@ -23,6 +23,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <talloc.h>
 
 #include <popt.h>
 #include "util/util.h"
@@ -105,6 +106,93 @@ void test_csv_to_uid_list_neg(void **state)
     talloc_free(tmp_ctx);
 }
 
+struct create_pipe_ctx {
+    int fd;
+    const char *sock_name;
+};
+
+void test_create_pipe_fd_setup(void **state)
+{
+    struct create_pipe_ctx *ctx;
+
+    ctx = talloc(global_talloc_context, struct create_pipe_ctx);
+    assert_non_null(ctx);
+    ctx->fd = -1;
+
+    *state = ctx;
+}
+
+void check_sock_properties(struct create_pipe_ctx *ctx, mode_t mode)
+{
+    int ret;
+    int optval;
+    socklen_t optlen;
+    struct stat sbuf;
+
+    /* Check existence of the file and the permissions */
+    ret = stat(ctx->sock_name, &sbuf);
+    assert_int_equal(ret, 0);
+    assert_true(S_ISSOCK(sbuf.st_mode));
+    assert_true((sbuf.st_mode & ~S_IFMT) == mode);
+
+    /* Check it's a UNIX socket */
+    optlen = sizeof(optval);
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_DOMAIN, &optval, &optlen);
+    assert_int_equal(ret, 0);
+    assert_int_equal(optval, AF_UNIX);
+
+    optlen = sizeof(optval);
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_TYPE, &optval, &optlen);
+    assert_int_equal(ret, 0);
+    assert_int_equal(optval, SOCK_STREAM);
+
+    /* Make sure this is a listening socket */
+    optlen = sizeof(optval);
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_ACCEPTCONN, &optval, &optlen);
+    assert_int_equal(ret, 0);
+    assert_int_equal(optval, 1);
+
+    /* Check the right protocol */
+    optlen = sizeof(optval);
+    ret = getsockopt(ctx->fd, SOL_SOCKET, SO_PROTOCOL, &optval, &optlen);
+    assert_int_equal(ret, 0);
+    assert_int_equal(optval, 0);
+
+}
+
+void test_create_pipe_fd(void **state)
+{
+    int ret;
+    struct create_pipe_ctx *ctx;
+
+    ctx = talloc_get_type(*state, struct create_pipe_ctx);
+
+    ctx->sock_name = __FUNCTION__;
+
+    ret = create_pipe_fd(ctx->sock_name, &ctx->fd, 0111);
+    assert_int_equal(ret, EOK);
+    assert_int_not_equal(ctx->fd, -1);
+    check_sock_properties(ctx, 0666);
+
+    /* Make sure we can overwrite an existing socket */
+    ret = create_pipe_fd(ctx->sock_name, &ctx->fd, 0000);
+    assert_int_equal(ret, EOK);
+    assert_int_not_equal(ctx->fd, -1);
+    check_sock_properties(ctx, 0777);
+}
+
+void test_create_pipe_fd_teardown(void **state)
+{
+    struct create_pipe_ctx *ctx;
+
+    ctx = talloc_get_type(*state, struct create_pipe_ctx);
+
+    if (ctx->fd != -1) {
+        unlink(ctx->sock_name);
+        close(ctx->fd);
+    }
+}
+
 int main(int argc, const char *argv[])
 {
     poptContext pc;
@@ -119,6 +207,9 @@ int main(int argc, const char *argv[])
         unit_test(test_uid_csv_to_uid_list),
         unit_test(test_name_csv_to_uid_list),
         unit_test(test_csv_to_uid_list_neg),
+        unit_test_setup_teardown(test_create_pipe_fd,
+                                 test_create_pipe_fd_setup,
+                                 test_create_pipe_fd_teardown)
     };
 
     /* Set debug level to invalid value so we can deside if -d 0 was used. */
-- 
1.9.3