From a24ae8622cc2f829a8101a7f812fc98297053cc3 Mon Sep 17 00:00:00 2001 From: igorechek06 Date: Sat, 10 Aug 2024 22:58:27 +0900 Subject: Add more tests --- src/zip/driver.rs | 55 +++++++++++++++++++++++++++----------------------- tests/files/blank | 0 tests/files/empty.zip | Bin 22 -> 0 bytes tests/zip.rs | 39 ++++++++++++++++++++++++++--------- 4 files changed, 60 insertions(+), 34 deletions(-) delete mode 100644 tests/files/blank delete mode 100644 tests/files/empty.zip diff --git a/src/zip/driver.rs b/src/zip/driver.rs index 81629dd..7758479 100644 --- a/src/zip/driver.rs +++ b/src/zip/driver.rs @@ -98,33 +98,38 @@ impl ArchiveRead for Zip { // Try to find eocdr64locator io.seek(SeekFrom::Start(pos.saturating_sub(20)))?; let buf = io.read_arr::<20>()?; - let (cd_pointer, cd_size, cd_records) = - // If locator found then read eocdr64 - if buf[0..4] == EOCDR64_LOCATOR_SIGNATURE { - let eocdr64locator: Eocdr64Locator = deserialize(&buf[4..]).unwrap(); - - io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?; - let buf = io.read_arr::<56>()?; - if buf[0..4] != EOCDR64_SIGNATURE { - return Err(ZipError::InvalidSignature("Eocdr64")); - } - let eocdr64: Eocdr64 = deserialize(&buf[4..]).unwrap(); - if eocdr64.cd_pointer + eocdr64.cd_size > eocdr64locator.eocdr64_pointer { - return Err(ZipError::Overlapping("Central directory records", "Zip64 end of central directory record")); - } + let (cd_pointer, cd_size, cd_records) = if buf[..4] == EOCDR64_LOCATOR_SIGNATURE { + // Locator found + let eocdr64locator: Eocdr64Locator = deserialize(&buf[4..]).unwrap(); - (eocdr64.cd_pointer, eocdr64.cd_size, eocdr64.cd_records) - } else { - if (eocdr.cd_pointer + eocdr.cd_size) as u64 > pos { - return Err(ZipError::Overlapping("Central directory records", "End of central directory record")); - } + io.seek(SeekFrom::Start(eocdr64locator.eocdr64_pointer))?; + if io.read_arr()? != EOCDR64_SIGNATURE { + return Err(ZipError::InvalidSignature("Eocdr64")); + } - ( - eocdr.cd_pointer as u64, - eocdr.cd_size as u64, - eocdr.cd_records as u64, - ) - }; + let eocdr64: Eocdr64 = deserialize(&io.read_arr::<52>()?).unwrap(); + if eocdr64.cd_pointer + eocdr64.cd_size > eocdr64locator.eocdr64_pointer { + return Err(ZipError::Overlapping( + "Central directory records", + "Zip64 end of central directory record", + )); + } + + (eocdr64.cd_pointer, eocdr64.cd_size, eocdr64.cd_records) + } else { + if (eocdr.cd_pointer + eocdr.cd_size) as u64 > pos { + return Err(ZipError::Overlapping( + "Central directory records", + "End of central directory record", + )); + } + + ( + eocdr.cd_pointer as u64, + eocdr.cd_size as u64, + eocdr.cd_records as u64, + ) + }; // Read cd records let mut indexes = Map::with_capacity(cd_records as usize); diff --git a/tests/files/blank b/tests/files/blank deleted file mode 100644 index e69de29..0000000 diff --git a/tests/files/empty.zip b/tests/files/empty.zip deleted file mode 100644 index 15cb0ec..0000000 Binary files a/tests/files/empty.zip and /dev/null differ diff --git a/tests/zip.rs b/tests/zip.rs index e2b5c20..1422bb3 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -1,6 +1,6 @@ use archivator::zip::ZipError; use archivator::{Archive, Zip}; -use std::io::{Read, Seek, SeekFrom}; +use std::io::{Cursor, Read, Seek, SeekFrom}; #[test] fn test_zip_aes() { @@ -90,14 +90,11 @@ fn test_zip_weak() { } } +const EMPTY: Cursor<&[u8]> = Cursor::new(b"PK\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); + #[test] fn test_zip() { - assert_eq!( - Archive::::read_from_file("tests/files/empty.zip") - .unwrap() - .len(), - 0 - ); + assert_eq!(Archive::>::read(EMPTY).unwrap().len(), 0); let mut archive = Archive::::read_from_file("tests/files/archive.zip").unwrap(); @@ -161,8 +158,32 @@ fn test_zip() { } } +const NOT_FOUND: Cursor<&[u8]> = Cursor::new(b""); +const INVALID: Cursor<&[u8]> = Cursor::new( + b"PK\x06\x07\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK\x05\x06\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0", +); +const OVERLAP: Cursor<&[u8]> = Cursor::new(b"PK\x05\x06\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0"); +const OVERLAP64: Cursor<&[u8]> = Cursor::new(b"PK\x06\x06\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\x01\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK\x06\x07\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0PK\x05\x06\0\0\0\0\0\0\0\0\x01\0\0\0\0\0\0\0\0\0"); + #[test] fn test_bad_zip() { - assert!(Archive::::read_from_file("tests/files/blank") - .is_err_and(|e| e == ZipError::StructNotFound("Eocdr"))); + assert!( + Archive::>::read(NOT_FOUND).is_err_and(|e| e == ZipError::StructNotFound("Eocdr")) + ); + + assert!( + Archive::>::read(INVALID).is_err_and(|e| e == ZipError::InvalidSignature("Eocdr64")) + ); + + assert!(Archive::>::read(OVERLAP).is_err_and(|e| e + == ZipError::Overlapping( + "Central directory records", + "End of central directory record" + ))); + + assert!(Archive::>::read(OVERLAP64).is_err_and(|e| e + == ZipError::Overlapping( + "Central directory records", + "Zip64 end of central directory record" + ))); } -- cgit v1.3