summaryrefslogtreecommitdiffstats
path: root/src/tests/cmocka/test_io.c
blob: 2f95388570f4189fc2ca70e6a55be688f7921467 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
    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 <errno.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 <libgen.h>

#include "limits.h"
#include "util/io.h"
#include "util/util.h"
#include "tests/common.h"

#define TESTS_PATH "tests_io"
#define FILE_TEMPLATE TESTS_PATH"/test_io.XXXXXX"
#define NON_EX_PATH TESTS_PATH"/non-existent-path"

/* Creates a unique temporary file inside TEST_DIR and returns its path*/
static char *get_random_filepath(const char *template)
{
    int ret;
    char *path;

    path = strdup(template);
    assert_non_null(path);

    ret = mkstemp(path);
    if (ret == -1) {
        int err = errno;
        fprintf(stderr, "mkstemp failed with path:'%s' [%s]\n",
                path, strerror(err));
    }
    assert_int_not_equal(ret, -1);

    /* We do not need this file descriptor */
    close(ret);

    return path;
}

void test_file_setup(void **state)
{
    int ret;
    char *file_path;

    ret = mkdir(TESTS_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    assert_int_equal(ret, EOK);

    file_path = get_random_filepath(FILE_TEMPLATE);
    assert_non_null(file_path);

    ret = unlink(NON_EX_PATH);
    ret = errno;
    assert_int_equal(ret, ENOENT);

    *state = file_path;
}

void test_file_teardown(void **state)
{
    int ret;
    char *file_path = (char *)*state;

    ret = unlink(file_path);
    assert_int_equal(ret, EOK);
    free(file_path);

    ret = rmdir(TESTS_PATH);
    assert_int_equal(ret, EOK);
}

struct dir_state {
    int dir_fd;
    char *basename;

    /* resources for cleanup*/
    DIR *dirp;
    char *filename;
};

void test_dir_setup(void **state)
{
    struct dir_state *data;
    int ret;

    data = (struct dir_state *)calloc(1, sizeof(struct dir_state));
    assert_non_null(data);

    ret = mkdir(TESTS_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
    assert_int_equal(ret, EOK);

    data->dirp = opendir(TESTS_PATH);
    if (data->dirp == NULL) {
        int err = errno;
        fprintf(stderr, "Could not open directory:'%s' [%s]\n",
                TESTS_PATH, strerror(err));
    }
    assert_non_null(data->dirp);

    data->dir_fd = dirfd(data->dirp);
    assert_int_not_equal(data->dir_fd, -1);

    data->filename = get_random_filepath(FILE_TEMPLATE);
    assert_non_null(data->filename);

    data->basename = basename(data->filename);

    ret = unlink(NON_EX_PATH);
    ret = errno;
    assert_int_equal(ret, ENOENT);

    *state = data;
}

void test_dir_teardown(void **state)
{
    int ret;
    struct dir_state *data = (struct dir_state *) *state;

    ret = unlink(data->filename);
    assert_int_equal(ret, EOK);
    free(data->filename);

    ret = closedir(data->dirp);
    assert_int_equal(ret, EOK);

    ret = rmdir(TESTS_PATH);
    assert_int_equal(ret, EOK);

    free(data);
}

void test_sss_open_cloexec_success(void **state)
{
    int fd;
    int ret;
    int ret_flag;
    int expec_flag;
    int flags = O_RDWR;
    char *file_path = (char *) *state;

    fd = sss_open_cloexec(file_path, flags, &ret);
    assert_int_not_equal(fd, -1);

    ret_flag = fcntl(fd, F_GETFD, 0);
    expec_flag = FD_CLOEXEC;
    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);
}

void test_sss_openat_cloexec_success(void **state)
{
    int fd;
    int ret;
    int ret_flag;
    int expec_flag;
    const int flags = O_RDWR;
    struct dir_state *data = (struct dir_state *) *state;

    fd = sss_openat_cloexec(data->dir_fd, data->basename, flags, &ret);
    assert_int_not_equal(fd, -1);

    ret_flag = fcntl(fd, F_GETFD, 0);
    expec_flag = FD_CLOEXEC;
    assert_true(ret_flag & expec_flag);

    close(fd);
}

void test_sss_openat_cloexec_fail(void **state)
{
    int fd;
    int ret;
    int flags = O_RDWR;
    struct dir_state *data = (struct dir_state *) *state;

    fd = sss_openat_cloexec(data->dir_fd, NON_EX_PATH, flags, &ret);
    assert_int_equal(fd, -1);
    assert_int_equal(ret, ENOENT);
}

int main(void)
{
    const UnitTest tests[] = {
        unit_test_setup_teardown(test_sss_open_cloexec_success,
                                 test_file_setup, test_file_teardown),
        unit_test_setup_teardown(test_sss_open_cloexec_fail,
                                  test_file_setup, test_file_teardown),
        unit_test_setup_teardown(test_sss_openat_cloexec_success,
                                 test_dir_setup, test_dir_teardown),
        unit_test_setup_teardown(test_sss_openat_cloexec_fail,
                                 test_dir_setup, test_dir_teardown)
    };

    tests_set_cwd();
    return run_tests(tests);
}