]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fs-util.c
Merge pull request #25602 from fbuihuu/fix-TEST-73-LOCALE
[thirdparty/systemd.git] / src / test / test-fs-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c270684a
RC
2
3#include <unistd.h>
4
5#include "alloc-util.h"
f4351959 6#include "chase-symlinks.h"
10981424 7#include "copy.h"
c270684a 8#include "fd-util.h"
10981424 9#include "fileio.h"
c270684a 10#include "fs-util.h"
1ed34d75 11#include "id128-util.h"
c270684a
RC
12#include "macro.h"
13#include "mkdir.h"
d944dc95 14#include "path-util.h"
06fed305 15#include "random-util.h"
c270684a 16#include "rm-rf.h"
1ed34d75 17#include "stdio-util.h"
c270684a
RC
18#include "string-util.h"
19#include "strv.h"
bf819d3a 20#include "sync-util.h"
27964854 21#include "tests.h"
e4de7287 22#include "tmpfile-util.h"
c1447be4 23#include "umask-util.h"
f14f1806 24#include "user-util.h"
9590065f 25#include "virt.h"
c270684a 26
27964854
ZJS
27static const char *arg_test_dir = NULL;
28
4f7452a8 29TEST(chase_symlinks) {
94799c30 30 _cleanup_free_ char *result = NULL, *pwd = NULL;
19962747 31 _cleanup_close_ int pfd = -1;
27964854 32 char *temp;
b12d25a8 33 const char *top, *p, *pslash, *q, *qslash;
1f56e4ce 34 struct stat st;
19962747 35 int r;
d944dc95 36
27964854 37 temp = strjoina(arg_test_dir ?: "/tmp", "/test-chase.XXXXXX");
d944dc95
LP
38 assert_se(mkdtemp(temp));
39
40 top = strjoina(temp, "/top");
41 assert_se(mkdir(top, 0700) >= 0);
42
43 p = strjoina(top, "/dot");
27964854
ZJS
44 if (symlink(".", p) < 0) {
45 assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
46 log_tests_skipped_errno(errno, "symlink() not possible");
47 goto cleanup;
48 };
d944dc95
LP
49
50 p = strjoina(top, "/dotdot");
51 assert_se(symlink("..", p) >= 0);
52
53 p = strjoina(top, "/dotdota");
54 assert_se(symlink("../a", p) >= 0);
55
56 p = strjoina(temp, "/a");
57 assert_se(symlink("b", p) >= 0);
58
59 p = strjoina(temp, "/b");
60 assert_se(symlink("/usr", p) >= 0);
61
62 p = strjoina(temp, "/start");
63 assert_se(symlink("top/dot/dotdota", p) >= 0);
64
df878e68
ZJS
65 /* Paths that use symlinks underneath the "root" */
66
a5648b80 67 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867 68 assert_se(r > 0);
d944dc95 69 assert_se(path_equal(result, "/usr"));
b12d25a8 70 result = mfree(result);
d944dc95 71
b12d25a8 72 pslash = strjoina(p, "/");
a5648b80 73 r = chase_symlinks(pslash, NULL, 0, &result, NULL);
b12d25a8
ZJS
74 assert_se(r > 0);
75 assert_se(path_equal(result, "/usr/"));
d944dc95 76 result = mfree(result);
b12d25a8 77
a5648b80 78 r = chase_symlinks(p, temp, 0, &result, NULL);
d944dc95
LP
79 assert_se(r == -ENOENT);
80
a5648b80 81 r = chase_symlinks(pslash, temp, 0, &result, NULL);
b12d25a8
ZJS
82 assert_se(r == -ENOENT);
83
d944dc95 84 q = strjoina(temp, "/usr");
a9fb0867 85
a5648b80 86 r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
87 assert_se(r == 0);
88 assert_se(path_equal(result, q));
b12d25a8 89 result = mfree(result);
a9fb0867 90
b12d25a8 91 qslash = strjoina(q, "/");
d944dc95 92
a5648b80 93 r = chase_symlinks(pslash, temp, CHASE_NONEXISTENT, &result, NULL);
b12d25a8
ZJS
94 assert_se(r == 0);
95 assert_se(path_equal(result, qslash));
f4b85a0f 96 result = mfree(result);
b12d25a8
ZJS
97
98 assert_se(mkdir(q, 0700) >= 0);
99
a5648b80 100 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 101 assert_se(r > 0);
d944dc95 102 assert_se(path_equal(result, q));
b12d25a8
ZJS
103 result = mfree(result);
104
a5648b80 105 r = chase_symlinks(pslash, temp, 0, &result, NULL);
b12d25a8
ZJS
106 assert_se(r > 0);
107 assert_se(path_equal(result, qslash));
108 result = mfree(result);
d944dc95
LP
109
110 p = strjoina(temp, "/slash");
111 assert_se(symlink("/", p) >= 0);
112
a5648b80 113 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867 114 assert_se(r > 0);
d944dc95 115 assert_se(path_equal(result, "/"));
d944dc95 116 result = mfree(result);
b12d25a8 117
a5648b80 118 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 119 assert_se(r > 0);
d944dc95 120 assert_se(path_equal(result, temp));
b12d25a8 121 result = mfree(result);
d944dc95 122
df878e68
ZJS
123 /* Paths that would "escape" outside of the "root" */
124
125 p = strjoina(temp, "/6dots");
126 assert_se(symlink("../../..", p) >= 0);
127
a5648b80 128 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 129 assert_se(r > 0 && path_equal(result, temp));
b12d25a8 130 result = mfree(result);
df878e68
ZJS
131
132 p = strjoina(temp, "/6dotsusr");
133 assert_se(symlink("../../../usr", p) >= 0);
134
a5648b80 135 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 136 assert_se(r > 0 && path_equal(result, q));
b12d25a8 137 result = mfree(result);
df878e68
ZJS
138
139 p = strjoina(temp, "/top/8dotsusr");
140 assert_se(symlink("../../../../usr", p) >= 0);
141
a5648b80 142 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 143 assert_se(r > 0 && path_equal(result, q));
b12d25a8 144 result = mfree(result);
df878e68
ZJS
145
146 /* Paths that contain repeated slashes */
147
d944dc95
LP
148 p = strjoina(temp, "/slashslash");
149 assert_se(symlink("///usr///", p) >= 0);
150
a5648b80 151 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867 152 assert_se(r > 0);
d944dc95 153 assert_se(path_equal(result, "/usr"));
3c7b4ebf 154 assert_se(streq(result, "/usr")); /* we guarantee that we drop redundant slashes */
d944dc95 155 result = mfree(result);
b12d25a8 156
a5648b80 157 r = chase_symlinks(p, temp, 0, &result, NULL);
a9fb0867 158 assert_se(r > 0);
d944dc95 159 assert_se(path_equal(result, q));
b12d25a8 160 result = mfree(result);
d944dc95 161
2a2fe6ed
DM
162 /* Paths underneath the "root" with different UIDs while using CHASE_SAFE */
163
164 if (geteuid() == 0) {
165 p = strjoina(temp, "/user");
166 assert_se(mkdir(p, 0755) >= 0);
167 assert_se(chown(p, UID_NOBODY, GID_NOBODY) >= 0);
168
169 q = strjoina(temp, "/user/root");
170 assert_se(mkdir(q, 0755) >= 0);
171
172 p = strjoina(q, "/link");
173 assert_se(symlink("/", p) >= 0);
174
175 /* Fail when user-owned directories contain root-owned subdirectories. */
a5648b80 176 r = chase_symlinks(p, temp, CHASE_SAFE, &result, NULL);
2a2fe6ed
DM
177 assert_se(r == -ENOLINK);
178 result = mfree(result);
179
180 /* Allow this when the user-owned directories are all in the "root". */
a5648b80 181 r = chase_symlinks(p, q, CHASE_SAFE, &result, NULL);
2a2fe6ed
DM
182 assert_se(r > 0);
183 result = mfree(result);
184 }
185
df878e68
ZJS
186 /* Paths using . */
187
a5648b80 188 r = chase_symlinks("/etc/./.././", NULL, 0, &result, NULL);
a9fb0867 189 assert_se(r > 0);
d944dc95 190 assert_se(path_equal(result, "/"));
d944dc95 191 result = mfree(result);
b12d25a8 192
a5648b80 193 r = chase_symlinks("/etc/./.././", "/etc", 0, &result, NULL);
a9fb0867 194 assert_se(r > 0 && path_equal(result, "/etc"));
d944dc95 195 result = mfree(result);
b12d25a8 196
a5648b80 197 r = chase_symlinks("/../.././//../../etc", NULL, 0, &result, NULL);
95f35ccc
YW
198 assert_se(r > 0);
199 assert_se(streq(result, "/etc"));
200 result = mfree(result);
201
a5648b80 202 r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result, NULL);
95f35ccc
YW
203 assert_se(r == 0);
204 assert_se(streq(result, "/test-chase.fsldajfl"));
205 result = mfree(result);
206
a5648b80 207 r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result, NULL);
95f35ccc
YW
208 assert_se(r > 0);
209 assert_se(streq(result, "/etc"));
210 result = mfree(result);
211
a5648b80 212 r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result, NULL);
95f35ccc
YW
213 assert_se(r == 0);
214 assert_se(streq(result, "/test-chase.fsldajfl"));
215 result = mfree(result);
216
a5648b80 217 r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result, NULL);
92ebf177 218 assert_se(IN_SET(r, -ENOTDIR, -ENOENT));
b12d25a8 219 result = mfree(result);
d944dc95 220
df878e68
ZJS
221 /* Path that loops back to self */
222
d944dc95
LP
223 p = strjoina(temp, "/recursive-symlink");
224 assert_se(symlink("recursive-symlink", p) >= 0);
a5648b80 225 r = chase_symlinks(p, NULL, 0, &result, NULL);
d944dc95
LP
226 assert_se(r == -ELOOP);
227
a9fb0867
LP
228 /* Path which doesn't exist */
229
230 p = strjoina(temp, "/idontexist");
a5648b80 231 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867
LP
232 assert_se(r == -ENOENT);
233
a5648b80 234 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
235 assert_se(r == 0);
236 assert_se(path_equal(result, p));
237 result = mfree(result);
238
239 p = strjoina(temp, "/idontexist/meneither");
a5648b80 240 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867
LP
241 assert_se(r == -ENOENT);
242
a5648b80 243 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
244 assert_se(r == 0);
245 assert_se(path_equal(result, p));
246 result = mfree(result);
247
94799c30
DDM
248 /* Relative paths */
249
250 assert_se(safe_getcwd(&pwd) >= 0);
251
252 assert_se(chdir(temp) >= 0);
253
254 p = "this/is/a/relative/path";
255 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
256 assert_se(r == 0);
257
258 p = strjoina(temp, "/", p);
259 assert_se(path_equal(result, p));
260 result = mfree(result);
261
262 p = "this/is/a/relative/path";
263 r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result, NULL);
264 assert_se(r == 0);
265
266 p = strjoina(temp, "/", p);
267 assert_se(path_equal(result, p));
268 result = mfree(result);
269
270 assert_se(chdir(pwd) >= 0);
271
a9fb0867
LP
272 /* Path which doesn't exist, but contains weird stuff */
273
274 p = strjoina(temp, "/idontexist/..");
a5648b80 275 r = chase_symlinks(p, NULL, 0, &result, NULL);
a9fb0867
LP
276 assert_se(r == -ENOENT);
277
a5648b80 278 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result, NULL);
a9fb0867
LP
279 assert_se(r == -ENOENT);
280
877777d7
CCW
281 p = strjoina(temp, "/target");
282 q = strjoina(temp, "/top");
283 assert_se(symlink(q, p) >= 0);
284 p = strjoina(temp, "/target/idontexist");
a5648b80 285 r = chase_symlinks(p, NULL, 0, &result, NULL);
877777d7
CCW
286 assert_se(r == -ENOENT);
287
f14f1806
LP
288 if (geteuid() == 0) {
289 p = strjoina(temp, "/priv1");
290 assert_se(mkdir(p, 0755) >= 0);
291
292 q = strjoina(p, "/priv2");
293 assert_se(mkdir(q, 0755) >= 0);
294
a5648b80 295 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
296
297 assert_se(chown(q, UID_NOBODY, GID_NOBODY) >= 0);
a5648b80 298 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
299
300 assert_se(chown(p, UID_NOBODY, GID_NOBODY) >= 0);
a5648b80 301 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
302
303 assert_se(chown(q, 0, 0) >= 0);
a5648b80 304 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) == -ENOLINK);
f14f1806
LP
305
306 assert_se(rmdir(q) >= 0);
307 assert_se(symlink("/etc/passwd", q) >= 0);
a5648b80 308 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) == -ENOLINK);
f14f1806
LP
309
310 assert_se(chown(p, 0, 0) >= 0);
a5648b80 311 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL, NULL) >= 0);
f14f1806
LP
312 }
313
1ed34d75
LP
314 p = strjoina(temp, "/machine-id-test");
315 assert_se(symlink("/usr/../etc/./machine-id", p) >= 0);
316
a5648b80 317 r = chase_symlinks(p, NULL, 0, NULL, &pfd);
079fcdd0 318 if (r != -ENOENT && sd_id128_get_machine(NULL) >= 0) {
1ed34d75
LP
319 _cleanup_close_ int fd = -1;
320 sd_id128_t a, b;
321
322 assert_se(pfd >= 0);
323
f2324783 324 fd = fd_reopen(pfd, O_RDONLY|O_CLOEXEC);
1ed34d75 325 assert_se(fd >= 0);
1ed34d75
LP
326 safe_close(pfd);
327
057bf780 328 assert_se(id128_read_fd(fd, ID128_FORMAT_PLAIN, &a) >= 0);
1ed34d75
LP
329 assert_se(sd_id128_get_machine(&b) >= 0);
330 assert_se(sd_id128_equal(a, b));
331 }
332
1f56e4ce
FB
333 /* Test CHASE_NOFOLLOW */
334
335 p = strjoina(temp, "/target");
336 q = strjoina(temp, "/symlink");
337 assert_se(symlink(p, q) >= 0);
a5648b80
ZJS
338 r = chase_symlinks(q, NULL, CHASE_NOFOLLOW, &result, &pfd);
339 assert_se(r >= 0);
340 assert_se(pfd >= 0);
1f56e4ce
FB
341 assert_se(path_equal(result, q));
342 assert_se(fstat(pfd, &st) >= 0);
343 assert_se(S_ISLNK(st.st_mode));
344 result = mfree(result);
19962747 345 pfd = safe_close(pfd);
1f56e4ce
FB
346
347 /* s1 -> s2 -> nonexistent */
348 q = strjoina(temp, "/s1");
349 assert_se(symlink("s2", q) >= 0);
350 p = strjoina(temp, "/s2");
351 assert_se(symlink("nonexistent", p) >= 0);
a5648b80
ZJS
352 r = chase_symlinks(q, NULL, CHASE_NOFOLLOW, &result, &pfd);
353 assert_se(r >= 0);
354 assert_se(pfd >= 0);
1f56e4ce
FB
355 assert_se(path_equal(result, q));
356 assert_se(fstat(pfd, &st) >= 0);
357 assert_se(S_ISLNK(st.st_mode));
358 result = mfree(result);
19962747 359 pfd = safe_close(pfd);
1f56e4ce 360
39d7af99 361 /* Test CHASE_STEP */
49eb3659
LP
362
363 p = strjoina(temp, "/start");
a5648b80 364 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
365 assert_se(r == 0);
366 p = strjoina(temp, "/top/dot/dotdota");
367 assert_se(streq(p, result));
368 result = mfree(result);
369
a5648b80 370 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659 371 assert_se(r == 0);
39d7af99 372 p = strjoina(temp, "/top/dotdota");
49eb3659
LP
373 assert_se(streq(p, result));
374 result = mfree(result);
375
a5648b80 376 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
377 assert_se(r == 0);
378 p = strjoina(temp, "/top/../a");
379 assert_se(streq(p, result));
380 result = mfree(result);
381
a5648b80 382 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
383 assert_se(r == 0);
384 p = strjoina(temp, "/a");
385 assert_se(streq(p, result));
386 result = mfree(result);
387
a5648b80 388 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
389 assert_se(r == 0);
390 p = strjoina(temp, "/b");
391 assert_se(streq(p, result));
392 result = mfree(result);
393
a5648b80 394 r = chase_symlinks(p, NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
395 assert_se(r == 0);
396 assert_se(streq("/usr", result));
397 result = mfree(result);
398
a5648b80 399 r = chase_symlinks("/usr", NULL, CHASE_STEP, &result, NULL);
49eb3659
LP
400 assert_se(r > 0);
401 assert_se(streq("/usr", result));
402 result = mfree(result);
403
6efb1257
LP
404 /* Make sure that symlinks in the "root" path are not resolved, but those below are */
405 p = strjoina("/etc/..", temp, "/self");
406 assert_se(symlink(".", p) >= 0);
407 q = strjoina(p, "/top/dot/dotdota");
408 r = chase_symlinks(q, p, 0, &result, NULL);
409 assert_se(r > 0);
410 assert_se(path_equal(path_startswith(result, p), "usr"));
411 result = mfree(result);
412
d43e78b6
LP
413 /* Test CHASE_PROHIBIT_SYMLINKS */
414
415 assert_se(chase_symlinks("top/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, NULL, NULL) == -EREMCHG);
416 assert_se(chase_symlinks("top/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_WARN, NULL, NULL) == -EREMCHG);
417 assert_se(chase_symlinks("top/dotdot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, NULL, NULL) == -EREMCHG);
418 assert_se(chase_symlinks("top/dotdot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_WARN, NULL, NULL) == -EREMCHG);
419 assert_se(chase_symlinks("top/dot/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS, NULL, NULL) == -EREMCHG);
420 assert_se(chase_symlinks("top/dot/dot", temp, CHASE_PREFIX_ROOT|CHASE_PROHIBIT_SYMLINKS|CHASE_WARN, NULL, NULL) == -EREMCHG);
421
27964854 422 cleanup:
d944dc95
LP
423 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
424}
425
4f7452a8 426TEST(unlink_noerrno) {
27964854 427 char *name;
c270684a
RC
428 int fd;
429
27964854 430 name = strjoina(arg_test_dir ?: "/tmp", "/test-close_nointr.XXXXXX");
646853bd 431 fd = mkostemp_safe(name);
c270684a
RC
432 assert_se(fd >= 0);
433 assert_se(close_nointr(fd) >= 0);
434
435 {
436 PROTECT_ERRNO;
840f606d 437 errno = 42;
c270684a 438 assert_se(unlink_noerrno(name) >= 0);
840f606d 439 assert_se(errno == 42);
c270684a 440 assert_se(unlink_noerrno(name) < 0);
840f606d 441 assert_se(errno == 42);
c270684a
RC
442 }
443}
444
4f7452a8 445TEST(readlink_and_make_absolute) {
27964854
ZJS
446 const char *tempdir, *name, *name2, *name_alias;
447 _cleanup_free_ char *r1 = NULL, *r2 = NULL, *pwd = NULL;
448
27964854
ZJS
449 tempdir = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute");
450 name = strjoina(tempdir, "/original");
451 name2 = "test-readlink_and_make_absolute/original";
452 name_alias = strjoina(arg_test_dir ?: "/tmp", "/test-readlink_and_make_absolute-alias");
c270684a 453
37c1d5e9 454 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0);
c270684a
RC
455 assert_se(touch(name) >= 0);
456
27964854
ZJS
457 if (symlink(name, name_alias) < 0) {
458 assert_se(IN_SET(errno, EINVAL, ENOSYS, ENOTTY, EPERM));
459 log_tests_skipped_errno(errno, "symlink() not possible");
460 } else {
461 assert_se(readlink_and_make_absolute(name_alias, &r1) >= 0);
462 assert_se(streq(r1, name));
463 assert_se(unlink(name_alias) >= 0);
c270684a 464
27964854 465 assert_se(safe_getcwd(&pwd) >= 0);
cd76d4c2 466
27964854
ZJS
467 assert_se(chdir(tempdir) >= 0);
468 assert_se(symlink(name2, name_alias) >= 0);
469 assert_se(readlink_and_make_absolute(name_alias, &r2) >= 0);
470 assert_se(streq(r2, name));
471 assert_se(unlink(name_alias) >= 0);
c270684a 472
27964854
ZJS
473 assert_se(chdir(pwd) >= 0);
474 }
cd76d4c2 475
c270684a
RC
476 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
477}
478
4f7452a8 479TEST(get_files_in_directory) {
c270684a
RC
480 _cleanup_strv_free_ char **l = NULL, **t = NULL;
481
27964854 482 assert_se(get_files_in_directory(arg_test_dir ?: "/tmp", &l) >= 0);
c270684a
RC
483 assert_se(get_files_in_directory(".", &t) >= 0);
484 assert_se(get_files_in_directory(".", NULL) >= 0);
485}
486
4f7452a8 487TEST(var_tmp) {
4245eb50 488 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
992e8f22 489 const char *tmp_dir = NULL, *t;
34a8f081 490
992e8f22
LP
491 t = getenv("TMPDIR");
492 if (t) {
493 tmpdir_backup = strdup(t);
494 assert_se(tmpdir_backup);
495 }
34a8f081 496
4245eb50
MAP
497 t = getenv("TEMP");
498 if (t) {
499 temp_backup = strdup(t);
500 assert_se(temp_backup);
501 }
502
503 t = getenv("TMP");
504 if (t) {
505 tmp_backup = strdup(t);
506 assert_se(tmp_backup);
507 }
508
85e55d14
YW
509 assert_se(unsetenv("TMPDIR") >= 0);
510 assert_se(unsetenv("TEMP") >= 0);
511 assert_se(unsetenv("TMP") >= 0);
34a8f081 512
992e8f22
LP
513 assert_se(var_tmp_dir(&tmp_dir) >= 0);
514 assert_se(streq(tmp_dir, "/var/tmp"));
34a8f081 515
992e8f22
LP
516 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
517 assert_se(streq(getenv("TMPDIR"), "/tmp"));
34a8f081 518
992e8f22
LP
519 assert_se(var_tmp_dir(&tmp_dir) >= 0);
520 assert_se(streq(tmp_dir, "/tmp"));
34a8f081 521
992e8f22
LP
522 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
523 assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
34a8f081 524
992e8f22
LP
525 assert_se(var_tmp_dir(&tmp_dir) >= 0);
526 assert_se(streq(tmp_dir, "/var/tmp"));
34a8f081 527
992e8f22
LP
528 if (tmpdir_backup) {
529 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
530 assert_se(streq(getenv("TMPDIR"), tmpdir_backup));
34a8f081 531 }
4245eb50
MAP
532
533 if (temp_backup) {
534 assert_se(setenv("TEMP", temp_backup, true) >= 0);
535 assert_se(streq(getenv("TEMP"), temp_backup));
536 }
537
538 if (tmp_backup) {
539 assert_se(setenv("TMP", tmp_backup, true) >= 0);
540 assert_se(streq(getenv("TMP"), tmp_backup));
541 }
34a8f081
OW
542}
543
4f7452a8 544TEST(dot_or_dot_dot) {
49bfc877
LP
545 assert_se(!dot_or_dot_dot(NULL));
546 assert_se(!dot_or_dot_dot(""));
547 assert_se(!dot_or_dot_dot("xxx"));
548 assert_se(dot_or_dot_dot("."));
549 assert_se(dot_or_dot_dot(".."));
550 assert_se(!dot_or_dot_dot(".foo"));
551 assert_se(!dot_or_dot_dot("..foo"));
552}
553
4f7452a8 554TEST(access_fd) {
57a4359e
LP
555 _cleanup_(rmdir_and_freep) char *p = NULL;
556 _cleanup_close_ int fd = -1;
27964854 557 const char *a;
57a4359e 558
27964854
ZJS
559 a = strjoina(arg_test_dir ?: "/tmp", "/access-fd.XXXXXX");
560 assert_se(mkdtemp_malloc(a, &p) >= 0);
57a4359e
LP
561
562 fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
563 assert_se(fd >= 0);
564
565 assert_se(access_fd(fd, R_OK) >= 0);
566 assert_se(access_fd(fd, F_OK) >= 0);
567 assert_se(access_fd(fd, W_OK) >= 0);
568
569 assert_se(fchmod(fd, 0000) >= 0);
570
571 assert_se(access_fd(fd, F_OK) >= 0);
572
573 if (geteuid() == 0) {
574 assert_se(access_fd(fd, R_OK) >= 0);
575 assert_se(access_fd(fd, W_OK) >= 0);
576 } else {
577 assert_se(access_fd(fd, R_OK) == -EACCES);
578 assert_se(access_fd(fd, W_OK) == -EACCES);
579 }
580}
581
4f7452a8 582TEST(touch_file) {
9e3fa6e8
LP
583 uid_t test_uid, test_gid;
584 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
585 struct stat st;
586 const char *a;
587 usec_t test_mtime;
9590065f 588 int r;
9e3fa6e8
LP
589
590 test_uid = geteuid() == 0 ? 65534 : getuid();
591 test_gid = geteuid() == 0 ? 65534 : getgid();
592
593 test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK);
594
27964854
ZJS
595 a = strjoina(arg_test_dir ?: "/dev/shm", "/touch-file-XXXXXX");
596 assert_se(mkdtemp_malloc(a, &p) >= 0);
9e3fa6e8
LP
597
598 a = strjoina(p, "/regular");
27964854
ZJS
599 r = touch_file(a, false, test_mtime, test_uid, test_gid, 0640);
600 if (r < 0) {
601 assert_se(IN_SET(r, -EINVAL, -ENOSYS, -ENOTTY, -EPERM));
602 log_tests_skipped_errno(errno, "touch_file() not possible");
603 return;
604 }
605
9e3fa6e8
LP
606 assert_se(lstat(a, &st) >= 0);
607 assert_se(st.st_uid == test_uid);
608 assert_se(st.st_gid == test_gid);
609 assert_se(S_ISREG(st.st_mode));
610 assert_se((st.st_mode & 0777) == 0640);
611 assert_se(timespec_load(&st.st_mtim) == test_mtime);
612
613 a = strjoina(p, "/dir");
614 assert_se(mkdir(a, 0775) >= 0);
615 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
616 assert_se(lstat(a, &st) >= 0);
617 assert_se(st.st_uid == test_uid);
618 assert_se(st.st_gid == test_gid);
619 assert_se(S_ISDIR(st.st_mode));
620 assert_se((st.st_mode & 0777) == 0640);
621 assert_se(timespec_load(&st.st_mtim) == test_mtime);
622
623 a = strjoina(p, "/fifo");
624 assert_se(mkfifo(a, 0775) >= 0);
625 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
626 assert_se(lstat(a, &st) >= 0);
627 assert_se(st.st_uid == test_uid);
628 assert_se(st.st_gid == test_gid);
629 assert_se(S_ISFIFO(st.st_mode));
630 assert_se((st.st_mode & 0777) == 0640);
631 assert_se(timespec_load(&st.st_mtim) == test_mtime);
632
633 a = strjoina(p, "/sock");
634 assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0);
635 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
636 assert_se(lstat(a, &st) >= 0);
637 assert_se(st.st_uid == test_uid);
638 assert_se(st.st_gid == test_gid);
639 assert_se(S_ISSOCK(st.st_mode));
640 assert_se((st.st_mode & 0777) == 0640);
641 assert_se(timespec_load(&st.st_mtim) == test_mtime);
642
643 if (geteuid() == 0) {
5b5ce629
LP
644 a = strjoina(p, "/bdev");
645 r = mknod(a, 0775 | S_IFBLK, makedev(0, 0));
9590065f
YW
646 if (r < 0 && errno == EPERM && detect_container() > 0) {
647 log_notice("Running in unprivileged container? Skipping remaining tests in %s", __func__);
648 return;
649 }
650 assert_se(r >= 0);
9e3fa6e8
LP
651 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
652 assert_se(lstat(a, &st) >= 0);
653 assert_se(st.st_uid == test_uid);
654 assert_se(st.st_gid == test_gid);
5b5ce629 655 assert_se(S_ISBLK(st.st_mode));
9e3fa6e8
LP
656 assert_se((st.st_mode & 0777) == 0640);
657 assert_se(timespec_load(&st.st_mtim) == test_mtime);
658
5b5ce629
LP
659 a = strjoina(p, "/cdev");
660 assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0);
9e3fa6e8
LP
661 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
662 assert_se(lstat(a, &st) >= 0);
663 assert_se(st.st_uid == test_uid);
664 assert_se(st.st_gid == test_gid);
5b5ce629 665 assert_se(S_ISCHR(st.st_mode));
9e3fa6e8
LP
666 assert_se((st.st_mode & 0777) == 0640);
667 assert_se(timespec_load(&st.st_mtim) == test_mtime);
668 }
669
670 a = strjoina(p, "/lnk");
671 assert_se(symlink("target", a) >= 0);
672 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
673 assert_se(lstat(a, &st) >= 0);
674 assert_se(st.st_uid == test_uid);
675 assert_se(st.st_gid == test_gid);
676 assert_se(S_ISLNK(st.st_mode));
9e3fa6e8
LP
677 assert_se(timespec_load(&st.st_mtim) == test_mtime);
678}
679
4f7452a8 680TEST(unlinkat_deallocate) {
43767d9d
LP
681 _cleanup_free_ char *p = NULL;
682 _cleanup_close_ int fd = -1;
683 struct stat st;
684
27964854 685 assert_se(tempfn_random_child(arg_test_dir, "unlink-deallocation", &p) >= 0);
43767d9d
LP
686
687 fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600);
688 assert_se(fd >= 0);
689
690 assert_se(write(fd, "hallo\n", 6) == 6);
691
692 assert_se(fstat(fd, &st) >= 0);
693 assert_se(st.st_size == 6);
694 assert_se(st.st_blocks > 0);
695 assert_se(st.st_nlink == 1);
696
053e0626 697 assert_se(unlinkat_deallocate(AT_FDCWD, p, UNLINK_ERASE) >= 0);
43767d9d
LP
698
699 assert_se(fstat(fd, &st) >= 0);
27964854
ZJS
700 assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6
701 (it worked) or 0 (we had to resort to truncation) */
43767d9d
LP
702 assert_se(st.st_blocks == 0);
703 assert_se(st.st_nlink == 0);
704}
705
4f7452a8 706TEST(fsync_directory_of_file) {
11b29a96
LP
707 _cleanup_close_ int fd = -1;
708
27964854 709 fd = open_tmpfile_unlinkable(arg_test_dir, O_RDWR);
11b29a96
LP
710 assert_se(fd >= 0);
711
712 assert_se(fsync_directory_of_file(fd) >= 0);
713}
714
4f7452a8 715TEST(rename_noreplace) {
7158b4b3 716 static const char* const table[] = {
4a5d7761
LP
717 "/reg",
718 "/dir",
719 "/fifo",
720 "/socket",
721 "/symlink",
722 NULL
723 };
724
725 _cleanup_(rm_rf_physical_and_freep) char *z = NULL;
27964854 726 const char *j = NULL;
4a5d7761 727
27964854
ZJS
728 if (arg_test_dir)
729 j = strjoina(arg_test_dir, "/testXXXXXX");
7158b4b3 730 assert_se(mkdtemp_malloc(j, &z) >= 0);
4a5d7761
LP
731
732 j = strjoina(z, table[0]);
733 assert_se(touch(j) >= 0);
734
735 j = strjoina(z, table[1]);
736 assert_se(mkdir(j, 0777) >= 0);
737
738 j = strjoina(z, table[2]);
739 (void) mkfifo(j, 0777);
740
741 j = strjoina(z, table[3]);
742 (void) mknod(j, S_IFSOCK | 0777, 0);
743
744 j = strjoina(z, table[4]);
745 (void) symlink("foobar", j);
746
2034c8b8 747 STRV_FOREACH(a, table) {
4a5d7761
LP
748 _cleanup_free_ char *x = NULL, *y = NULL;
749
750 x = strjoin(z, *a);
751 assert_se(x);
752
753 if (access(x, F_OK) < 0) {
754 assert_se(errno == ENOENT);
755 continue;
756 }
757
2034c8b8 758 STRV_FOREACH(b, table) {
b81b9d40 759 _cleanup_free_ char *w = NULL;
4a5d7761 760
bcb1eadc 761 w = strjoin(z, *b);
b81b9d40
YW
762 assert_se(w);
763
764 if (access(w, F_OK) < 0) {
4a5d7761
LP
765 assert_se(errno == ENOENT);
766 continue;
767 }
768
bcb1eadc 769 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, w) == -EEXIST);
4a5d7761
LP
770 }
771
772 y = strjoin(z, "/somethingelse");
773 assert_se(y);
774
775 assert_se(rename_noreplace(AT_FDCWD, x, AT_FDCWD, y) >= 0);
776 assert_se(rename_noreplace(AT_FDCWD, y, AT_FDCWD, x) >= 0);
777 }
778}
779
4f7452a8 780TEST(chmod_and_chown) {
c1447be4 781 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
c1447be4
LP
782 struct stat st;
783 const char *p;
784
785 if (geteuid() != 0)
786 return;
787
52f05ef2
LP
788 BLOCK_WITH_UMASK(0000);
789
c1447be4
LP
790 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
791
792 p = strjoina(d, "/reg");
793 assert_se(mknod(p, S_IFREG | 0123, 0) >= 0);
794
795 assert_se(chmod_and_chown(p, S_IFREG | 0321, 1, 2) >= 0);
796 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
797
798 assert_se(lstat(p, &st) >= 0);
799 assert_se(S_ISREG(st.st_mode));
800 assert_se((st.st_mode & 07777) == 0321);
801
802 p = strjoina(d, "/dir");
803 assert_se(mkdir(p, 0123) >= 0);
804
805 assert_se(chmod_and_chown(p, S_IFDIR | 0321, 1, 2) >= 0);
806 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
807
808 assert_se(lstat(p, &st) >= 0);
809 assert_se(S_ISDIR(st.st_mode));
810 assert_se((st.st_mode & 07777) == 0321);
811
812 p = strjoina(d, "/lnk");
813 assert_se(symlink("idontexist", p) >= 0);
814
815 assert_se(chmod_and_chown(p, S_IFLNK | 0321, 1, 2) >= 0);
816 assert_se(chmod_and_chown(p, S_IFREG | 0555, 3, 4) == -EINVAL);
817 assert_se(chmod_and_chown(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
818
819 assert_se(lstat(p, &st) >= 0);
820 assert_se(S_ISLNK(st.st_mode));
821}
822
06fed305
LP
823static void create_binary_file(const char *p, const void *data, size_t l) {
824 _cleanup_close_ int fd = -1;
825
826 fd = open(p, O_CREAT|O_WRONLY|O_EXCL|O_CLOEXEC, 0600);
827 assert_se(fd >= 0);
828 assert_se(write(fd, data, l) == (ssize_t) l);
829}
830
4f7452a8 831TEST(conservative_rename) {
10981424
LP
832 _cleanup_(unlink_and_freep) char *p = NULL;
833 _cleanup_free_ char *q = NULL;
06fed305
LP
834 size_t l = 16*1024 + random_u64() % (32 * 1024); /* some randomly sized buffer 16k…48k */
835 uint8_t buffer[l+1];
836
837 random_bytes(buffer, l);
10981424
LP
838
839 assert_se(tempfn_random_child(NULL, NULL, &p) >= 0);
06fed305 840 create_binary_file(p, buffer, l);
10981424
LP
841
842 assert_se(tempfn_random_child(NULL, NULL, &q) >= 0);
843
844 /* Check that the hardlinked "copy" is detected */
845 assert_se(link(p, q) >= 0);
10195179 846 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
847 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
848
849 /* Check that a manual copy is detected */
f5fbe71d 850 assert_se(copy_file(p, q, 0, MODE_INVALID, 0, 0, COPY_REFLINK) >= 0);
10195179 851 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
852 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
853
854 /* Check that a manual new writeout is also detected */
06fed305 855 create_binary_file(q, buffer, l);
10195179 856 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
857 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
858
859 /* Check that a minimally changed version is detected */
06fed305
LP
860 buffer[47] = ~buffer[47];
861 create_binary_file(q, buffer, l);
10195179 862 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
863 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
864
865 /* Check that this really is new updated version */
06fed305 866 create_binary_file(q, buffer, l);
10195179 867 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) == 0);
10981424
LP
868 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
869
870 /* Make sure we detect extended files */
06fed305
LP
871 buffer[l++] = 47;
872 create_binary_file(q, buffer, l);
10195179 873 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
874 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
875
876 /* Make sure we detect truncated files */
06fed305
LP
877 l--;
878 create_binary_file(q, buffer, l);
10195179 879 assert_se(conservative_renameat(AT_FDCWD, q, AT_FDCWD, p) > 0);
10981424
LP
880 assert_se(access(q, F_OK) < 0 && errno == ENOENT);
881}
882
4e046c5c
YW
883static void test_rmdir_parents_one(
884 const char *prefix,
885 const char *path,
886 const char *stop,
887 int expected,
888 const char *test_exist,
889 const char *test_nonexist_subdir) {
890
891 const char *p, *s;
892
893 log_debug("/* %s(%s, %s) */", __func__, path, stop);
894
895 p = strjoina(prefix, path);
896 s = strjoina(prefix, stop);
897
898 if (expected >= 0)
899 assert_se(mkdir_parents(p, 0700) >= 0);
900
901 assert_se(rmdir_parents(p, s) == expected);
902
903 if (expected >= 0) {
904 const char *e, *f;
905
906 e = strjoina(prefix, test_exist);
907 f = strjoina(e, test_nonexist_subdir);
908
909 assert_se(access(e, F_OK) >= 0);
910 assert_se(access(f, F_OK) < 0);
911 }
912}
913
4f7452a8 914TEST(rmdir_parents) {
4e046c5c
YW
915 char *temp;
916
4e046c5c
YW
917 temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX");
918 assert_se(mkdtemp(temp));
919
920 test_rmdir_parents_one(temp, "/aaa/../hoge/foo", "/hoge/foo", -EINVAL, NULL, NULL);
921 test_rmdir_parents_one(temp, "/aaa/bbb/ccc", "/hoge/../aaa", -EINVAL, NULL, NULL);
922
923 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc/ddd", 0, "/aaa/bbb/ccc/ddd", "/eee");
924 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc", 0, "/aaa/bbb/ccc", "/ddd");
925 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb", 0, "/aaa/bbb", "/ccc");
926 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa", 0, "/aaa", "/bbb");
927 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/", 0, "/", "/aaa");
928
929 test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/hoge/foo", 0, "/aaa", "/bbb");
930 test_rmdir_parents_one(temp, "/aaa////bbb/.//ccc//ddd/eee///./.", "///././aaa/.", 0, "/aaa", "/bbb");
931
932 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
933}
934
68def5a9
LP
935static void test_parse_cifs_service_one(const char *f, const char *h, const char *s, const char *d, int ret) {
936 _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL;
937
938 assert_se(parse_cifs_service(f, &a, &b, &c) == ret);
939 assert_se(streq_ptr(a, h));
940 assert_se(streq_ptr(b, s));
941 assert_se(streq_ptr(c, d));
942}
943
4f7452a8 944TEST(parse_cifs_service) {
68def5a9
LP
945 test_parse_cifs_service_one("//foo/bar/baz", "foo", "bar", "baz", 0);
946 test_parse_cifs_service_one("\\\\foo\\bar\\baz", "foo", "bar", "baz", 0);
947 test_parse_cifs_service_one("//foo/bar", "foo", "bar", NULL, 0);
948 test_parse_cifs_service_one("\\\\foo\\bar", "foo", "bar", NULL, 0);
949 test_parse_cifs_service_one("//foo/bar/baz/uuu", "foo", "bar", "baz/uuu", 0);
950 test_parse_cifs_service_one("\\\\foo\\bar\\baz\\uuu", "foo", "bar", "baz/uuu", 0);
951
952 test_parse_cifs_service_one(NULL, NULL, NULL, NULL, -EINVAL);
953 test_parse_cifs_service_one("", NULL, NULL, NULL, -EINVAL);
954 test_parse_cifs_service_one("abc", NULL, NULL, NULL, -EINVAL);
955 test_parse_cifs_service_one("abc/cde/efg", NULL, NULL, NULL, -EINVAL);
956 test_parse_cifs_service_one("//foo/bar/baz/..", NULL, NULL, NULL, -EINVAL);
957 test_parse_cifs_service_one("//foo///", NULL, NULL, NULL, -EINVAL);
958 test_parse_cifs_service_one("//foo/.", NULL, NULL, NULL, -EINVAL);
959 test_parse_cifs_service_one("//foo/a/.", NULL, NULL, NULL, -EINVAL);
960 test_parse_cifs_service_one("//./a", NULL, NULL, NULL, -EINVAL);
961}
962
4f7452a8 963TEST(open_mkdir_at) {
c73094f3
LP
964 _cleanup_close_ int fd = -1, subdir_fd = -1, subsubdir_fd = -1;
965 _cleanup_(rm_rf_physical_and_freep) char *t = NULL;
c73094f3
LP
966
967 assert_se(open_mkdir_at(AT_FDCWD, "/proc", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
968
969 fd = open_mkdir_at(AT_FDCWD, "/proc", O_CLOEXEC, 0);
970 assert_se(fd >= 0);
971 fd = safe_close(fd);
972
973 assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
974 assert_se(open_mkdir_at(AT_FDCWD, "/bin/sh", O_CLOEXEC, 0) == -EEXIST);
975
976 assert_se(mkdtemp_malloc(NULL, &t) >= 0);
977
978 assert_se(open_mkdir_at(AT_FDCWD, t, O_EXCL|O_CLOEXEC, 0) == -EEXIST);
979 assert_se(open_mkdir_at(AT_FDCWD, t, O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
980
981 fd = open_mkdir_at(AT_FDCWD, t, O_CLOEXEC, 0000);
982 assert_se(fd >= 0);
983 fd = safe_close(fd);
984
985 fd = open_mkdir_at(AT_FDCWD, t, O_PATH|O_CLOEXEC, 0000);
986 assert_se(fd >= 0);
987
988 subdir_fd = open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0700);
989 assert_se(subdir_fd >= 0);
990
991 assert_se(open_mkdir_at(fd, "xxx", O_PATH|O_EXCL|O_CLOEXEC, 0) == -EEXIST);
992
993 subsubdir_fd = open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0700);
994 assert_se(subsubdir_fd >= 0);
995 subsubdir_fd = safe_close(subsubdir_fd);
996
997 assert_se(open_mkdir_at(subdir_fd, "yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
998
999 assert_se(open_mkdir_at(fd, "xxx/yyy", O_EXCL|O_CLOEXEC, 0) == -EEXIST);
1000
1001 subsubdir_fd = open_mkdir_at(fd, "xxx/yyy", O_CLOEXEC, 0700);
1002 assert_se(subsubdir_fd >= 0);
1003}
1004
ca8503f1
LP
1005TEST(openat_report_new) {
1006 _cleanup_free_ char *j = NULL;
1007 _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
1008 _cleanup_close_ int fd = -1;
1009 bool b;
1010
1011 assert_se(mkdtemp_malloc(NULL, &d) >= 0);
1012
1013 j = path_join(d, "test");
1014 assert_se(j);
1015
1016 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
1017 assert_se(fd >= 0);
1018 fd = safe_close(fd);
1019 assert_se(b);
1020
1021 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
1022 assert_se(fd >= 0);
1023 fd = safe_close(fd);
1024 assert_se(!b);
1025
1026 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
1027 assert_se(fd >= 0);
1028 fd = safe_close(fd);
1029 assert_se(!b);
1030
1031 assert_se(unlink(j) >= 0);
1032
1033 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
1034 assert_se(fd >= 0);
1035 fd = safe_close(fd);
1036 assert_se(b);
1037
1038 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
1039 assert_se(fd >= 0);
1040 fd = safe_close(fd);
1041 assert_se(!b);
1042
1043 assert_se(unlink(j) >= 0);
1044
1045 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, NULL);
1046 assert_se(fd >= 0);
1047 fd = safe_close(fd);
1048
1049 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT, 0666, &b);
1050 assert_se(fd >= 0);
1051 fd = safe_close(fd);
1052 assert_se(!b);
4d5dacbe
LP
1053
1054 fd = openat_report_new(AT_FDCWD, j, O_RDWR, 0666, &b);
1055 assert_se(fd >= 0);
1056 fd = safe_close(fd);
1057 assert_se(!b);
1058
1059 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT|O_EXCL, 0666, &b);
1060 assert_se(fd == -EEXIST);
1061
1062 assert_se(unlink(j) >= 0);
1063
1064 fd = openat_report_new(AT_FDCWD, j, O_RDWR, 0666, &b);
1065 assert_se(fd == -ENOENT);
1066
1067 fd = openat_report_new(AT_FDCWD, j, O_RDWR|O_CREAT|O_EXCL, 0666, &b);
1068 assert_se(fd >= 0);
1069 fd = safe_close(fd);
1070 assert_se(b);
ca8503f1
LP
1071}
1072
99839c7e
LP
1073static int intro(void) {
1074 arg_test_dir = saved_argv[1];
1075 return EXIT_SUCCESS;
1076}
1077
e85fdacc 1078DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro);