From 84ed28e3746acf2f746076d7c2ff390b4107967a 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 2/2] cmocka unittest for io added

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

diff --git a/Makefile.am b/Makefile.am
index aa79ae6fbd637c7b1301f37130e729013602ff0d..68161dbc9c2815763b50058a9a7edcbd5b615d3e 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 = \
@@ -1232,6 +1233,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..5dcea2b0a9e36859863cc127c0a99d7d35b595f0
--- /dev/null
+++ b/src/tests/cmocka/test_io.c
@@ -0,0 +1,152 @@
+/*
+    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 "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[23])
+{
+    int ret;
+
+    strncpy(path, FILE_PATH, 23);
+    ret = mkstemp(path);
+ 
+    if (ret == -1) {
+        fprintf(stderr, "mkstemp failed\n");
+    }
+
+    return path;
+}
+
+static int get_dirfd(const char *dir)
+{
+    int dir_fd;
+    DIR *tmp = opendir(dir);
+    if (tmp != NULL){
+        dir_fd = dirfd(tmp);
+    }
+
+    return dir_fd;
+}
+
+void test_sss_open_cloexec_success(void **state)
+{
+    int fd;
+    int ret;
+    int ret_flag;
+    int expec_flag;
+    int flags = O_RDWR;
+    char path[23] = {'\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 dirfd;
+    int flags = O_RDWR;
+    char path[23] = {'\0'};
+    const char *relativepath;
+
+    relativepath = strchr(get_filepath(path), 't');
+    dirfd = get_dirfd(TEST_DIR);
+    fd = sss_openat_cloexec(dirfd, 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 dirfd;
+    int flags = O_RDWR;
+
+    dirfd = get_dirfd(TEST_DIR);
+    fd = sss_openat_cloexec(dirfd, 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(test_sss_openat_cloexec_success),
+        unit_test(test_sss_openat_cloexec_fail)
+    };
+
+    return run_tests(tests);
+}
-- 
1.8.1.4

