From f2cf08b4e6d116910a80ed608853283fcbcb697a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Mon, 3 Feb 2025 02:09:45 +0100 Subject: [PATCH] test(cksum): fix and un-ignore `test_md5_bits` --- src/uucore/src/lib/features/checksum.rs | 24 +++++++++++++++--------- tests/by-util/test_cksum.rs | 1 - 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs index 9912bea74..8346f9c6d 100644 --- a/src/uucore/src/lib/features/checksum.rs +++ b/src/uucore/src/lib/features/checksum.rs @@ -692,7 +692,7 @@ fn identify_algo_name_and_length( line_info: &LineInfo, algo_name_input: Option<&str>, last_algo: &mut Option, -) -> Option<(String, Option)> { +) -> Result<(String, Option), LineCheckError> { let algo_from_line = line_info.algo_name.clone().unwrap_or_default(); let algorithm = algo_from_line.to_lowercase(); *last_algo = Some(algo_from_line); @@ -701,18 +701,25 @@ fn identify_algo_name_and_length( // (for example SHA1 (f) = d...) // Also handle the case cksum -s sm3 but the file contains other formats if algo_name_input.is_some() && algo_name_input != Some(&algorithm) { - return None; + return Err(LineCheckError::ImproperlyFormatted); } if !SUPPORTED_ALGORITHMS.contains(&algorithm.as_str()) { // Not supported algo, leave early - return None; + return Err(LineCheckError::ImproperlyFormatted); } let bytes = if let Some(bitlen) = line_info.algo_bit_len { - if bitlen % 8 != 0 { - // The given length is wrong - return None; + if algorithm != ALGORITHM_OPTIONS_BLAKE2B || bitlen % 8 != 0 { + // Either + // the algo based line is provided with a bit length + // with an algorithm that does not support it (only Blake2B does). + // + // eg: MD5-128 (foo.txt) = fffffffff + // ^ This is illegal + // OR + // the given length is wrong because it's not a multiple of 8. + return Err(LineCheckError::ImproperlyFormatted); } Some(bitlen / 8) } else if algorithm == ALGORITHM_OPTIONS_BLAKE2B { @@ -722,7 +729,7 @@ fn identify_algo_name_and_length( None }; - Some((algorithm, bytes)) + Ok((algorithm, bytes)) } /// Given a filename and an algorithm, compute the digest and compare it with @@ -773,8 +780,7 @@ fn process_algo_based_line( let filename_to_check = line_info.filename.as_slice(); let (algo_name, algo_byte_len) = - identify_algo_name_and_length(line_info, cli_algo_name, last_algo) - .ok_or(LineCheckError::ImproperlyFormatted)?; + identify_algo_name_and_length(line_info, cli_algo_name, last_algo)?; // If the digest bitlen is known, we can check the format of the expected // checksum with it. diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 6120e1a18..9f2869b54 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1136,7 +1136,6 @@ fn test_cksum_garbage() { .stderr_contains("check-file: no properly formatted checksum lines found"); } -#[ignore = "Should fail on bits"] #[test] fn test_md5_bits() { let (at, mut ucmd) = at_and_ucmd!();