Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/uu/cksum/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let input_length = matches.get_one::<usize>(options::LENGTH);

let length = match input_length {
Some(length) => {
if algo_name == ALGORITHM_OPTIONS_BLAKE2B {
calculate_blake2b_length(*length)?
} else {
return Err(ChecksumError::LengthOnlyForBlake2b.into());
}
None | Some(0) => None,
Some(length) if algo_name == ALGORITHM_OPTIONS_BLAKE2B => {
calculate_blake2b_length(*length)?
}
_ => {
return Err(ChecksumError::LengthOnlyForBlake2b.into());
}
None => None,
};

if LEGACY_ALGORITHMS.contains(&algo_name) && check {
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,20 @@ fn test_length_with_wrong_algorithm() {
.stderr_contains("cksum: --length is only supported with --algorithm=blake2b");
}

/// Giving --length to a wrong algorithm doesn't fail if the length is zero
#[test]
fn test_length_is_zero_with_wrong_algorithm() {
for algo in ["md5", "crc", "sha1", "sha224", "sha256", "sha384", "sha512"] {
new_ucmd!()
.arg("--length=0")
.args(&["-a", algo])
.arg("lorem_ipsum.txt")
.succeeds()
.no_stderr()
.stdout_is_fixture(format!("{algo}_single_file.expected"));
}
}

#[test]
fn test_length_not_supported() {
new_ucmd!()
Expand Down
Loading