2017-09-23 05:20:06 +08:00
|
|
|
extern crate rustfat;
|
|
|
|
|
|
|
|
use std::fs::File;
|
2017-09-24 08:10:59 +08:00
|
|
|
use std::io::{BufReader, SeekFrom};
|
2017-09-24 04:22:25 +08:00
|
|
|
use std::io::prelude::*;
|
2017-09-23 05:20:06 +08:00
|
|
|
use std::str;
|
|
|
|
|
|
|
|
use rustfat::FatFileSystem;
|
|
|
|
|
2017-09-24 04:22:25 +08:00
|
|
|
const TEST_TEXT: &'static str = "Rust is cool!\n";
|
|
|
|
|
2017-09-24 03:24:34 +08:00
|
|
|
fn test_img(name: &str) {
|
|
|
|
let file = File::open(name).unwrap();
|
2017-09-24 01:42:09 +08:00
|
|
|
let buf_rdr = BufReader::new(file);
|
|
|
|
let mut fs = FatFileSystem::new(Box::new(buf_rdr)).unwrap();
|
2017-09-23 05:20:06 +08:00
|
|
|
let mut root_dir = fs.root_dir();
|
2017-09-24 01:42:09 +08:00
|
|
|
let entries = root_dir.list().unwrap();
|
2017-09-24 08:10:59 +08:00
|
|
|
let names = entries.iter().map(|e| e.get_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(names, ["LONG.TXT", "SHORT.TXT", "VERY"]);
|
|
|
|
// Try read again
|
|
|
|
let entries = root_dir.list().unwrap();
|
|
|
|
let names2 = entries.iter().map(|e| e.get_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(names2, names);
|
2017-09-24 04:22:25 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut short_file = entries[1].get_file();
|
|
|
|
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 08:10:59 +08:00
|
|
|
|
|
|
|
short_file.seek(SeekFrom::Start(5)).unwrap();
|
|
|
|
buf.clear();
|
|
|
|
short_file.read_to_end(&mut buf).unwrap();
|
|
|
|
assert_eq!(str::from_utf8(&buf).unwrap(), &TEST_TEXT[5..]);
|
2017-09-24 04:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut long_file = entries[0].get_file();
|
|
|
|
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
|
|
|
{
|
|
|
|
let mut root_dir = fs.root_dir();
|
|
|
|
let mut dir = root_dir.get_dir("very/long/path/").unwrap();
|
|
|
|
let entries = dir.list().unwrap();
|
|
|
|
let names = entries.iter().map(|e| e.get_name()).collect::<Vec<String>>();
|
|
|
|
assert_eq!(names, [".", "..", "TEST.TXT"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut root_dir = fs.root_dir();
|
|
|
|
let mut file = root_dir.get_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-09-24 03:24:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fat12() {
|
|
|
|
test_img("resources/fat12.img");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fat16() {
|
|
|
|
test_img("resources/fat16.img");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fat32() {
|
|
|
|
test_img("resources/fat32.img");
|
2017-09-23 05:20:06 +08:00
|
|
|
}
|