Skip to content

Commit

Permalink
hashsum/b2sum: when the checksum file is untagged, detect the size
Browse files Browse the repository at this point in the history
Should fix tests/cksum/b2sum.sh
  • Loading branch information
sylvestre committed Jun 27, 2024
1 parent 888eae7 commit 00e831e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ where
get_expected_checksum(filename_to_check, &caps, &chosen_regex)?;

// If the algo_name is provided, we use it, otherwise we try to detect it

let (algo_name, length) = if is_algo_based_format {
identify_algo_name_and_length(
&caps,
Expand All @@ -515,7 +514,16 @@ where
.unwrap_or((String::new(), None))
} else if let Some(a) = algo_name_input {
// When a specific algorithm name is input, use it and use the provided bits
(a.to_lowercase(), length_input)
// except when dealing with blake2b, where we will detect the length
if algo_name_input == Some(ALGORITHM_OPTIONS_BLAKE2B) {
let length = match expected_checksum.len() {
128 | 96 | 64 | 48 | 32 => Some(expected_checksum.len() / 2),
_ => length_input,
};
(ALGORITHM_OPTIONS_BLAKE2B.to_string(), length)
} else {
(a.to_lowercase(), length_input)
}
} else {
// Default case if no algorithm is specified and non-algo based format is matched
(String::new(), None)
Expand Down
23 changes: 23 additions & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,3 +890,26 @@ fn test_star_to_start() {
.succeeds()
.stdout_only("f: OK\n");
}

#[test]
fn test_check_b2sum_strict_check() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("ck", "cae66941d9efbd404e4d88758ea67670 f\n");

scene
.ccmd("b2sum")
.arg("-c")
.arg(at.subdir.join("ck"))
.succeeds()
.stdout_only("f: OK\n");

scene
.ccmd("b2sum")
.arg("--strict")
.arg("-c")
.arg(at.subdir.join("ck"))
.succeeds()
.stdout_only("f: OK\n");
}

0 comments on commit 00e831e

Please sign in to comment.