[wip] unit tests for file module
- testing for `compute_hash` and `check_hash`
This commit is contained in:
parent
cab6d13d28
commit
6c385ffeea
3 changed files with 59 additions and 12 deletions
|
|
@ -100,6 +100,11 @@ impl<'t> FileTrait<'t> for Checked {
|
|||
}
|
||||
|
||||
fn check_hash(&self, on_progress: impl Fn(u64)) -> crate::Result<()> {
|
||||
super::check_hash(&self.path, self.size, self.hash.as_ref(), on_progress)
|
||||
super::check_hash(
|
||||
&self.path,
|
||||
self.size,
|
||||
self.hash.as_ref().map(String::as_str),
|
||||
on_progress,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ fn compute_hash(path: &Path, size: u64, mut on_progress: impl FnMut(u64)) -> cra
|
|||
fn check_hash(
|
||||
path: &Path,
|
||||
size: u64,
|
||||
hash: Option<&String>,
|
||||
hash: Option<&str>,
|
||||
on_progress: impl FnMut(u64),
|
||||
) -> crate::Result<()> {
|
||||
let Some(expected) = hash else {
|
||||
|
|
@ -126,11 +126,11 @@ mod tests {
|
|||
fn compute_hash_as_expected() {
|
||||
for (&(content, size), expected_hash) in CASES.iter().zip(HASHES) {
|
||||
// to capture progress updates from `compute_hash`
|
||||
let file = create_file(content);
|
||||
let mut read_total = 0;
|
||||
let callback = |n| read_total += n;
|
||||
|
||||
let hash = compute_hash(create_file(content).path(), size, callback)
|
||||
.expect("hash should succeed");
|
||||
let hash = compute_hash(file.path(), size, callback).expect("hash should succeed");
|
||||
|
||||
assert_eq!(hash, expected_hash);
|
||||
assert_eq!(read_total, size);
|
||||
|
|
@ -138,7 +138,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn compute_hash_size_mismatch() {
|
||||
fn hash_size_mismatch() {
|
||||
let bad_sizes = [
|
||||
36, // common pangram
|
||||
12, // simple greeting
|
||||
|
|
@ -151,18 +151,55 @@ mod tests {
|
|||
];
|
||||
|
||||
for (&(content, good_size), bad_size) in CASES.iter().zip(bad_sizes) {
|
||||
let file = create_file(content);
|
||||
let callback = drop;
|
||||
|
||||
let err = compute_hash(create_file(content).path(), bad_size, callback)
|
||||
.expect_err("should get a size-mismatch error");
|
||||
let err_callback = |err| {
|
||||
// check error
|
||||
match err {
|
||||
crate::Error::Mismatch { expected, actual } => {
|
||||
assert_eq!(expected, bad_size.to_string());
|
||||
assert_eq!(actual, good_size.to_string());
|
||||
}
|
||||
_ => panic!(
|
||||
"Expected Error::Mismatch for input size `{bad_size}` but got {err:?}"
|
||||
),
|
||||
};
|
||||
};
|
||||
|
||||
// make sure it's the Mismatch variant, and that it contains the original input
|
||||
compute_hash(file.path(), bad_size, callback)
|
||||
.map_err(err_callback)
|
||||
.expect_err("compute_hash should report a mismatch");
|
||||
|
||||
check_hash(file.path(), bad_size, Some("foobar"), callback)
|
||||
.map_err(err_callback)
|
||||
.expect_err("check_hash should report a mismatch");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_value_mismatch() {
|
||||
let bad_hashes = [
|
||||
"invalid9k+SHfSdG5igXsRY2Sh+nvBSNlQkLxzM7NnP4JAHPeqLkyx7NkCluPxTLVBP47Xe+cwRbE5FM3NapGA", // common pangram
|
||||
"", // simple greeting
|
||||
"eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiG/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg", // empty slice
|
||||
"Hash", // single-byte
|
||||
];
|
||||
|
||||
for ((&(content, size), good_hash), bad_hash) in CASES.iter().zip(HASHES).zip(bad_hashes) {
|
||||
let file = create_file(content);
|
||||
let callback = drop;
|
||||
|
||||
let err = check_hash(file.path(), size, Some(&bad_hash.to_string()), callback)
|
||||
.expect_err("check_hash should report a mismatch");
|
||||
|
||||
// check error
|
||||
match err {
|
||||
crate::Error::Mismatch { expected, actual } => {
|
||||
assert_eq!(expected, bad_size.to_string());
|
||||
assert_eq!(actual, good_size.to_string());
|
||||
assert_eq!(expected, bad_hash);
|
||||
assert_eq!(actual, good_hash);
|
||||
}
|
||||
_ => panic!("Expected Error::Mismatch for input size `{bad_size}` but got {err:?}"),
|
||||
_ => panic!("Expected Error::Mismatch but got {err:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,6 +107,11 @@ impl<'t> FileTrait<'t> for Uploading {
|
|||
}
|
||||
|
||||
fn check_hash(&self, on_progress: impl Fn(u64)) -> crate::Result<()> {
|
||||
super::check_hash(&self.path, self.size, self.hash.as_ref(), on_progress)
|
||||
super::check_hash(
|
||||
&self.path,
|
||||
self.size,
|
||||
self.hash.as_ref().map(String::as_str),
|
||||
on_progress,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue