]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-xattr-util.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[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 "fs-util.h"
12 #include "macro.h"
13 #include "string-util.h"
14 #include "tests.h"
15 #include "tmpfile-util.h"
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;
22 char v[3];
23 int r;
24 size_t size;
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
38 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0, &size) >= 0);
39 assert_se(size == 3);
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);
45 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0, &size) == -ENODATA);
46
47 cleanup:
48 assert_se(unlink(x) >= 0);
49 assert_se(rmdir(t) >= 0);
50 }
51
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
81 int main(void) {
82 test_setup_logging(LOG_DEBUG);
83
84 test_fgetxattrat_fake();
85 test_getcrtime();
86
87 return 0;
88 }