LCOV - code coverage report
Current view: top level - tests/cmocka - test_io.c (source / functions) Hit Total Coverage
Test: .coverage.total Lines: 92 96 95.8 %
Date: 2015-10-19 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :     SSSD
       3             : 
       4             :     find_uid - Utilities tests
       5             : 
       6             :     Authors:
       7             :         Abhishek Singh <abhishekkumarsingh.cse@gmail.com>
       8             : 
       9             :     Copyright (C) 2013 Red Hat
      10             : 
      11             :     This program is free software; you can redistribute it and/or modify
      12             :     it under the terms of the GNU General Public License as published by
      13             :     the Free Software Foundation; either version 3 of the License, or
      14             :     (at your option) any later version.
      15             : 
      16             :     This program is distributed in the hope that it will be useful,
      17             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             :     GNU General Public License for more details.
      20             : 
      21             :     You should have received a copy of the GNU General Public License
      22             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      23             : */
      24             : 
      25             : #include <stdio.h>
      26             : #include <fcntl.h>
      27             : #include <errno.h>
      28             : #include <stdlib.h>
      29             : #include <stdarg.h>
      30             : #include <string.h>
      31             : #include <stddef.h>
      32             : #include <setjmp.h>
      33             : #include <cmocka.h>
      34             : #include <dirent.h>
      35             : #include <unistd.h>
      36             : #include <libgen.h>
      37             : 
      38             : #include "limits.h"
      39             : #include "util/io.h"
      40             : #include "util/util.h"
      41             : #include "tests/common.h"
      42             : 
      43             : #define TESTS_PATH "tp_" BASE_FILE_STEM
      44             : #define FILE_TEMPLATE TESTS_PATH"/test_io.XXXXXX"
      45             : #define NON_EX_PATH TESTS_PATH"/non-existent-path"
      46             : 
      47             : /* Creates a unique temporary file inside TEST_DIR and returns its path*/
      48           4 : static char *get_random_filepath(const char *template)
      49             : {
      50             :     int ret;
      51             :     char *path;
      52             : 
      53           4 :     path = strdup(template);
      54           4 :     assert_non_null(path);
      55             : 
      56           4 :     ret = mkstemp(path);
      57           4 :     if (ret == -1) {
      58           0 :         int err = errno;
      59           0 :         fprintf(stderr, "mkstemp failed with path:'%s' [%s]\n",
      60             :                 path, strerror(err));
      61             :     }
      62           4 :     assert_int_not_equal(ret, -1);
      63             : 
      64             :     /* We do not need this file descriptor */
      65           4 :     close(ret);
      66             : 
      67           4 :     return path;
      68             : }
      69             : 
      70           2 : static int test_file_setup(void **state)
      71             : {
      72             :     int ret;
      73             :     char *file_path;
      74             : 
      75           2 :     ret = mkdir(TESTS_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
      76           2 :     assert_int_equal(ret, EOK);
      77             : 
      78           2 :     file_path = get_random_filepath(FILE_TEMPLATE);
      79           2 :     assert_non_null(file_path);
      80             : 
      81           2 :     ret = unlink(NON_EX_PATH);
      82           2 :     ret = errno;
      83           2 :     assert_int_equal(ret, ENOENT);
      84             : 
      85           2 :     *state = file_path;
      86           2 :     return 0;
      87             : }
      88             : 
      89           2 : static int test_file_teardown(void **state)
      90             : {
      91             :     int ret;
      92           2 :     char *file_path = (char *)*state;
      93             : 
      94           2 :     ret = unlink(file_path);
      95           2 :     assert_int_equal(ret, EOK);
      96           2 :     free(file_path);
      97             : 
      98           2 :     ret = rmdir(TESTS_PATH);
      99           2 :     assert_int_equal(ret, EOK);
     100           2 :     return 0;
     101             : }
     102             : 
     103             : struct dir_state {
     104             :     int dir_fd;
     105             :     char *basename;
     106             : 
     107             :     /* resources for cleanup*/
     108             :     DIR *dirp;
     109             :     char *filename;
     110             : };
     111             : 
     112           2 : static int test_dir_setup(void **state)
     113             : {
     114             :     struct dir_state *data;
     115             :     int ret;
     116             : 
     117           2 :     data = (struct dir_state *)calloc(1, sizeof(struct dir_state));
     118           2 :     assert_non_null(data);
     119             : 
     120           2 :     ret = mkdir(TESTS_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
     121           2 :     assert_int_equal(ret, EOK);
     122             : 
     123           2 :     data->dirp = opendir(TESTS_PATH);
     124           2 :     if (data->dirp == NULL) {
     125           0 :         int err = errno;
     126           0 :         fprintf(stderr, "Could not open directory:'%s' [%s]\n",
     127             :                 TESTS_PATH, strerror(err));
     128             :     }
     129           2 :     assert_non_null(data->dirp);
     130             : 
     131           2 :     data->dir_fd = dirfd(data->dirp);
     132           2 :     assert_int_not_equal(data->dir_fd, -1);
     133             : 
     134           2 :     data->filename = get_random_filepath(FILE_TEMPLATE);
     135           2 :     assert_non_null(data->filename);
     136             : 
     137           2 :     data->basename = basename(data->filename);
     138             : 
     139           2 :     ret = unlink(NON_EX_PATH);
     140           2 :     ret = errno;
     141           2 :     assert_int_equal(ret, ENOENT);
     142             : 
     143           2 :     *state = data;
     144           2 :     return 0;
     145             : }
     146             : 
     147           2 : static int test_dir_teardown(void **state)
     148             : {
     149             :     int ret;
     150           2 :     struct dir_state *data = (struct dir_state *) *state;
     151             : 
     152           2 :     ret = unlink(data->filename);
     153           2 :     assert_int_equal(ret, EOK);
     154           2 :     free(data->filename);
     155             : 
     156           2 :     ret = closedir(data->dirp);
     157           2 :     assert_int_equal(ret, EOK);
     158             : 
     159           2 :     ret = rmdir(TESTS_PATH);
     160           2 :     assert_int_equal(ret, EOK);
     161             : 
     162           2 :     free(data);
     163           2 :     return 0;
     164             : }
     165             : 
     166           1 : void test_sss_open_cloexec_success(void **state)
     167             : {
     168             :     int fd;
     169             :     int ret;
     170             :     int ret_flag;
     171             :     int expec_flag;
     172           1 :     int flags = O_RDWR;
     173           1 :     char *file_path = (char *) *state;
     174             : 
     175           1 :     fd = sss_open_cloexec(file_path, flags, &ret);
     176           1 :     assert_int_not_equal(fd, -1);
     177             : 
     178           1 :     ret_flag = fcntl(fd, F_GETFD, 0);
     179           1 :     expec_flag = FD_CLOEXEC;
     180           1 :     assert_true(ret_flag & expec_flag);
     181             : 
     182           1 :     close(fd);
     183           1 : }
     184             : 
     185           1 : void test_sss_open_cloexec_fail(void **state)
     186             : {
     187             :     int fd;
     188             :     int ret;
     189           1 :     int flags = O_RDWR;
     190             : 
     191           1 :     fd = sss_open_cloexec(NON_EX_PATH, flags, &ret);
     192             : 
     193           1 :     assert_true(fd == -1);
     194           1 :     assert_int_not_equal(ret, 0);
     195           1 : }
     196             : 
     197           1 : void test_sss_openat_cloexec_success(void **state)
     198             : {
     199             :     int fd;
     200             :     int ret;
     201             :     int ret_flag;
     202             :     int expec_flag;
     203           1 :     const int flags = O_RDWR;
     204           1 :     struct dir_state *data = (struct dir_state *) *state;
     205             : 
     206           1 :     fd = sss_openat_cloexec(data->dir_fd, data->basename, flags, &ret);
     207           1 :     assert_int_not_equal(fd, -1);
     208             : 
     209           1 :     ret_flag = fcntl(fd, F_GETFD, 0);
     210           1 :     expec_flag = FD_CLOEXEC;
     211           1 :     assert_true(ret_flag & expec_flag);
     212             : 
     213           1 :     close(fd);
     214           1 : }
     215             : 
     216           1 : void test_sss_openat_cloexec_fail(void **state)
     217             : {
     218             :     int fd;
     219             :     int ret;
     220           1 :     int flags = O_RDWR;
     221           1 :     struct dir_state *data = (struct dir_state *) *state;
     222             : 
     223           1 :     fd = sss_openat_cloexec(data->dir_fd, NON_EX_PATH, flags, &ret);
     224           1 :     assert_int_equal(fd, -1);
     225           1 :     assert_int_equal(ret, ENOENT);
     226           1 : }
     227             : 
     228           1 : int main(void)
     229             : {
     230           1 :     const struct CMUnitTest tests[] = {
     231             :         cmocka_unit_test_setup_teardown(test_sss_open_cloexec_success,
     232             :                                         test_file_setup, test_file_teardown),
     233             :         cmocka_unit_test_setup_teardown(test_sss_open_cloexec_fail,
     234             :                                         test_file_setup, test_file_teardown),
     235             :         cmocka_unit_test_setup_teardown(test_sss_openat_cloexec_success,
     236             :                                         test_dir_setup, test_dir_teardown),
     237             :         cmocka_unit_test_setup_teardown(test_sss_openat_cloexec_fail,
     238             :                                         test_dir_setup, test_dir_teardown)
     239             :     };
     240             : 
     241           1 :     tests_set_cwd();
     242           1 :     return cmocka_run_group_tests(tests, NULL, NULL);
     243             : }

Generated by: LCOV version 1.10