2019-08-12 13:05:11 +03:00
use std ::io ::Cursor ;
2022-04-10 18:49:26 +03:00
use std ::io ::{ Read , Seek , SeekFrom , Write } ;
use std ::sync ::Arc ;
2021-10-08 12:19:37 +03:00
use anyhow ::{ bail , Error } ;
2019-08-13 19:10:38 +03:00
use lazy_static ::lazy_static ;
2019-08-12 13:05:11 +03:00
2021-09-07 10:22:14 +03:00
use pbs_datastore ::{ DataBlob , DataBlobReader , DataBlobWriter } ;
2022-04-10 18:49:26 +03:00
use pbs_tools ::crypt_config ::CryptConfig ;
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
lazy_static! {
static ref TEST_DATA : Vec < u8 > = {
let mut data = Vec ::new ( ) ;
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
for i in 0 .. 100_000 {
2022-04-10 18:49:26 +03:00
data . push ( ( i % 255 ) as u8 ) ;
2019-08-12 13:05:11 +03:00
}
2019-08-13 19:10:38 +03:00
data
2019-08-12 13:05:11 +03:00
} ;
2019-08-16 10:19:01 +03:00
static ref CRYPT_CONFIG : Arc < CryptConfig > = {
2019-08-13 19:10:38 +03:00
let key = [ 1 u8 ; 32 ] ;
2019-08-16 10:19:01 +03:00
Arc ::new ( CryptConfig ::new ( key ) . unwrap ( ) )
2019-08-13 19:10:38 +03:00
} ;
2022-04-10 18:49:26 +03:00
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
] ;
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
] ;
2019-08-13 19:10:38 +03:00
}
2019-08-12 13:05:11 +03:00
2020-08-03 15:10:43 +03:00
fn verify_test_blob ( mut cursor : Cursor < Vec < u8 > > , digest : & [ u8 ; 32 ] ) -> Result < ( ) , Error > {
2019-08-13 19:10:38 +03:00
// run read tests with different buffer sizes
2022-04-10 18:49:26 +03:00
for size in [ 1 , 3 , 64 * 1024 ] . iter ( ) {
2019-08-13 19:10:38 +03:00
println! ( " Starting DataBlobReader test (size = {} ) " , size ) ;
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
cursor . seek ( SeekFrom ::Start ( 0 ) ) ? ;
2019-08-16 10:19:01 +03:00
let mut reader = DataBlobReader ::new ( & mut cursor , Some ( CRYPT_CONFIG . clone ( ) ) ) ? ;
2019-08-13 19:10:38 +03:00
let mut buffer = Vec ::< u8 > ::new ( ) ;
// read the whole file
//reader.read_to_end(&mut buffer)?;
let mut buf = vec! [ 0 u8 ; * size ] ;
loop {
let count = reader . read ( & mut buf ) ? ;
2022-04-10 18:49:26 +03:00
if count = = 0 {
break ;
}
2019-08-13 19:10:38 +03:00
buffer . extend ( & buf [ .. count ] ) ;
}
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
reader . finish ( ) ? ;
if buffer ! = * TEST_DATA {
bail! ( " blob data is wrong (read buffer size {}) " , size ) ;
}
2019-08-12 13:05:11 +03:00
}
2019-08-13 19:10:38 +03:00
let raw_data = cursor . into_inner ( ) ;
2019-08-12 13:05:11 +03:00
2020-07-28 11:23:16 +03:00
let blob = DataBlob ::load_from_reader ( & mut & raw_data [ .. ] ) ? ;
2019-08-12 13:05:11 +03:00
2020-08-03 15:10:43 +03:00
let data = blob . decode ( Some ( & CRYPT_CONFIG ) , Some ( digest ) ) ? ;
2019-08-13 19:10:38 +03:00
if data ! = * TEST_DATA {
bail! ( " blob data is wrong (decode) " ) ;
2019-08-12 13:05:11 +03:00
}
2019-08-13 19:10:38 +03:00
Ok ( ( ) )
}
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
#[ test ]
fn test_uncompressed_blob_writer ( ) -> Result < ( ) , Error > {
let tmp = Cursor ::new ( Vec ::< u8 > ::new ( ) ) ;
let mut blob_writer = DataBlobWriter ::new_uncompressed ( tmp ) ? ;
blob_writer . write_all ( & TEST_DATA ) ? ;
2019-08-12 13:05:11 +03:00
2022-12-05 13:27:40 +03:00
verify_test_blob ( blob_writer . finish ( ) ? , & TEST_DIGEST_PLAIN )
2019-08-13 19:10:38 +03:00
}
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
#[ test ]
fn test_compressed_blob_writer ( ) -> Result < ( ) , Error > {
let tmp = Cursor ::new ( Vec ::< u8 > ::new ( ) ) ;
let mut blob_writer = DataBlobWriter ::new_compressed ( tmp ) ? ;
blob_writer . write_all ( & TEST_DATA ) ? ;
2019-08-12 13:05:11 +03:00
2022-12-05 13:27:40 +03:00
verify_test_blob ( blob_writer . finish ( ) ? , & TEST_DIGEST_PLAIN )
2019-08-13 19:10:38 +03:00
}
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
#[ test ]
fn test_encrypted_blob_writer ( ) -> Result < ( ) , Error > {
let tmp = Cursor ::new ( Vec ::< u8 > ::new ( ) ) ;
2019-08-16 10:19:01 +03:00
let mut blob_writer = DataBlobWriter ::new_encrypted ( tmp , CRYPT_CONFIG . clone ( ) ) ? ;
2019-08-13 19:10:38 +03:00
blob_writer . write_all ( & TEST_DATA ) ? ;
2019-08-12 13:05:11 +03:00
2022-12-05 13:27:40 +03:00
verify_test_blob ( blob_writer . finish ( ) ? , & TEST_DIGEST_ENC )
2019-08-13 19:10:38 +03:00
}
2019-08-12 13:05:11 +03:00
2019-08-13 19:10:38 +03:00
#[ test ]
fn test_encrypted_compressed_blob_writer ( ) -> Result < ( ) , Error > {
let tmp = Cursor ::new ( Vec ::< u8 > ::new ( ) ) ;
2019-08-16 10:19:01 +03:00
let mut blob_writer = DataBlobWriter ::new_encrypted_compressed ( tmp , CRYPT_CONFIG . clone ( ) ) ? ;
2019-08-13 19:10:38 +03:00
blob_writer . write_all ( & TEST_DATA ) ? ;
2022-12-05 13:27:40 +03:00
verify_test_blob ( blob_writer . finish ( ) ? , & TEST_DIGEST_ENC )
2019-08-12 13:05:11 +03:00
}