diff options
author | Colin Finck <colin@reactos.org> | 2021-04-21 22:36:57 +0300 |
---|---|---|
committer | Colin Finck <colin@reactos.org> | 2021-04-21 22:36:57 +0300 |
commit | c815c5e771eb9f29291594c25ee247b365adcd68 (patch) | |
tree | b2097bf7d1884766196aa79e7412caa6639faf45 | |
parent | 5867e4bb56d605953df1ff2bc0cce9deef497c9a (diff) |
Move attribute tests to their corresponding structured values and improve them.
-rw-r--r-- | src/attribute.rs | 67 | ||||
-rw-r--r-- | src/structured_values/file_name.rs | 57 | ||||
-rw-r--r-- | src/structured_values/standard_information.rs | 48 |
3 files changed, 100 insertions, 72 deletions
diff --git a/src/attribute.rs b/src/attribute.rs index 8f4b969..dd56811 100644 --- a/src/attribute.rs +++ b/src/attribute.rs @@ -341,70 +341,3 @@ where } impl<'a, T> FusedIterator for NtfsAttributes<'a, T> where T: Read + Seek {} - -#[cfg(test)] -mod tests { - use super::*; - use crate::ntfs::Ntfs; - use crate::ntfs_file::KnownNtfsFile; - use crate::time::tests::NT_TIMESTAMP_2021_01_01; - - #[test] - fn test_data() { - let mut testfs1 = crate::helpers::tests::testfs1(); - let ntfs = Ntfs::new(&mut testfs1).unwrap(); - let mft = ntfs - .ntfs_file(&mut testfs1, KnownNtfsFile::MFT as u64) - .unwrap(); - let mut mft_attributes = mft.attributes(&mut testfs1); - - // Check the StandardInformation attribute. - let attribute = mft_attributes.next().unwrap().unwrap(); - assert_eq!( - attribute.ty().unwrap(), - NtfsAttributeType::StandardInformation, - ); - assert_eq!(attribute.attribute_length(), 96); - assert!(attribute.is_resident()); - assert_eq!(attribute.name_length(), 0); - assert_eq!(attribute.value_length(), 72); - - // Check the FileName attribute. - let attribute = mft_attributes.next().unwrap().unwrap(); - assert_eq!(attribute.ty().unwrap(), NtfsAttributeType::FileName); - assert_eq!(attribute.attribute_length(), 104); - assert!(attribute.is_resident()); - assert_eq!(attribute.name_length(), 0); - assert_eq!(attribute.value_length(), 74); - - // Check the actual "file name" of the MFT. - let value = attribute.read_structured_value(&mut testfs1).unwrap(); - let file_name = match value { - NtfsStructuredValue::FileName(file_name) => file_name, - v => panic!("Unexpected NtfsStructuredValue: {:?}", v), - }; - - let creation_time = file_name.creation_time(); - assert!(*creation_time > NT_TIMESTAMP_2021_01_01); - assert_eq!(creation_time, file_name.modification_time()); - assert_eq!(creation_time, file_name.mft_record_modification_time()); - assert_eq!(creation_time, file_name.access_time()); - - let allocated_size = file_name.allocated_size(); - assert!(allocated_size > 0); - assert_eq!(allocated_size, file_name.data_size()); - - assert_eq!(file_name.name_length(), 8); - - let mut buf = [0u8; 8]; - let file_name_string = file_name.read_name(&mut testfs1, &mut buf).unwrap(); - - // Test various ways to compare the same string. - assert_eq!(file_name_string, "$MFT"); - assert_eq!(file_name_string.to_string_lossy(), String::from("$MFT")); - assert_eq!( - file_name_string, - NtfsString(&[b'$', 0, b'M', 0, b'F', 0, b'T', 0]) - ); - } -} diff --git a/src/structured_values/file_name.rs b/src/structured_values/file_name.rs index 19f50cc..840fa82 100644 --- a/src/structured_values/file_name.rs +++ b/src/structured_values/file_name.rs @@ -140,3 +140,60 @@ impl NtfsFileName { Ok(NtfsString(&buf[..name_length])) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::ntfs::Ntfs; + use crate::ntfs_file::KnownNtfsFile; + use crate::structured_values::NtfsStructuredValue; + use crate::time::tests::NT_TIMESTAMP_2021_01_01; + + #[test] + fn test_file_name() { + let mut testfs1 = crate::helpers::tests::testfs1(); + let ntfs = Ntfs::new(&mut testfs1).unwrap(); + let mft = ntfs + .ntfs_file(&mut testfs1, KnownNtfsFile::MFT as u64) + .unwrap(); + let mut mft_attributes = mft.attributes(&mut testfs1); + + // Check the FileName attribute of the MFT. + let attribute = mft_attributes.nth(1).unwrap().unwrap(); + assert_eq!(attribute.ty().unwrap(), NtfsAttributeType::FileName); + assert_eq!(attribute.attribute_length(), 104); + assert!(attribute.is_resident()); + assert_eq!(attribute.name_length(), 0); + assert_eq!(attribute.value_length(), 74); + + // Check the actual "file name" of the MFT. + let value = attribute.read_structured_value(&mut testfs1).unwrap(); + let file_name = match value { + NtfsStructuredValue::FileName(file_name) => file_name, + v => panic!("Unexpected NtfsStructuredValue: {:?}", v), + }; + + let creation_time = file_name.creation_time(); + assert!(*creation_time > NT_TIMESTAMP_2021_01_01); + assert_eq!(creation_time, file_name.modification_time()); + assert_eq!(creation_time, file_name.mft_record_modification_time()); + assert_eq!(creation_time, file_name.access_time()); + + let allocated_size = file_name.allocated_size(); + assert!(allocated_size > 0); + assert_eq!(allocated_size, file_name.data_size()); + + assert_eq!(file_name.name_length(), 8); + + let mut buf = [0u8; 8]; + let file_name_string = file_name.read_name(&mut testfs1, &mut buf).unwrap(); + + // Test various ways to compare the same string. + assert_eq!(file_name_string, "$MFT"); + assert_eq!(file_name_string.to_string_lossy(), String::from("$MFT")); + assert_eq!( + file_name_string, + NtfsString(&[b'$', 0, b'M', 0, b'F', 0, b'T', 0]) + ); + } +} diff --git a/src/structured_values/standard_information.rs b/src/structured_values/standard_information.rs index f566278..e7bfb51 100644 --- a/src/structured_values/standard_information.rs +++ b/src/structured_values/standard_information.rs @@ -60,11 +60,11 @@ impl NtfsStandardInformation { } let data = value_attached.read_le::<StandardInformationData>()?; - let ntfs3_data = if value_length >= STANDARD_INFORMATION_SIZE_NTFS3 as u64 { - Some(value_attached.read_le::<StandardInformationDataNtfs3>()?) - } else { - None - }; + + let mut ntfs3_data = None; + if value_length >= STANDARD_INFORMATION_SIZE_NTFS3 { + ntfs3_data = Some(value_attached.read_le::<StandardInformationDataNtfs3>()?); + } Ok(Self { data, ntfs3_data }) } @@ -117,3 +117,41 @@ impl NtfsStandardInformation { self.ntfs3_data.as_ref().map(|x| x.version) } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::ntfs::Ntfs; + use crate::ntfs_file::KnownNtfsFile; + use crate::structured_values::NtfsStructuredValue; + + #[test] + fn test_standard_information() { + let mut testfs1 = crate::helpers::tests::testfs1(); + let ntfs = Ntfs::new(&mut testfs1).unwrap(); + let mft = ntfs + .ntfs_file(&mut testfs1, KnownNtfsFile::MFT as u64) + .unwrap(); + let mut mft_attributes = mft.attributes(&mut testfs1); + + // Check the StandardInformation attribute of the MFT. + let attribute = mft_attributes.nth(0).unwrap().unwrap(); + assert_eq!( + attribute.ty().unwrap(), + NtfsAttributeType::StandardInformation, + ); + assert_eq!(attribute.attribute_length(), 96); + assert!(attribute.is_resident()); + assert_eq!(attribute.name_length(), 0); + assert_eq!(attribute.value_length(), 72); + + // Try to read the actual information. + let value = attribute.read_structured_value(&mut testfs1).unwrap(); + let _standard_info = match value { + NtfsStructuredValue::StandardInformation(standard_info) => standard_info, + v => panic!("Unexpected NtfsStructuredValue: {:?}", v), + }; + + // There are no reliable values to check here, so that's it. + } +} |