]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-xattr-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "alloc-util.h"
14 #include "string-util.h"
16 #include "tmpfile-util.h"
17 #include "xattr-util.h"
19 TEST(getxattr_at_malloc
) {
20 _cleanup_(rm_rf_physical_and_freep
) char *t
= NULL
;
21 _cleanup_free_
char *value
= NULL
;
22 _cleanup_close_
int fd
= -EBADF
;
26 fd
= mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", O_RDONLY
|O_NOCTTY
, &t
);
28 x
= strjoina(t
, "/test");
29 assert_se(touch(x
) >= 0);
31 r
= setxattr(x
, "user.foo", "bar", 3, 0);
32 if (r
< 0 && ERRNO_IS_NOT_SUPPORTED(errno
))
33 return (void) log_tests_skipped_errno(errno
, "no xattrs supported on /var/tmp");
36 assert_se(getxattr_at_malloc(fd
, "test", "user.foo", 0, &value
) == 3);
37 assert_se(memcmp(value
, "bar", 3) == 0);
40 assert_se(getxattr_at_malloc(AT_FDCWD
, x
, "user.foo", 0, &value
) == 3);
41 assert_se(memcmp(value
, "bar", 3) == 0);
45 fd
= open("/", O_RDONLY
|O_DIRECTORY
|O_CLOEXEC
|O_NOCTTY
);
47 r
= getxattr_at_malloc(fd
, "usr", "user.idontexist", 0, &value
);
48 assert_se(ERRNO_IS_NEG_XATTR_ABSENT(r
));
51 fd
= open(x
, O_PATH
|O_CLOEXEC
);
53 assert_se(getxattr_at_malloc(fd
, NULL
, "user.foo", 0, &value
) == 3);
54 ASSERT_STREQ(value
, "bar");
58 _cleanup_(rm_rf_physical_and_freep
) char *t
= NULL
;
59 _cleanup_close_
int fd
= -EBADF
;
63 fd
= mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", 0, &t
);
66 r
= fd_getcrtime(fd
, &usec
);
68 log_debug_errno(r
, "btime: %m");
70 log_debug("btime: %s", FORMAT_TIMESTAMP(usec
));
72 k
= now(CLOCK_REALTIME
);
74 r
= fd_setcrtime(fd
, 1519126446UL * USEC_PER_SEC
);
75 if (!IN_SET(r
, -EOPNOTSUPP
, -ENOTTY
)) {
76 assert_se(fd_getcrtime(fd
, &usec
) >= 0);
77 assert_se(k
< 1519126446UL * USEC_PER_SEC
||
78 usec
== 1519126446UL * USEC_PER_SEC
);
82 static void verify_xattr(int dfd
, const char *expected
) {
83 _cleanup_free_
char *value
= NULL
;
85 assert_se(getxattr_at_malloc(dfd
, "test", "user.foo", 0, &value
) == (int) strlen(expected
));
86 ASSERT_STREQ(value
, expected
);
90 _cleanup_(rm_rf_physical_and_freep
) char *t
= NULL
;
91 _cleanup_close_
int dfd
= -EBADF
, fd
= -EBADF
;
95 dfd
= mkdtemp_open("/var/tmp/test-xattrtestXXXXXX", O_PATH
, &t
);
97 x
= strjoina(t
, "/test");
98 assert_se(touch(x
) >= 0);
101 r
= xsetxattr(AT_FDCWD
, x
, "user.foo", "fullpath", SIZE_MAX
, 0);
102 if (ERRNO_IS_NEG_NOT_SUPPORTED(r
))
103 return (void) log_tests_skipped_errno(r
, "no xattrs supported on /var/tmp");
105 verify_xattr(dfd
, "fullpath");
108 assert_se(xsetxattr(dfd
, "test", "user.foo", "dirfd", SIZE_MAX
, 0) >= 0);
109 verify_xattr(dfd
, "dirfd");
112 fd
= openat(dfd
, "test", O_PATH
|O_CLOEXEC
);
114 assert_se(xsetxattr(fd
, NULL
, "user.foo", "fd_opath", SIZE_MAX
, 0) >= 0);
115 verify_xattr(dfd
, "fd_opath");
116 assert_se(xsetxattr(fd
, "", "user.foo", "fd_opath", SIZE_MAX
, 0) == -EINVAL
);
117 assert_se(xsetxattr(fd
, "", "user.foo", "fd_opath_empty", SIZE_MAX
, AT_EMPTY_PATH
) >= 0);
118 verify_xattr(dfd
, "fd_opath_empty");
121 fd
= openat(dfd
, "test", O_RDONLY
|O_CLOEXEC
);
122 assert_se(xsetxattr(fd
, NULL
, "user.foo", "fd_regular", SIZE_MAX
, 0) >= 0);
123 verify_xattr(dfd
, "fd_regular");
124 assert_se(xsetxattr(fd
, "", "user.foo", "fd_regular_empty", SIZE_MAX
, 0) == -EINVAL
);
125 assert_se(xsetxattr(fd
, "", "user.foo", "fd_regular_empty", SIZE_MAX
, AT_EMPTY_PATH
) >= 0);
126 verify_xattr(dfd
, "fd_regular_empty");
129 DEFINE_TEST_MAIN(LOG_DEBUG
);