Skip to content

Commit dd30f39

Browse files
committed
tests: make xattr test more robust
Currently the xattr test asserts that listxattr() on Cargo.toml returns a length of 0. This causes a spurious failure when Cargo.toml does have xattrs, and this can happen in fairly normal situations like when a Linux distribution enables selinux (as it does on an AlmaLinux 10 test system, for example): $ cat /etc/almalinux-release AlmaLinux release 10.0 (Purple Lion) $ getfattr -d -m - Cargo.toml security.selinux=[...] $ cargo test --features=fs,stdio xattr [...] thread 'xattr::xattr_basic' panicked at tests/fs/xattr.rs:88:5: assertion `left == right` failed left: 17 right: 0 This change fixes the failure by comparing the output of rustix *listxattr() with an external implementation, the libc crate's implementation of listxattr(). Rather than asserting a specific value for rustix *listxattr(), it should match the value produced by libc listxattr().
1 parent 5245b81 commit dd30f39

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

tests/fs/xattr.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,14 @@ fn xattr_basic() {
8585
.raw_os_error(),
8686
enodata
8787
);
88-
assert_eq!(rustix::fs::listxattr("Cargo.toml", &mut empty).unwrap(), 0);
89-
assert_eq!(rustix::fs::llistxattr("Cargo.toml", &mut empty).unwrap(), 0);
88+
assert_eq!(
89+
rustix::fs::listxattr("Cargo.toml", &mut empty).unwrap(),
90+
libc_listxattr("Cargo.toml")
91+
);
92+
assert_eq!(
93+
rustix::fs::llistxattr("Cargo.toml", &mut empty).unwrap(),
94+
libc_listxattr("Cargo.toml")
95+
);
9096
assert_eq!(
9197
rustix::fs::removexattr("Cargo.toml", "user.test")
9298
.unwrap_err()
@@ -113,11 +119,26 @@ fn xattr_basic() {
113119
.raw_os_error(),
114120
enodata
115121
);
116-
assert_eq!(rustix::fs::flistxattr(&file, &mut empty).unwrap(), 0);
122+
assert_eq!(
123+
rustix::fs::flistxattr(&file, &mut empty).unwrap(),
124+
libc_listxattr("Cargo.toml"),
125+
);
117126
assert_eq!(
118127
rustix::fs::fremovexattr(&file, "user.test")
119128
.unwrap_err()
120129
.raw_os_error(),
121130
enodata
122131
);
123132
}
133+
134+
/// To check the correctness of the tested implementations of *listxattr(), their output can be
135+
/// compared to an external implementation, in this case listxattr() from the libc crate.
136+
fn libc_listxattr(path: &str) -> usize {
137+
let path = std::ffi::CString::new(path).unwrap();
138+
let path: *const _ = path.as_ptr();
139+
140+
let list = std::ffi::CString::new("").unwrap();
141+
let list = list.as_ptr() as *mut _;
142+
143+
(unsafe { libc::listxattr(path, list, 0) }) as usize
144+
}

0 commit comments

Comments
 (0)