]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-xattr-util.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / test / test-xattr-util.c
CommitLineData
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"
4c2e1b39 11#include "fileio.h"
ac229ed8
RC
12#include "fs-util.h"
13#include "macro.h"
14#include "string-util.h"
15#include "xattr-util.h"
16
17static void test_fgetxattrat_fake(void) {
18 char t[] = "/var/tmp/xattrtestXXXXXX";
19 _cleanup_close_ int fd = -1;
20 const char *x;
e4de6259 21 char v[3];
ac229ed8 22 int r;
e4de6259 23 size_t size;
ac229ed8
RC
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
e4de6259
ZJS
37 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0, &size) >= 0);
38 assert_se(size == 3);
ac229ed8
RC
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);
e4de6259 44 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0, &size) == -ENODATA);
ac229ed8
RC
45
46cleanup:
47 assert_se(unlink(x) >= 0);
48 assert_se(rmdir(t) >= 0);
49}
50
4c2e1b39
LP
51static 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
ac229ed8 80int main(void) {
4c2e1b39
LP
81 log_set_max_level(LOG_DEBUG);
82 log_parse_environment();
83 log_open();
84
ac229ed8 85 test_fgetxattrat_fake();
4c2e1b39 86 test_getcrtime();
ac229ed8
RC
87
88 return 0;
89}