Skip to content

Commit

Permalink
test(fat32): create various fat32-related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oakchris1955 committed Aug 31, 2024
1 parent d8a5cfd commit 418451a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
Binary file modified imgs/fat32.img
Binary file not shown.
2 changes: 1 addition & 1 deletion imgs/fat32.img.check
Original file line number Diff line number Diff line change
@@ -1 +1 @@
604b7fdf8131868241dc4f33817994838f0da4d9795d400c0120e460cf7897e1 *imgs/fat32.img
5e49c78dfcf001666926151f1a95df991f91729016cc3949fc589702604d2a5f *imgs/fat32.img
105 changes: 105 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2717,6 +2717,111 @@ mod tests {
assert_file_is_bee_movie_script(&mut file);
}

#[test]
fn read_file_fat32() {
use std::io::Cursor;

let mut storage = Cursor::new(FAT32.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

let mut file = fs
.get_ro_file(PathBuf::from("/secret/bee movie script.txt"))
.unwrap();

assert_file_is_bee_movie_script(&mut file);
}

#[test]
fn seek_n_read_fat32() {
use std::io::Cursor;

let mut storage = Cursor::new(FAT32.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

let mut file = fs.get_ro_file(PathBuf::from("/hello.txt")).unwrap();
file.seek(SeekFrom::Start(13)).unwrap();

let mut string = String::new();
file.read_to_string(&mut string).unwrap();
const EXPECTED_STR: &str = "FAT32 filesystem!!!\n";

assert_eq!(string, EXPECTED_STR);
}

#[test]
fn write_to_fat32_file() {
use std::io::Cursor;

let mut storage = Cursor::new(FAT32.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

let mut file = fs.get_rw_file(PathBuf::from("/hello.txt")).unwrap();
// an arbitrary offset to seek to
const START_OFFSET: u64 = 1436;
file.seek(SeekFrom::Start(START_OFFSET)).unwrap();

file.write_all(BEE_MOVIE_SCRIPT.as_bytes()).unwrap();

// seek back
file.seek(SeekFrom::Current(-(BEE_MOVIE_SCRIPT.len() as i64)))
.unwrap();

// read back what we wrote
let mut string = String::new();
file.read_to_string(&mut string).unwrap();
assert_eq!(string, BEE_MOVIE_SCRIPT);

// let's also read back what was (and hopefully still is)
// at the start of the file
const EXPECTED_STR: &str = "Hello from a FAT32 filesystem!!!\n";
file.rewind().unwrap();
let mut buf = [0_u8; EXPECTED_STR.len()];
file.read_exact(&mut buf).unwrap();

let stored_text = str::from_utf8(&buf).unwrap();
assert_eq!(stored_text, EXPECTED_STR)
}

#[test]
fn truncate_fat32_file() {
use std::io::Cursor;

let mut storage = Cursor::new(FAT32.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

const EXPECTED_STR: &str = "Hello fr";

let mut file = fs.get_rw_file(PathBuf::from("/hello.txt")).unwrap();
file.truncate(EXPECTED_STR.len() as u32).unwrap();

let mut string = String::new();
file.read_to_string(&mut string).unwrap();
assert_eq!(string, EXPECTED_STR);
}

#[test]
fn remove_fat32_file() {
use std::io::Cursor;

let mut storage = Cursor::new(FAT32.to_owned());
let mut fs = FileSystem::from_storage(&mut storage).unwrap();

let file_path = PathBuf::from("/secret/bee movie script.txt");

let file = fs.get_rw_file(file_path.clone()).unwrap();
file.remove().unwrap();

// the file should now be gone
let file_result = fs.get_ro_file(file_path);
match file_result {
Err(err) => match err {
FSError::NotFound => (),
_ => panic!("unexpected IOError: {:?}", err),
},
_ => panic!("file should have been deleted by now"),
}
}

#[test]
fn assert_img_fat_type() {
static TEST_CASES: &[(&[u8], FATType)] = &[
Expand Down

0 comments on commit 418451a

Please sign in to comment.