hashsum: handle the case when md5sum is used but the file contains a different algo

This commit is contained in:
Sylvestre Ledru 2024-05-25 12:39:06 +02:00
parent 0882eea07c
commit 89b7a1a8fb
3 changed files with 37 additions and 4 deletions

View file

@ -378,6 +378,16 @@ where
let (algo_name, length) = if algo_based_format { let (algo_name, length) = if algo_based_format {
// When the algo-based format is matched, extract details from regex captures // 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(); 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()) { if !SUPPORTED_ALGO.contains(&algorithm.as_str()) {
// Not supported algo, leave early // Not supported algo, leave early
properly_formatted = false; properly_formatted = false;
@ -507,6 +517,7 @@ where
.maybe_quote() .maybe_quote()
); );
set_exit_code(1); set_exit_code(1);
return Ok(());
} }
if ignore_missing && correct_format == 0 { if ignore_missing && correct_format == 0 {

View file

@ -1057,15 +1057,21 @@ fn test_cksum_mixed() {
let result = scene.ucmd().args(command).arg("f").succeeds(); let result = scene.ucmd().args(command).arg("f").succeeds();
at.append("CHECKSUM", result.stdout_str()); at.append("CHECKSUM", result.stdout_str());
} }
scene println!("Content of CHECKSUM:\n{}", at.read("CHECKSUM"));
let result = scene
.ucmd() .ucmd()
.arg("--check") .arg("--check")
.arg("-a") .arg("-a")
.arg("sm3") .arg("sm3")
.arg("CHECKSUM") .arg("CHECKSUM")
.succeeds() .succeeds();
.stdout_contains("f: OK")
.stderr_contains("3 lines are improperly formatted"); 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] #[test]

View file

@ -668,6 +668,22 @@ fn test_check_status_code() {
.stdout_is(""); .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] #[test]
fn test_check_no_backslash_no_space() { fn test_check_no_backslash_no_space() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());