sys: make xattr tests integration tests

these touch files, so should use the cargo-provided tmp dir, but that is
only available to benchmarks and integration tests, not unit tests.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2021-12-30 11:01:19 +01:00
parent e8cb382442
commit 540fb905c2
2 changed files with 49 additions and 38 deletions

View File

@ -195,47 +195,9 @@ mod tests {
use super::*; use super::*;
use std::ffi::CString; use std::ffi::CString;
use std::fs::OpenOptions;
use std::os::unix::io::AsRawFd;
use nix::errno::Errno;
use proxmox_lang::c_str; use proxmox_lang::c_str;
#[test]
fn test_fsetxattr_fgetxattr() {
let path = "./test-xattrs.txt";
let file = OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.unwrap();
let fd = file.as_raw_fd();
assert!(fsetxattr(fd, c_str!("user.attribute0"), b"value0").is_ok());
assert!(fsetxattr(fd, c_str!("user.empty"), b"").is_ok());
if nix::unistd::Uid::current() != nix::unistd::ROOT {
assert_eq!(
fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"),
Err(Errno::EPERM)
);
}
let v0 = fgetxattr(fd, c_str!("user.attribute0")).unwrap();
let v1 = fgetxattr(fd, c_str!("user.empty")).unwrap();
assert_eq!(v0, b"value0".as_ref());
assert_eq!(v1, b"".as_ref());
assert_eq!(
fgetxattr(fd, c_str!("user.attribute1")),
Err(Errno::ENODATA)
);
std::fs::remove_file(&path).unwrap();
}
#[test] #[test]
fn test_is_valid_xattr_name() { fn test_is_valid_xattr_name() {
let too_long = CString::new(vec![b'a'; 265]).unwrap(); let too_long = CString::new(vec![b'a'; 265]).unwrap();

View File

@ -0,0 +1,49 @@
use std::path::PathBuf;
use std::fs::OpenOptions;
use std::os::unix::io::AsRawFd;
use nix::errno::Errno;
use proxmox_lang::c_str;
use proxmox_sys::fs::xattr::{fgetxattr,fsetxattr};
#[test]
fn test_fsetxattr_fgetxattr() {
let mut path = PathBuf::from(env!("CARGO_TARGET_TMPDIR").to_string());
path.push("test-xattrs.txt");
let file = OpenOptions::new()
.write(true)
.create(true)
.open(&path)
.unwrap();
let fd = file.as_raw_fd();
if let Err(Errno::EOPNOTSUPP) = fsetxattr(fd, c_str!("user.attribute0"), b"value0") {
return;
}
assert!(fsetxattr(fd, c_str!("user.attribute0"), b"value0").is_ok());
assert!(fsetxattr(fd, c_str!("user.empty"), b"").is_ok());
if nix::unistd::Uid::current() != nix::unistd::ROOT {
assert_eq!(
fsetxattr(fd, c_str!("trusted.attribute0"), b"value0"),
Err(Errno::EPERM)
);
}
let v0 = fgetxattr(fd, c_str!("user.attribute0")).unwrap();
let v1 = fgetxattr(fd, c_str!("user.empty")).unwrap();
assert_eq!(v0, b"value0".as_ref());
assert_eq!(v1, b"".as_ref());
assert_eq!(
fgetxattr(fd, c_str!("user.attribute1")),
Err(Errno::ENODATA)
);
std::fs::remove_file(&path).unwrap();
}