From 4c411b76cad9cc735687dc739d2e2db5d00e5eac Mon Sep 17 00:00:00 2001 From: Igor Tolmachev Date: Sun, 21 Jul 2024 16:59:14 +0900 Subject: Add AES encryption --- tests/files/archive_aes.zip | Bin 0 -> 3898 bytes tests/files/archive_passwd.zip | Bin 1059 -> 0 bytes tests/files/archive_weak.zip | Bin 0 -> 1059 bytes tests/zip.rs | 67 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 tests/files/archive_aes.zip delete mode 100644 tests/files/archive_passwd.zip create mode 100644 tests/files/archive_weak.zip (limited to 'tests') diff --git a/tests/files/archive_aes.zip b/tests/files/archive_aes.zip new file mode 100644 index 0000000..a64415b Binary files /dev/null and b/tests/files/archive_aes.zip differ diff --git a/tests/files/archive_passwd.zip b/tests/files/archive_passwd.zip deleted file mode 100644 index 291bd12..0000000 Binary files a/tests/files/archive_passwd.zip and /dev/null differ diff --git a/tests/files/archive_weak.zip b/tests/files/archive_weak.zip new file mode 100644 index 0000000..291bd12 Binary files /dev/null and b/tests/files/archive_weak.zip differ diff --git a/tests/zip.rs b/tests/zip.rs index 2c8fc56..9283df3 100644 --- a/tests/zip.rs +++ b/tests/zip.rs @@ -3,8 +3,60 @@ use archivator::{Archive, Zip}; use std::io::{Read, Seek, SeekFrom}; #[test] -fn test_zip_passwd() { - let mut archive = Archive::::read_from_file("tests/files/archive_passwd.zip").unwrap(); +fn test_zip_aes() { + let mut archive = Archive::::read_from_file("tests/files/archive_aes.zip").unwrap(); + + assert_eq!(archive.comment(), "archive comment"); + assert_eq!( + archive + .files() + .iter() + .map(|f| &f.name) + .collect::>(), + vec![ + "aes128/store", + "aes192/store", + "aes256/store", + "aes128/deflate", + "aes192/deflate", + "aes256/deflate", + "aes128/bzip", + "aes192/bzip", + "aes256/bzip", + "aes128/lzma", + "aes192/lzma", + "aes256/lzma", + ] + ); + + 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)); + + for (name, check) in [ + ("store", "98f64f03b3d168875ffa778f7fb4"), + ("deflate", "0230e7cadb76460e80cd9de611eb"), + ("bzip", "061c17646f025837e33e00425cca"), + ("lzma", "43ef5e8ed799eb7a0f25501824ff"), + ] { + let mut f = archive + .get_file_reader_by_name_with_password(&format!("{encryption}/{name}"), b"passwd") + .unwrap(); + let mut data = String::new(); + f.read_to_string(&mut data).unwrap(); + assert_eq!(&data[..24], "test encrypted file data"); + assert_eq!(&data[172..], check); + } + } +} + +#[test] +fn test_zip_weak() { + let mut archive = Archive::::read_from_file("tests/files/archive_weak.zip").unwrap(); assert_eq!(archive.comment(), "archive comment"); assert_eq!( @@ -21,9 +73,9 @@ fn test_zip_passwd() { .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::IncorrectPassword)); + .is_err_and(|e| e == ZipError::WrongPassword)); - for (name, check_data) in [ + for (name, check) in [ ("store", "1e643774f40510e37c6f3c451d9d"), ("deflate", "a70aff4b6b2754ad47852503236a"), ("bzip", "f7085f4f8ecc512a8c2c3cbe8227"), @@ -34,7 +86,7 @@ fn test_zip_passwd() { let mut data = String::new(); f.read_to_string(&mut data).unwrap(); assert_eq!(&data[..24], "test encrypted file data"); - assert_eq!(&data[172..], check_data); + assert_eq!(&data[172..], check); } } @@ -52,6 +104,11 @@ fn test_zip() { vec!["store", "deflate", "bzip", "lzma", "zstd", "xz"] ); + assert_eq!( + archive.get_file_info_by_name("none").unwrap_err(), + ZipError::FileNotFound + ); + let mut f = archive.get_file_reader_by_name("store").unwrap(); let mut data = String::new(); -- cgit v1.2.3