2017-10-06 23:00:38 +08:00
|
|
|
extern crate fatfs;
|
2017-09-23 05:20:06 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
use std::fs;
|
2017-10-09 20:59:52 +08:00
|
|
|
use std::io::SeekFrom;
|
2017-09-24 04:22:25 +08:00
|
|
|
use std::io::prelude::*;
|
2017-09-23 05:20:06 +08:00
|
|
|
use std::str;
|
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
use fatfs::{FileSystem, FatType, DirEntry, BufStream};
|
2017-09-23 05:20:06 +08:00
|
|
|
|
2017-09-27 20:05:58 +08:00
|
|
|
const TEST_TEXT: &str = "Rust is cool!\n";
|
|
|
|
const FAT12_IMG: &str = "resources/fat12.img";
|
|
|
|
const FAT16_IMG: &str = "resources/fat16.img";
|
|
|
|
const FAT32_IMG: &str = "resources/fat32.img";
|
2017-09-24 04:22:25 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn call_with_fs(f: &Fn(FileSystem) -> (), filename: &str) {
|
|
|
|
let file = fs::File::open(filename).unwrap();
|
2017-10-09 20:59:52 +08:00
|
|
|
let mut buf_file = BufStream::new(file);
|
|
|
|
let fs = FileSystem::new(&mut buf_file).unwrap();
|
2017-09-27 20:05:58 +08:00
|
|
|
f(fs);
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn test_root_dir(fs: FileSystem) {
|
2017-10-02 00:45:58 +08:00
|
|
|
let root_dir = fs.root_dir();
|
2017-10-07 20:56:50 +08:00
|
|
|
let entries = root_dir.iter().map(|r| r.unwrap()).collect::<Vec<DirEntry>>();
|
2017-09-25 04:12:38 +08:00
|
|
|
let short_names = entries.iter().map(|e| e.short_file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(short_names, ["LONG.TXT", "SHORT.TXT", "VERY", "VERY-L~1"]);
|
2017-09-24 20:34:07 +08:00
|
|
|
let names = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>();
|
2017-09-25 04:12:38 +08:00
|
|
|
assert_eq!(names, ["long.txt", "short.txt", "very", "very-long-dir-name"]);
|
2017-09-24 08:10:59 +08:00
|
|
|
// Try read again
|
2017-10-06 22:07:11 +08:00
|
|
|
let names2 = root_dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(names2, names);
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_root_dir_fat12() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_root_dir, FAT12_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_root_dir_fat16() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_root_dir, FAT16_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_root_dir_fat32() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_root_dir, FAT32_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn test_read_seek_short_file(fs: FileSystem) {
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut root_dir = fs.root_dir();
|
2017-09-25 04:12:38 +08:00
|
|
|
let mut short_file = root_dir.open_file("short.txt").unwrap();
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
short_file.read_to_end(&mut buf).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT);
|
2017-09-24 04:22:25 +08:00
|
|
|
|
2017-10-07 22:07:33 +08:00
|
|
|
assert_eq!(short_file.seek(SeekFrom::Start(5)).unwrap(), 5);
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut buf2 = [0; 5];
|
|
|
|
short_file.read_exact(&mut buf2).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&buf2).unwrap(), &TEST_TEXT[5..10]);
|
2017-10-07 22:07:33 +08:00
|
|
|
|
2017-10-10 22:05:19 +08:00
|
|
|
assert_eq!(short_file.seek(SeekFrom::Start(1000)).unwrap(), TEST_TEXT.len() as u64);
|
2017-10-07 22:07:33 +08:00
|
|
|
let mut buf2 = [0; 5];
|
|
|
|
assert_eq!(short_file.read(&mut buf2).unwrap(), 0);
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_seek_short_file_fat12() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_read_seek_short_file, FAT12_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_seek_short_file_fat16() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_read_seek_short_file, FAT16_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_seek_short_file_fat32() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_read_seek_short_file, FAT32_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn test_read_long_file(fs: FileSystem) {
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut root_dir = fs.root_dir();
|
2017-09-25 04:12:38 +08:00
|
|
|
let mut long_file = root_dir.open_file("long.txt").unwrap();
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
long_file.read_to_end(&mut buf).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT.repeat(1000));
|
2017-09-24 06:05:43 +08:00
|
|
|
|
2017-10-07 22:07:33 +08:00
|
|
|
assert_eq!(long_file.seek(SeekFrom::Start(2017)).unwrap(), 2017);
|
2017-09-24 09:08:00 +08:00
|
|
|
buf.clear();
|
|
|
|
let mut buf2 = [0; 10];
|
|
|
|
long_file.read_exact(&mut buf2).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&buf2).unwrap(), &TEST_TEXT.repeat(1000)[2017..2027]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_long_file_fat12() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_read_long_file, FAT12_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_long_file_fat16() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_read_long_file, FAT16_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_long_file_fat32() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_read_long_file, FAT32_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn test_get_dir_by_path(fs: FileSystem) {
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut root_dir = fs.root_dir();
|
2017-10-02 00:45:58 +08:00
|
|
|
let dir = root_dir.open_dir("very/long/path/").unwrap();
|
2017-10-06 22:07:11 +08:00
|
|
|
let names = dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
2017-09-25 04:12:38 +08:00
|
|
|
assert_eq!(names, [".", "..", "test.txt"]);
|
2017-10-07 22:37:15 +08:00
|
|
|
|
|
|
|
let dir2 = root_dir.open_dir("very/long/path/././.").unwrap();
|
|
|
|
let names2 = dir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(names2, [".", "..", "test.txt"]);
|
|
|
|
|
|
|
|
let root_dir2 = root_dir.open_dir("very/long/path/../../..").unwrap();
|
|
|
|
let root_names = root_dir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
let root_names2 = root_dir.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(root_names, root_names2);
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_dir_by_path_fat12() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_get_dir_by_path, FAT12_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_dir_by_path_fat16() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_get_dir_by_path, FAT16_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_dir_by_path_fat32() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_get_dir_by_path, FAT32_IMG)
|
2017-09-24 09:08:00 +08:00
|
|
|
}
|
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn test_get_file_by_path(fs: FileSystem) {
|
2017-09-24 09:08:00 +08:00
|
|
|
let mut root_dir = fs.root_dir();
|
2017-09-25 04:12:38 +08:00
|
|
|
let mut file = root_dir.open_file("very/long/path/test.txt").unwrap();
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
file.read_to_end(&mut buf).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT);
|
|
|
|
|
2017-10-09 20:59:52 +08:00
|
|
|
// let mut file = root_dir.open_file("very-long-dir-name/very-long-file-name.txt").unwrap();
|
|
|
|
// let mut buf = Vec::new();
|
|
|
|
// file.read_to_end(&mut buf).unwrap();
|
|
|
|
// assert_eq!(str::from_utf8(&buf).unwrap(), TEST_TEXT);
|
2017-09-24 03:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-09-24 09:08:00 +08:00
|
|
|
fn test_get_file_by_path_fat12() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_get_file_by_path, FAT12_IMG)
|
2017-09-24 03:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-09-24 09:08:00 +08:00
|
|
|
fn test_get_file_by_path_fat16() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_get_file_by_path, FAT16_IMG)
|
2017-09-24 03:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-09-24 09:08:00 +08:00
|
|
|
fn test_get_file_by_path_fat32() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&test_get_file_by_path, FAT32_IMG)
|
2017-09-23 05:20:06 +08:00
|
|
|
}
|
2017-09-25 04:12:38 +08:00
|
|
|
|
2017-10-07 20:56:50 +08:00
|
|
|
fn test_volume_metadata(fs: FileSystem, fat_type: FatType) {
|
2017-09-25 04:12:38 +08:00
|
|
|
assert_eq!(fs.volume_id(), 0x12345678);
|
|
|
|
assert_eq!(fs.volume_label(), "Test!");
|
|
|
|
assert_eq!(fs.fat_type(), fat_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_volume_metadata_fat12() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&|fs| test_volume_metadata(fs, FatType::Fat12), FAT12_IMG)
|
2017-09-25 04:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_volume_metadata_fat16() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&|fs| test_volume_metadata(fs, FatType::Fat16), FAT16_IMG)
|
2017-09-25 04:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_volume_metadata_fat32() {
|
2017-09-27 20:05:58 +08:00
|
|
|
call_with_fs(&|fs| test_volume_metadata(fs, FatType::Fat32), FAT32_IMG)
|
2017-09-25 04:12:38 +08:00
|
|
|
}
|