From d6d94556d58b754b7110e6c16df7ba68de27fcea Mon Sep 17 00:00:00 2001 From: Abhishek Singh Date: Sat, 23 Mar 2013 00:15:40 +0530 Subject: [PATCH] cmocka unittest for io added --- Makefile.am | 13 +++- src/tests/cmocka/test_io.c | 148 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/tests/cmocka/test_io.c diff --git a/Makefile.am b/Makefile.am index a52629748c6633a7c6ded5a110704292ceb70e90..02dd90ce46f7143c3f8ac7cfa79471ab95eaffe5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -141,7 +141,8 @@ endif if HAVE_CMOCKA non_interactive_cmocka_based_tests = \ nss-srv-tests \ - test-find-uid + test-find-uid \ + test-io endif check_PROGRAMS = \ @@ -1218,6 +1219,16 @@ test_find_uid_LDADD = \ $(DHASH_LIBS) \ $(CMOCKA_LIBS) \ libsss_util.la + +test_io_DEPENDENCIES = \ + $(ldblib_LTLIBRARIES) +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..bed7ef196e73e7e93879b8b659721369e6c0cbb4 --- /dev/null +++ b/src/tests/cmocka/test_io.c @@ -0,0 +1,148 @@ +/* + SSSD + + find_uid - Utilities tests + + Authors: + Abhishek Singh + + 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 . +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "util/io.h" + +#define FILE_PATH "/tmp/test.in" +#define NON_EX_PATH "non-existent-path" + +static void setup_all(void) +{ + FILE *fp; + fp = fopen(FILE_PATH, "w"); + if (fp != NULL) + fclose(fp); + else + printf("Error! file test.in can't be created"); +} + +static void teardown_all(void) +{ + remove(FILE_PATH); +} + +static int get_dirfd(void) +{ + int dir_fd; + DIR *tmp = opendir("/tmp"); + 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; + + fd = sss_open_cloexec(FILE_PATH, flags, &ret); + + ret_flag = fcntl(fd, F_GETFD, 0); + expec_flag = FD_CLOEXEC; + + assert_true(fd != -1); + assert_true(ret_flag & expec_flag); + + close(fd); +} + +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; + + dir_fd = get_dirfd(); + fd = sss_openat_cloexec(dir_fd, "test.in", flags, &ret); + + ret_flag = fcntl(fd, F_GETFD, 0); + expec_flag = FD_CLOEXEC; + + assert_true(fd != -1); + assert_true(ret_flag & expec_flag); + + close(fd); +} + +void test_sss_openat_cloexec_fail(void **state) +{ + int fd; + int ret; + int dir_fd; + int flags = O_RDWR; + + dir_fd = get_dirfd(); + 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_setup_teardown(test_sss_open_cloexec_success, setup_all, + teardown_all), + unit_test_setup_teardown(test_sss_open_cloexec_fail, setup_all, + teardown_all), + unit_test_setup_teardown(test_sss_openat_cloexec_success, setup_all, + teardown_all), + unit_test_setup_teardown(test_sss_openat_cloexec_fail, setup_all, + teardown_all) + }; + + return run_tests(tests); +} -- 1.8.1.4