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