From a24ae8622cc2f829a8101a7f812fc98297053cc3 Mon Sep 17 00:00:00 2001 From: igorechek06 Date: Sat, 10 Aug 2024 22:58:27 +0900 Subject: Add more tests --- tests/zip.rs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) (limited to 'tests/zip.rs') 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.2.3