From dafe3b01d7dfe5f314dea37c312beae20e017f4e Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Sun, 1 Sep 2024 20:15:08 +0300 Subject: Add test for InvalidSignature for struct CDR --- src/zip/driver.rs | 6 +- tests/zip.rs | 188 ++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 129 insertions(+), 65 deletions(-) diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 0502c85..b44c453 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -145,12 +145,10 @@ impl ArchiveRead for Zip { ); for i in 0..cd_records as usize { - let buf = cd_reader.read_arr::<46>()?; - - if buf[..4] != CDR_SIGNATURE { + if cd_reader.read_arr()? != CDR_SIGNATURE { return Err(ZipError::InvalidSignature("Cdr")); } - let cdr: Cdr = deserialize(&buf[4..46]).unwrap(); + let cdr: Cdr = deserialize(&cd_reader.read_arr::<42>()?).unwrap(); let bit_flag = BitFlag::new(cdr.bit_flag); let name = cd_reader.read_vec(cdr.name_len as usize)?; diff --git a/tests/zip.rs b/tests/zip.rs index e00789d..f0451cb 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -30,12 +30,20 @@ fn test_zip_aes() { ); for encryption in ["aes128", "aes192", "aes256"] { - assert!(archive - .get_file_reader_by_name(&format!("{encryption}/store")) - .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); - assert!(archive - .get_file_reader_by_name_with_password("aes128/store", b"wrong_passwd") - .is_err_and(|e| e == ZipError::WrongPassword)); + assert_eq!( + archive + .get_file_reader_by_name(&format!("{encryption}/store")) + .err() + .unwrap(), + ZipError::PasswordIsNotSpecified + ); + assert_eq!( + archive + .get_file_reader_by_name_with_password("aes128/store", b"wrong_passwd") + .err() + .unwrap(), + ZipError::WrongPassword + ); for (name, check) in [ ("store", "98f64f03b3d168875ffa778f7fb4"), @@ -68,12 +76,17 @@ fn test_zip_weak() { vec!["store", "deflate", "bzip"] ); - assert!(archive - .get_file_reader_by_name("store") - .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); - assert!(archive - .get_file_reader_by_name_with_password("store", b"wrong_passwd") - .is_err_and(|e| e == ZipError::WrongPassword)); + assert_eq!( + archive.get_file_reader_by_name("store").err().unwrap(), + ZipError::PasswordIsNotSpecified + ); + assert_eq!( + archive + .get_file_reader_by_name_with_password("store", b"wrong_passwd") + .err() + .unwrap(), + ZipError::WrongPassword + ); for (name, check) in [ ("store", "1e643774f40510e37c6f3c451d9d"), @@ -147,66 +160,119 @@ fn test_zip() { f.seek(SeekFrom::Start(7)).unwrap(); assert_eq!(f.seek(SeekFrom::Current(0)).unwrap(), 7); - assert!(f - .seek(SeekFrom::End(-100)) - .is_err_and(|e| e.get_ref().unwrap().to_string() - == "Invalid seek to a negative or overflowing position")); + assert_eq!( + f.seek(SeekFrom::End(-100)) + .err() + .unwrap() + .get_ref() + .unwrap() + .to_string(), + "Invalid seek to a negative or overflowing position" + ); assert_eq!(f.seek(SeekFrom::Current(0)).unwrap(), 7); assert_eq!(f.seek(SeekFrom::Start(100)).unwrap(), 14); - for index in 0..archive.len() { + for index in 1..archive.len() { let mut f = archive.get_file_reader_by_index(index).unwrap(); let mut data = String::new(); f.read_to_string(&mut data).unwrap(); assert_eq!(data, "test file data"); - assert!(!f.is_seekable() || f.info().name == "store") + assert!(!f.is_seekable()) } } #[test] fn test_bad_zip() { - assert!(Archive::>::read(Cursor::new(&[])) - .is_err_and(|e| e == ZipError::StructNotFound("Eocdr"))); - - assert!(Archive::>::read(Cursor::new(&[ - // No Eocdr64 - 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator - // - 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr - ])) - .is_err_and(|e| e == ZipError::InvalidSignature("Eocdr64"))); - - assert!(Archive::>::read(Cursor::new(&[ - 0x50, 0x4b, 0x06, 0x06, // Eocdr64 - // - 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator - // - 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr - ])) - .is_err_and(|e| e == ZipError::Overlapping("Eocdr64", "Eocdr64Locator"))); - - assert!(Archive::>::read(Cursor::new(&[ - // No records - 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr - ])) - .is_err_and(|e| e == ZipError::Overlapping("Cdr", "Eocdr"))); - - assert!(Archive::>::read(Cursor::new(&[ - 0x50, 0x4b, 0x06, 0x06, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64 - // - 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator - // - 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr - ])) - .is_err_and(|e| e == ZipError::Overlapping("Cdr", "Eocdr64"))); + assert_eq!( + Archive::>::read(Cursor::new(&[])).err().unwrap(), + ZipError::StructNotFound("Eocdr") + ); + + assert_eq!( + Archive::>::read(Cursor::new(&[ + 0, 0, 0, 0, // Eocdr64 + // + 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator + // + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .err() + .unwrap(), + ZipError::InvalidSignature("Eocdr64") + ); + + assert_eq!( + Archive::>::read(Cursor::new(&[ + 0x50, 0x4b, 0x06, 0x06, // Eocdr64 + // + 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator + // + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .err() + .unwrap(), + ZipError::Overlapping("Eocdr64", "Eocdr64Locator") + ); + + assert_eq!( + Archive::>::read(Cursor::new(&[ + 0x50, 0x4b, 0x06, 0x06, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64 + // + 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator + // + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .err() + .unwrap(), + ZipError::Overlapping("Eocdr64", "Eocdr64Locator") + ); + + assert_eq!( + Archive::>::read(Cursor::new(&[ + // No records + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .err() + .unwrap(), + ZipError::Overlapping("Cdr", "Eocdr") + ); + + assert_eq!( + Archive::>::read(Cursor::new(&[ + 0x50, 0x4b, 0x06, 0x06, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64 + // + 0x50, 0x4b, 0x06, 0x07, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr64Locator + // + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .err() + .unwrap(), + ZipError::Overlapping("Cdr", "Eocdr64") + ); + + assert_eq!( + Archive::>::read(Cursor::new(&[ + 0, 0, 0, 0, // Cdr + // + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 1, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .err() + .unwrap(), + ZipError::InvalidSignature("Cdr") + ); } -- cgit v1.2.3