]>
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 DM |
23 | }; |
24 | } | |
e7a2b7da | 25 | |
50c0cdfd | 26 | fn verify_test_blob(mut cursor: Cursor<Vec<u8>>) -> Result<(), Error> { |
e7a2b7da | 27 | |
50c0cdfd DM |
28 | // run read tests with different buffer sizes |
29 | for size in [1, 3, 64*1024].iter() { | |
e7a2b7da | 30 | |
50c0cdfd | 31 | println!("Starting DataBlobReader test (size = {})", size); |
e7a2b7da | 32 | |
50c0cdfd | 33 | cursor.seek(SeekFrom::Start(0))?; |
9025312a | 34 | let mut reader = DataBlobReader::new(&mut cursor, Some(CRYPT_CONFIG.clone()))?; |
50c0cdfd DM |
35 | let mut buffer = Vec::<u8>::new(); |
36 | // read the whole file | |
37 | //reader.read_to_end(&mut buffer)?; | |
38 | let mut buf = vec![0u8; *size]; | |
39 | loop { | |
40 | let count = reader.read(&mut buf)?; | |
41 | if count == 0 { break; } | |
42 | buffer.extend(&buf[..count]); | |
43 | } | |
e7a2b7da | 44 | |
50c0cdfd DM |
45 | reader.finish()?; |
46 | if buffer != *TEST_DATA { | |
47 | bail!("blob data is wrong (read buffer size {})", size); | |
48 | } | |
e7a2b7da DM |
49 | } |
50 | ||
50c0cdfd | 51 | let raw_data = cursor.into_inner(); |
e7a2b7da | 52 | |
50c0cdfd DM |
53 | let blob = DataBlob::from_raw(raw_data)?; |
54 | blob.verify_crc()?; | |
e7a2b7da | 55 | |
7123ff7d | 56 | let data = blob.decode(Some(&CRYPT_CONFIG))?; |
50c0cdfd DM |
57 | if data != *TEST_DATA { |
58 | bail!("blob data is wrong (decode)"); | |
e7a2b7da | 59 | } |
50c0cdfd DM |
60 | Ok(()) |
61 | } | |
e7a2b7da | 62 | |
50c0cdfd DM |
63 | #[test] |
64 | fn test_uncompressed_blob_writer() -> Result<(), Error> { | |
65 | let tmp = Cursor::new(Vec::<u8>::new()); | |
66 | let mut blob_writer = DataBlobWriter::new_uncompressed(tmp)?; | |
67 | blob_writer.write_all(&TEST_DATA)?; | |
e7a2b7da | 68 | |
50c0cdfd DM |
69 | verify_test_blob(blob_writer.finish()?) |
70 | } | |
e7a2b7da | 71 | |
50c0cdfd DM |
72 | #[test] |
73 | fn test_compressed_blob_writer() -> Result<(), Error> { | |
74 | let tmp = Cursor::new(Vec::<u8>::new()); | |
75 | let mut blob_writer = DataBlobWriter::new_compressed(tmp)?; | |
76 | blob_writer.write_all(&TEST_DATA)?; | |
e7a2b7da | 77 | |
50c0cdfd DM |
78 | verify_test_blob(blob_writer.finish()?) |
79 | } | |
e7a2b7da | 80 | |
50c0cdfd DM |
81 | #[test] |
82 | fn test_signed_blob_writer() -> Result<(), Error> { | |
83 | let tmp = Cursor::new(Vec::<u8>::new()); | |
9025312a | 84 | let mut blob_writer = DataBlobWriter::new_signed(tmp, CRYPT_CONFIG.clone())?; |
50c0cdfd | 85 | blob_writer.write_all(&TEST_DATA)?; |
e7a2b7da | 86 | |
50c0cdfd DM |
87 | verify_test_blob(blob_writer.finish()?) |
88 | } | |
89 | ||
90 | #[test] | |
91 | fn test_signed_compressed_blob_writer() -> Result<(), Error> { | |
92 | let tmp = Cursor::new(Vec::<u8>::new()); | |
9025312a | 93 | let mut blob_writer = DataBlobWriter::new_signed_compressed(tmp, CRYPT_CONFIG.clone())?; |
50c0cdfd | 94 | blob_writer.write_all(&TEST_DATA)?; |
e7a2b7da | 95 | |
50c0cdfd DM |
96 | verify_test_blob(blob_writer.finish()?) |
97 | } | |
e7a2b7da | 98 | |
50c0cdfd DM |
99 | #[test] |
100 | fn test_encrypted_blob_writer() -> Result<(), Error> { | |
101 | let tmp = Cursor::new(Vec::<u8>::new()); | |
9025312a | 102 | let mut blob_writer = DataBlobWriter::new_encrypted(tmp, CRYPT_CONFIG.clone())?; |
50c0cdfd | 103 | blob_writer.write_all(&TEST_DATA)?; |
e7a2b7da | 104 | |
50c0cdfd DM |
105 | verify_test_blob(blob_writer.finish()?) |
106 | } | |
e7a2b7da | 107 | |
50c0cdfd DM |
108 | #[test] |
109 | fn test_encrypted_compressed_blob_writer() -> Result<(), Error> { | |
110 | let tmp = Cursor::new(Vec::<u8>::new()); | |
9025312a | 111 | let mut blob_writer = DataBlobWriter::new_encrypted_compressed(tmp, CRYPT_CONFIG.clone())?; |
50c0cdfd DM |
112 | blob_writer.write_all(&TEST_DATA)?; |
113 | ||
114 | verify_test_blob(blob_writer.finish()?) | |
e7a2b7da | 115 | } |