cksum/blake2b: improve the error management

This commit is contained in:
Sylvestre Ledru 2024-05-14 13:32:49 +02:00
parent 8ddb2131df
commit 843275a136
4 changed files with 29 additions and 33 deletions

View file

@ -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<Option<usize>> {
fn calculate_blake2b_length(length: usize) -> UResult<Option<usize>> {
match length {
0 => Ok(None),
n if n % 8 != 0 => {
@ -374,21 +374,13 @@ fn calculate_length(algo_name: &str, length: usize) -> UResult<Option<usize>> {
.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::<usize>(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();
}
}

View file

@ -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]

View file

@ -1,2 +0,0 @@
BLAKE2b-16 (lorem_ipsum.txt) = 7e2f
BLAKE2b-16 (alice_in_wonderland.txt) = a546

View file

@ -1,2 +0,0 @@
cksum: invalid length: 15
cksum: length is not a multiple of 8