test(ext4): comprehensive edge case and integration tests
This commit is contained in:
@@ -460,8 +460,105 @@ fn read_past_eof_returns_zero() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
let h = fs.open(b"hello").unwrap();
|
||||
let mut buf = [0u8; 32];
|
||||
fs.read(h, &mut buf).unwrap(); // read all 13 bytes
|
||||
fs.read(h, &mut buf).unwrap();
|
||||
let read = fs.read(h, &mut buf).unwrap();
|
||||
assert_eq!(read, 0);
|
||||
fs.close(h);
|
||||
}
|
||||
|
||||
// --- Review Focus edge cases ---
|
||||
|
||||
#[test]
|
||||
fn write_allocates_multiple_blocks() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
let h = fs.create(b"multi").unwrap();
|
||||
let block = [0xABu8; 4096];
|
||||
fs.write(h, &block).unwrap();
|
||||
fs.write(h, &block).unwrap();
|
||||
fs.write(h, &block).unwrap();
|
||||
fs.close(h);
|
||||
let h = fs.open(b"multi").unwrap();
|
||||
assert_eq!(fs.file_size(h), Some(4096 * 3));
|
||||
let mut buf = [0u8; 4096];
|
||||
fs.read(h, &mut buf).unwrap();
|
||||
fs.read(h, &mut buf).unwrap();
|
||||
let n = fs.read(h, &mut buf).unwrap();
|
||||
assert_eq!(n, 4096);
|
||||
assert!(buf.iter().all(|&b| b == 0xAB));
|
||||
fs.close(h);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_multiple_files_in_directory() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
let h1 = fs.create(b"file_a").unwrap();
|
||||
fs.close(h1);
|
||||
let h2 = fs.create(b"file_b").unwrap();
|
||||
fs.close(h2);
|
||||
let h3 = fs.create(b"file_c").unwrap();
|
||||
fs.close(h3);
|
||||
let mut entries = [DirEntry::empty(); 8];
|
||||
let count = fs.list_dir(b"/", &mut entries).unwrap();
|
||||
assert_eq!(count, 4); // hello + 3 new
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_and_reuse_resources() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
let h = fs.create(b"ephemeral").unwrap();
|
||||
fs.write(h, b"temp").unwrap();
|
||||
fs.close(h);
|
||||
assert!(fs.delete(b"ephemeral"));
|
||||
let h2 = fs.create(b"recycled").unwrap();
|
||||
fs.write(h2, b"reused").unwrap();
|
||||
fs.close(h2);
|
||||
let h3 = fs.open(b"recycled").unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let n = fs.read(h3, &mut buf).unwrap();
|
||||
assert_eq!(&buf[..n], b"reused");
|
||||
fs.close(h3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_missing_parent_preserves_state() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
let mut before = [DirEntry::empty(); 8];
|
||||
let count_before = fs.list_dir(b"/", &mut before).unwrap();
|
||||
assert!(fs.create(b"nodir/file.txt").is_none());
|
||||
let mut after = [DirEntry::empty(); 8];
|
||||
let count_after = fs.list_dir(b"/", &mut after).unwrap();
|
||||
assert_eq!(count_before, count_after);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_files_independent_inodes() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
let h1 = fs.create(b"alpha").unwrap();
|
||||
let h2 = fs.create(b"beta").unwrap();
|
||||
fs.write(h1, b"aaa").unwrap();
|
||||
fs.write(h2, b"bbb").unwrap();
|
||||
fs.close(h1);
|
||||
fs.close(h2);
|
||||
let h1 = fs.open(b"alpha").unwrap();
|
||||
let h2 = fs.open(b"beta").unwrap();
|
||||
let mut buf = [0u8; 16];
|
||||
let n1 = fs.read(h1, &mut buf).unwrap();
|
||||
assert_eq!(&buf[..n1], b"aaa");
|
||||
let n2 = fs.read(h2, &mut buf).unwrap();
|
||||
assert_eq!(&buf[..n2], b"bbb");
|
||||
fs.close(h1);
|
||||
fs.close(h2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_nonexistent_returns_false() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
assert!(!fs.delete(b"ghost"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_directory_as_file_returns_none() {
|
||||
let mut fs = Ext4::mount(format_ext4_disk()).unwrap();
|
||||
assert!(fs.mkdir(b"adir"));
|
||||
assert!(fs.open(b"adir").is_none());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user