From 6c385ffeea9fd25e6bb241485a2a450178687374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 3 Jul 2025 16:18:46 +0000 Subject: [PATCH] [wip] unit tests for `file` module - testing for `compute_hash` and `check_hash` --- src/file/checked.rs | 7 +++++- src/file/mod.rs | 57 +++++++++++++++++++++++++++++++++++-------- src/file/uploading.rs | 7 +++++- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/file/checked.rs b/src/file/checked.rs index 56a4067..0ff4ea8 100644 --- a/src/file/checked.rs +++ b/src/file/checked.rs @@ -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, + ) } } diff --git a/src/file/mod.rs b/src/file/mod.rs index fc61acf..3d91f61 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -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:?}"), } } } diff --git a/src/file/uploading.rs b/src/file/uploading.rs index 3fe45b9..9ec85e9 100644 --- a/src/file/uploading.rs +++ b/src/file/uploading.rs @@ -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, + ) } }