From 088a3fe7b68c84e5f4db1d1a11220d42f07e69e2 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Mon, 9 Aug 2021 22:57:48 +0200 Subject: Add tests for the index functions. --- src/index.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++ testdata/create-testfs1.sh | 16 +++++++++- testdata/testfs1 | Bin 1049600 -> 2097152 bytes 3 files changed, 88 insertions(+), 1 deletion(-) diff --git a/src/index.rs b/src/index.rs index 6f8d18c..5c122fc 100644 --- a/src/index.rs +++ b/src/index.rs @@ -250,3 +250,76 @@ where } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::indexes::NtfsFileNameIndex; + use crate::ntfs::Ntfs; + + #[test] + fn test_index_find() { + let mut testfs1 = crate::helpers::tests::testfs1(); + let mut ntfs = Ntfs::new(&mut testfs1).unwrap(); + ntfs.read_upcase_table(&mut testfs1).unwrap(); + let root_dir = ntfs.root_directory(&mut testfs1).unwrap(); + + // Find the "many_subdirs" subdirectory. + let root_dir_index = root_dir.directory_index(&mut testfs1).unwrap(); + let mut root_dir_finder = root_dir_index.finder(); + let entry = + NtfsFileNameIndex::find(&mut root_dir_finder, &ntfs, &mut testfs1, "many_subdirs") + .unwrap() + .unwrap(); + let subdir = entry.to_file(&ntfs, &mut testfs1).unwrap(); + + // Prove that we can find all 512 indexed subdirectories. + let subdir_index = subdir.directory_index(&mut testfs1).unwrap(); + let mut subdir_finder = subdir_index.finder(); + + for i in 1..=512 { + let dir_name = format!("{}", i); + let entry = NtfsFileNameIndex::find(&mut subdir_finder, &ntfs, &mut testfs1, &dir_name) + .unwrap() + .unwrap(); + let entry_name = entry.key().unwrap().unwrap(); + assert_eq!(entry_name.name(), dir_name.as_str()); + } + } + + #[test] + fn test_index_iter() { + let mut testfs1 = crate::helpers::tests::testfs1(); + let mut ntfs = Ntfs::new(&mut testfs1).unwrap(); + ntfs.read_upcase_table(&mut testfs1).unwrap(); + let root_dir = ntfs.root_directory(&mut testfs1).unwrap(); + + // Find the "many_subdirs" subdirectory. + let root_dir_index = root_dir.directory_index(&mut testfs1).unwrap(); + let mut root_dir_finder = root_dir_index.finder(); + let entry = + NtfsFileNameIndex::find(&mut root_dir_finder, &ntfs, &mut testfs1, "many_subdirs") + .unwrap() + .unwrap(); + let subdir = entry.to_file(&ntfs, &mut testfs1).unwrap(); + + // Prove that we can iterate through all 512 indexed subdirectories in order. + // Keep in mind that subdirectories are ordered like "1", "10", "100", "101", ... + // We can create the same order by adding them to a vector and sorting that vector. + let mut dir_names = Vec::with_capacity(512); + for i in 1..=512 { + dir_names.push(format!("{}", i)); + } + + dir_names.sort_unstable(); + + let subdir_index = subdir.directory_index(&mut testfs1).unwrap(); + let mut subdir_iter = subdir_index.iter(); + + for dir_name in dir_names { + let entry = subdir_iter.next(&mut testfs1).unwrap().unwrap(); + let entry_name = entry.key().unwrap().unwrap(); + assert_eq!(entry_name.name(), dir_name.as_str()); + } + } +} diff --git a/testdata/create-testfs1.sh b/testdata/create-testfs1.sh index 65dac63..ba8badf 100644 --- a/testdata/create-testfs1.sh +++ b/testdata/create-testfs1.sh @@ -6,20 +6,34 @@ if [ "`whoami`" != "root" ]; then exit 1 fi -dd if=/dev/zero of=testfs1 bs=1k count=1025 +dd if=/dev/zero of=testfs1 bs=1k count=2048 mkntfs -c 512 -L mylabel -F testfs1 mkdir mnt mount -t ntfs-3g -o loop testfs1 mnt cd mnt +# Create a file with a specific modification time that we can check. touch -m -t 202101011337 empty-file + +# Create some zeroed files, as allocated and sparse files. dd if=/dev/zero of=file-with-5-zeros bs=1 count=5 dd if=/dev/zero of=big-sparse-file skip=5M bs=1 count=1 +# Create subdirectories of subdirectories. mkdir -p subdir/subsubdir + +# Create a file with some basic real content. echo abcdef > subdir/subsubdir/file-with-6-letters +# Create so many directories that the filesystem needs an INDEX_ROOT and INDEX_ALLOCATION. +mkdir many_subdirs +cd many_subdirs +for i in {1..512}; do + mkdir $i +done +cd .. + cd .. umount mnt rmdir mnt diff --git a/testdata/testfs1 b/testdata/testfs1 index 4afb44a..9d16e0e 100644 Binary files a/testdata/testfs1 and b/testdata/testfs1 differ -- cgit v1.2.3