From b1894a435e3d8088c689c4ced44be68bc1ce0710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Harabie=C5=84?= Date: Tue, 12 Jun 2018 01:16:35 +0200 Subject: [PATCH] Add FAT reading tests --- src/table.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/src/table.rs b/src/table.rs index e69988c..a81edcb 100644 --- a/src/table.rs +++ b/src/table.rs @@ -15,7 +15,7 @@ type Fat32 = Fat; const RESERVED_FAT_ENTRIES: u32 = 2; -#[derive(Debug, Clone, Copy)] +#[derive(Copy, Clone, Eq, PartialEq, Debug)] enum FatValue { Free, Data(u32), @@ -387,3 +387,66 @@ impl <'a, 'b> Iterator for ClusterIterator<'a, 'b> { self.cluster.map(|n| Ok(n)) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn test_fat(fat_type: FatType, mut cur: T) { + // based on cluster maps from Wikipedia: + // https://en.wikipedia.org/wiki/Design_of_the_FAT_file_system#Cluster_map + assert_eq!(read_fat(&mut cur, fat_type, 1).unwrap(), FatValue::EndOfChain); + assert_eq!(read_fat(&mut cur, fat_type, 4).unwrap(), FatValue::Data(5)); + assert_eq!(read_fat(&mut cur, fat_type, 5).unwrap(), FatValue::Data(6)); + assert_eq!(read_fat(&mut cur, fat_type, 8).unwrap(), FatValue::EndOfChain); + assert_eq!(read_fat(&mut cur, fat_type, 9).unwrap(), FatValue::Data(0xA)); + assert_eq!(read_fat(&mut cur, fat_type, 0xA).unwrap(), FatValue::Data(0x14)); + assert_eq!(read_fat(&mut cur, fat_type, 0x12).unwrap(), FatValue::Free); + assert_eq!(read_fat(&mut cur, fat_type, 0x17).unwrap(), FatValue::Bad); + assert_eq!(read_fat(&mut cur, fat_type, 0x18).unwrap(), FatValue::Bad); + assert_eq!(read_fat(&mut cur, fat_type, 0x1B).unwrap(), FatValue::Free); + + assert_eq!(find_free_cluster(&mut cur, fat_type, 2, 0x20).unwrap(), 0x12); + assert_eq!(find_free_cluster(&mut cur, fat_type, 0x12, 0x20).unwrap(), 0x12); + assert_eq!(find_free_cluster(&mut cur, fat_type, 0x13, 0x20).unwrap(), 0x1B); + assert!(find_free_cluster(&mut cur, fat_type, 0x13, 0x14).is_err()); + + assert_eq!(count_free_clusters(&mut cur, fat_type, 0x20).unwrap(), 5); + } + + #[test] + fn test_fat12() { + let mut fat: Vec = vec![ + 0xF0, 0xFF, 0xFF, 0x03, 0x40, 0x00, 0x05, 0x60, 0x00, 0x07, 0x80, 0x00, 0xFF, 0xAF, 0x00, 0x14, + 0xC0, 0x00, 0x0D, 0xE0, 0x00, 0x0F, 0x00, 0x01, 0x11, 0xF0, 0xFF, 0x00, 0xF0, 0xFF, 0x15, 0x60, + 0x01, 0x19, 0x70, 0xFF, 0xF7, 0xAF, 0x01, 0xFF, 0x0F, 0x00, 0x00, 0x70, 0xFF, 0x00, 0x00, 0x00, + ]; + test_fat(FatType::Fat12, io::Cursor::new(&mut fat)); + } + + #[test] + fn test_fat16() { + let mut fat: Vec = vec![ + 0xF0, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x08, 0x00, + 0xFF, 0xFF, 0x0A, 0x00, 0x14, 0x00, 0x0C, 0x00, 0x0D, 0x00, 0x0E, 0x00, 0x0F, 0x00, 0x10, 0x00, + 0x11, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x15, 0x00, 0x16, 0x00, 0x19, 0x00, 0xF7, 0xFF, + 0xF7, 0xFF, 0x1A, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xF7, 0xFF, 0x00, 0x00, 0x00, 0x00, + ]; + test_fat(FatType::Fat16, io::Cursor::new(&mut fat)); + } + + #[test] + fn test_fat32() { + let mut fat: Vec = vec![ + 0xF0, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0x0F, 0xFF, 0xFF, 0xFF, 0x0F, 0x04, 0x00, 0x00, 0x00, + 0x05, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, + 0xFF, 0xFF, 0xFF, 0x0F, 0x0A, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, + 0x0D, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x11, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x0F, + 0x15, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0xF7, 0xFF, 0xFF, 0x0F, + 0xF7, 0xFF, 0xFF, 0x0F, 0x1A, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xF7, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + ]; + test_fat(FatType::Fat32, io::Cursor::new(&mut fat)); + } +}