From 27da50f9d157927ec56dae8316d0edc34eaa244d Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Tue, 27 Aug 2024 18:03:30 +0900 Subject: Rewrite Eocdr64Locator search algorithm --- tests/zip.rs | 81 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 29 deletions(-) (limited to 'tests/zip.rs') diff --git a/tests/zip.rs b/tests/zip.rs index 1422bb3..e00789d 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -90,11 +90,17 @@ 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(EMPTY).unwrap().len(), 0); + assert_eq!( + Archive::>::read(Cursor::new(&[ + 0x50, 0x4b, 0x05, 0x06, 0, 0, 0, 0, 0, // + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Eocdr + ])) + .unwrap() + .len(), + 0 + ); let mut archive = Archive::::read_from_file("tests/files/archive.zip").unwrap(); @@ -158,32 +164,49 @@ 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(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" - ))); + 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"))); } -- cgit v1.2.3