diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs index c24f0419c..a2b32d641 100644 --- a/src/uucore/src/lib/features/checksum.rs +++ b/src/uucore/src/lib/features/checksum.rs @@ -378,6 +378,16 @@ where let (algo_name, length) = if algo_based_format { // When the algo-based format is matched, extract details from regex captures let algorithm = caps.name("algo").map_or("", |m| m.as_str()).to_lowercase(); + + // check if we are called with XXXsum (example: md5sum) but we detected a different algo parsing the file + // (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) { + bad_format += 1; + properly_formatted = false; + continue; + } + if !SUPPORTED_ALGO.contains(&algorithm.as_str()) { // Not supported algo, leave early properly_formatted = false; @@ -507,6 +517,7 @@ where .maybe_quote() ); set_exit_code(1); + return Ok(()); } if ignore_missing && correct_format == 0 { diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index c1bd492c9..b4f561c66 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1057,15 +1057,21 @@ fn test_cksum_mixed() { let result = scene.ucmd().args(command).arg("f").succeeds(); at.append("CHECKSUM", result.stdout_str()); } - scene + println!("Content of CHECKSUM:\n{}", at.read("CHECKSUM")); + let result = scene .ucmd() .arg("--check") .arg("-a") .arg("sm3") .arg("CHECKSUM") - .succeeds() - .stdout_contains("f: OK") - .stderr_contains("3 lines are improperly formatted"); + .succeeds(); + + println!("result.stderr_str() {}", result.stderr_str()); + println!("result.stdout_str() {}", result.stdout_str()); + assert!(result.stdout_str().contains("f: OK")); + assert!(result + .stderr_str() + .contains("3 lines are improperly formatted")); } #[test] diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index c6199fdc1..2ad32fa31 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -668,6 +668,22 @@ fn test_check_status_code() { .stdout_is(""); } +#[test] +fn test_sha1_with_md5sum_should_fail() { + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.touch("f"); + at.write("f.sha1", "SHA1 (f) = d41d8cd98f00b204e9800998ecf8427e\n"); + scene + .ccmd("md5sum") + .arg("--check") + .arg(at.subdir.join("f.sha1")) + .fails() + .stderr_contains("f.sha1: no properly formatted checksum lines found") + .stderr_does_not_contain("WARNING: 1 line is improperly formatted"); +} + #[test] fn test_check_no_backslash_no_space() { let scene = TestScenario::new(util_name!());