| 
 | 1 | +//! Tests functionality from the `uefi::fs` module. See function [`test`].  | 
 | 2 | +
  | 
 | 3 | +use alloc::string::{String, ToString};  | 
 | 4 | +use alloc::vec::Vec;  | 
 | 5 | +use uefi::fs::{FileSystem, FileSystemError};  | 
 | 6 | +use uefi::proto::media::fs::SimpleFileSystem;  | 
 | 7 | +use uefi::table::boot::ScopedProtocol;  | 
 | 8 | + | 
 | 9 | +/// Tests functionality from the `uefi::fs` module. This test relies on a  | 
 | 10 | +/// working File System Protocol, which is tested at a dedicated place.  | 
 | 11 | +pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), FileSystemError> {  | 
 | 12 | +    let mut fs = FileSystem::new(sfs);  | 
 | 13 | + | 
 | 14 | +    fs.create_dir("test_file_system_abs")?;  | 
 | 15 | + | 
 | 16 | +    // slash is transparently transformed to backslash  | 
 | 17 | +    fs.write("test_file_system_abs/foo", "hello")?;  | 
 | 18 | +    // absolute or relative paths are supported; ./ is ignored  | 
 | 19 | +    fs.copy("\\test_file_system_abs/foo", "\\test_file_system_abs/./bar")?;  | 
 | 20 | +    let read = fs.read("\\test_file_system_abs\\bar")?;  | 
 | 21 | +    let read = String::from_utf8(read).expect("Should be valid utf8");  | 
 | 22 | +    assert_eq!(read, "hello");  | 
 | 23 | + | 
 | 24 | +    assert_eq!(  | 
 | 25 | +        fs.try_exists("test_file_system_abs\\barfoo"),  | 
 | 26 | +        Err(FileSystemError::OpenError(  | 
 | 27 | +            "\\test_file_system_abs\\barfoo".to_string()  | 
 | 28 | +        ))  | 
 | 29 | +    );  | 
 | 30 | +    fs.rename("test_file_system_abs\\bar", "test_file_system_abs\\barfoo")?;  | 
 | 31 | +    assert!(fs.try_exists("test_file_system_abs\\barfoo").is_ok());  | 
 | 32 | + | 
 | 33 | +    let entries = fs  | 
 | 34 | +        .read_dir("test_file_system_abs")?  | 
 | 35 | +        .map(|e| {  | 
 | 36 | +            e.expect("Should return boxed file info")  | 
 | 37 | +                .file_name()  | 
 | 38 | +                .to_string()  | 
 | 39 | +        })  | 
 | 40 | +        .collect::<Vec<_>>();  | 
 | 41 | +    assert_eq!(&[".", "..", "foo", "barfoo"], entries.as_slice());  | 
 | 42 | + | 
 | 43 | +    fs.create_dir("/deeply_nested_test")?;  | 
 | 44 | +    fs.create_dir("/deeply_nested_test/1")?;  | 
 | 45 | +    fs.create_dir("/deeply_nested_test/1/2")?;  | 
 | 46 | +    fs.create_dir("/deeply_nested_test/1/2/3")?;  | 
 | 47 | +    fs.create_dir("/deeply_nested_test/1/2/3/4")?;  | 
 | 48 | +    fs.create_dir_all("/deeply_nested_test/1/2/3/4/5/6/7")?;  | 
 | 49 | +    fs.try_exists("/deeply_nested_test/1/2/3/4/5/6/7")?;  | 
 | 50 | +    // TODO  | 
 | 51 | +    // fs.remove_dir_all("/deeply_nested_test/1/2/3/4/5/6/7")?;  | 
 | 52 | +    fs.remove_dir("/deeply_nested_test/1/2/3/4/5/6/7")?;  | 
 | 53 | +    let exists = matches!(fs.try_exists("/deeply_nested_test/1/2/3/4/5/6/7"), Ok(_));  | 
 | 54 | +    assert!(!exists);  | 
 | 55 | + | 
 | 56 | +    Ok(())  | 
 | 57 | +}  | 
0 commit comments