2 * Unix SMB/CIFS implementation.
6 * Copyright (C) Christof Schmitt 2020
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "lib/util/util.c"
26 char testdir[PATH_MAX];
32 char symlink_none[PATH_MAX];
33 char symlink_dir[PATH_MAX];
34 char symlink_file[PATH_MAX];
37 static int group_setup(void **state)
39 struct test_paths *paths = NULL;
45 paths = malloc(sizeof(struct test_paths));
46 assert_non_null(paths);
48 strlcpy(paths->testdir, tmpdir(), sizeof(paths->testdir));
49 strlcat(paths->testdir, "/test_util_XXXXXX", sizeof(paths->testdir));
50 testdir = mkdtemp(paths->testdir);
51 assert_non_null(testdir);
53 strlcpy(paths->none, testdir, sizeof(paths->none));
54 strlcat(paths->none, "/none", sizeof(paths->none));
56 strlcpy(paths->dir, testdir, sizeof(paths->dir));
57 strlcat(paths->dir, "/dir", sizeof(paths->dir));
58 paths->dir_mode = 0750;
59 ret = mkdir(paths->dir, paths->dir_mode);
60 assert_return_code(ret, errno);
62 strlcpy(paths->file, testdir, sizeof(paths->file));
63 strlcat(paths->file, "/file", sizeof(paths->file));
64 paths->file_mode = 0640;
65 fd = creat(paths->file, paths->file_mode);
66 assert_return_code(fd, errno);
68 assert_return_code(ret, errno);
70 strlcpy(paths->symlink_none, testdir, sizeof(paths->symlink_none));
71 strlcat(paths->symlink_none, "/symlink_none",
72 sizeof(paths->symlink_none));
73 ret = symlink("/none", paths->symlink_none);
74 assert_return_code(ret, errno);
76 strlcpy(paths->symlink_dir, testdir, sizeof(paths->symlink_dir));
77 strlcat(paths->symlink_dir, "/symlink_dir", sizeof(paths->symlink_dir));
78 ret = symlink(paths->dir, paths->symlink_dir);
79 assert_return_code(ret, errno);
81 strlcpy(paths->symlink_file, testdir, sizeof(paths->symlink_file));
82 strlcat(paths->symlink_file, "/symlink_file",
83 sizeof(paths->symlink_file));
84 ret = symlink(paths->file, paths->symlink_file);
85 assert_return_code(ret, errno);
92 static int group_teardown(void **state)
94 struct test_paths *paths = *state;
97 ret = rmdir(paths->dir);
98 assert_return_code(ret, errno);
100 ret = unlink(paths->file);
101 assert_return_code(ret, errno);
103 ret = unlink(paths->symlink_none);
104 assert_return_code(ret, errno);
106 ret = unlink(paths->symlink_dir);
107 assert_return_code(ret, errno);
109 ret = unlink(paths->symlink_file);
110 assert_return_code(ret, errno);
112 ret = rmdir(paths->testdir);
113 assert_return_code(ret, errno);
119 static void test_directory_create_or_exists_none(void **state)
121 struct test_paths *paths = *state;
126 b = directory_create_or_exist(paths->none, 0775);
129 ret = lstat(paths->none, &sbuf);
130 assert_return_code(ret, errno);
131 assert_int_equal(sbuf.st_mode & 0777, 0775);
132 assert_true(S_ISDIR(sbuf.st_mode));
135 static int teardown_none_directory(void **state)
137 struct test_paths *paths = *state;
143 static void test_directory_create_or_exists_dir(void **state)
145 struct test_paths *paths = *state;
150 b = directory_create_or_exist(paths->dir, 770);
153 ret = lstat(paths->dir, &sbuf);
154 assert_return_code(ret, errno);
155 assert_int_equal(sbuf.st_mode & 0777, paths->dir_mode);
156 assert_true(S_ISDIR(sbuf.st_mode));
159 static void test_directory_create_or_exists_file(void **state)
161 struct test_paths *paths = *state;
166 b = directory_create_or_exist(paths->file, 770);
169 ret = lstat(paths->file, &sbuf);
170 assert_return_code(ret, errno);
171 assert_int_equal(sbuf.st_mode & 0777, paths->file_mode);
172 assert_true(S_ISREG(sbuf.st_mode));
175 static void test_directory_create_or_exists_symlink_none(void **state)
177 struct test_paths *paths = *state;
182 b = directory_create_or_exist(paths->symlink_none, 770);
185 ret = lstat(paths->symlink_none, &sbuf);
186 assert_return_code(ret, errno);
187 assert_int_equal(sbuf.st_mode & 0777, 0777);
188 assert_true(S_ISLNK(sbuf.st_mode));
191 static void test_directory_create_or_exists_symlink_dir(void **state)
193 struct test_paths *paths = *state;
198 b = directory_create_or_exist(paths->symlink_dir, 770);
201 ret = lstat(paths->symlink_dir, &sbuf);
202 assert_return_code(ret, errno);
203 assert_int_equal(sbuf.st_mode & 0777, 0777);
204 assert_true(S_ISLNK(sbuf.st_mode));
207 static void test_directory_create_or_exists_symlink_file(void **state)
209 struct test_paths *paths = *state;
214 b = directory_create_or_exist(paths->symlink_file, 770);
217 ret = lstat(paths->symlink_file, &sbuf);
218 assert_return_code(ret, errno);
219 assert_int_equal(sbuf.st_mode & 0777, 0777);
220 assert_true(S_ISLNK(sbuf.st_mode));
223 int main(int argc, char **argv)
225 const struct CMUnitTest tests[] = {
226 cmocka_unit_test_teardown(test_directory_create_or_exists_none,
227 teardown_none_directory),
228 cmocka_unit_test(test_directory_create_or_exists_dir),
229 cmocka_unit_test(test_directory_create_or_exists_file),
230 cmocka_unit_test(test_directory_create_or_exists_symlink_none),
231 cmocka_unit_test(test_directory_create_or_exists_symlink_dir),
232 cmocka_unit_test(test_directory_create_or_exists_symlink_file),
235 cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
237 return cmocka_run_group_tests(tests, group_setup, group_teardown);