]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-fs-util.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / test / test-fs-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c270684a
RC
2
3#include <unistd.h>
4
5#include "alloc-util.h"
c270684a 6#include "fd-util.h"
1ed34d75 7#include "fd-util.h"
d944dc95 8#include "fileio.h"
c270684a 9#include "fs-util.h"
1ed34d75 10#include "id128-util.h"
c270684a
RC
11#include "macro.h"
12#include "mkdir.h"
d944dc95 13#include "path-util.h"
c270684a 14#include "rm-rf.h"
1ed34d75 15#include "stdio-util.h"
c270684a
RC
16#include "string-util.h"
17#include "strv.h"
f14f1806 18#include "user-util.h"
c270684a
RC
19#include "util.h"
20
d944dc95 21static void test_chase_symlinks(void) {
c9825701 22 _cleanup_free_ char *result = NULL;
d944dc95 23 char temp[] = "/tmp/test-chase.XXXXXX";
b12d25a8 24 const char *top, *p, *pslash, *q, *qslash;
1ed34d75 25 int r, pfd;
d944dc95
LP
26
27 assert_se(mkdtemp(temp));
28
29 top = strjoina(temp, "/top");
30 assert_se(mkdir(top, 0700) >= 0);
31
32 p = strjoina(top, "/dot");
33 assert_se(symlink(".", p) >= 0);
34
35 p = strjoina(top, "/dotdot");
36 assert_se(symlink("..", p) >= 0);
37
38 p = strjoina(top, "/dotdota");
39 assert_se(symlink("../a", p) >= 0);
40
41 p = strjoina(temp, "/a");
42 assert_se(symlink("b", p) >= 0);
43
44 p = strjoina(temp, "/b");
45 assert_se(symlink("/usr", p) >= 0);
46
47 p = strjoina(temp, "/start");
48 assert_se(symlink("top/dot/dotdota", p) >= 0);
49
df878e68
ZJS
50 /* Paths that use symlinks underneath the "root" */
51
c4f4fce7 52 r = chase_symlinks(p, NULL, 0, &result);
a9fb0867 53 assert_se(r > 0);
d944dc95 54 assert_se(path_equal(result, "/usr"));
b12d25a8 55 result = mfree(result);
d944dc95 56
b12d25a8
ZJS
57 pslash = strjoina(p, "/");
58 r = chase_symlinks(pslash, NULL, 0, &result);
59 assert_se(r > 0);
60 assert_se(path_equal(result, "/usr/"));
d944dc95 61 result = mfree(result);
b12d25a8 62
c4f4fce7 63 r = chase_symlinks(p, temp, 0, &result);
d944dc95
LP
64 assert_se(r == -ENOENT);
65
b12d25a8
ZJS
66 r = chase_symlinks(pslash, temp, 0, &result);
67 assert_se(r == -ENOENT);
68
d944dc95 69 q = strjoina(temp, "/usr");
a9fb0867 70
cb638b5e 71 r = chase_symlinks(p, temp, CHASE_NONEXISTENT, &result);
a9fb0867
LP
72 assert_se(r == 0);
73 assert_se(path_equal(result, q));
b12d25a8 74 result = mfree(result);
a9fb0867 75
b12d25a8 76 qslash = strjoina(q, "/");
d944dc95 77
b12d25a8
ZJS
78 r = chase_symlinks(pslash, temp, CHASE_NONEXISTENT, &result);
79 assert_se(r == 0);
80 assert_se(path_equal(result, qslash));
f4b85a0f 81 result = mfree(result);
b12d25a8
ZJS
82
83 assert_se(mkdir(q, 0700) >= 0);
84
c4f4fce7 85 r = chase_symlinks(p, temp, 0, &result);
a9fb0867 86 assert_se(r > 0);
d944dc95 87 assert_se(path_equal(result, q));
b12d25a8
ZJS
88 result = mfree(result);
89
90 r = chase_symlinks(pslash, temp, 0, &result);
91 assert_se(r > 0);
92 assert_se(path_equal(result, qslash));
93 result = mfree(result);
d944dc95
LP
94
95 p = strjoina(temp, "/slash");
96 assert_se(symlink("/", p) >= 0);
97
c4f4fce7 98 r = chase_symlinks(p, NULL, 0, &result);
a9fb0867 99 assert_se(r > 0);
d944dc95 100 assert_se(path_equal(result, "/"));
d944dc95 101 result = mfree(result);
b12d25a8 102
c4f4fce7 103 r = chase_symlinks(p, temp, 0, &result);
a9fb0867 104 assert_se(r > 0);
d944dc95 105 assert_se(path_equal(result, temp));
b12d25a8 106 result = mfree(result);
d944dc95 107
df878e68
ZJS
108 /* Paths that would "escape" outside of the "root" */
109
110 p = strjoina(temp, "/6dots");
111 assert_se(symlink("../../..", p) >= 0);
112
c4f4fce7 113 r = chase_symlinks(p, temp, 0, &result);
a9fb0867 114 assert_se(r > 0 && path_equal(result, temp));
b12d25a8 115 result = mfree(result);
df878e68
ZJS
116
117 p = strjoina(temp, "/6dotsusr");
118 assert_se(symlink("../../../usr", p) >= 0);
119
c4f4fce7 120 r = chase_symlinks(p, temp, 0, &result);
a9fb0867 121 assert_se(r > 0 && path_equal(result, q));
b12d25a8 122 result = mfree(result);
df878e68
ZJS
123
124 p = strjoina(temp, "/top/8dotsusr");
125 assert_se(symlink("../../../../usr", p) >= 0);
126
c4f4fce7 127 r = chase_symlinks(p, temp, 0, &result);
a9fb0867 128 assert_se(r > 0 && path_equal(result, q));
b12d25a8 129 result = mfree(result);
df878e68
ZJS
130
131 /* Paths that contain repeated slashes */
132
d944dc95
LP
133 p = strjoina(temp, "/slashslash");
134 assert_se(symlink("///usr///", p) >= 0);
135
c4f4fce7 136 r = chase_symlinks(p, NULL, 0, &result);
a9fb0867 137 assert_se(r > 0);
d944dc95 138 assert_se(path_equal(result, "/usr"));
d944dc95 139 result = mfree(result);
b12d25a8 140
c4f4fce7 141 r = chase_symlinks(p, temp, 0, &result);
a9fb0867 142 assert_se(r > 0);
d944dc95 143 assert_se(path_equal(result, q));
b12d25a8 144 result = mfree(result);
d944dc95 145
df878e68
ZJS
146 /* Paths using . */
147
c4f4fce7 148 r = chase_symlinks("/etc/./.././", NULL, 0, &result);
a9fb0867 149 assert_se(r > 0);
d944dc95 150 assert_se(path_equal(result, "/"));
d944dc95 151 result = mfree(result);
b12d25a8 152
c4f4fce7 153 r = chase_symlinks("/etc/./.././", "/etc", 0, &result);
a9fb0867 154 assert_se(r > 0 && path_equal(result, "/etc"));
d944dc95 155 result = mfree(result);
b12d25a8 156
95f35ccc
YW
157 r = chase_symlinks("/../.././//../../etc", NULL, 0, &result);
158 assert_se(r > 0);
159 assert_se(streq(result, "/etc"));
160 result = mfree(result);
161
162 r = chase_symlinks("/../.././//../../test-chase.fsldajfl", NULL, CHASE_NONEXISTENT, &result);
163 assert_se(r == 0);
164 assert_se(streq(result, "/test-chase.fsldajfl"));
165 result = mfree(result);
166
167 r = chase_symlinks("/../.././//../../etc", "/", CHASE_PREFIX_ROOT, &result);
168 assert_se(r > 0);
169 assert_se(streq(result, "/etc"));
170 result = mfree(result);
171
172 r = chase_symlinks("/../.././//../../test-chase.fsldajfl", "/", CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &result);
173 assert_se(r == 0);
174 assert_se(streq(result, "/test-chase.fsldajfl"));
175 result = mfree(result);
176
c4f4fce7 177 r = chase_symlinks("/etc/machine-id/foo", NULL, 0, &result);
d944dc95 178 assert_se(r == -ENOTDIR);
b12d25a8 179 result = mfree(result);
d944dc95 180
df878e68
ZJS
181 /* Path that loops back to self */
182
d944dc95
LP
183 p = strjoina(temp, "/recursive-symlink");
184 assert_se(symlink("recursive-symlink", p) >= 0);
c4f4fce7 185 r = chase_symlinks(p, NULL, 0, &result);
d944dc95
LP
186 assert_se(r == -ELOOP);
187
a9fb0867
LP
188 /* Path which doesn't exist */
189
190 p = strjoina(temp, "/idontexist");
191 r = chase_symlinks(p, NULL, 0, &result);
192 assert_se(r == -ENOENT);
193
cb638b5e 194 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
a9fb0867
LP
195 assert_se(r == 0);
196 assert_se(path_equal(result, p));
197 result = mfree(result);
198
199 p = strjoina(temp, "/idontexist/meneither");
200 r = chase_symlinks(p, NULL, 0, &result);
201 assert_se(r == -ENOENT);
202
cb638b5e 203 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
a9fb0867
LP
204 assert_se(r == 0);
205 assert_se(path_equal(result, p));
206 result = mfree(result);
207
208 /* Path which doesn't exist, but contains weird stuff */
209
210 p = strjoina(temp, "/idontexist/..");
211 r = chase_symlinks(p, NULL, 0, &result);
212 assert_se(r == -ENOENT);
213
cb638b5e 214 r = chase_symlinks(p, NULL, CHASE_NONEXISTENT, &result);
a9fb0867
LP
215 assert_se(r == -ENOENT);
216
877777d7
CCW
217 p = strjoina(temp, "/target");
218 q = strjoina(temp, "/top");
219 assert_se(symlink(q, p) >= 0);
220 p = strjoina(temp, "/target/idontexist");
221 r = chase_symlinks(p, NULL, 0, &result);
222 assert_se(r == -ENOENT);
223
f14f1806
LP
224 if (geteuid() == 0) {
225 p = strjoina(temp, "/priv1");
226 assert_se(mkdir(p, 0755) >= 0);
227
228 q = strjoina(p, "/priv2");
229 assert_se(mkdir(q, 0755) >= 0);
230
231 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
232
233 assert_se(chown(q, UID_NOBODY, GID_NOBODY) >= 0);
234 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
235
236 assert_se(chown(p, UID_NOBODY, GID_NOBODY) >= 0);
237 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
238
239 assert_se(chown(q, 0, 0) >= 0);
240 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) == -EPERM);
241
242 assert_se(rmdir(q) >= 0);
243 assert_se(symlink("/etc/passwd", q) >= 0);
244 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) == -EPERM);
245
246 assert_se(chown(p, 0, 0) >= 0);
247 assert_se(chase_symlinks(q, NULL, CHASE_SAFE, NULL) >= 0);
248 }
249
1ed34d75
LP
250 p = strjoina(temp, "/machine-id-test");
251 assert_se(symlink("/usr/../etc/./machine-id", p) >= 0);
252
253 pfd = chase_symlinks(p, NULL, CHASE_OPEN, NULL);
254 if (pfd != -ENOENT) {
1ed34d75
LP
255 _cleanup_close_ int fd = -1;
256 sd_id128_t a, b;
257
258 assert_se(pfd >= 0);
259
f2324783 260 fd = fd_reopen(pfd, O_RDONLY|O_CLOEXEC);
1ed34d75 261 assert_se(fd >= 0);
1ed34d75
LP
262 safe_close(pfd);
263
264 assert_se(id128_read_fd(fd, ID128_PLAIN, &a) >= 0);
265 assert_se(sd_id128_get_machine(&b) >= 0);
266 assert_se(sd_id128_equal(a, b));
267 }
268
49eb3659
LP
269 /* Test CHASE_ONE */
270
271 p = strjoina(temp, "/start");
272 r = chase_symlinks(p, NULL, CHASE_STEP, &result);
273 assert_se(r == 0);
274 p = strjoina(temp, "/top/dot/dotdota");
275 assert_se(streq(p, result));
276 result = mfree(result);
277
278 r = chase_symlinks(p, NULL, CHASE_STEP, &result);
279 assert_se(r == 0);
280 p = strjoina(temp, "/top/./dotdota");
281 assert_se(streq(p, result));
282 result = mfree(result);
283
284 r = chase_symlinks(p, NULL, CHASE_STEP, &result);
285 assert_se(r == 0);
286 p = strjoina(temp, "/top/../a");
287 assert_se(streq(p, result));
288 result = mfree(result);
289
290 r = chase_symlinks(p, NULL, CHASE_STEP, &result);
291 assert_se(r == 0);
292 p = strjoina(temp, "/a");
293 assert_se(streq(p, result));
294 result = mfree(result);
295
296 r = chase_symlinks(p, NULL, CHASE_STEP, &result);
297 assert_se(r == 0);
298 p = strjoina(temp, "/b");
299 assert_se(streq(p, result));
300 result = mfree(result);
301
302 r = chase_symlinks(p, NULL, CHASE_STEP, &result);
303 assert_se(r == 0);
304 assert_se(streq("/usr", result));
305 result = mfree(result);
306
307 r = chase_symlinks("/usr", NULL, CHASE_STEP, &result);
308 assert_se(r > 0);
309 assert_se(streq("/usr", result));
310 result = mfree(result);
311
d944dc95
LP
312 assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
313}
314
c270684a
RC
315static void test_unlink_noerrno(void) {
316 char name[] = "/tmp/test-close_nointr.XXXXXX";
317 int fd;
318
646853bd 319 fd = mkostemp_safe(name);
c270684a
RC
320 assert_se(fd >= 0);
321 assert_se(close_nointr(fd) >= 0);
322
323 {
324 PROTECT_ERRNO;
325 errno = -42;
326 assert_se(unlink_noerrno(name) >= 0);
327 assert_se(errno == -42);
328 assert_se(unlink_noerrno(name) < 0);
329 assert_se(errno == -42);
330 }
331}
332
333static void test_readlink_and_make_absolute(void) {
334 char tempdir[] = "/tmp/test-readlink_and_make_absolute";
335 char name[] = "/tmp/test-readlink_and_make_absolute/original";
336 char name2[] = "test-readlink_and_make_absolute/original";
337 char name_alias[] = "/tmp/test-readlink_and_make_absolute-alias";
338 char *r = NULL;
cd76d4c2 339 _cleanup_free_ char *pwd = NULL;
c270684a 340
37c1d5e9 341 assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid(), MKDIR_WARN_MODE) >= 0);
c270684a
RC
342 assert_se(touch(name) >= 0);
343
344 assert_se(symlink(name, name_alias) >= 0);
345 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
346 assert_se(streq(r, name));
347 free(r);
348 assert_se(unlink(name_alias) >= 0);
349
d7249575 350 assert_se(safe_getcwd(&pwd) >= 0);
cd76d4c2 351
c270684a
RC
352 assert_se(chdir(tempdir) >= 0);
353 assert_se(symlink(name2, name_alias) >= 0);
354 assert_se(readlink_and_make_absolute(name_alias, &r) >= 0);
355 assert_se(streq(r, name));
356 free(r);
357 assert_se(unlink(name_alias) >= 0);
358
cd76d4c2
YW
359 assert_se(chdir(pwd) >= 0);
360
c270684a
RC
361 assert_se(rm_rf(tempdir, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
362}
363
364static void test_get_files_in_directory(void) {
365 _cleanup_strv_free_ char **l = NULL, **t = NULL;
366
367 assert_se(get_files_in_directory("/tmp", &l) >= 0);
368 assert_se(get_files_in_directory(".", &t) >= 0);
369 assert_se(get_files_in_directory(".", NULL) >= 0);
370}
371
34a8f081 372static void test_var_tmp(void) {
4245eb50 373 _cleanup_free_ char *tmpdir_backup = NULL, *temp_backup = NULL, *tmp_backup = NULL;
992e8f22 374 const char *tmp_dir = NULL, *t;
34a8f081 375
992e8f22
LP
376 t = getenv("TMPDIR");
377 if (t) {
378 tmpdir_backup = strdup(t);
379 assert_se(tmpdir_backup);
380 }
34a8f081 381
4245eb50
MAP
382 t = getenv("TEMP");
383 if (t) {
384 temp_backup = strdup(t);
385 assert_se(temp_backup);
386 }
387
388 t = getenv("TMP");
389 if (t) {
390 tmp_backup = strdup(t);
391 assert_se(tmp_backup);
392 }
393
85e55d14
YW
394 assert_se(unsetenv("TMPDIR") >= 0);
395 assert_se(unsetenv("TEMP") >= 0);
396 assert_se(unsetenv("TMP") >= 0);
34a8f081 397
992e8f22
LP
398 assert_se(var_tmp_dir(&tmp_dir) >= 0);
399 assert_se(streq(tmp_dir, "/var/tmp"));
34a8f081 400
992e8f22
LP
401 assert_se(setenv("TMPDIR", "/tmp", true) >= 0);
402 assert_se(streq(getenv("TMPDIR"), "/tmp"));
34a8f081 403
992e8f22
LP
404 assert_se(var_tmp_dir(&tmp_dir) >= 0);
405 assert_se(streq(tmp_dir, "/tmp"));
34a8f081 406
992e8f22
LP
407 assert_se(setenv("TMPDIR", "/88_does_not_exist_88", true) >= 0);
408 assert_se(streq(getenv("TMPDIR"), "/88_does_not_exist_88"));
34a8f081 409
992e8f22
LP
410 assert_se(var_tmp_dir(&tmp_dir) >= 0);
411 assert_se(streq(tmp_dir, "/var/tmp"));
34a8f081 412
992e8f22
LP
413 if (tmpdir_backup) {
414 assert_se(setenv("TMPDIR", tmpdir_backup, true) >= 0);
415 assert_se(streq(getenv("TMPDIR"), tmpdir_backup));
34a8f081 416 }
4245eb50
MAP
417
418 if (temp_backup) {
419 assert_se(setenv("TEMP", temp_backup, true) >= 0);
420 assert_se(streq(getenv("TEMP"), temp_backup));
421 }
422
423 if (tmp_backup) {
424 assert_se(setenv("TMP", tmp_backup, true) >= 0);
425 assert_se(streq(getenv("TMP"), tmp_backup));
426 }
34a8f081
OW
427}
428
49bfc877
LP
429static void test_dot_or_dot_dot(void) {
430 assert_se(!dot_or_dot_dot(NULL));
431 assert_se(!dot_or_dot_dot(""));
432 assert_se(!dot_or_dot_dot("xxx"));
433 assert_se(dot_or_dot_dot("."));
434 assert_se(dot_or_dot_dot(".."));
435 assert_se(!dot_or_dot_dot(".foo"));
436 assert_se(!dot_or_dot_dot("..foo"));
437}
438
57a4359e
LP
439static void test_access_fd(void) {
440 _cleanup_(rmdir_and_freep) char *p = NULL;
441 _cleanup_close_ int fd = -1;
442
443 assert_se(mkdtemp_malloc("/tmp/access-fd.XXXXXX", &p) >= 0);
444
445 fd = open(p, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
446 assert_se(fd >= 0);
447
448 assert_se(access_fd(fd, R_OK) >= 0);
449 assert_se(access_fd(fd, F_OK) >= 0);
450 assert_se(access_fd(fd, W_OK) >= 0);
451
452 assert_se(fchmod(fd, 0000) >= 0);
453
454 assert_se(access_fd(fd, F_OK) >= 0);
455
456 if (geteuid() == 0) {
457 assert_se(access_fd(fd, R_OK) >= 0);
458 assert_se(access_fd(fd, W_OK) >= 0);
459 } else {
460 assert_se(access_fd(fd, R_OK) == -EACCES);
461 assert_se(access_fd(fd, W_OK) == -EACCES);
462 }
463}
464
9e3fa6e8
LP
465static void test_touch_file(void) {
466 uid_t test_uid, test_gid;
467 _cleanup_(rm_rf_physical_and_freep) char *p = NULL;
468 struct stat st;
469 const char *a;
470 usec_t test_mtime;
471
472 test_uid = geteuid() == 0 ? 65534 : getuid();
473 test_gid = geteuid() == 0 ? 65534 : getgid();
474
475 test_mtime = usec_sub_unsigned(now(CLOCK_REALTIME), USEC_PER_WEEK);
476
477 assert_se(mkdtemp_malloc("/dev/shm/touch-file-XXXXXX", &p) >= 0);
478
479 a = strjoina(p, "/regular");
480 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
481 assert_se(lstat(a, &st) >= 0);
482 assert_se(st.st_uid == test_uid);
483 assert_se(st.st_gid == test_gid);
484 assert_se(S_ISREG(st.st_mode));
485 assert_se((st.st_mode & 0777) == 0640);
486 assert_se(timespec_load(&st.st_mtim) == test_mtime);
487
488 a = strjoina(p, "/dir");
489 assert_se(mkdir(a, 0775) >= 0);
490 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
491 assert_se(lstat(a, &st) >= 0);
492 assert_se(st.st_uid == test_uid);
493 assert_se(st.st_gid == test_gid);
494 assert_se(S_ISDIR(st.st_mode));
495 assert_se((st.st_mode & 0777) == 0640);
496 assert_se(timespec_load(&st.st_mtim) == test_mtime);
497
498 a = strjoina(p, "/fifo");
499 assert_se(mkfifo(a, 0775) >= 0);
500 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
501 assert_se(lstat(a, &st) >= 0);
502 assert_se(st.st_uid == test_uid);
503 assert_se(st.st_gid == test_gid);
504 assert_se(S_ISFIFO(st.st_mode));
505 assert_se((st.st_mode & 0777) == 0640);
506 assert_se(timespec_load(&st.st_mtim) == test_mtime);
507
508 a = strjoina(p, "/sock");
509 assert_se(mknod(a, 0775 | S_IFSOCK, 0) >= 0);
510 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
511 assert_se(lstat(a, &st) >= 0);
512 assert_se(st.st_uid == test_uid);
513 assert_se(st.st_gid == test_gid);
514 assert_se(S_ISSOCK(st.st_mode));
515 assert_se((st.st_mode & 0777) == 0640);
516 assert_se(timespec_load(&st.st_mtim) == test_mtime);
517
518 if (geteuid() == 0) {
519 a = strjoina(p, "/cdev");
520 assert_se(mknod(a, 0775 | S_IFCHR, makedev(0, 0)) >= 0);
521 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
522 assert_se(lstat(a, &st) >= 0);
523 assert_se(st.st_uid == test_uid);
524 assert_se(st.st_gid == test_gid);
525 assert_se(S_ISCHR(st.st_mode));
526 assert_se((st.st_mode & 0777) == 0640);
527 assert_se(timespec_load(&st.st_mtim) == test_mtime);
528
529 a = strjoina(p, "/bdev");
530 assert_se(mknod(a, 0775 | S_IFBLK, makedev(0, 0)) >= 0);
531 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
532 assert_se(lstat(a, &st) >= 0);
533 assert_se(st.st_uid == test_uid);
534 assert_se(st.st_gid == test_gid);
535 assert_se(S_ISBLK(st.st_mode));
536 assert_se((st.st_mode & 0777) == 0640);
537 assert_se(timespec_load(&st.st_mtim) == test_mtime);
538 }
539
540 a = strjoina(p, "/lnk");
541 assert_se(symlink("target", a) >= 0);
542 assert_se(touch_file(a, false, test_mtime, test_uid, test_gid, 0640) >= 0);
543 assert_se(lstat(a, &st) >= 0);
544 assert_se(st.st_uid == test_uid);
545 assert_se(st.st_gid == test_gid);
546 assert_se(S_ISLNK(st.st_mode));
547 assert_se((st.st_mode & 0777) == 0640);
548 assert_se(timespec_load(&st.st_mtim) == test_mtime);
549}
550
43767d9d
LP
551static void test_unlinkat_deallocate(void) {
552 _cleanup_free_ char *p = NULL;
553 _cleanup_close_ int fd = -1;
554 struct stat st;
555
556 assert_se(tempfn_random_child(NULL, "unlink-deallocation", &p) >= 0);
557
558 fd = open(p, O_WRONLY|O_CLOEXEC|O_CREAT|O_EXCL, 0600);
559 assert_se(fd >= 0);
560
561 assert_se(write(fd, "hallo\n", 6) == 6);
562
563 assert_se(fstat(fd, &st) >= 0);
564 assert_se(st.st_size == 6);
565 assert_se(st.st_blocks > 0);
566 assert_se(st.st_nlink == 1);
567
568 assert_se(unlinkat_deallocate(AT_FDCWD, p, 0) >= 0);
569
570 assert_se(fstat(fd, &st) >= 0);
571 assert_se(IN_SET(st.st_size, 0, 6)); /* depending on whether hole punching worked the size will be 6 (it worked) or 0 (we had to resort to truncation) */
572 assert_se(st.st_blocks == 0);
573 assert_se(st.st_nlink == 0);
574}
575
11b29a96
LP
576static void test_fsync_directory_of_file(void) {
577 _cleanup_close_ int fd = -1;
578
579 fd = open_tmpfile_unlinkable(NULL, O_RDWR);
580 assert_se(fd >= 0);
581
582 assert_se(fsync_directory_of_file(fd) >= 0);
583}
584
c270684a
RC
585int main(int argc, char *argv[]) {
586 test_unlink_noerrno();
c270684a 587 test_get_files_in_directory();
496c486f 588 test_readlink_and_make_absolute();
34a8f081 589 test_var_tmp();
d944dc95 590 test_chase_symlinks();
49bfc877 591 test_dot_or_dot_dot();
57a4359e 592 test_access_fd();
9e3fa6e8 593 test_touch_file();
43767d9d 594 test_unlinkat_deallocate();
11b29a96 595 test_fsync_directory_of_file();
c270684a
RC
596
597 return 0;
598}