libndr: Avoid assigning duplicate versions to symbols
[amitay/samba.git] / lib / util / tests / test_util.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  Unit test for util.c
5  *
6  *  Copyright (C) Christof Schmitt 2020
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "lib/util/util.c"
23 #include <cmocka.h>
24
25 struct test_paths {
26         char testdir[PATH_MAX];
27         char none[PATH_MAX];
28         char dir[PATH_MAX];
29         mode_t dir_mode;
30         char file[PATH_MAX];
31         mode_t file_mode;
32         char symlink_none[PATH_MAX];
33         char symlink_dir[PATH_MAX];
34         char symlink_file[PATH_MAX];
35 };
36
37 static int group_setup(void **state)
38 {
39         struct test_paths *paths = NULL;
40         char *testdir = NULL;
41         int ret, fd;
42
43         umask(0);
44
45         paths = malloc(sizeof(struct test_paths));
46         assert_non_null(paths);
47
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);
52
53         strlcpy(paths->none, testdir, sizeof(paths->none));
54         strlcat(paths->none, "/none", sizeof(paths->none));
55
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);
61
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);
67         ret = close(fd);
68         assert_return_code(ret, errno);
69
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);
75
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);
80
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);
86
87         *state = paths;
88
89         return 0;
90 }
91
92 static int group_teardown(void **state)
93 {
94         struct test_paths *paths = *state;
95         int ret;
96
97         ret = rmdir(paths->dir);
98         assert_return_code(ret, errno);
99
100         ret = unlink(paths->file);
101         assert_return_code(ret, errno);
102
103         ret = unlink(paths->symlink_none);
104         assert_return_code(ret, errno);
105
106         ret = unlink(paths->symlink_dir);
107         assert_return_code(ret, errno);
108
109         ret = unlink(paths->symlink_file);
110         assert_return_code(ret, errno);
111
112         ret = rmdir(paths->testdir);
113         assert_return_code(ret, errno);
114
115         free(paths);
116         return 0;
117 }
118
119 static void test_directory_create_or_exists_none(void **state)
120 {
121         struct test_paths *paths = *state;
122         bool b;
123         struct stat sbuf;
124         int ret;
125
126         b = directory_create_or_exist(paths->none, 0775);
127         assert_true(b);
128
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));
133 }
134
135 static int teardown_none_directory(void **state)
136 {
137         struct test_paths *paths = *state;
138
139         rmdir(paths->none);
140         return 0;
141 }
142
143 static void test_directory_create_or_exists_dir(void **state)
144 {
145         struct test_paths *paths = *state;
146         bool b;
147         struct stat sbuf;
148         int ret;
149
150         b = directory_create_or_exist(paths->dir, 770);
151         assert_true(b);
152
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));
157 }
158
159 static void test_directory_create_or_exists_file(void **state)
160 {
161         struct test_paths *paths = *state;
162         bool b;
163         struct stat sbuf;
164         int ret;
165
166         b = directory_create_or_exist(paths->file, 770);
167         assert_false(b);
168
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));
173 }
174
175 static void test_directory_create_or_exists_symlink_none(void **state)
176 {
177         struct test_paths *paths = *state;
178         bool b;
179         struct stat sbuf;
180         int ret;
181
182         b = directory_create_or_exist(paths->symlink_none, 770);
183         assert_false(b);
184
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));
189 }
190
191 static void test_directory_create_or_exists_symlink_dir(void **state)
192 {
193         struct test_paths *paths = *state;
194         bool b;
195         struct stat sbuf;
196         int ret;
197
198         b = directory_create_or_exist(paths->symlink_dir, 770);
199         assert_true(b);
200
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));
205 }
206
207 static void test_directory_create_or_exists_symlink_file(void **state)
208 {
209         struct test_paths *paths = *state;
210         bool b;
211         struct stat sbuf;
212         int ret;
213
214         b = directory_create_or_exist(paths->symlink_file, 770);
215         assert_false(b);
216
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));
221 }
222
223 int main(int argc, char **argv)
224 {
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),
233         };
234
235         cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
236
237         return cmocka_run_group_tests(tests, group_setup, group_teardown);
238 }