summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile.am11
-rw-r--r--src/tests/cmocka/test_io.c157
2 files changed, 167 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index e12b769ce..3209cbab3 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 000000000..4d21b2e25
--- /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[])
+{
+ 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);
+}