diff options
| author | Igor Tolmachev <me@igorek.dev> | 2024-07-21 16:59:14 +0900 |
|---|---|---|
| committer | Igor Tolmachev <me@igorek.dev> | 2024-07-21 16:59:14 +0900 |
| commit | 4c411b76cad9cc735687dc739d2e2db5d00e5eac (patch) | |
| tree | 818168ca5726ad3f9d24089dba31a24ff6b1b1f4 /tests/zip.rs | |
| parent | 7bcdc3b4ca460aec2b98fb2dca6165788c562b05 (diff) | |
| download | archivator-4c411b76cad9cc735687dc739d2e2db5d00e5eac.tar.gz archivator-4c411b76cad9cc735687dc739d2e2db5d00e5eac.zip | |
Add AES encryption
Diffstat (limited to 'tests/zip.rs')
| -rw-r--r-- | tests/zip.rs | 67 |
1 files changed, 62 insertions, 5 deletions
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}; | |||
| 3 | use std::io::{Read, Seek, SeekFrom}; | 3 | use std::io::{Read, Seek, SeekFrom}; |
| 4 | 4 | ||
| 5 | #[test] | 5 | #[test] |
| 6 | fn test_zip_passwd() { | 6 | fn test_zip_aes() { |
| 7 | let mut archive = Archive::<Zip>::read_from_file("tests/files/archive_passwd.zip").unwrap(); | 7 | let mut archive = Archive::<Zip>::read_from_file("tests/files/archive_aes.zip").unwrap(); |
| 8 | |||
| 9 | assert_eq!(archive.comment(), "archive comment"); | ||
| 10 | assert_eq!( | ||
| 11 | archive | ||
| 12 | .files() | ||
| 13 | .iter() | ||
| 14 | .map(|f| &f.name) | ||
| 15 | .collect::<Vec<&String>>(), | ||
| 16 | vec![ | ||
| 17 | "aes128/store", | ||
| 18 | "aes192/store", | ||
| 19 | "aes256/store", | ||
| 20 | "aes128/deflate", | ||
| 21 | "aes192/deflate", | ||
| 22 | "aes256/deflate", | ||
| 23 | "aes128/bzip", | ||
| 24 | "aes192/bzip", | ||
| 25 | "aes256/bzip", | ||
| 26 | "aes128/lzma", | ||
| 27 | "aes192/lzma", | ||
| 28 | "aes256/lzma", | ||
| 29 | ] | ||
| 30 | ); | ||
| 31 | |||
| 32 | for encryption in ["aes128", "aes192", "aes256"] { | ||
| 33 | assert!(archive | ||
| 34 | .get_file_reader_by_name(&format!("{encryption}/store")) | ||
| 35 | .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); | ||
| 36 | assert!(archive | ||
| 37 | .get_file_reader_by_name_with_password("aes128/store", b"wrong_passwd") | ||
| 38 | .is_err_and(|e| e == ZipError::WrongPassword)); | ||
| 39 | |||
| 40 | for (name, check) in [ | ||
| 41 | ("store", "98f64f03b3d168875ffa778f7fb4"), | ||
| 42 | ("deflate", "0230e7cadb76460e80cd9de611eb"), | ||
| 43 | ("bzip", "061c17646f025837e33e00425cca"), | ||
| 44 | ("lzma", "43ef5e8ed799eb7a0f25501824ff"), | ||
| 45 | ] { | ||
| 46 | let mut f = archive | ||
| 47 | .get_file_reader_by_name_with_password(&format!("{encryption}/{name}"), b"passwd") | ||
| 48 | .unwrap(); | ||
| 49 | let mut data = String::new(); | ||
| 50 | f.read_to_string(&mut data).unwrap(); | ||
| 51 | assert_eq!(&data[..24], "test encrypted file data"); | ||
| 52 | assert_eq!(&data[172..], check); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | #[test] | ||
| 58 | fn test_zip_weak() { | ||
| 59 | let mut archive = Archive::<Zip>::read_from_file("tests/files/archive_weak.zip").unwrap(); | ||
| 8 | 60 | ||
| 9 | assert_eq!(archive.comment(), "archive comment"); | 61 | assert_eq!(archive.comment(), "archive comment"); |
| 10 | assert_eq!( | 62 | assert_eq!( |
| @@ -21,9 +73,9 @@ fn test_zip_passwd() { | |||
| 21 | .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); | 73 | .is_err_and(|e| e == ZipError::PasswordIsNotSpecified)); |
| 22 | assert!(archive | 74 | assert!(archive |
| 23 | .get_file_reader_by_name_with_password("store", b"wrong_passwd") | 75 | .get_file_reader_by_name_with_password("store", b"wrong_passwd") |
| 24 | .is_err_and(|e| e == ZipError::IncorrectPassword)); | 76 | .is_err_and(|e| e == ZipError::WrongPassword)); |
| 25 | 77 | ||
| 26 | for (name, check_data) in [ | 78 | for (name, check) in [ |
| 27 | ("store", "1e643774f40510e37c6f3c451d9d"), | 79 | ("store", "1e643774f40510e37c6f3c451d9d"), |
| 28 | ("deflate", "a70aff4b6b2754ad47852503236a"), | 80 | ("deflate", "a70aff4b6b2754ad47852503236a"), |
| 29 | ("bzip", "f7085f4f8ecc512a8c2c3cbe8227"), | 81 | ("bzip", "f7085f4f8ecc512a8c2c3cbe8227"), |
| @@ -34,7 +86,7 @@ fn test_zip_passwd() { | |||
| 34 | let mut data = String::new(); | 86 | let mut data = String::new(); |
| 35 | f.read_to_string(&mut data).unwrap(); | 87 | f.read_to_string(&mut data).unwrap(); |
| 36 | assert_eq!(&data[..24], "test encrypted file data"); | 88 | assert_eq!(&data[..24], "test encrypted file data"); |
| 37 | assert_eq!(&data[172..], check_data); | 89 | assert_eq!(&data[172..], check); |
| 38 | } | 90 | } |
| 39 | } | 91 | } |
| 40 | 92 | ||
| @@ -52,6 +104,11 @@ fn test_zip() { | |||
| 52 | vec!["store", "deflate", "bzip", "lzma", "zstd", "xz"] | 104 | vec!["store", "deflate", "bzip", "lzma", "zstd", "xz"] |
| 53 | ); | 105 | ); |
| 54 | 106 | ||
| 107 | assert_eq!( | ||
| 108 | archive.get_file_info_by_name("none").unwrap_err(), | ||
| 109 | ZipError::FileNotFound | ||
| 110 | ); | ||
| 111 | |||
| 55 | let mut f = archive.get_file_reader_by_name("store").unwrap(); | 112 | let mut f = archive.get_file_reader_by_name("store").unwrap(); |
| 56 | 113 | ||
| 57 | let mut data = String::new(); | 114 | let mut data = String::new(); |
