]>
Commit | Line | Data |
---|---|---|
f7d4e4b5 | 1 | use anyhow::{bail, Error}; |
9025312a | 2 | use std::sync::Arc; |
e7a2b7da | 3 | use std::io::Cursor; |
9025312a | 4 | use std::io::{Read, Write, Seek, SeekFrom }; |
50c0cdfd | 5 | use lazy_static::lazy_static; |
e7a2b7da DM |
6 | |
7 | use proxmox_backup::backup::*; | |
8 | ||
50c0cdfd DM |
9 | lazy_static! { |
10 | static ref TEST_DATA: Vec<u8> = { | |
11 | let mut data = Vec::new(); | |
e7a2b7da | 12 | |
50c0cdfd DM |
13 | for i in 0..100_000 { |
14 | data.push((i%255) as u8); | |
e7a2b7da | 15 | } |
50c0cdfd DM |
16 | |
17 | data | |
e7a2b7da DM |
18 | }; |
19 | ||
9025312a | 20 | static ref CRYPT_CONFIG: Arc<CryptConfig> = { |
50c0cdfd | 21 | let key = [1u8; 32]; |
9025312a | 22 | Arc::new(CryptConfig::new(key).unwrap()) |
50c0cdfd | 23 | }; |
8819d1f2 FG |
24 | |
25 | static ref TEST_DIGEST_PLAIN: [u8; 32] = [83, 154, 96, 195, 167, 204, 38, 142, 204, 224, 130, 201, 24, 71, 2, 188, 130, 155, 177, 6, 162, 100, 61, 238, 38, 219, 63, 240, 191, 132, 87, 238]; | |
26 | ||
27 | static ref TEST_DIGEST_ENC: [u8; 32] = [50, 162, 191, 93, 255, 132, 9, 14, 127, 23, 92, 39, 246, 102, 245, 204, 130, 104, 4, 106, 182, 239, 218, 14, 80, 17, 150, 188, 239, 253, 198, 117]; | |
50c0cdfd | 28 | } |
e7a2b7da | 29 | |
8819d1f2 | 30 | fn verify_test_blob(mut cursor: Cursor<Vec<u8>>, digest: &[u8; 32]) -> Result<(), Error> { |
e7a2b7da | 31 | |
50c0cdfd DM |
32 | // run read tests with different buffer sizes |
33 | for size in [1, 3, 64*1024].iter() { | |
e7a2b7da | 34 | |
50c0cdfd | 35 | println!("Starting DataBlobReader test (size = {})", size); |
e7a2b7da | 36 | |
50c0cdfd | 37 | cursor.seek(SeekFrom::Start(0))?; |
9025312a | 38 | let mut reader = DataBlobReader::new(&mut cursor, Some(CRYPT_CONFIG.clone()))?; |
50c0cdfd DM |
39 | let mut buffer = Vec::<u8>::new(); |
40 | // read the whole file | |
41 | //reader.read_to_end(&mut buffer)?; | |
42 | let mut buf = vec![0u8; *size]; | |
43 | loop { | |
44 | let count = reader.read(&mut buf)?; | |
45 | if count == 0 { break; } | |
46 | buffer.extend(&buf[..count]); | |
47 | } | |
e7a2b7da | 48 | |
50c0cdfd DM |
49 | reader.finish()?; |
50 | if buffer != *TEST_DATA { | |
51 | bail!("blob data is wrong (read buffer size {})", size); | |
52 | } | |
e7a2b7da DM |
53 | } |
54 | ||
50c0cdfd | 55 | let raw_data = cursor.into_inner(); |
e7a2b7da | 56 | |
39f18b30 | 57 | let blob = DataBlob::load_from_reader(&mut &raw_data[..])?; |
e7a2b7da | 58 | |
8819d1f2 | 59 | let data = blob.decode(Some(&CRYPT_CONFIG), Some(digest))?; |
50c0cdfd DM |
60 | if data != *TEST_DATA { |
61 | bail!("blob data is wrong (decode)"); | |
e7a2b7da | 62 | } |
50c0cdfd DM |
63 | Ok(()) |
64 | } | |
e7a2b7da | 65 | |
50c0cdfd DM |
66 | #[test] |
67 | fn test_uncompressed_blob_writer() -> Result<(), Error> { | |
68 | let tmp = Cursor::new(Vec::<u8>::new()); | |
69 | let mut blob_writer = DataBlobWriter::new_uncompressed(tmp)?; | |
70 | blob_writer.write_all(&TEST_DATA)?; | |
e7a2b7da | 71 | |
8819d1f2 | 72 | verify_test_blob(blob_writer.finish()?, &*TEST_DIGEST_PLAIN) |
50c0cdfd | 73 | } |
e7a2b7da | 74 | |
50c0cdfd DM |
75 | #[test] |
76 | fn test_compressed_blob_writer() -> Result<(), Error> { | |
77 | let tmp = Cursor::new(Vec::<u8>::new()); | |
78 | let mut blob_writer = DataBlobWriter::new_compressed(tmp)?; | |
79 | blob_writer.write_all(&TEST_DATA)?; | |
e7a2b7da | 80 | |
8819d1f2 | 81 | verify_test_blob(blob_writer.finish()?, &*TEST_DIGEST_PLAIN) |
50c0cdfd | 82 | } |
e7a2b7da | 83 | |
50c0cdfd DM |
84 | #[test] |
85 | fn test_encrypted_blob_writer() -> Result<(), Error> { | |
86 | let tmp = Cursor::new(Vec::<u8>::new()); | |
9025312a | 87 | let mut blob_writer = DataBlobWriter::new_encrypted(tmp, CRYPT_CONFIG.clone())?; |
50c0cdfd | 88 | blob_writer.write_all(&TEST_DATA)?; |
e7a2b7da | 89 | |
8819d1f2 | 90 | verify_test_blob(blob_writer.finish()?, &*TEST_DIGEST_ENC) |
50c0cdfd | 91 | } |
e7a2b7da | 92 | |
50c0cdfd DM |
93 | #[test] |
94 | fn test_encrypted_compressed_blob_writer() -> Result<(), Error> { | |
95 | let tmp = Cursor::new(Vec::<u8>::new()); | |
9025312a | 96 | let mut blob_writer = DataBlobWriter::new_encrypted_compressed(tmp, CRYPT_CONFIG.clone())?; |
50c0cdfd DM |
97 | blob_writer.write_all(&TEST_DATA)?; |
98 | ||
8819d1f2 | 99 | verify_test_blob(blob_writer.finish()?, &*TEST_DIGEST_ENC) |
e7a2b7da | 100 | } |