]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <stdlib.h> | |
4 | #include <sys/file.h> | |
5 | #include <sys/sysmacros.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "alloc-util.h" | |
9 | #include "argv-util.h" | |
10 | #include "copy.h" | |
11 | #include "fd-util.h" | |
12 | #include "fs-util.h" | |
13 | #include "mkdir.h" | |
14 | #include "path-util.h" | |
15 | #include "process-util.h" | |
16 | #include "random-util.h" | |
17 | #include "rm-rf.h" | |
18 | #include "stat-util.h" | |
19 | #include "string-util.h" | |
20 | #include "strv.h" | |
21 | #include "sync-util.h" | |
22 | #include "tests.h" | |
23 | #include "time-util.h" | |
24 | #include "tmpfile-util.h" | |
25 | #include "umask-util.h" | |
26 | #include "virt.h" | |
27 | ||
28 | static const char *arg_test_dir = NULL; | |
29 | ||
30 | TEST(readlink_and_make_absolute) { | |
31 | const char *tempdir, *name, *name2, *name_alias; | |
32 | _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL; | |
33 | ||
34 | tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute"); | |
35 | name = strjoina(tempdir, "/original"); | |
36 | name2 = "test-readlink_and_make_absolute/original"; | |
37 | name_alias = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute-alias"); | |
38 | ||
39 | assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0); | |
40 | assert_se(touch(name) >= 0); | |
41 | ||
42 | if (symlink(name, name_alias) < 0) { | |
43 | assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM)); | |
44 | log_tests_skipped_errno(errno, "symlink() not possible"); | |
45 | } else { | |
46 | assert_se(readlink_and_make_absolute(name_alias, &r1) >= 0); | |
47 | ASSERT_STREQ(r1, name); | |
48 | assert_se(unlink(name_alias) >= 0); | |
49 | ||
50 | assert_se(safe_getcwd(&pwd) >= 0); | |
51 | ||
52 | assert_se(chdir(tempdir) >= 0); | |
53 | assert_se(symlink(name2, name_alias) >= 0); | |
54 | assert_se(readlink_and_make_absolute(name_alias, &r2) >= 0); | |
55 | ASSERT_STREQ(r2, name); | |
56 | assert_se(unlink(name_alias) >= 0); | |
57 | ||
58 | assert_se(chdir(pwd) >= 0); | |
59 | } | |
60 | ||
61 | assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); | |
62 | } | |
63 | ||
64 | TEST(get_files_in_directory) { | |
65 | _cleanup_strv_free_ char **l = NULL, **t = NULL; | |
66 | ||
67 | assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0); | |
68 | assert_se(get_files_in_directory(".", &t) >= 0); | |
69 | assert_se(get_files_in_directory(".", NULL) >= 0); | |
70 | } | |
71 | ||
72 | TEST(var_tmp) { | |
73 | _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL; | |
74 | const char *tmp_dir = NULL, *t; | |
75 | ||
76 | t = getenv("TMPDIR"); | |
77 | if (t) { | |
78 | tmpdir_backup = strdup(t); | |
79 | assert_se(tmpdir_backup); | |
80 | } | |
81 | ||
82 | t = getenv("TEMP"); | |
83 | if (t) { | |
84 | temp_backup = strdup(t); | |
85 | assert_se(temp_backup); | |
86 | } | |
87 | ||
88 | t = getenv("TMP"); | |
89 | if (t) { | |
90 | tmp_backup = strdup(t); | |
91 | assert_se(tmp_backup); | |
92 | } | |
93 | ||
94 | assert_se(unsetenv("TMPDIR") >= 0); | |
95 | assert_se(unsetenv("TEMP") >= 0); | |
96 | assert_se(unsetenv("TMP") >= 0); | |
97 | ||
98 | assert_se(var_tmp_dir(&tmp_dir) >= 0); | |
99 | ASSERT_STREQ(tmp_dir, "/var/tmp"); | |
100 | ||
101 | assert_se(setenv("TMPDIR", "/tmp", true) >= 0); | |
102 | ASSERT_STREQ(getenv("TMPDIR"), "/tmp"); | |
103 | ||
104 | assert_se(var_tmp_dir(&tmp_dir) >= 0); | |
105 | ASSERT_STREQ(tmp_dir, "/tmp"); | |
106 | ||
107 | assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0); | |
108 | ASSERT_STREQ(getenv("TMPDIR"), "/88_does_not_exist_88"); | |
109 | ||
110 | assert_se(var_tmp_dir(&tmp_dir) >= 0); | |
111 | ASSERT_STREQ(tmp_dir, "/var/tmp"); | |
112 | ||
113 | if (tmpdir_backup) { | |
114 | assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0); | |
115 | ASSERT_STREQ(getenv("TMPDIR"), tmpdir_backup); | |
116 | } | |
117 | ||
118 | if (temp_backup) { | |
119 | assert_se(setenv("TEMP", temp_backup, true) >= 0); | |
120 | ASSERT_STREQ(getenv("TEMP"), temp_backup); | |
121 | } | |
122 | ||
123 | if (tmp_backup) { | |
124 | assert_se(setenv("TMP", tmp_backup, true) >= 0); | |
125 | ASSERT_STREQ(getenv("TMP"), tmp_backup); | |
126 | } | |
127 | } | |
128 | ||
129 | TEST(dot_or_dot_dot) { | |
130 | assert_se(!dot_or_dot_dot(NULL)); | |
131 | assert_se(!dot_or_dot_dot("")); | |
132 | assert_se(!dot_or_dot_dot("xxx")); | |
133 | assert_se(dot_or_dot_dot(".")); | |
134 | assert_se(dot_or_dot_dot("..")); | |
135 | assert_se(!dot_or_dot_dot(".foo")); | |
136 | assert_se(!dot_or_dot_dot("..foo")); | |
137 | } | |
138 | ||
139 | TEST(access_fd) { | |
140 | _cleanup_(rmdir_and_freep) char *p = NULL; | |
141 | _cleanup_close_ int fd = -EBADF; | |
142 | const char *a; | |
143 | ||
144 | a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX"); | |
145 | assert_se(mkdtemp_malloc(a, &p) >= 0); | |
146 | ||
147 | fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC); | |
148 | assert_se(fd >= 0); | |
149 | ||
150 | assert_se(access_fd(fd, R_OK) >= 0); | |
151 | assert_se(access_fd(fd, F_OK) >= 0); | |
152 | assert_se(access_fd(fd, W_OK) >= 0); | |
153 | ||
154 | assert_se(fchmod(fd, 0000) >= 0); | |
155 | ||
156 | assert_se(access_fd(fd, F_OK) >= 0); | |
157 | ||
158 | if (geteuid() == 0) { | |
159 | assert_se(access_fd(fd, R_OK) >= 0); | |
160 | assert_se(access_fd(fd, W_OK) >= 0); | |
161 | } else { | |
162 | assert_se(access_fd(fd, R_OK) == -EACCES); | |
163 | assert_se(access_fd(fd, W_OK) == -EACCES); | |
164 | } | |
165 | } | |
166 | ||
167 | TEST(touch_file) { | |
168 | uid_t test_uid, test_gid; | |
169 | _cleanup_(rm_rf_physical_and_freep) char *p = NULL; | |
170 | struct stat st; | |
171 | const char *a; | |
172 | usec_t test_mtime; | |
173 | int r; | |
174 | ||
175 | test_uid = geteuid() == 0 ? 65534 : getuid(); | |
176 | test_gid = geteuid() == 0 ? 65534 : getgid(); | |
177 | ||
178 | test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK); | |
179 | ||
180 | a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX"); | |
181 | assert_se(mkdtemp_malloc(a, &p) >= 0); | |
182 | ||
183 | a = strjoina(p, "/regular"); | |
184 | r = touch_file(a, false, test_mtime, test_uid, test_gid, 0640); | |
185 | if (r < 0) { | |
186 | assert_se(IN_SET(r, -EINVAL, -ENOSYS, -ENOTTY, -EPERM)); | |
187 | return (void) log_tests_skipped_errno(errno, "touch_file() not possible"); | |
188 | } | |
189 | ||
190 | assert_se(lstat(a, &st) >= 0); | |
191 | assert_se(st.st_uid == test_uid); | |
192 | assert_se(st.st_gid == test_gid); | |
193 | assert_se(S_ISREG(st.st_mode)); | |
194 | assert_se((st.st_mode & 0777) == 0640); | |
195 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
196 | ||
197 | a = strjoina(p, "/dir"); | |
198 | assert_se(mkdir(a, 0775) >= 0); | |
199 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
200 | assert_se(lstat(a, &st) >= 0); | |
201 | assert_se(st.st_uid == test_uid); | |
202 | assert_se(st.st_gid == test_gid); | |
203 | assert_se(S_ISDIR(st.st_mode)); | |
204 | assert_se((st.st_mode & 0777) == 0640); | |
205 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
206 | ||
207 | a = strjoina(p, "/fifo"); | |
208 | assert_se(mkfifo(a, 0775) >= 0); | |
209 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
210 | assert_se(lstat(a, &st) >= 0); | |
211 | assert_se(st.st_uid == test_uid); | |
212 | assert_se(st.st_gid == test_gid); | |
213 | assert_se(S_ISFIFO(st.st_mode)); | |
214 | assert_se((st.st_mode & 0777) == 0640); | |
215 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
216 | ||
217 | a = strjoina(p, "/sock"); | |
218 | assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0); | |
219 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
220 | assert_se(lstat(a, &st) >= 0); | |
221 | assert_se(st.st_uid == test_uid); | |
222 | assert_se(st.st_gid == test_gid); | |
223 | assert_se(S_ISSOCK(st.st_mode)); | |
224 | assert_se((st.st_mode & 0777) == 0640); | |
225 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
226 | ||
227 | if (geteuid() == 0) { | |
228 | a = strjoina(p, "/bdev"); | |
229 | r = mknod(a, 0775 | S_IFBLK, makedev(0, 0)); | |
230 | if (r < 0 && errno == EPERM && detect_container() > 0) { | |
231 | log_notice("Running in unprivileged container? Skipping remaining tests in %s", __func__); | |
232 | return; | |
233 | } | |
234 | assert_se(r >= 0); | |
235 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
236 | assert_se(lstat(a, &st) >= 0); | |
237 | assert_se(st.st_uid == test_uid); | |
238 | assert_se(st.st_gid == test_gid); | |
239 | assert_se(S_ISBLK(st.st_mode)); | |
240 | assert_se((st.st_mode & 0777) == 0640); | |
241 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
242 | ||
243 | a = strjoina(p, "/cdev"); | |
244 | assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0); | |
245 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
246 | assert_se(lstat(a, &st) >= 0); | |
247 | assert_se(st.st_uid == test_uid); | |
248 | assert_se(st.st_gid == test_gid); | |
249 | assert_se(S_ISCHR(st.st_mode)); | |
250 | assert_se((st.st_mode & 0777) == 0640); | |
251 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
252 | } | |
253 | ||
254 | a = strjoina(p, "/lnk"); | |
255 | assert_se(symlink("target", a) >= 0); | |
256 | assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0); | |
257 | assert_se(lstat(a, &st) >= 0); | |
258 | assert_se(st.st_uid == test_uid); | |
259 | assert_se(st.st_gid == test_gid); | |
260 | assert_se(S_ISLNK(st.st_mode)); | |
261 | assert_se(timespec_load(&st.st_mtim) == test_mtime); | |
262 | } | |
263 | ||
264 | TEST(unlinkat_deallocate) { | |
265 | _cleanup_free_ char *p = NULL; | |
266 | _cleanup_close_ int fd = -EBADF; | |
267 | struct stat st; | |
268 | ||
269 | assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0); | |
270 | ||
271 | fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600); | |
272 | assert_se(fd >= 0); | |
273 | ||
274 | assert_se(write(fd, "hallo\n", 6) == 6); | |
275 | ||
276 | ASSERT_OK_ERRNO(fstat(fd, &st)); | |
277 | assert_se(st.st_size == 6); | |
278 | assert_se(st.st_blocks > 0); | |
279 | assert_se(st.st_nlink == 1); | |
280 | ||
281 | assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0); | |
282 | ||
283 | ASSERT_OK_ERRNO(fstat(fd, &st)); | |
284 | assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6 | |
285 | (it worked) or 0 (we had to resort to truncation) */ | |
286 | assert_se(st.st_blocks == 0); | |
287 | assert_se(st.st_nlink == 0); | |
288 | } | |
289 | ||
290 | TEST(fsync_directory_of_file) { | |
291 | _cleanup_close_ int fd = -EBADF; | |
292 | ||
293 | fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR); | |
294 | assert_se(fd >= 0); | |
295 | ||
296 | assert_se(fsync_directory_of_file(fd) >= 0); | |
297 | } | |
298 | ||
299 | TEST(rename_noreplace) { | |
300 | static const char* const table[] = { | |
301 | "/reg", | |
302 | "/dir", | |
303 | "/fifo", | |
304 | "/socket", | |
305 | "/symlink", | |
306 | NULL | |
307 | }; | |
308 | ||
309 | _cleanup_(rm_rf_physical_and_freep) char *z = NULL; | |
310 | const char *j = NULL; | |
311 | ||
312 | if (arg_test_dir) | |
313 | j = strjoina(arg_test_dir, "/testXXXXXX"); | |
314 | assert_se(mkdtemp_malloc(j, &z) >= 0); | |
315 | ||
316 | j = strjoina(z, table[0]); | |
317 | assert_se(touch(j) >= 0); | |
318 | ||
319 | j = strjoina(z, table[1]); | |
320 | assert_se(mkdir(j, 0777) >= 0); | |
321 | ||
322 | j = strjoina(z, table[2]); | |
323 | (void) mkfifo(j, 0777); | |
324 | ||
325 | j = strjoina(z, table[3]); | |
326 | (void) mknod(j, S_IFSOCK | 0777, 0); | |
327 | ||
328 | j = strjoina(z, table[4]); | |
329 | (void) symlink("foobar", j); | |
330 | ||
331 | STRV_FOREACH(a, table) { | |
332 | _cleanup_free_ char *x = NULL, *y = NULL; | |
333 | ||
334 | x = strjoin(z, *a); | |
335 | assert_se(x); | |
336 | ||
337 | if (access(x, F_OK) < 0) { | |
338 | assert_se(errno == ENOENT); | |
339 | continue; | |
340 | } | |
341 | ||
342 | STRV_FOREACH(b, table) { | |
343 | _cleanup_free_ char *w = NULL; | |
344 | ||
345 | w = strjoin(z, *b); | |
346 | assert_se(w); | |
347 | ||
348 | if (access(w, F_OK) < 0) { | |
349 | assert_se(errno == ENOENT); | |
350 | continue; | |
351 | } | |
352 | ||
353 | assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, w) == -EEXIST); | |
354 | } | |
355 | ||
356 | y = strjoin(z, "/somethingelse"); | |
357 | assert_se(y); | |
358 | ||
359 | assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, y) >= 0); | |
360 | assert_se(rename_noreplace(AT_FDCWD, y, AT_FDCWD, x) >= 0); | |
361 | } | |
362 | } | |
363 | ||
364 | TEST(chmod_and_chown) { | |
365 | _cleanup_(rm_rf_physical_and_freep) char *d = NULL; | |
366 | struct stat st; | |
367 | const char *p; | |
368 | ||
369 | if (geteuid() != 0 || userns_has_single_user()) | |
370 | return (void) log_tests_skipped("not running as root or in userns with single user"); | |
371 | ||
372 | BLOCK_WITH_UMASK(0000); | |
373 | ||
374 | assert_se(mkdtemp_malloc(NULL, &d) >= 0); | |
375 | ||
376 | p = strjoina(d, "/reg"); | |
377 | assert_se(mknod(p, S_IFREG | 0123, 0) >= 0); | |
378 | ||
379 | assert_se(chmod_and_chown(p, S_IFREG | 0321, 1, 2) >= 0); | |
380 | assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL); | |
381 | ||
382 | assert_se(lstat(p, &st) >= 0); | |
383 | assert_se(S_ISREG(st.st_mode)); | |
384 | assert_se((st.st_mode & 07777) == 0321); | |
385 | ||
386 | p = strjoina(d, "/dir"); | |
387 | assert_se(mkdir(p, 0123) >= 0); | |
388 | ||
389 | assert_se(chmod_and_chown(p, S_IFDIR | 0321, 1, 2) >= 0); | |
390 | assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL); | |
391 | ||
392 | assert_se(lstat(p, &st) >= 0); | |
393 | assert_se(S_ISDIR(st.st_mode)); | |
394 | assert_se((st.st_mode & 07777) == 0321); | |
395 | ||
396 | p = strjoina(d, "/lnk"); | |
397 | assert_se(symlink("idontexist", p) >= 0); | |
398 | ||
399 | assert_se(chmod_and_chown(p, S_IFLNK | 0321, 1, 2) >= 0); | |
400 | assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL); | |
401 | assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL); | |
402 | ||
403 | assert_se(lstat(p, &st) >= 0); | |
404 | assert_se(S_ISLNK(st.st_mode)); | |
405 | } | |
406 | ||
407 | static void create_binary_file(const char *p, const void *data, size_t l) { | |
408 | _cleanup_close_ int fd = -EBADF; | |
409 | ||
410 | fd = open(p, O_CREAT|O_WRONLY|O_EXCL|O_CLOEXEC, 0600); | |
411 | assert_se(fd >= 0); | |
412 | assert_se(write(fd, data, l) == (ssize_t) l); | |
413 | } | |
414 | ||
415 | TEST(conservative_rename) { | |
416 | _cleanup_(unlink_and_freep) char *p = NULL; | |
417 | _cleanup_free_ char *q = NULL; | |
418 | size_t l = 16*1024 + random_u64() % (32 * 1024); /* some randomly sized buffer 16k…48k */ | |
419 | uint8_t buffer[l+1]; | |
420 | ||
421 | random_bytes(buffer, l); | |
422 | ||
423 | assert_se(tempfn_random_child(NULL, NULL, &p) >= 0); | |
424 | create_binary_file(p, buffer, l); | |
425 | ||
426 | assert_se(tempfn_random_child(NULL, NULL, &q) >= 0); | |
427 | ||
428 | /* Check that the hardlinked "copy" is detected */ | |
429 | assert_se(link(p, q) >= 0); | |
430 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0); | |
431 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
432 | ||
433 | /* Check that a manual copy is detected */ | |
434 | assert_se(copy_file(p, q, 0, MODE_INVALID, COPY_REFLINK) >= 0); | |
435 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0); | |
436 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
437 | ||
438 | /* Check that a manual new writeout is also detected */ | |
439 | create_binary_file(q, buffer, l); | |
440 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0); | |
441 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
442 | ||
443 | /* Check that a minimally changed version is detected */ | |
444 | buffer[47] = ~buffer[47]; | |
445 | create_binary_file(q, buffer, l); | |
446 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0); | |
447 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
448 | ||
449 | /* Check that this really is new updated version */ | |
450 | create_binary_file(q, buffer, l); | |
451 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0); | |
452 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
453 | ||
454 | /* Make sure we detect extended files */ | |
455 | buffer[l++] = 47; | |
456 | create_binary_file(q, buffer, l); | |
457 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0); | |
458 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
459 | ||
460 | /* Make sure we detect truncated files */ | |
461 | l--; | |
462 | create_binary_file(q, buffer, l); | |
463 | assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0); | |
464 | assert_se(access(q, F_OK) < 0 && errno == ENOENT); | |
465 | } | |
466 | ||
467 | static void test_rmdir_parents_one( | |
468 | const char *prefix, | |
469 | const char *path, | |
470 | const char *stop, | |
471 | int expected, | |
472 | const char *test_exist, | |
473 | const char *test_nonexist_subdir) { | |
474 | ||
475 | const char *p, *s; | |
476 | ||
477 | log_debug("/* %s(%s, %s) */", __func__, path, stop); | |
478 | ||
479 | p = strjoina(prefix, path); | |
480 | s = strjoina(prefix, stop); | |
481 | ||
482 | if (expected >= 0) | |
483 | assert_se(mkdir_parents(p, 0700) >= 0); | |
484 | ||
485 | assert_se(rmdir_parents(p, s) == expected); | |
486 | ||
487 | if (expected >= 0) { | |
488 | const char *e, *f; | |
489 | ||
490 | e = strjoina(prefix, test_exist); | |
491 | f = strjoina(e, test_nonexist_subdir); | |
492 | ||
493 | assert_se(access(e, F_OK) >= 0); | |
494 | assert_se(access(f, F_OK) < 0); | |
495 | } | |
496 | } | |
497 | ||
498 | TEST(rmdir_parents) { | |
499 | char *temp; | |
500 | ||
501 | temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX"); | |
502 | assert_se(mkdtemp(temp)); | |
503 | ||
504 | test_rmdir_parents_one(temp, "/aaa/../hoge/foo", "/hoge/foo", -EINVAL, NULL, NULL); | |
505 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc", "/hoge/../aaa", -EINVAL, NULL, NULL); | |
506 | ||
507 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc/ddd", 0, "/aaa/bbb/ccc/ddd", "/eee"); | |
508 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc", 0, "/aaa/bbb/ccc", "/ddd"); | |
509 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb", 0, "/aaa/bbb", "/ccc"); | |
510 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa", 0, "/aaa", "/bbb"); | |
511 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/", 0, "/", "/aaa"); | |
512 | ||
513 | test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/hoge/foo", 0, "/aaa", "/bbb"); | |
514 | test_rmdir_parents_one(temp, "/aaa////bbb/.//ccc//ddd/eee///./.", "///././aaa/.", 0, "/aaa", "/bbb"); | |
515 | ||
516 | assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); | |
517 | } | |
518 | ||
519 | static void test_parse_cifs_service_one(const char *f, const char *h, const char *s, const char *d, int ret) { | |
520 | _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL; | |
521 | ||
522 | assert_se(parse_cifs_service(f, &a, &b, &c) == ret); | |
523 | ASSERT_STREQ(a, h); | |
524 | ASSERT_STREQ(b, s); | |
525 | ASSERT_STREQ(c, d); | |
526 | } | |
527 | ||
528 | TEST(parse_cifs_service) { | |
529 | test_parse_cifs_service_one("//foo/bar/baz", "foo", "bar", "baz", 0); | |
530 | test_parse_cifs_service_one("\\\\foo\\bar\\baz", "foo", "bar", "baz", 0); | |
531 | test_parse_cifs_service_one("//foo/bar", "foo", "bar", NULL, 0); | |
532 | test_parse_cifs_service_one("\\\\foo\\bar", "foo", "bar", NULL, 0); | |
533 | test_parse_cifs_service_one("//foo/bar/baz/uuu", "foo", "bar", "baz/uuu", 0); | |
534 | test_parse_cifs_service_one("\\\\foo\\bar\\baz\\uuu", "foo", "bar", "baz/uuu", 0); | |
535 | ||
536 | test_parse_cifs_service_one(NULL, NULL, NULL, NULL, -EINVAL); | |
537 | test_parse_cifs_service_one("", NULL, NULL, NULL, -EINVAL); | |
538 | test_parse_cifs_service_one("abc", NULL, NULL, NULL, -EINVAL); | |
539 | test_parse_cifs_service_one("abc/cde/efg", NULL, NULL, NULL, -EINVAL); | |
540 | test_parse_cifs_service_one("//foo/bar/baz/..", NULL, NULL, NULL, -EINVAL); | |
541 | test_parse_cifs_service_one("//foo///", NULL, NULL, NULL, -EINVAL); | |
542 | test_parse_cifs_service_one("//foo/.", NULL, NULL, NULL, -EINVAL); | |
543 | test_parse_cifs_service_one("//foo/a/.", NULL, NULL, NULL, -EINVAL); | |
544 | test_parse_cifs_service_one("//./a", NULL, NULL, NULL, -EINVAL); | |
545 | } | |
546 | ||
547 | TEST(open_mkdir_at) { | |
548 | _cleanup_close_ int fd = -EBADF, subdir_fd = -EBADF, subsubdir_fd = -EBADF; | |
549 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
550 | struct stat sta, stb; | |
551 | ||
552 | assert_se(open_mkdir_at(AT_FDCWD, "/", O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
553 | assert_se(open_mkdir_at(AT_FDCWD, ".", O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
554 | ||
555 | fd = open_mkdir_at(AT_FDCWD, "/", O_CLOEXEC, 0); | |
556 | assert_se(fd >= 0); | |
557 | assert_se(stat("/", &sta) >= 0); | |
558 | ASSERT_OK_ERRNO(fstat(fd, &stb)); | |
559 | assert_se(stat_inode_same(&sta, &stb)); | |
560 | fd = safe_close(fd); | |
561 | ||
562 | fd = open_mkdir_at(AT_FDCWD, ".", O_CLOEXEC, 0); | |
563 | assert_se(stat(".", &sta) >= 0); | |
564 | ASSERT_OK_ERRNO(fstat(fd, &stb)); | |
565 | assert_se(stat_inode_same(&sta, &stb)); | |
566 | fd = safe_close(fd); | |
567 | ||
568 | assert_se(open_mkdir_at(AT_FDCWD, "/proc", O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
569 | ||
570 | fd = open_mkdir_at(AT_FDCWD, "/proc", O_CLOEXEC, 0); | |
571 | assert_se(fd >= 0); | |
572 | fd = safe_close(fd); | |
573 | ||
574 | assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
575 | assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_CLOEXEC, 0) == -EEXIST); | |
576 | ||
577 | assert_se(mkdtemp_malloc(NULL, &t) >= 0); | |
578 | ||
579 | assert_se(open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
580 | assert_se(open_mkdir_at(AT_FDCWD, t, O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
581 | ||
582 | fd = open_mkdir_at(AT_FDCWD, t, O_CLOEXEC, 0000); | |
583 | assert_se(fd >= 0); | |
584 | fd = safe_close(fd); | |
585 | ||
586 | fd = open_mkdir_at(AT_FDCWD, t, O_PATH|O_CLOEXEC, 0000); | |
587 | assert_se(fd >= 0); | |
588 | ||
589 | subdir_fd = open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0700); | |
590 | assert_se(subdir_fd >= 0); | |
591 | ||
592 | assert_se(open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
593 | ||
594 | subsubdir_fd = open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0700); | |
595 | assert_se(subsubdir_fd >= 0); | |
596 | subsubdir_fd = safe_close(subsubdir_fd); | |
597 | ||
598 | assert_se(open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
599 | ||
600 | assert_se(open_mkdir_at(fd, "xxx/yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST); | |
601 | ||
602 | subsubdir_fd = open_mkdir_at(fd, "xxx/yyy", O_CLOEXEC, 0700); | |
603 | assert_se(subsubdir_fd >= 0); | |
604 | } | |
605 | ||
606 | TEST(openat_report_new) { | |
607 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
608 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF; | |
609 | bool b; | |
610 | ||
611 | ASSERT_OK(tfd = mkdtemp_open(NULL, 0, &t)); | |
612 | ||
613 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b); | |
614 | ASSERT_OK(fd); | |
615 | fd = safe_close(fd); | |
616 | ASSERT_TRUE(b); | |
617 | ||
618 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b); | |
619 | ASSERT_OK(fd); | |
620 | fd = safe_close(fd); | |
621 | ASSERT_FALSE(b); | |
622 | ||
623 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b); | |
624 | ASSERT_OK(fd); | |
625 | fd = safe_close(fd); | |
626 | ASSERT_FALSE(b); | |
627 | ||
628 | ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0)); | |
629 | ||
630 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b); | |
631 | ASSERT_OK(fd); | |
632 | fd = safe_close(fd); | |
633 | ASSERT_TRUE(b); | |
634 | ||
635 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b); | |
636 | ASSERT_OK(fd); | |
637 | fd = safe_close(fd); | |
638 | ASSERT_FALSE(b); | |
639 | ||
640 | ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0)); | |
641 | ||
642 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, NULL); | |
643 | ASSERT_OK(fd); | |
644 | fd = safe_close(fd); | |
645 | ||
646 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b); | |
647 | ASSERT_OK(fd); | |
648 | fd = safe_close(fd); | |
649 | ASSERT_FALSE(b); | |
650 | ||
651 | fd = openat_report_new(tfd, "test", O_RDWR, 0666, &b); | |
652 | ASSERT_OK(fd); | |
653 | fd = safe_close(fd); | |
654 | ASSERT_FALSE(b); | |
655 | ||
656 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT|O_EXCL, 0666, &b); | |
657 | ASSERT_ERROR(fd, EEXIST); | |
658 | ||
659 | ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0)); | |
660 | ||
661 | fd = openat_report_new(tfd, "test", O_RDWR, 0666, &b); | |
662 | ASSERT_ERROR(fd, ENOENT); | |
663 | ||
664 | fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT|O_EXCL, 0666, &b); | |
665 | ASSERT_OK(fd); | |
666 | fd = safe_close(fd); | |
667 | ASSERT_TRUE(b); | |
668 | ||
669 | ASSERT_OK_ERRNO(symlinkat("target", tfd, "link")); | |
670 | fd = openat_report_new(tfd, "link", O_RDWR|O_CREAT, 0666, &b); | |
671 | ASSERT_ERROR(fd, EEXIST); | |
672 | ||
673 | fd = openat_report_new(tfd, "target", O_RDWR|O_CREAT, 0666, &b); | |
674 | ASSERT_OK(fd); | |
675 | fd = safe_close(fd); | |
676 | ASSERT_TRUE(b); | |
677 | ||
678 | fd = openat_report_new(tfd, "link", O_RDWR|O_CREAT, 0666, &b); | |
679 | ASSERT_OK(fd); | |
680 | fd = safe_close(fd); | |
681 | ASSERT_FALSE(b); | |
682 | } | |
683 | ||
684 | TEST(xopenat_full) { | |
685 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
686 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF; | |
687 | ||
688 | assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); | |
689 | ||
690 | /* Test that xopenat_full() creates directories if O_DIRECTORY is specified. */ | |
691 | ||
692 | assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755)) >= 0); | |
693 | assert_se((fd_verify_directory(fd) >= 0)); | |
694 | fd = safe_close(fd); | |
695 | ||
696 | assert_se(xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755) == -EEXIST); | |
697 | ||
698 | assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_CLOEXEC, 0, 0755)) >= 0); | |
699 | assert_se((fd_verify_directory(fd) >= 0)); | |
700 | fd = safe_close(fd); | |
701 | ||
702 | /* Test that xopenat_full() creates regular files if O_DIRECTORY is not specified. */ | |
703 | ||
704 | assert_se((fd = xopenat_full(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0, 0644)) >= 0); | |
705 | assert_se(fd_verify_regular(fd) >= 0); | |
706 | fd = safe_close(fd); | |
707 | ||
708 | /* Test that we can reopen an existing fd with xopenat_full() by specifying an empty path. */ | |
709 | ||
710 | assert_se((fd = xopenat_full(tfd, "def", O_PATH|O_CLOEXEC, 0, 0)) >= 0); | |
711 | assert_se((fd2 = xopenat_full(fd, "", O_RDWR|O_CLOEXEC, 0, 0644)) >= 0); | |
712 | } | |
713 | ||
714 | TEST(xopenat_regular) { | |
715 | ||
716 | _cleanup_close_ int fd = -EBADF; | |
717 | ||
718 | assert_se(xopenat_full(AT_FDCWD, "/dev/null", O_RDWR|O_CLOEXEC, XO_REGULAR, 0) == -EBADFD); | |
719 | assert_se(xopenat_full(AT_FDCWD, "/proc", O_RDONLY|O_CLOEXEC, XO_REGULAR, 0) == -EISDIR); | |
720 | assert_se(xopenat_full(AT_FDCWD, "/proc/self", O_RDONLY|O_CLOEXEC, XO_REGULAR, 0) == -EISDIR); | |
721 | assert_se(xopenat_full(AT_FDCWD, "/proc/self", O_RDONLY|O_CLOEXEC|O_NOFOLLOW, XO_REGULAR, 0) == -ELOOP); | |
722 | ||
723 | fd = xopenat_full(AT_FDCWD, "/proc/mounts", O_RDONLY|O_CLOEXEC, XO_REGULAR, 0); | |
724 | assert_se(fd >= 0); | |
725 | fd = safe_close(fd); | |
726 | ||
727 | assert_se(xopenat_full(AT_FDCWD, "/proc/mounts", O_RDONLY|O_CLOEXEC|O_NOFOLLOW, XO_REGULAR, 0) == -ELOOP); | |
728 | ||
729 | fd = xopenat_full(AT_FDCWD, "/proc/mounts", O_RDONLY|O_CLOEXEC|O_PATH, XO_REGULAR, 0); | |
730 | assert_se(fd >= 0); | |
731 | fd = safe_close(fd); | |
732 | ||
733 | fd = xopenat_full(AT_FDCWD, "/tmp/xopenat-regular-test", O_RDWR|O_CLOEXEC|O_CREAT, XO_REGULAR, 0600); | |
734 | assert_se(fd >= 0); | |
735 | fd = safe_close(fd); | |
736 | ||
737 | assert_se(xopenat_full(AT_FDCWD, "/tmp/xopenat-regular-test", O_RDWR|O_CLOEXEC|O_CREAT|O_EXCL, XO_REGULAR, 0600) == -EEXIST); | |
738 | ||
739 | assert_se(unlink("/tmp/xopenat-regular-test") >= 0); | |
740 | } | |
741 | ||
742 | TEST(xopenat_lock_full) { | |
743 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
744 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF; | |
745 | siginfo_t si; | |
746 | ||
747 | assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); | |
748 | ||
749 | /* Test that we can acquire an exclusive lock on a directory in one process, remove the directory, | |
750 | * and close the file descriptor and still properly create the directory and acquire the lock in | |
751 | * another process. */ | |
752 | ||
753 | fd = xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX); | |
754 | assert_se(fd >= 0); | |
755 | assert_se(faccessat(tfd, "abc", F_OK, 0) >= 0); | |
756 | assert_se(fd_verify_directory(fd) >= 0); | |
757 | assert_se(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); | |
758 | ||
759 | pid_t pid = fork(); | |
760 | assert_se(pid >= 0); | |
761 | ||
762 | if (pid == 0) { | |
763 | safe_close(fd); | |
764 | ||
765 | fd = xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX); | |
766 | assert_se(fd >= 0); | |
767 | assert_se(faccessat(tfd, "abc", F_OK, 0) >= 0); | |
768 | assert_se(fd_verify_directory(fd) >= 0); | |
769 | assert_se(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN); | |
770 | ||
771 | _exit(EXIT_SUCCESS); | |
772 | } | |
773 | ||
774 | /* We need to give the child process some time to get past the xopenat() call in xopenat_lock_full() | |
775 | * and block in the call to lock_generic() waiting for the lock to become free. We can't modify | |
776 | * xopenat_lock_full() to signal an eventfd to let us know when that has happened, so we just sleep | |
777 | * for a little and assume that's enough time for the child process to get along far enough. It | |
778 | * doesn't matter if it doesn't get far enough, in that case we just won't trigger the fallback logic | |
779 | * in xopenat_lock_full(), but the test will still succeed. */ | |
780 | assert_se(usleep_safe(20 * USEC_PER_MSEC) >= 0); | |
781 | ||
782 | assert_se(unlinkat(tfd, "abc", AT_REMOVEDIR) >= 0); | |
783 | fd = safe_close(fd); | |
784 | ||
785 | assert_se(wait_for_terminate(pid, &si) >= 0); | |
786 | assert_se(si.si_code == CLD_EXITED); | |
787 | ||
788 | assert_se(xopenat_lock_full(tfd, "abc", 0, 0, 0755, LOCK_POSIX, LOCK_EX) == -EBADF); | |
789 | assert_se(xopenat_lock_full(tfd, "def", O_DIRECTORY, 0, 0755, LOCK_POSIX, LOCK_EX) == -EBADF); | |
790 | } | |
791 | ||
792 | TEST(linkat_replace) { | |
793 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
794 | _cleanup_close_ int tfd = -EBADF; | |
795 | ||
796 | assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); | |
797 | ||
798 | _cleanup_close_ int fd1 = openat(tfd, "foo", O_CREAT|O_RDWR|O_CLOEXEC, 0600); | |
799 | assert_se(fd1 >= 0); | |
800 | ||
801 | assert_se(linkat_replace(tfd, "foo", tfd, "bar") >= 0); | |
802 | assert_se(linkat_replace(tfd, "foo", tfd, "bar") >= 0); | |
803 | ||
804 | _cleanup_close_ int fd1_check = openat(tfd, "bar", O_RDWR|O_CLOEXEC); | |
805 | assert_se(fd1_check >= 0); | |
806 | ||
807 | assert_se(inode_same_at(fd1, NULL, fd1_check, NULL, AT_EMPTY_PATH) > 0); | |
808 | ||
809 | _cleanup_close_ int fd2 = openat(tfd, "baz", O_CREAT|O_RDWR|O_CLOEXEC, 0600); | |
810 | assert_se(fd2 >= 0); | |
811 | ||
812 | assert_se(inode_same_at(fd1, NULL, fd2, NULL, AT_EMPTY_PATH) == 0); | |
813 | ||
814 | assert_se(linkat_replace(tfd, "foo", tfd, "baz") >= 0); | |
815 | ||
816 | _cleanup_close_ int fd2_check = openat(tfd, "baz", O_RDWR|O_CLOEXEC); | |
817 | ||
818 | assert_se(inode_same_at(fd2, NULL, fd2_check, NULL, AT_EMPTY_PATH) == 0); | |
819 | assert_se(inode_same_at(fd1, NULL, fd2_check, NULL, AT_EMPTY_PATH) > 0); | |
820 | } | |
821 | ||
822 | static int intro(void) { | |
823 | arg_test_dir = saved_argv[1]; | |
824 | return EXIT_SUCCESS; | |
825 | } | |
826 | ||
827 | TEST(readlinkat_malloc) { | |
828 | _cleanup_(rm_rf_physical_and_freep) char *t = NULL; | |
829 | _cleanup_close_ int tfd = -EBADF, fd = -EBADF; | |
830 | _cleanup_free_ char *p = NULL, *q = NULL; | |
831 | const char *expect = "hgoehogefoobar"; | |
832 | ||
833 | tfd = mkdtemp_open(NULL, O_PATH, &t); | |
834 | assert_se(tfd >= 0); | |
835 | ||
836 | assert_se(symlinkat(expect, tfd, "linkname") >= 0); | |
837 | ||
838 | assert_se(readlinkat_malloc(tfd, "linkname", &p) >= 0); | |
839 | ASSERT_STREQ(p, expect); | |
840 | p = mfree(p); | |
841 | ||
842 | fd = openat(tfd, "linkname", O_PATH | O_NOFOLLOW | O_CLOEXEC); | |
843 | assert_se(fd >= 0); | |
844 | assert_se(readlinkat_malloc(fd, NULL, &p) >= 0); | |
845 | ASSERT_STREQ(p, expect); | |
846 | p = mfree(p); | |
847 | assert_se(readlinkat_malloc(fd, "", &p) >= 0); | |
848 | ASSERT_STREQ(p, expect); | |
849 | p = mfree(p); | |
850 | fd = safe_close(fd); | |
851 | ||
852 | assert_se(q = path_join(t, "linkname")); | |
853 | assert_se(readlinkat_malloc(AT_FDCWD, q, &p) >= 0); | |
854 | ASSERT_STREQ(p, expect); | |
855 | p = mfree(p); | |
856 | assert_se(readlinkat_malloc(INT_MAX, q, &p) >= 0); | |
857 | ASSERT_STREQ(p, expect); | |
858 | p = mfree(p); | |
859 | q = mfree(q); | |
860 | } | |
861 | ||
862 | DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |