Added test to write and read directory entries.

Fixed `DirPtr::read_entries` returning `Err` on eof.
This commit is contained in:
Filipe Rodrigues 2021-07-15 00:51:25 +01:00
parent f05823f60c
commit 508db2a680
2 changed files with 50 additions and 1 deletions

View File

@ -2,6 +2,8 @@
// Modules
mod error;
#[cfg(test)]
mod test;
// Exports
pub use error::{FindEntryError, FindError, ReadEntriesError, ReadEntryError, WriteEntriesError};
@ -71,7 +73,12 @@ impl DirPtr {
Some(DirEntry::deserialize_bytes(&bytes).map_err(ReadEntryError::ParseEntry)?)
};
entry.transpose()
// If the error was `ReadEntry` with an unexpected eof, return `None` instead
// TODO: Maybe only allow this at the end of a sector?
match entry {
Err(ReadEntryError::ReadEntry(err)) if err.kind() == io::ErrorKind::UnexpectedEof => None,
entry => entry.transpose(),
}
});
Ok(iter)

View File

@ -0,0 +1,42 @@
//! Tests
use crate::FilePtr;
// Imports
use super::*;
use chrono::NaiveDateTime;
use zutil::AsciiStrArr;
#[test]
fn write_read_entries() {
let mut buffer = io::Cursor::new(vec![0; 0x16 * 2]);
let dir = DirPtr::root();
let entries = vec![
DirEntry {
name: AsciiStrArr::from_bytes("dir-1").expect("Invalid string"),
date: NaiveDateTime::from_timestamp(123, 0),
kind: DirEntryKind::Dir { ptr: DirPtr::new(123) },
},
DirEntry {
name: AsciiStrArr::from_bytes("file-1").expect("Invalid string"),
date: NaiveDateTime::from_timestamp(123, 0),
kind: DirEntryKind::File {
ptr: FilePtr::new(123, 456),
extension: AsciiStrArr::from_bytes("ext").expect("Invalid string"),
},
},
];
dir.write_entries(&mut buffer, entries.iter().cloned())
.expect("Unable to write entries");
let read_entries = dir
.read_entries(&mut buffer)
.expect("Unable to read entries")
.collect::<Result<Vec<_>, _>>()
.expect("Unable to read all entries");
assert_eq!(entries, read_entries);
}