summaryrefslogtreecommitdiffstats
path: root/src/tests/cmocka/test_io.c
blob: 4d21b2e25d3f35cc155b6410bcb4ce8963a59c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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);
}