diff options
author | Abhishek Singh <abhishekkumarsingh.cse@gmail.com> | 2013-03-23 01:42:48 +0530 |
---|---|---|
committer | Jakub Hrozek <jhrozek@redhat.com> | 2013-04-10 14:21:23 +0200 |
commit | ae6c1596225c65bec2a2dabff9eee4e3e0691181 (patch) | |
tree | 8532882d4967d2a41ee768216902512223221461 /src/tests | |
parent | a65a64aee968bd2ac18156ced15a1e2509a8acba (diff) | |
download | sssd-ae6c1596225c65bec2a2dabff9eee4e3e0691181.tar.gz sssd-ae6c1596225c65bec2a2dabff9eee4e3e0691181.tar.bz2 sssd-ae6c1596225c65bec2a2dabff9eee4e3e0691181.zip |
cmocka unittest for io added
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/cmocka/test_io.c | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/src/tests/cmocka/test_io.c b/src/tests/cmocka/test_io.c new file mode 100644 index 00000000..4d21b2e2 --- /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); +} |