From 540fb905c244ad8761a90f6287f9942440a39c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Thu, 30 Dec 2021 11:01:19 +0100 Subject: [PATCH] sys: make xattr tests integration tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- proxmox-sys/src/fs/xattr.rs | 38 ---------------------------- proxmox-sys/tests/xattr.rs | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 38 deletions(-) create mode 100644 proxmox-sys/tests/xattr.rs diff --git a/proxmox-sys/src/fs/xattr.rs b/proxmox-sys/src/fs/xattr.rs index 28be2504..41fafa5c 100644 --- a/proxmox-sys/src/fs/xattr.rs +++ b/proxmox-sys/src/fs/xattr.rs @@ -195,47 +195,9 @@ mod tests { use super::*; use std::ffi::CString; - use std::fs::OpenOptions; - use std::os::unix::io::AsRawFd; - - use nix::errno::Errno; 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] fn test_is_valid_xattr_name() { let too_long = CString::new(vec![b'a'; 265]).unwrap(); diff --git a/proxmox-sys/tests/xattr.rs b/proxmox-sys/tests/xattr.rs new file mode 100644 index 00000000..55ce81ac --- /dev/null +++ b/proxmox-sys/tests/xattr.rs @@ -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(); +} \ No newline at end of file