]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
ac229ed8 RC |
2 | |
3 | #include <errno.h> | |
4 | #include <fcntl.h> | |
5 | #include <sys/types.h> | |
6 | #include <sys/xattr.h> | |
7 | #include <unistd.h> | |
8 | ||
9 | #include "alloc-util.h" | |
10 | #include "fd-util.h" | |
11 | #include "fs-util.h" | |
12 | #include "macro.h" | |
13 | #include "string-util.h" | |
6d7c4033 | 14 | #include "tests.h" |
e4de7287 | 15 | #include "tmpfile-util.h" |
ac229ed8 RC |
16 | #include "xattr-util.h" |
17 | ||
18 | static void test_fgetxattrat_fake(void) { | |
19 | char t[] = "/var/tmp/xattrtestXXXXXX"; | |
20 | _cleanup_close_ int fd = -1; | |
21 | const char *x; | |
e4de6259 | 22 | char v[3]; |
ac229ed8 | 23 | int r; |
e4de6259 | 24 | size_t size; |
ac229ed8 RC |
25 | |
26 | assert_se(mkdtemp(t)); | |
27 | x = strjoina(t, "/test"); | |
28 | assert_se(touch(x) >= 0); | |
29 | ||
30 | r = setxattr(x, "user.foo", "bar", 3, 0); | |
31 | if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */ | |
32 | goto cleanup; | |
33 | assert_se(r >= 0); | |
34 | ||
35 | fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY); | |
36 | assert_se(fd >= 0); | |
37 | ||
e4de6259 ZJS |
38 | assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0, &size) >= 0); |
39 | assert_se(size == 3); | |
ac229ed8 RC |
40 | assert_se(memcmp(v, "bar", 3) == 0); |
41 | ||
42 | safe_close(fd); | |
43 | fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY); | |
44 | assert_se(fd >= 0); | |
e4de6259 | 45 | assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0, &size) == -ENODATA); |
ac229ed8 RC |
46 | |
47 | cleanup: | |
48 | assert_se(unlink(x) >= 0); | |
49 | assert_se(rmdir(t) >= 0); | |
50 | } | |
51 | ||
4c2e1b39 LP |
52 | static void test_getcrtime(void) { |
53 | ||
54 | _cleanup_close_ int fd = -1; | |
55 | char ts[FORMAT_TIMESTAMP_MAX]; | |
56 | const char *vt; | |
57 | usec_t usec, k; | |
58 | int r; | |
59 | ||
60 | assert_se(tmp_dir(&vt) >= 0); | |
61 | ||
62 | fd = open_tmpfile_unlinkable(vt, O_RDWR); | |
63 | assert_se(fd >= 0); | |
64 | ||
65 | r = fd_getcrtime(fd, &usec); | |
66 | if (r < 0) | |
67 | log_debug_errno(r, "btime: %m"); | |
68 | else | |
69 | log_debug("btime: %s", format_timestamp(ts, sizeof(ts), usec)); | |
70 | ||
71 | k = now(CLOCK_REALTIME); | |
72 | ||
73 | r = fd_setcrtime(fd, 1519126446UL * USEC_PER_SEC); | |
74 | if (!IN_SET(r, -EOPNOTSUPP, -ENOTTY)) { | |
75 | assert_se(fd_getcrtime(fd, &usec) >= 0); | |
76 | assert_se(k < 1519126446UL * USEC_PER_SEC || | |
77 | usec == 1519126446UL * USEC_PER_SEC); | |
78 | } | |
79 | } | |
80 | ||
ac229ed8 | 81 | int main(void) { |
6d7c4033 | 82 | test_setup_logging(LOG_DEBUG); |
4c2e1b39 | 83 | |
ac229ed8 | 84 | test_fgetxattrat_fake(); |
4c2e1b39 | 85 | test_getcrtime(); |
ac229ed8 RC |
86 | |
87 | return 0; | |
88 | } |