]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-xattr-util.c
Merge pull request #7908 from yuwata/rfe-7895
[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 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/types.h>
24 #include <sys/xattr.h>
25 #include <unistd.h>
26
27 #include "alloc-util.h"
28 #include "fd-util.h"
29 #include "fileio.h"
30 #include "fs-util.h"
31 #include "macro.h"
32 #include "string-util.h"
33 #include "xattr-util.h"
34
35 static void test_fgetxattrat_fake(void) {
36 char t[] = "/var/tmp/xattrtestXXXXXX";
37 _cleanup_close_ int fd = -1;
38 const char *x;
39 char v[3] = {};
40 int r;
41
42 assert_se(mkdtemp(t));
43 x = strjoina(t, "/test");
44 assert_se(touch(x) >= 0);
45
46 r = setxattr(x, "user.foo", "bar", 3, 0);
47 if (r < 0 && errno == EOPNOTSUPP) /* no xattrs supported on /var/tmp... */
48 goto cleanup;
49 assert_se(r >= 0);
50
51 fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
52 assert_se(fd >= 0);
53
54 assert_se(fgetxattrat_fake(fd, "test", "user.foo", v, 3, 0) >= 0);
55 assert_se(memcmp(v, "bar", 3) == 0);
56
57 safe_close(fd);
58 fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
59 assert_se(fd >= 0);
60 assert_se(fgetxattrat_fake(fd, "usr", "user.idontexist", v, 3, 0) == -ENODATA);
61
62 cleanup:
63 assert_se(unlink(x) >= 0);
64 assert_se(rmdir(t) >= 0);
65 }
66
67 static void test_getcrtime(void) {
68
69 _cleanup_close_ int fd = -1;
70 char ts[FORMAT_TIMESTAMP_MAX];
71 const char *vt;
72 usec_t usec, k;
73 int r;
74
75 assert_se(tmp_dir(&vt) >= 0);
76
77 fd = open_tmpfile_unlinkable(vt, O_RDWR);
78 assert_se(fd >= 0);
79
80 r = fd_getcrtime(fd, &usec);
81 if (r < 0)
82 log_debug_errno(r, "btime: %m");
83 else
84 log_debug("btime: %s", format_timestamp(ts, sizeof(ts), usec));
85
86 k = now(CLOCK_REALTIME);
87
88 r = fd_setcrtime(fd, 1519126446UL * USEC_PER_SEC);
89 if (!IN_SET(r, -EOPNOTSUPP, -ENOTTY)) {
90 assert_se(fd_getcrtime(fd, &usec) >= 0);
91 assert_se(k < 1519126446UL * USEC_PER_SEC ||
92 usec == 1519126446UL * USEC_PER_SEC);
93 }
94 }
95
96 int main(void) {
97 log_set_max_level(LOG_DEBUG);
98 log_parse_environment();
99 log_open();
100
101 test_fgetxattrat_fake();
102 test_getcrtime();
103
104 return 0;
105 }