]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-xattr-util.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / test / test-xattr-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
11#include "fs-util.h"
12#include "macro.h"
13#include "string-util.h"
6d7c4033 14#include "tests.h"
e4de7287 15#include "tmpfile-util.h"
ac229ed8
RC
16#include "xattr-util.h"
17
4f7452a8 18TEST(getxattr_at_malloc) {
ac229ed8 19 char t[] = "/var/tmp/xattrtestXXXXXX";
5ce46344 20 _cleanup_free_ char *value = NULL;
254d1313 21 _cleanup_close_ int fd = -EBADF;
ac229ed8 22 const char *x;
ac229ed8
RC
23 int r;
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);
84319c5c 30 if (r < 0 && ERRNO_IS_NOT_SUPPORTED(errno)) /* no xattrs supported on /var/tmp... */
ac229ed8
RC
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
c53e07e2
LP
37 assert_se(getxattr_at_malloc(fd, "test", "user.foo", 0, &value) == 3);
38 assert_se(memcmp(value, "bar", 3) == 0);
39 value = mfree(value);
40
41 assert_se(getxattr_at_malloc(AT_FDCWD, x, "user.foo", 0, &value) == 3);
42 assert_se(memcmp(value, "bar", 3) == 0);
43 value = mfree(value);
ac229ed8
RC
44
45 safe_close(fd);
46 fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
47 assert_se(fd >= 0);
c53e07e2 48 r = getxattr_at_malloc(fd, "usr", "user.idontexist", 0, &value);
00675c36 49 assert_se(r < 0 && ERRNO_IS_XATTR_ABSENT(r));
ac229ed8 50
5ce46344
LB
51 safe_close(fd);
52 fd = open(x, O_PATH|O_CLOEXEC);
53 assert_se(fd >= 0);
c53e07e2 54 assert_se(getxattr_at_malloc(fd, NULL, "user.foo", 0, &value) == 3);
5ce46344
LB
55 assert_se(streq(value, "bar"));
56
ac229ed8
RC
57cleanup:
58 assert_se(unlink(x) >= 0);
59 assert_se(rmdir(t) >= 0);
60}
61
4f7452a8 62TEST(getcrtime) {
254d1313 63 _cleanup_close_ int fd = -EBADF;
4c2e1b39
LP
64 const char *vt;
65 usec_t usec, k;
66 int r;
67
c53e07e2 68 assert_se(var_tmp_dir(&vt) >= 0);
4c2e1b39
LP
69
70 fd = open_tmpfile_unlinkable(vt, O_RDWR);
71 assert_se(fd >= 0);
72
73 r = fd_getcrtime(fd, &usec);
74 if (r < 0)
75 log_debug_errno(r, "btime: %m");
76 else
04f5c018 77 log_debug("btime: %s", FORMAT_TIMESTAMP(usec));
4c2e1b39
LP
78
79 k = now(CLOCK_REALTIME);
80
81 r = fd_setcrtime(fd, 1519126446UL * USEC_PER_SEC);
82 if (!IN_SET(r, -EOPNOTSUPP, -ENOTTY)) {
83 assert_se(fd_getcrtime(fd, &usec) >= 0);
84 assert_se(k < 1519126446UL * USEC_PER_SEC ||
85 usec == 1519126446UL * USEC_PER_SEC);
86 }
87}
88
4f7452a8 89DEFINE_TEST_MAIN(LOG_DEBUG);