From df57e8b5100e536dd429a107892e05de6e23d93f Mon Sep 17 00:00:00 2001 From: veeso Date: Sat, 2 Nov 2024 14:48:02 +0100 Subject: [PATCH] fix: test --- remotefs-fuse/src/driver/windows.rs | 10 +- remotefs-fuse/src/driver/windows/test.rs | 97 ++++++++++++++++++ remotefs-fuse/src/mount/option.rs | 125 +++++++++++++++++++++++ 3 files changed, 227 insertions(+), 5 deletions(-) diff --git a/remotefs-fuse/src/driver/windows.rs b/remotefs-fuse/src/driver/windows.rs index ac778dd..ccb995d 100644 --- a/remotefs-fuse/src/driver/windows.rs +++ b/remotefs-fuse/src/driver/windows.rs @@ -119,7 +119,7 @@ where return Ok(stat); } - let path_info = self.path_info(file_name); + let path_info = Self::path_info(file_name); let file = self.remote(|remote| remote.stat(&path_info.path))?; @@ -137,7 +137,7 @@ where } /// Get the path information for a given `file_name`. - fn path_info(&self, file_name: &U16CStr) -> PathInfo { + fn path_info(file_name: &U16CStr) -> PathInfo { let p = PathBuf::from(file_name.to_string_lossy()); let parent = p .parent() @@ -708,7 +708,7 @@ where } else if create_disposition == FILE_OPEN || create_disposition == FILE_OPEN_IF { if create_options & FILE_NON_DIRECTORY_FILE > 0 { debug!("create file: {file_name:?}"); - let path_info = self.path_info(file_name); + let path_info = Self::path_info(file_name); if let Err(err) = self.write( &File { path: path_info.path, @@ -744,7 +744,7 @@ where // create directory debug!("create directory: {file_name:?}"); let stat = { - let path_info = self.path_info(file_name); + let path_info = Self::path_info(file_name); if let Err(err) = self .remote(|remote| remote.create_dir(&path_info.path, UnixPex::from(0o755))) @@ -1297,7 +1297,7 @@ where ) -> OperationResult<()> { info!("move_file({file_name:?}, {new_file_name:?}, {replace_if_existing:?}, {context:?})"); - let dest = self.path_info(new_file_name); + let dest = Self::path_info(new_file_name); // check if destination exists if !replace_if_existing && self diff --git a/remotefs-fuse/src/driver/windows/test.rs b/remotefs-fuse/src/driver/windows/test.rs index 51956ee..4ec50bd 100644 --- a/remotefs-fuse/src/driver/windows/test.rs +++ b/remotefs-fuse/src/driver/windows/test.rs @@ -1 +1,98 @@ +use std::path::{Path, PathBuf}; + +use pretty_assertions::{assert_eq, assert_ne}; +use remotefs::fs::{FileType, Metadata, UnixPex}; +use remotefs::File; +use remotefs_memory::MemoryFs; +use widestring::U16CString; + use super::Driver; +use crate::driver::windows::ROOT_ID; + +#[test] +fn test_should_get_file_index() { + let index = Driver::::file_index(&File { + path: PathBuf::from("C:\\Users\\user\\Desktop\\file.txt"), + metadata: Default::default(), + }); + assert_ne!(index, ROOT_ID); + + let index = Driver::::file_index(&File { + path: PathBuf::from("/"), + metadata: Default::default(), + }); + + assert_eq!(index, ROOT_ID); +} + +#[test] +fn test_should_get_filename() { + let filename = Driver::::file_name(Path::new("C:\\Users\\user\\Desktop\\file.txt")); + let expected = U16CString::from_str("file.txt").unwrap().to_ucstring(); + assert_eq!(filename, expected); +} + +#[test] +fn test_should_make_attributes_from_file() { + let file = File { + path: PathBuf::from("C:\\Users\\user\\Desktop\\file.txt"), + metadata: Metadata::default().file_type(FileType::File), + }; + + let attributes = Driver::::attributes_from_file(&file); + assert_eq!(attributes & winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY, 0); + assert_eq!( + attributes & winapi::um::winnt::FILE_ATTRIBUTE_NORMAL, + winapi::um::winnt::FILE_ATTRIBUTE_NORMAL + ); + assert_eq!(attributes & winapi::um::winnt::FILE_ATTRIBUTE_READONLY, 0); + + let file = File { + path: PathBuf::from("C:\\Users\\user\\Desktop"), + metadata: Metadata::default().file_type(FileType::Directory), + }; + + let attributes = Driver::::attributes_from_file(&file); + assert_eq!( + attributes & winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY, + winapi::um::winnt::FILE_ATTRIBUTE_DIRECTORY + ); + + let file = File { + path: PathBuf::from("C:\\Users\\user\\Desktop"), + metadata: Metadata::default() + .file_type(FileType::File) + .mode(UnixPex::from(0o444)), + }; + + let attributes = Driver::::attributes_from_file(&file); + assert_eq!( + attributes & winapi::um::winnt::FILE_ATTRIBUTE_READONLY, + winapi::um::winnt::FILE_ATTRIBUTE_READONLY + ); + + let file = File { + path: PathBuf::from("C:\\Users\\user\\Desktop\\.gitignore"), + metadata: Metadata::default().file_type(FileType::File), + }; + + let attributes = Driver::::attributes_from_file(&file); + assert_eq!( + attributes & winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN, + winapi::um::winnt::FILE_ATTRIBUTE_HIDDEN + ); +} + +#[test] +fn test_should_get_path_info() { + let p = U16CString::from_str("/dev/null").unwrap(); + + let path_info = Driver::::path_info(&p); + + assert_eq!(path_info.path, PathBuf::from("/dev/null")); + assert_eq!( + path_info.file_name, + U16CString::from_str("/dev/null").unwrap().to_ucstring() + ); + assert_eq!(path_info.parent, PathBuf::from("/dev")); +} diff --git a/remotefs-fuse/src/mount/option.rs b/remotefs-fuse/src/mount/option.rs index 9eb35fe..1d038d0 100644 --- a/remotefs-fuse/src/mount/option.rs +++ b/remotefs-fuse/src/mount/option.rs @@ -326,3 +326,128 @@ impl FromStr for MountOption { } } } + +#[cfg(test)] +mod test { + + use pretty_assertions::assert_eq; + + use super::*; + + #[test] + fn test_should_convert_str_to_option() { + #[cfg(unix)] + assert_eq!( + MountOption::from_str("uid=1000").unwrap(), + MountOption::Uid(1000) + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("gid=1000").unwrap(), + MountOption::Gid(1000) + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("default_mode=0755").unwrap(), + MountOption::DefaultMode(0o755) + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("fsname=foo").unwrap(), + MountOption::FSName("foo".to_string()) + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("subtype=foo").unwrap(), + MountOption::Subtype("foo".to_string()) + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("custom=foo").unwrap(), + MountOption::Custom("foo".to_string()) + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("allow_other").unwrap(), + MountOption::AllowOther + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("allow_root").unwrap(), + MountOption::AllowRoot + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("auto_unmount").unwrap(), + MountOption::AutoUnmount + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("default_permissions").unwrap(), + MountOption::DefaultPermissions + ); + #[cfg(unix)] + assert_eq!(MountOption::from_str("dev").unwrap(), MountOption::Dev); + #[cfg(unix)] + assert_eq!(MountOption::from_str("nodev").unwrap(), MountOption::NoDev); + #[cfg(unix)] + assert_eq!(MountOption::from_str("suid").unwrap(), MountOption::Suid); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("nosuid").unwrap(), + MountOption::NoSuid + ); + #[cfg(unix)] + assert_eq!(MountOption::from_str("ro").unwrap(), MountOption::RO); + #[cfg(unix)] + assert_eq!(MountOption::from_str("rw").unwrap(), MountOption::RW); + #[cfg(unix)] + assert_eq!(MountOption::from_str("exec").unwrap(), MountOption::Exec); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("noexec").unwrap(), + MountOption::NoExec + ); + #[cfg(unix)] + assert_eq!(MountOption::from_str("atime").unwrap(), MountOption::Atime); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("noatime").unwrap(), + MountOption::NoAtime + ); + #[cfg(unix)] + assert_eq!( + MountOption::from_str("dirsync").unwrap(), + MountOption::DirSync + ); + #[cfg(unix)] + assert_eq!(MountOption::from_str("sync").unwrap(), MountOption::Sync); + #[cfg(unix)] + assert_eq!(MountOption::from_str("async").unwrap(), MountOption::Async); + #[cfg(windows)] + assert_eq!( + MountOption::from_str("single_thread").unwrap(), + MountOption::SingleThread + ); + #[cfg(windows)] + assert_eq!( + MountOption::from_str("flags=1").unwrap(), + MountOption::Flags(1) + ); + #[cfg(windows)] + assert_eq!( + MountOption::from_str("timeout=1000").unwrap(), + MountOption::Timeout(Duration::from_millis(1000)) + ); + #[cfg(windows)] + assert_eq!( + MountOption::from_str("allocation_unit_size=4096").unwrap(), + MountOption::AllocationUnitSize(4096) + ); + #[cfg(windows)] + assert_eq!( + MountOption::from_str("sector_size=512").unwrap(), + MountOption::SectorSize(512) + ); + } +}