Skip to content

Commit

Permalink
Add tests for invalid memory store requests
Browse files Browse the repository at this point in the history
  • Loading branch information
allada committed Dec 26, 2020
1 parent 7922f84 commit 4f8e5a7
Show file tree
Hide file tree
Showing 14 changed files with 921 additions and 47 deletions.
113 changes: 88 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hex = "0.4.2"
async-trait = "0.1.42"
tokio = { version = "0.2.2", features = ["macros"] }
tonic = "0.3.1"
tokio-test = "0.4.0"

[dev-dependencies]
clap = "2.33.3"
Expand Down
1 change: 1 addition & 0 deletions cas/store/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ rust_test(
":memory_store",
":traits",
"//third_party:tokio",
"//third_party:tokio_test",
],
)
40 changes: 28 additions & 12 deletions cas/store/memory_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use hex::FromHex;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, Error};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, Error, ErrorKind};

use traits::StoreTrait;

Expand All @@ -22,27 +22,42 @@ impl MemoryStore {
}
}

macro_rules! make_err {
($($arg:tt)+) => {{
Error::new(
ErrorKind::InvalidInput,
format!("{}", format_args!($($arg)+)
),
)
}};
}

#[async_trait]
impl StoreTrait for MemoryStore {
async fn has(&self, hash: &str, _expected_size: usize) -> Result<bool, Error> {
let raw_key = <[u8; 32]>::from_hex(&hash).expect("Hex length is not 64 hex characters");
let raw_key = <[u8; 32]>::from_hex(&hash)
.or_else(|_| Err(make_err!("Hex length is not 64 hex characters")))?;
Ok(self.map.contains_key(&raw_key))
}

async fn update<'a>(
async fn update<'a, 'b>(
&'a mut self,
hash: &'a str,
expected_size: usize,
mut reader: Box<dyn AsyncRead + Send + Unpin>,
mut reader: Box<dyn AsyncRead + Send + Unpin + 'b>,
) -> Result<(), Error> {
let raw_key = <[u8; 32]>::from_hex(&hash).expect("Hex length is not 64 hex characters");
let raw_key = <[u8; 32]>::from_hex(&hash)
.or_else(|_| Err(make_err!("Hex length is not 64 hex characters")))?;
let mut buffer = Vec::new();
let read_size = reader.read_to_end(&mut buffer).await?;
assert_eq!(
read_size, expected_size,
"Expected size {} but got size {} for hash {} CAS insert",
expected_size, read_size, hash
);
if read_size != expected_size {
return Err(make_err!(
"Expected size {} but got size {} for hash {} CAS insert",
expected_size,
read_size,
hash
));
}
self.map.insert(raw_key, Arc::new(buffer));
Ok(())
}
Expand All @@ -53,11 +68,12 @@ impl StoreTrait for MemoryStore {
_expected_size: usize,
writer: &mut (dyn AsyncWrite + Send + Unpin),
) -> Result<(), Error> {
let raw_key = <[u8; 32]>::from_hex(&hash).expect("Hex length is not 64 hex characters");
let raw_key = <[u8; 32]>::from_hex(&hash)
.or_else(|_| Err(make_err!("Hex length is not 64 hex characters")))?;
let value = self
.map
.get(&raw_key)
.unwrap_or_else(|| panic!("Trying to get object but could not find hash: {}", hash));
.ok_or_else(|| make_err!("Trying to get object but could not find hash: {}", hash))?;
writer.write_all(value).await?;
Ok(())
}
Expand Down
Loading

0 comments on commit 4f8e5a7

Please sign in to comment.