//! Reading with limits and decoding; atomic writes. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::indexing_slicing )] mod common; use std::fs; use common::tree; use lutra_vfs::{VfsError, read_bytes, read_text, read_text_with_limit, write_atomic, write_text}; #[test] fn reads_utf8_with_and_without_bom() { let dir = tree(&[("plain.php", " = fs::read_dir(dir.path()) .unwrap() .map(|e| e.unwrap().file_name().into_string().unwrap()) .collect(); names.sort(); assert_eq!(names, ["a.php", "created.php"]); } #[test] fn atomic_write_refuses_read_only_files_and_directories() { let dir = tree(&[("locked.php", "keep"), ("sub/", "")]); let path = dir.path().join("locked.php"); let mut perms = fs::metadata(&path).unwrap().permissions(); perms.set_readonly(true); fs::set_permissions(&path, perms.clone()).unwrap(); assert!(matches!( write_atomic(&path, b"lost"), Err(VfsError::ReadOnly { .. }) )); assert_eq!(fs::read(&path).unwrap(), b"keep"); #[allow(clippy::permissions_set_readonly_false)] perms.set_readonly(false); fs::set_permissions(&path, perms).unwrap(); assert!(matches!( write_atomic(&dir.path().join("sub"), b"x"), Err(VfsError::NotAFile { .. }) )); assert!(matches!( write_atomic(&dir.path().join("missing").join("a.php"), b"x"), Err(VfsError::NotFound { .. }) )); } #[cfg(unix)] #[test] fn atomic_write_keeps_permissions() { use std::os::unix::fs::PermissionsExt; let dir = tree(&[("run.sh", "#!/bin/sh")]); let path = dir.path().join("run.sh"); fs::set_permissions(&path, fs::Permissions::from_mode(0o751)).unwrap(); write_atomic(&path, b"#!/bin/sh\necho hi").unwrap(); let mode = fs::metadata(&path).unwrap().permissions().mode() & 0o777; assert_eq!(mode, 0o751); } #[cfg(unix)] #[test] fn atomic_write_through_a_symlink_keeps_the_link() { let dir = tree(&[("real.php", "old")]); let link = dir.path().join("link.php"); std::os::unix::fs::symlink(dir.path().join("real.php"), &link).unwrap(); write_atomic(&link, b"new").unwrap(); assert!( fs::symlink_metadata(&link) .unwrap() .file_type() .is_symlink() ); assert_eq!(fs::read(dir.path().join("real.php")).unwrap(), b"new"); } /// Decoding random bytes never panics, and valid UTF-8 round-trips with and /// without a BOM. A small deterministic generator stands in for proptest. #[test] fn decode_never_panics_on_random_bytes() { let mut state: u64 = 0x9E37_79B9_7F4A_7C15; let mut next = move || { state ^= state << 13; state ^= state >> 7; state ^= state << 17; state }; let path = std::path::Path::new("random.bin"); for _ in 0..5_000 { let len = (next() % 64) as usize; let mut bytes: Vec = (0..len).map(|_| next() as u8).collect(); if next() % 4 == 0 { bytes.splice(0..0, [0xEF, 0xBB, 0xBF]); } match lutra_vfs::decode(path, bytes.clone()) { Ok(file) => { let mut again = if file.bom { vec![0xEF, 0xBB, 0xBF] } else { Vec::new() }; again.extend_from_slice(file.text.as_bytes()); assert_eq!(again, bytes); } Err(VfsError::InvalidUtf8 { valid_up_to, .. }) => assert!(valid_up_to < bytes.len()), Err(VfsError::UnsupportedEncoding { .. }) => {} Err(other) => panic!("unexpected error {other:?}"), } } }