From c17df86dbf6658bdeadbe476e2437dcb97d6c9dd Mon Sep 17 00:00:00 2001 From: Lireer Date: Tue, 25 Jan 2022 21:51:57 +0100 Subject: [PATCH] test decryption of aes encrypted files --- tests/aes_encryption.rs | 78 +++++++++++++++++++++++++++++++++++++ tests/data/aes_archive.zip | Bin 0 -> 908 bytes 2 files changed, 78 insertions(+) create mode 100644 tests/aes_encryption.rs create mode 100644 tests/data/aes_archive.zip diff --git a/tests/aes_encryption.rs b/tests/aes_encryption.rs new file mode 100644 index 000000000..84099d76e --- /dev/null +++ b/tests/aes_encryption.rs @@ -0,0 +1,78 @@ +use std::io::{self, Read}; +use zip::ZipArchive; + +const SECRET_CONTENT: &str = "Lorem ipsum dolor sit amet"; + +const PASSWORD: &[u8] = b"helloworld"; + +#[test] +fn aes256_encrypted_uncompressed_file() { + let mut v = Vec::new(); + v.extend_from_slice(include_bytes!("../tests/data/aes_archive.zip")); + let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file"); + + let mut file = archive + .by_name_decrypt("secret_data_256_uncompressed", PASSWORD) + .expect("couldn't find file in archive") + .expect("invalid password"); + assert_eq!("secret_data_256_uncompressed", file.name()); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("couldn't read encrypted file"); + assert_eq!(SECRET_CONTENT, content); +} + +#[test] +fn aes256_encrypted_file() { + let mut v = Vec::new(); + v.extend_from_slice(include_bytes!("../tests/data/aes_archive.zip")); + let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file"); + + let mut file = archive + .by_name_decrypt("secret_data_256", PASSWORD) + .expect("couldn't find file in archive") + .expect("invalid password"); + assert_eq!("secret_data_256", file.name()); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("couldn't read encrypted and compressed file"); + assert_eq!(SECRET_CONTENT, content); +} + +#[test] +fn aes192_encrypted_file() { + let mut v = Vec::new(); + v.extend_from_slice(include_bytes!("../tests/data/aes_archive.zip")); + let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file"); + + let mut file = archive + .by_name_decrypt("secret_data_192", PASSWORD) + .expect("couldn't find file in archive") + .expect("invalid password"); + assert_eq!("secret_data_192", file.name()); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("couldn't read encrypted file"); + assert_eq!(SECRET_CONTENT, content); +} + +#[test] +fn aes128_encrypted_file() { + let mut v = Vec::new(); + v.extend_from_slice(include_bytes!("../tests/data/aes_archive.zip")); + let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file"); + + let mut file = archive + .by_name_decrypt("secret_data_128", PASSWORD) + .expect("couldn't find file in archive") + .expect("invalid password"); + assert_eq!("secret_data_128", file.name()); + + let mut content = String::new(); + file.read_to_string(&mut content) + .expect("couldn't read encrypted file"); + assert_eq!(SECRET_CONTENT, content); +} diff --git a/tests/data/aes_archive.zip b/tests/data/aes_archive.zip new file mode 100644 index 0000000000000000000000000000000000000000..4cf1fd21ece84b21b9c332bdf033e71b0a4bbd43 GIT binary patch literal 908 zcmWIWW@a&FW@JcaDA-^b!T<{i8;pg;}V>i{aVb+wGGS5wX^`86{5e>iRq_1H8m%cmz zZc8!L79(7?SQ^1>VPaqi*u4GAjekt@SP0`(_f@r zY%-JFRj&LwA!AlX`Sw2{2%E~FHkskD$;i|UW)m|5L$#Rk*G$u_nd{EyX5Uh)+#n)% zch?)norw%gljPFOId4ky{HSn$bEnj0!KHQ5%>ng~GN0R?+k~*K63sRlw9o_E7GIi| zoS$1zlv-Sznu28Y>RG~xNefOm`{?f~;f~L7xo|tDRX>#@^Y2R0Z;MO0ZTMbrHh$t$ z*vS~xqHx`#>CRq{ztfM~2Y53w*)!vcI(?w0K|rBlNh63xi%2d81&}BMBZCA(!{uX) z`a(At8w78E)dSH`KFo!{$VRpcGXjzAN&^~-Vwa^6vRw=xKe2-Cvg7#A+lW;(Ch*RF|u6^58rZt?NZwJ0_-m+A7&RY(vj`Lj6h_&?0}(w>@W1_M7EgW iza%f%;wLM=fISK2qgc$!1`2-`AlwTydM6MwFaQ919PY^g literal 0 HcmV?d00001