1 #![cfg(feature = "aes-crypto")]
2
3 use std::io::{self, Read};
4 use zip::ZipArchive;
5
6 const SECRET_CONTENT: &str = "Lorem ipsum dolor sit amet";
7
8 const PASSWORD: &[u8] = b"helloworld";
9
10 #[test]
aes256_encrypted_uncompressed_file()11 fn aes256_encrypted_uncompressed_file() {
12 let mut v = Vec::new();
13 v.extend_from_slice(include_bytes!("data/aes_archive.zip"));
14 let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
15
16 let mut file = archive
17 .by_name_decrypt("secret_data_256_uncompressed", PASSWORD)
18 .expect("couldn't find file in archive")
19 .expect("invalid password");
20 assert_eq!("secret_data_256_uncompressed", file.name());
21
22 let mut content = String::new();
23 file.read_to_string(&mut content)
24 .expect("couldn't read encrypted file");
25 assert_eq!(SECRET_CONTENT, content);
26 }
27
28 #[test]
aes256_encrypted_file()29 fn aes256_encrypted_file() {
30 let mut v = Vec::new();
31 v.extend_from_slice(include_bytes!("data/aes_archive.zip"));
32 let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
33
34 let mut file = archive
35 .by_name_decrypt("secret_data_256", PASSWORD)
36 .expect("couldn't find file in archive")
37 .expect("invalid password");
38 assert_eq!("secret_data_256", file.name());
39
40 let mut content = String::new();
41 file.read_to_string(&mut content)
42 .expect("couldn't read encrypted and compressed file");
43 assert_eq!(SECRET_CONTENT, content);
44 }
45
46 #[test]
aes192_encrypted_file()47 fn aes192_encrypted_file() {
48 let mut v = Vec::new();
49 v.extend_from_slice(include_bytes!("data/aes_archive.zip"));
50 let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
51
52 let mut file = archive
53 .by_name_decrypt("secret_data_192", PASSWORD)
54 .expect("couldn't find file in archive")
55 .expect("invalid password");
56 assert_eq!("secret_data_192", file.name());
57
58 let mut content = String::new();
59 file.read_to_string(&mut content)
60 .expect("couldn't read encrypted file");
61 assert_eq!(SECRET_CONTENT, content);
62 }
63
64 #[test]
aes128_encrypted_file()65 fn aes128_encrypted_file() {
66 let mut v = Vec::new();
67 v.extend_from_slice(include_bytes!("data/aes_archive.zip"));
68 let mut archive = ZipArchive::new(io::Cursor::new(v)).expect("couldn't open test zip file");
69
70 let mut file = archive
71 .by_name_decrypt("secret_data_128", PASSWORD)
72 .expect("couldn't find file in archive")
73 .expect("invalid password");
74 assert_eq!("secret_data_128", file.name());
75
76 let mut content = String::new();
77 file.read_to_string(&mut content)
78 .expect("couldn't read encrypted file");
79 assert_eq!(SECRET_CONTENT, content);
80 }
81