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