changes done.
On Sat, Mar 23, 2013 at 12:07 AM, Stephen Gallagher <sgall...@redhat.com>wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On 03/22/2013 02:33 PM, Abhishek Singh wrote: > > New tests added to success tests. Please review it. > > > > Almost :) > > You can't just test for integer equivalence, because it's conceivable > that there may be other flags returned beyond just the FD_CLOEXEC flag. > > You need to do: > assert_true(ret_flag & FD_CLOEXEC) > > The bitwise comparison will ensure that it's non-zero if the > FD_CLOEXEC flag is set. > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.13 (GNU/Linux) > Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ > > iEYEARECAAYFAlFMpO4ACgkQeiVVYja6o6OJsQCfeifrXZefJL2CfKR5qFl267XG > LzwAn2AzSlodw/YfT1phEHOFuk6fTXSs > =EyN7 > -----END PGP SIGNATURE----- > _______________________________________________ > sssd-devel mailing list > sssd-devel@lists.fedorahosted.org > https://lists.fedorahosted.org/mailman/listinfo/sssd-devel >
From d6d94556d58b754b7110e6c16df7ba68de27fcea Mon Sep 17 00:00:00 2001 From: Abhishek Singh <abhishekkumarsingh....@gmail.com> 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 <abhishekkumarsingh....@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 <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> +#include <dirent.h> +#include <unistd.h> + +#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
_______________________________________________ sssd-devel mailing list sssd-devel@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/sssd-devel