]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-xattr-util.c
Merge pull request #9274 from poettering/comment-header-cleanup
[thirdparty/systemd.git] / src / test / test-xattr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "fileio.h"
12 #include "fs-util.h"
13 #include "macro.h"
14 #include "string-util.h"
15 #include "xattr-util.h"
16
17 static void test_fgetxattrat_fake(void) {
18 char t[] = "/var/tmp/xattrtestXXXXXX";
19 _cleanup_close_ int fd = -1;
20 const char *x;
21 char v[3];
22 int r;
23 size_t size;
24
25 assert_se(mkdtemp(t));
26 x = strjoina(t, "/test");
27 assert_se(touch(x) >= 0);
28
29 r = setxattr(x, "user.foo", "bar", 3, 0);
30 if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */
31 goto cleanup;
32 assert_se(r >= 0);
33
34 fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
35 assert_se(fd >= 0);
36
37 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0, &size) >= 0);
38 assert_se(size == 3);
39 assert_se(memcmp(v, "bar", 3) == 0);
40
41 safe_close(fd);
42 fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
43 assert_se(fd >= 0);
44 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0, &size) == -ENODATA);
45
46 cleanup:
47 assert_se(unlink(x) >= 0);
48 assert_se(rmdir(t) >= 0);
49 }
50
51 static void test_getcrtime(void) {
52
53 _cleanup_close_ int fd = -1;
54 char ts[FORMAT_TIMESTAMP_MAX];
55 const char *vt;
56 usec_t usec, k;
57 int r;
58
59 assert_se(tmp_dir(&vt) >= 0);
60
61 fd = open_tmpfile_unlinkable(vt, O_RDWR);
62 assert_se(fd >= 0);
63
64 r = fd_getcrtime(fd, &usec);
65 if (r < 0)
66 log_debug_errno(r, "btime: %m");
67 else
68 log_debug("btime: %s", format_timestamp(ts, sizeof(ts), usec));
69
70 k = now(CLOCK_REALTIME);
71
72 r = fd_setcrtime(fd, 1519126446UL * USEC_PER_SEC);
73 if (!IN_SET(r, -EOPNOTSUPP, -ENOTTY)) {
74 assert_se(fd_getcrtime(fd, &usec) >= 0);
75 assert_se(k < 1519126446UL * USEC_PER_SEC ||
76 usec == 1519126446UL * USEC_PER_SEC);
77 }
78 }
79
80 int main(void) {
81 log_set_max_level(LOG_DEBUG);
82 log_parse_environment();
83 log_open();
84
85 test_fgetxattrat_fake();
86 test_getcrtime();
87
88 return 0;
89 }