[wip] unit tests for file module

- testing for `compute_hash` and `check_hash`
This commit is contained in:
Jörn-Michael Miehe 2025-07-03 16:18:46 +00:00
parent cab6d13d28
commit 6c385ffeea
3 changed files with 59 additions and 12 deletions

View file

@ -100,6 +100,11 @@ impl<'t> FileTrait<'t> for Checked {
} }
fn check_hash(&self, on_progress: impl Fn(u64)) -> crate::Result<()> { 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,
)
} }
} }

View file

@ -46,7 +46,7 @@ fn compute_hash(path: &Path, size: u64, mut on_progress: impl FnMut(u64)) -> cra
fn check_hash( fn check_hash(
path: &Path, path: &Path,
size: u64, size: u64,
hash: Option<&String>, hash: Option<&str>,
on_progress: impl FnMut(u64), on_progress: impl FnMut(u64),
) -> crate::Result<()> { ) -> crate::Result<()> {
let Some(expected) = hash else { let Some(expected) = hash else {
@ -126,11 +126,11 @@ mod tests {
fn compute_hash_as_expected() { fn compute_hash_as_expected() {
for (&(content, size), expected_hash) in CASES.iter().zip(HASHES) { for (&(content, size), expected_hash) in CASES.iter().zip(HASHES) {
// to capture progress updates from `compute_hash` // to capture progress updates from `compute_hash`
let file = create_file(content);
let mut read_total = 0; let mut read_total = 0;
let callback = |n| read_total += n; let callback = |n| read_total += n;
let hash = compute_hash(create_file(content).path(), size, callback) let hash = compute_hash(file.path(), size, callback).expect("hash should succeed");
.expect("hash should succeed");
assert_eq!(hash, expected_hash); assert_eq!(hash, expected_hash);
assert_eq!(read_total, size); assert_eq!(read_total, size);
@ -138,7 +138,7 @@ mod tests {
} }
#[test] #[test]
fn compute_hash_size_mismatch() { fn hash_size_mismatch() {
let bad_sizes = [ let bad_sizes = [
36, // common pangram 36, // common pangram
12, // simple greeting 12, // simple greeting
@ -151,18 +151,55 @@ mod tests {
]; ];
for (&(content, good_size), bad_size) in CASES.iter().zip(bad_sizes) { for (&(content, good_size), bad_size) in CASES.iter().zip(bad_sizes) {
let file = create_file(content);
let callback = drop; let callback = drop;
let err = compute_hash(create_file(content).path(), bad_size, callback) let err_callback = |err| {
.expect_err("should get a size-mismatch error"); // 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 { match err {
crate::Error::Mismatch { expected, actual } => { crate::Error::Mismatch { expected, actual } => {
assert_eq!(expected, bad_size.to_string()); assert_eq!(expected, bad_hash);
assert_eq!(actual, good_size.to_string()); assert_eq!(actual, good_hash);
} }
_ => panic!("Expected Error::Mismatch for input size `{bad_size}` but got {err:?}"), _ => panic!("Expected Error::Mismatch but got {err:?}"),
} }
} }
} }

View file

@ -107,6 +107,11 @@ impl<'t> FileTrait<'t> for Uploading {
} }
fn check_hash(&self, on_progress: impl Fn(u64)) -> crate::Result<()> { 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,
)
} }
} }