]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fs-util.c
man: improve Description= documentation (#38101)
[thirdparty/systemd.git] / src / test / test-fs-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c270684a 2
fa34123c 3#include <stdlib.h>
ac8db36c 4#include <sys/file.h>
3b7d3320 5#include <sys/sysmacros.h>
c270684a
RC
6#include <unistd.h>
7
8#include "alloc-util.h"
fa34123c 9#include "argv-util.h"
10981424 10#include "copy.h"
c270684a
RC
11#include "fd-util.h"
12#include "fs-util.h"
c270684a 13#include "mkdir.h"
d944dc95 14#include "path-util.h"
2646b86d 15#include "process-util.h"
06fed305 16#include "random-util.h"
c270684a 17#include "rm-rf.h"
8f22fe32 18#include "stat-util.h"
c270684a
RC
19#include "string-util.h"
20#include "strv.h"
bf819d3a 21#include "sync-util.h"
27964854 22#include "tests.h"
fa34123c 23#include "time-util.h"
e4de7287 24#include "tmpfile-util.h"
c1447be4 25#include "umask-util.h"
9590065f 26#include "virt.h"
c270684a 27
27964854
ZJS
28static const char *arg_test_dir = NULL;
29
4f7452a8 30TEST(readlink_and_make_absolute) {
27964854
ZJS
31 const char *tempdir, *name, *name2, *name_alias;
32 _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL;
33
27964854
ZJS
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");
c270684a 38
37c1d5e9 39 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0);
c270684a
RC
40 assert_se(touch(name) >= 0);
41
27964854
ZJS
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);
c79e88b3 47 ASSERT_STREQ(r1, name);
27964854 48 assert_se(unlink(name_alias) >= 0);
c270684a 49
27964854 50 assert_se(safe_getcwd(&pwd) >= 0);
cd76d4c2 51
27964854
ZJS
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);
c79e88b3 55 ASSERT_STREQ(r2, name);
27964854 56 assert_se(unlink(name_alias) >= 0);
c270684a 57
27964854
ZJS
58 assert_se(chdir(pwd) >= 0);
59 }
cd76d4c2 60
c270684a
RC
61 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
62}
63
4f7452a8 64TEST(get_files_in_directory) {
c270684a
RC
65 _cleanup_strv_free_ char **l = NULL, **t = NULL;
66
27964854 67 assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0);
c270684a
RC
68 assert_se(get_files_in_directory(".", &t) >= 0);
69 assert_se(get_files_in_directory(".", NULL) >= 0);
70}
71
4f7452a8 72TEST(var_tmp) {
4245eb50 73 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
992e8f22 74 const char *tmp_dir = NULL, *t;
34a8f081 75
992e8f22
LP
76 t = getenv("TMPDIR");
77 if (t) {
78 tmpdir_backup = strdup(t);
79 assert_se(tmpdir_backup);
80 }
34a8f081 81
4245eb50
MAP
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
85e55d14
YW
94 assert_se(unsetenv("TMPDIR") >= 0);
95 assert_se(unsetenv("TEMP") >= 0);
96 assert_se(unsetenv("TMP") >= 0);
34a8f081 97
992e8f22 98 assert_se(var_tmp_dir(&tmp_dir) >= 0);
c79e88b3 99 ASSERT_STREQ(tmp_dir, "/var/tmp");
34a8f081 100
992e8f22 101 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
c79e88b3 102 ASSERT_STREQ(getenv("TMPDIR"), "/tmp");
34a8f081 103
992e8f22 104 assert_se(var_tmp_dir(&tmp_dir) >= 0);
c79e88b3 105 ASSERT_STREQ(tmp_dir, "/tmp");
34a8f081 106
992e8f22 107 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
c79e88b3 108 ASSERT_STREQ(getenv("TMPDIR"), "/88_does_not_exist_88");
34a8f081 109
992e8f22 110 assert_se(var_tmp_dir(&tmp_dir) >= 0);
c79e88b3 111 ASSERT_STREQ(tmp_dir, "/var/tmp");
34a8f081 112
992e8f22
LP
113 if (tmpdir_backup) {
114 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
c79e88b3 115 ASSERT_STREQ(getenv("TMPDIR"), tmpdir_backup);
34a8f081 116 }
4245eb50
MAP
117
118 if (temp_backup) {
119 assert_se(setenv("TEMP", temp_backup, true) >= 0);
c79e88b3 120 ASSERT_STREQ(getenv("TEMP"), temp_backup);
4245eb50
MAP
121 }
122
123 if (tmp_backup) {
124 assert_se(setenv("TMP", tmp_backup, true) >= 0);
c79e88b3 125 ASSERT_STREQ(getenv("TMP"), tmp_backup);
4245eb50 126 }
34a8f081
OW
127}
128
4f7452a8 129TEST(dot_or_dot_dot) {
49bfc877
LP
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
4f7452a8 139TEST(access_fd) {
57a4359e 140 _cleanup_(rmdir_and_freep) char *p = NULL;
254d1313 141 _cleanup_close_ int fd = -EBADF;
27964854 142 const char *a;
57a4359e 143
27964854
ZJS
144 a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX");
145 assert_se(mkdtemp_malloc(a, &p) >= 0);
57a4359e
LP
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
4f7452a8 167TEST(touch_file) {
9e3fa6e8
LP
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;
9590065f 173 int r;
9e3fa6e8
LP
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
27964854
ZJS
180 a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX");
181 assert_se(mkdtemp_malloc(a, &p) >= 0);
9e3fa6e8
LP
182
183 a = strjoina(p, "/regular");
27964854
ZJS
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));
eb5b8541 187 return (void) log_tests_skipped_errno(errno, "touch_file() not possible");
27964854
ZJS
188 }
189
9e3fa6e8
LP
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) {
5b5ce629
LP
228 a = strjoina(p, "/bdev");
229 r = mknod(a, 0775 | S_IFBLK, makedev(0, 0));
9590065f
YW
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);
9e3fa6e8
LP
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);
5b5ce629 239 assert_se(S_ISBLK(st.st_mode));
9e3fa6e8
LP
240 assert_se((st.st_mode & 0777) == 0640);
241 assert_se(timespec_load(&st.st_mtim) == test_mtime);
242
5b5ce629
LP
243 a = strjoina(p, "/cdev");
244 assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0);
9e3fa6e8
LP
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);
5b5ce629 249 assert_se(S_ISCHR(st.st_mode));
9e3fa6e8
LP
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));
9e3fa6e8
LP
261 assert_se(timespec_load(&st.st_mtim) == test_mtime);
262}
263
4f7452a8 264TEST(unlinkat_deallocate) {
43767d9d 265 _cleanup_free_ char *p = NULL;
254d1313 266 _cleanup_close_ int fd = -EBADF;
43767d9d
LP
267 struct stat st;
268
27964854 269 assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0);
43767d9d
LP
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
681e39c1 276 ASSERT_OK_ERRNO(fstat(fd, &st));
43767d9d
LP
277 assert_se(st.st_size == 6);
278 assert_se(st.st_blocks > 0);
279 assert_se(st.st_nlink == 1);
280
053e0626 281 assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0);
43767d9d 282
681e39c1 283 ASSERT_OK_ERRNO(fstat(fd, &st));
27964854
ZJS
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) */
43767d9d
LP
286 assert_se(st.st_blocks == 0);
287 assert_se(st.st_nlink == 0);
288}
289
4f7452a8 290TEST(fsync_directory_of_file) {
254d1313 291 _cleanup_close_ int fd = -EBADF;
11b29a96 292
27964854 293 fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR);
11b29a96
LP
294 assert_se(fd >= 0);
295
296 assert_se(fsync_directory_of_file(fd) >= 0);
297}
298
4f7452a8 299TEST(rename_noreplace) {
7158b4b3 300 static const char* const table[] = {
4a5d7761
LP
301 "/reg",
302 "/dir",
303 "/fifo",
304 "/socket",
305 "/symlink",
306 NULL
307 };
308
309 _cleanup_(rm_rf_physical_and_freep) char *z = NULL;
27964854 310 const char *j = NULL;
4a5d7761 311
27964854
ZJS
312 if (arg_test_dir)
313 j = strjoina(arg_test_dir, "/testXXXXXX");
7158b4b3 314 assert_se(mkdtemp_malloc(j, &z) >= 0);
4a5d7761
LP
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
2034c8b8 331 STRV_FOREACH(a, table) {
4a5d7761
LP
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
2034c8b8 342 STRV_FOREACH(b, table) {
b81b9d40 343 _cleanup_free_ char *w = NULL;
4a5d7761 344
bcb1eadc 345 w = strjoin(z, *b);
b81b9d40
YW
346 assert_se(w);
347
348 if (access(w, F_OK) < 0) {
4a5d7761
LP
349 assert_se(errno == ENOENT);
350 continue;
351 }
352
bcb1eadc 353 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, w) == -EEXIST);
4a5d7761
LP
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
4f7452a8 364TEST(chmod_and_chown) {
c1447be4 365 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
c1447be4
LP
366 struct stat st;
367 const char *p;
368
ef31767e
DDM
369 if (geteuid() != 0 || userns_has_single_user())
370 return (void) log_tests_skipped("not running as root or in userns with single user");
c1447be4 371
52f05ef2
LP
372 BLOCK_WITH_UMASK(0000);
373
c1447be4
LP
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
06fed305 407static void create_binary_file(const char *p, const void *data, size_t l) {
254d1313 408 _cleanup_close_ int fd = -EBADF;
06fed305
LP
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
4f7452a8 415TEST(conservative_rename) {
10981424
LP
416 _cleanup_(unlink_and_freep) char *p = NULL;
417 _cleanup_free_ char *q = NULL;
06fed305
LP
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);
10981424
LP
422
423 assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
06fed305 424 create_binary_file(p, buffer, l);
10981424
LP
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);
10195179 430 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
431 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
432
433 /* Check that a manual copy is detected */
7c2f5495 434 assert_se(copy_file(p, q, 0, MODE_INVALID, COPY_REFLINK) >= 0);
10195179 435 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
436 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
437
438 /* Check that a manual new writeout is also detected */
06fed305 439 create_binary_file(q, buffer, l);
10195179 440 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
441 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
442
443 /* Check that a minimally changed version is detected */
06fed305
LP
444 buffer[47] = ~buffer[47];
445 create_binary_file(q, buffer, l);
10195179 446 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
447 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
448
449 /* Check that this really is new updated version */
06fed305 450 create_binary_file(q, buffer, l);
10195179 451 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
452 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
453
454 /* Make sure we detect extended files */
06fed305
LP
455 buffer[l++] = 47;
456 create_binary_file(q, buffer, l);
10195179 457 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
458 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
459
460 /* Make sure we detect truncated files */
06fed305
LP
461 l--;
462 create_binary_file(q, buffer, l);
10195179 463 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
464 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
465}
466
4e046c5c
YW
467static 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
4f7452a8 498TEST(rmdir_parents) {
4e046c5c
YW
499 char *temp;
500
4e046c5c
YW
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
68def5a9
LP
519static 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);
c79e88b3
IK
523 ASSERT_STREQ(a, h);
524 ASSERT_STREQ(b, s);
525 ASSERT_STREQ(c, d);
68def5a9
LP
526}
527
4f7452a8 528TEST(parse_cifs_service) {
68def5a9
LP
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
4f7452a8 547TEST(open_mkdir_at) {
254d1313 548 _cleanup_close_ int fd = -EBADF, subdir_fd = -EBADF, subsubdir_fd = -EBADF;
c73094f3 549 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
797f6cc5
LP
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);
681e39c1 558 ASSERT_OK_ERRNO(fstat(fd, &stb));
797f6cc5
LP
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);
681e39c1 564 ASSERT_OK_ERRNO(fstat(fd, &stb));
797f6cc5
LP
565 assert_se(stat_inode_same(&sta, &stb));
566 fd = safe_close(fd);
c73094f3
LP
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
ca8503f1 606TEST(openat_report_new) {
b91ad562
DDM
607 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
608 _cleanup_close_ int tfd = -EBADF, fd = -EBADF;
ca8503f1
LP
609 bool b;
610
d9000d70 611 ASSERT_OK(tfd = mkdtemp_open(NULL, 0, &t));
ca8503f1 612
b91ad562
DDM
613 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
614 ASSERT_OK(fd);
ca8503f1 615 fd = safe_close(fd);
b91ad562 616 ASSERT_TRUE(b);
ca8503f1 617
b91ad562
DDM
618 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
619 ASSERT_OK(fd);
ca8503f1 620 fd = safe_close(fd);
b91ad562 621 ASSERT_FALSE(b);
ca8503f1 622
b91ad562
DDM
623 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
624 ASSERT_OK(fd);
ca8503f1 625 fd = safe_close(fd);
b91ad562 626 ASSERT_FALSE(b);
ca8503f1 627
b91ad562 628 ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0));
ca8503f1 629
b91ad562
DDM
630 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
631 ASSERT_OK(fd);
ca8503f1 632 fd = safe_close(fd);
b91ad562 633 ASSERT_TRUE(b);
ca8503f1 634
b91ad562
DDM
635 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
636 ASSERT_OK(fd);
ca8503f1 637 fd = safe_close(fd);
b91ad562 638 ASSERT_FALSE(b);
ca8503f1 639
b91ad562 640 ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0));
ca8503f1 641
b91ad562
DDM
642 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, NULL);
643 ASSERT_OK(fd);
ca8503f1
LP
644 fd = safe_close(fd);
645
b91ad562
DDM
646 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT, 0666, &b);
647 ASSERT_OK(fd);
ca8503f1 648 fd = safe_close(fd);
b91ad562 649 ASSERT_FALSE(b);
4d5dacbe 650
b91ad562
DDM
651 fd = openat_report_new(tfd, "test", O_RDWR, 0666, &b);
652 ASSERT_OK(fd);
4d5dacbe 653 fd = safe_close(fd);
b91ad562 654 ASSERT_FALSE(b);
4d5dacbe 655
b91ad562
DDM
656 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT|O_EXCL, 0666, &b);
657 ASSERT_ERROR(fd, EEXIST);
4d5dacbe 658
b91ad562 659 ASSERT_OK_ERRNO(unlinkat(tfd, "test", 0));
4d5dacbe 660
b91ad562
DDM
661 fd = openat_report_new(tfd, "test", O_RDWR, 0666, &b);
662 ASSERT_ERROR(fd, ENOENT);
4d5dacbe 663
b91ad562
DDM
664 fd = openat_report_new(tfd, "test", O_RDWR|O_CREAT|O_EXCL, 0666, &b);
665 ASSERT_OK(fd);
4d5dacbe 666 fd = safe_close(fd);
b91ad562 667 ASSERT_TRUE(b);
0dd82dab
DDM
668
669 ASSERT_OK_ERRNO(symlinkat("target", tfd, "link"));
670 fd = openat_report_new(tfd, "link", O_RDWR|O_CREAT, 0666, &b);
4946dd41
LP
671 ASSERT_ERROR(fd, EEXIST);
672
673 fd = openat_report_new(tfd, "target", O_RDWR|O_CREAT, 0666, &b);
0dd82dab
DDM
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);
ca8503f1
LP
682}
683
e40b11be 684TEST(xopenat_full) {
7486f9c3 685 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
06ca2db3 686 _cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd2 = -EBADF;
7486f9c3
DDM
687
688 assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0);
689
e40b11be 690 /* Test that xopenat_full() creates directories if O_DIRECTORY is specified. */
7486f9c3 691
e40b11be 692 assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755)) >= 0);
7486f9c3
DDM
693 assert_se((fd_verify_directory(fd) >= 0));
694 fd = safe_close(fd);
695
e40b11be 696 assert_se(xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_EXCL|O_CLOEXEC, 0, 0755) == -EEXIST);
7486f9c3 697
e40b11be 698 assert_se((fd = xopenat_full(tfd, "abc", O_DIRECTORY|O_CREAT|O_CLOEXEC, 0, 0755)) >= 0);
7486f9c3
DDM
699 assert_se((fd_verify_directory(fd) >= 0));
700 fd = safe_close(fd);
701
e40b11be 702 /* Test that xopenat_full() creates regular files if O_DIRECTORY is not specified. */
7486f9c3 703
e40b11be 704 assert_se((fd = xopenat_full(tfd, "def", O_CREAT|O_EXCL|O_CLOEXEC, 0, 0644)) >= 0);
7486f9c3
DDM
705 assert_se(fd_verify_regular(fd) >= 0);
706 fd = safe_close(fd);
06ca2db3 707
e40b11be 708 /* Test that we can reopen an existing fd with xopenat_full() by specifying an empty path. */
06ca2db3 709
e40b11be
YW
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);
7486f9c3
DDM
712}
713
dffafa47
LP
714TEST(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
e40b11be 742TEST(xopenat_lock_full) {
2646b86d
DDM
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
e40b11be 753 fd = xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX);
2646b86d
DDM
754 assert_se(fd >= 0);
755 assert_se(faccessat(tfd, "abc", F_OK, 0) >= 0);
756 assert_se(fd_verify_directory(fd) >= 0);
e40b11be 757 assert_se(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
2646b86d
DDM
758
759 pid_t pid = fork();
760 assert_se(pid >= 0);
761
762 if (pid == 0) {
763 safe_close(fd);
764
e40b11be 765 fd = xopenat_lock_full(tfd, "abc", O_CREAT|O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX);
2646b86d
DDM
766 assert_se(fd >= 0);
767 assert_se(faccessat(tfd, "abc", F_OK, 0) >= 0);
768 assert_se(fd_verify_directory(fd) >= 0);
e40b11be 769 assert_se(xopenat_lock_full(tfd, "abc", O_DIRECTORY|O_CLOEXEC, 0, 0755, LOCK_BSD, LOCK_EX|LOCK_NB) == -EAGAIN);
2646b86d
DDM
770
771 _exit(EXIT_SUCCESS);
772 }
773
e40b11be
YW
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. */
4251512e 780 assert_se(usleep_safe(20 * USEC_PER_MSEC) >= 0);
2646b86d
DDM
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
e40b11be
YW
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);
2646b86d
DDM
790}
791
1f27e7b7
LP
792TEST(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
99839c7e
LP
822static int intro(void) {
823 arg_test_dir = saved_argv[1];
824 return EXIT_SUCCESS;
825}
826
e4c094c0
YW
827TEST(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);
c79e88b3 839 ASSERT_STREQ(p, expect);
e4c094c0
YW
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);
c79e88b3 845 ASSERT_STREQ(p, expect);
e4c094c0
YW
846 p = mfree(p);
847 assert_se(readlinkat_malloc(fd, "", &p) >= 0);
c79e88b3 848 ASSERT_STREQ(p, expect);
e4c094c0
YW
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);
c79e88b3 854 ASSERT_STREQ(p, expect);
e4c094c0
YW
855 p = mfree(p);
856 assert_se(readlinkat_malloc(INT_MAX, q, &p) >= 0);
c79e88b3 857 ASSERT_STREQ(p, expect);
e4c094c0
YW
858 p = mfree(p);
859 q = mfree(q);
860}
861
e85fdacc 862DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);