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