From cd65738d3071ac26f8236e8585d71d0f977c2507 Mon Sep 17 00:00:00 2001
From: Abhishek Singh <abhishekkumarsingh.cse@gmail.com>
Date: Sat, 23 Mar 2013 01:42:48 +0530
Subject: [PATCH] cmocka unittest for io added

---
 Makefile.am                |  11 +++-
 src/tests/cmocka/test_io.c | 157 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 src/tests/cmocka/test_io.c

diff --git a/Makefile.am b/Makefile.am
index e12b769ce3ac7573d8f6d670dae0ab75004ea187..3209cbab3c2d867fc5a420a6eae1f744a7a69dc6 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -147,7 +147,8 @@ endif
 if HAVE_CMOCKA
     non_interactive_cmocka_based_tests = \
         nss-srv-tests \
-        test-find-uid
+        test-find-uid \
+        test-io
 endif
 
 check_PROGRAMS = \
@@ -1233,6 +1234,14 @@ test_find_uid_LDADD = \
     $(DHASH_LIBS) \
     $(CMOCKA_LIBS) \
     libsss_util.la
+
+test_io_SOURCES = \
+    src/tests/cmocka/test_io.c \
+    src/util/io.c
+test_io_CFLAGS = \
+    $(AM_CFLAGS)
+test_io_LDADD = \
+    $(CMOCKA_LIBS)
 endif
 
 noinst_PROGRAMS = pam_test_client
diff --git a/src/tests/cmocka/test_io.c b/src/tests/cmocka/test_io.c
new file mode 100644
index 0000000000000000000000000000000000000000..c7c272f9cdec6202145e1c3b2325f1f5be00fb0f
--- /dev/null
+++ b/src/tests/cmocka/test_io.c
@@ -0,0 +1,157 @@
+/*
+    SSSD
+
+    find_uid - Utilities tests
+
+    Authors:
+        Abhishek Singh <abhishekkumarsingh.cse@gmail.com>
+
+    Copyright (C) 2013 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <dirent.h>
+#include <unistd.h>
+
+#include "limits.h"
+#include "util/io.h"
+#include "util/util.h"
+
+#define FILE_PATH TEST_DIR"/test_io.XXXXXX"
+#define NON_EX_PATH "non-existent-path"
+
+/* Creates a unique temporary file inside TEST_DIR and returns its path*/
+static char *get_filepath(char path[PATH_MAX])
+{
+    int ret;
+
+    strncpy(path, FILE_PATH, PATH_MAX-1);
+    ret = mkstemp(path);
+ 
+    if (ret == -1) {
+        fprintf(stderr, "mkstemp failed\n");
+    }
+
+    return path;
+}
+
+void setup_dirp(void **state)
+{
+    DIR *dirp = opendir(TEST_DIR);
+    if (dirp != NULL){
+        *state = (void *)dirp;
+    }
+}
+
+void teardown_dirp(void **state)
+{
+    closedir((DIR *)*state);
+}
+
+void test_sss_open_cloexec_success(void **state)
+{
+    int fd;
+    int ret;
+    int ret_flag;
+    int expec_flag;
+    int flags = O_RDWR;
+    char path[PATH_MAX] = {'\0'};
+
+    fd = sss_open_cloexec(get_filepath(path), flags, &ret);
+    assert_true(fd != -1);
+
+    ret_flag = fcntl(fd, F_GETFD, 0);
+    expec_flag = FD_CLOEXEC;
+    assert_true(ret_flag & expec_flag);
+
+    close(fd);
+    unlink(path);
+}
+
+void test_sss_open_cloexec_fail(void **state)
+{
+    int fd;
+    int ret;
+    int flags = O_RDWR;
+
+    fd = sss_open_cloexec(NON_EX_PATH, flags, &ret);
+
+    assert_true(fd == -1);
+    assert_int_not_equal(ret, 0);
+
+    close(fd);
+}
+
+void test_sss_openat_cloexec_success(void **state)
+{
+    int fd;
+    int ret;
+    int ret_flag;
+    int expec_flag;
+    int dir_fd;
+    int flags = O_RDWR;
+    char path[PATH_MAX] = {'\0'};
+    const char *relativepath;
+
+    relativepath = strchr(get_filepath(path), 't');
+    dir_fd = dirfd((DIR *)*state);
+    fd = sss_openat_cloexec(dir_fd, relativepath, flags, &ret);
+    assert_true(fd != -1);
+
+    ret_flag = fcntl(fd, F_GETFD, 0);
+    expec_flag = FD_CLOEXEC;
+    assert_true(ret_flag & expec_flag);
+
+    close(fd);
+    unlink(path);
+}
+
+void test_sss_openat_cloexec_fail(void **state)
+{
+    int fd;
+    int ret;
+    int dir_fd;
+    int flags = O_RDWR;
+
+    dir_fd = dirfd((DIR *)*state);
+    fd = sss_openat_cloexec(dir_fd, NON_EX_PATH, flags, &ret);
+
+    assert_true(fd == -1);
+    assert_int_not_equal(ret, 0);
+
+    close(fd);
+}
+
+int main(void)
+{
+    const UnitTest tests[] = {
+        unit_test(test_sss_open_cloexec_success),
+        unit_test(test_sss_open_cloexec_fail),
+        unit_test_setup_teardown(test_sss_openat_cloexec_success, setup_dirp,
+                                 teardown_dirp),
+        unit_test_setup_teardown(test_sss_openat_cloexec_fail, setup_dirp,
+                                 teardown_dirp)
+    };
+
+    return run_tests(tests);
+}
-- 
1.8.1.4

