mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 14:52:41 +00:00
hashsum: handle the case when md5sum is used but the file contains a different algo
This commit is contained in:
parent
0882eea07c
commit
89b7a1a8fb
3 changed files with 37 additions and 4 deletions
|
@ -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 {
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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!());
|
||||
|
|
Loading…
Reference in a new issue