From 843275a136b34c4bf1d9745c5942e16bd16a8e5c Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Tue, 14 May 2024 13:32:49 +0200 Subject: [PATCH] cksum/blake2b: improve the error management --- src/uu/cksum/src/cksum.rs | 52 +++++++++---------- tests/by-util/test_cksum.rs | 6 ++- .../fixtures/cksum/supported_length.expected | 2 - .../cksum/unsupported_length.expected | 2 - 4 files changed, 29 insertions(+), 33 deletions(-) delete mode 100644 tests/fixtures/cksum/supported_length.expected delete mode 100644 tests/fixtures/cksum/unsupported_length.expected diff --git a/src/uu/cksum/src/cksum.rs b/src/uu/cksum/src/cksum.rs index 1b6ace6da..00576d94e 100644 --- a/src/uu/cksum/src/cksum.rs +++ b/src/uu/cksum/src/cksum.rs @@ -358,7 +358,7 @@ fn had_reset(args: &[String]) -> bool { } /// Calculates the length of the digest for the given algorithm. -fn calculate_length(algo_name: &str, length: usize) -> UResult> { +fn calculate_blake2b_length(length: usize) -> UResult> { match length { 0 => Ok(None), n if n % 8 != 0 => { @@ -374,21 +374,13 @@ fn calculate_length(algo_name: &str, length: usize) -> UResult> { .into()) } n => { - if algo_name == ALGORITHM_OPTIONS_BLAKE2B { - // Divide by 8, as our blake2b implementation expects bytes instead of bits. - if n == 512 { - // When length is 512, it is blake2b's default. - // So, don't show it - Ok(None) - } else { - Ok(Some(n / 8)) - } + // Divide by 8, as our blake2b implementation expects bytes instead of bits. + if n == 512 { + // When length is 512, it is blake2b's default. + // So, don't show it + Ok(None) } else { - Err(io::Error::new( - io::ErrorKind::InvalidInput, - "--length is only supported with --algorithm=blake2b", - ) - .into()) + Ok(Some(n / 8)) } } } @@ -623,7 +615,17 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let input_length = matches.get_one::(options::LENGTH); let length = match input_length { - Some(length) => calculate_length(algo_name, *length)?, + Some(length) => { + if algo_name != ALGORITHM_OPTIONS_BLAKE2B { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "--length is only supported with --algorithm=blake2b", + ) + .into()); + } else { + calculate_blake2b_length(*length)? + } + } None => None, }; @@ -766,7 +768,7 @@ pub fn uu_app() -> Command { #[cfg(test)] mod tests { use super::had_reset; - use crate::calculate_length; + use crate::calculate_blake2b_length; use crate::prompt_asterisk; #[test] @@ -836,17 +838,13 @@ mod tests { #[test] fn test_calculate_length() { - assert_eq!( - calculate_length(crate::ALGORITHM_OPTIONS_BLAKE2B, 256).unwrap(), - Some(32) - ); + assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32)); + assert_eq!(calculate_blake2b_length(512).unwrap(), None); + assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32)); + calculate_blake2b_length(255).unwrap_err(); - calculate_length(crate::ALGORITHM_OPTIONS_BLAKE2B, 255).unwrap_err(); + calculate_blake2b_length(33).unwrap_err(); - calculate_length(crate::ALGORITHM_OPTIONS_SHA256, 33).unwrap_err(); - - calculate_length(crate::ALGORITHM_OPTIONS_BLAKE2B, 513).unwrap_err(); - - calculate_length(crate::ALGORITHM_OPTIONS_SHA256, 256).unwrap_err(); + calculate_blake2b_length(513).unwrap_err(); } } diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 62a182a7f..0a15f4ef3 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -325,7 +325,7 @@ fn test_length_not_supported() { .arg("lorem_ipsum.txt") .fails() .no_stdout() - .stderr_is_fixture("unsupported_length.expected") + .stderr_contains("--length is only supported with --algorithm=blake2b") .code_is(1); } @@ -337,7 +337,9 @@ fn test_length() { .arg("lorem_ipsum.txt") .arg("alice_in_wonderland.txt") .succeeds() - .stdout_is_fixture("supported_length.expected"); + .stdout_contains( + "BLAKE2b-16 (lorem_ipsum.txt) = 7e2f\nBLAKE2b-16 (alice_in_wonderland.txt) = a546", + ); } #[test] diff --git a/tests/fixtures/cksum/supported_length.expected b/tests/fixtures/cksum/supported_length.expected deleted file mode 100644 index a2edb23e9..000000000 --- a/tests/fixtures/cksum/supported_length.expected +++ /dev/null @@ -1,2 +0,0 @@ -BLAKE2b-16 (lorem_ipsum.txt) = 7e2f -BLAKE2b-16 (alice_in_wonderland.txt) = a546 diff --git a/tests/fixtures/cksum/unsupported_length.expected b/tests/fixtures/cksum/unsupported_length.expected deleted file mode 100644 index c2a05fec7..000000000 --- a/tests/fixtures/cksum/unsupported_length.expected +++ /dev/null @@ -1,2 +0,0 @@ -cksum: invalid length: ‘15’ -cksum: length is not a multiple of 8