add unit test for fs impl

This commit is contained in:
Ekko.bao 2024-07-04 08:50:46 +08:00
parent 6b9433f756
commit 6586dab3f0

View File

@ -6,6 +6,7 @@ use std::error::Error;
use crate::lws_vfs::{Fstat, FileInfo}; use crate::lws_vfs::{Fstat, FileInfo};
extern crate winapi; extern crate winapi;
use io::Write;
use winapi::um::winnt::{FILE_ATTRIBUTE_READONLY}; use winapi::um::winnt::{FILE_ATTRIBUTE_READONLY};
use std::os::windows::fs::MetadataExt; use std::os::windows::fs::MetadataExt;
use std::os::windows::prelude::*; use std::os::windows::prelude::*;
@ -152,3 +153,44 @@ pub fn faccess(path: &String, mask: u32) -> Result<i32, Box<dyn Error>> {
let permissions = get_file_mode_mask(path); let permissions = get_file_mode_mask(path);
Ok(if permissions & mask != 0 {0} else {-1}) Ok(if permissions & mask != 0 {0} else {-1})
} }
#[test]
fn test() {
use crate::fs_impl::*;
let path = String::from("test.txt");
let mut file: fs::File = fs::File::open("test.txt").unwrap();
let _ = file.write(b"This is a test file.\n");
let mut buffer = vec![0; 100];
let mut size = 0;
let mut offsize: usize = 0;
let mut fi = FileInfo::default();
size = 4;
let mut buffer = vec![0; size];
offsize = 5;
let _ = fread(&path, &mut buffer, &mut size, offsize, &mut fi);
assert_eq!(buffer.len(), 4);
assert_eq!(buffer, b"is a");
let result = fwrite(&path, &buffer, &mut size, offsize, &mut fi);
println!("result: {:?}", result);
println!("size: {:?}", size);
println!("buffer: {:?}", buffer);
let result = freaddir(&path, &mut vec![], 0, 0, &mut fi);
println!("result: {:?}", result);
println!("buffer: {:?}", buffer);
let result = fmkdir(&path, 0);
println!("result: {:?}", result);
let result = fchown(&path, 0, 0);
println!("result: {:?}", result);
let result = ftruncate(&path, 100);
println!("result: {:?}", result);
let result = futimens(&path, &vec![100, 0], &vec![100, 0]);
println!("result: {:?}", result);
let result = frelease(&path, &mut fi);
println!("result: {:?}", result);
let result = fsetxattr(&path, "test", &vec![1,2,3], 3, 0);
println!("result: {:?}", result);
let result = fgetxattr(&path, "test", 100);
println!("result: {:?}", result);
let result = faccess(&path, 0o777);
println!("result: {:?}", result);
}