diff --git a/easy-fs-fuse/src/main.rs b/easy-fs-fuse/src/main.rs index 09016c3..39b9eaf 100644 --- a/easy-fs-fuse/src/main.rs +++ b/easy-fs-fuse/src/main.rs @@ -149,3 +149,59 @@ fn efs_test() -> std::io::Result<()> { Ok(()) } + +#[test] +fn mac_test() -> std::io::Result<()> { + const BLOCK_SZ: usize = 512; + + let block_file = Arc::new(BlockFile(Mutex::new({ + let f = OpenOptions::new() + .read(true) + .write(true) + .create(true) + .open("target/fs_mac.img")?; + f.set_len(8192 * BLOCK_SZ).unwrap(); + f + }))); + + EasyFileSystem::create(block_file.clone(), 4096, 1); + let efs = EasyFileSystem::open(block_file.clone()); + let root_inode = EasyFileSystem::root_inode(&efs); + + root_inode.create("root_file"); + root_inode.create("public_file"); + + let secret_inode = root_inode.find("root_file").unwrap(); + secret_inode.write_at(0, b"TOP SECRET: root only!"); + let public_inode = root_inode.find("public_file").unwrap(); + public_inode.write_at(0, b"This file is public."); + + let check_permission = |user: &str, filename: &str| -> bool { + if filename == "root_file" && user != "root" { + false + } else { + true + } + }; + + let users = ["root", "nonroot"]; + for user in users.iter() { + println!("{} task:", user); + + for filename in ["root_file", "public_file"].iter() { + if check_permission(user, filename) { + let inode = root_inode.find(filename).unwrap(); + let mut buf = [0u8; 128]; + let len = inode.read_at(0, &mut buf); + let content = core::str::from_utf8(&buf[..len]).unwrap(); + println!("{}: Opened successfully"); + } else { + println!("{}: Permission denied"); + } + } + + println!(); + } + + Ok(()) +}