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
11 changes: 5 additions & 6 deletions src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use std::num::ParseIntError;
use std::path::Path;

use clap::builder::ValueParser;
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
use clap::{Arg, ArgAction, ArgMatches, Command};

use uucore::checksum::compute::{
ChecksumComputeOptions, figure_out_output_format, perform_checksum_computation,
};
use uucore::checksum::validate::{
ChecksumValidateOptions, ChecksumVerbose, perform_checksum_validation,
};
use uucore::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, calculate_blake2b_length};
use uucore::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, calculate_blake2b_length_str};
use uucore::error::UResult;
use uucore::line_ending::LineEnding;
use uucore::{format_usage, translate};
Expand Down Expand Up @@ -139,14 +139,14 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
// least somewhat better from a user's perspective.
let matches = uucore::clap_localization::handle_clap_result(command, args)?;

let input_length: Option<&usize> = if binary_name == "b2sum" {
matches.get_one::<usize>(options::LENGTH)
let input_length: Option<&String> = if binary_name == "b2sum" {
matches.get_one::<String>(options::LENGTH)
} else {
None
};

let length = match input_length {
Some(length) => calculate_blake2b_length(*length)?,
Some(length) => calculate_blake2b_length_str(length)?,
None => None,
};

Expand Down Expand Up @@ -378,7 +378,6 @@ fn uu_app_opt_length(command: Command) -> Command {
command.arg(
Arg::new(options::LENGTH)
.long(options::LENGTH)
.value_parser(value_parser!(usize))
.short('l')
.help(translate!("hashsum-help-length"))
.overrides_with(options::LENGTH)
Expand Down
19 changes: 8 additions & 11 deletions src/uucore/src/lib/features/checksum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ impl SizedAlgoKind {
}
// [`calculate_blake2b_length`] expects a length in bits but we
// have a length in bytes.
(ak::Blake2b, Some(l)) => Ok(Self::Blake2b(calculate_blake2b_length(8 * l)?)),
(ak::Blake2b, Some(l)) => Ok(Self::Blake2b(calculate_blake2b_length_str(
&(8 * l).to_string(),
)?)),
(ak::Blake2b, None) => Ok(Self::Blake2b(None)),

(ak::Sha224, None) => Ok(Self::Sha2(ShaLength::Len224)),
Expand Down Expand Up @@ -442,11 +444,6 @@ pub fn digest_reader<T: Read>(
Ok((digest.result(), output_size))
}

/// Calculates the length of the digest.
pub fn calculate_blake2b_length(bit_length: usize) -> UResult<Option<usize>> {
calculate_blake2b_length_str(bit_length.to_string().as_str())
}

/// Calculates the length of the digest.
pub fn calculate_blake2b_length_str(bit_length: &str) -> UResult<Option<usize>> {
// Blake2b's length is parsed in an u64.
Expand Down Expand Up @@ -596,10 +593,10 @@ mod tests {

#[test]
fn test_calculate_blake2b_length() {
assert_eq!(calculate_blake2b_length(0).unwrap(), None);
assert!(calculate_blake2b_length(10).is_err());
assert!(calculate_blake2b_length(520).is_err());
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
assert_eq!(calculate_blake2b_length_str("0").unwrap(), None);
assert!(calculate_blake2b_length_str("10").is_err());
assert!(calculate_blake2b_length_str("520").is_err());
assert_eq!(calculate_blake2b_length_str("512").unwrap(), None);
assert_eq!(calculate_blake2b_length_str("256").unwrap(), Some(32));
}
}
21 changes: 16 additions & 5 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use rstest::rstest;

use uutests::new_ucmd;
use uutests::util::TestScenario;
use uutests::util_name;
Expand Down Expand Up @@ -250,21 +252,30 @@ fn test_invalid_b2sum_length_option_not_multiple_of_8() {
.ccmd("b2sum")
.arg("--length=9")
.arg(at.subdir.join("testf"))
.fails_with_code(1);
.fails_with_code(1)
.stderr_contains("b2sum: invalid length: '9'")
.stderr_contains("b2sum: length is not a multiple of 8");
}

#[test]
fn test_invalid_b2sum_length_option_too_large() {
#[rstest]
#[case("513")]
#[case("1024")]
#[case("18446744073709552000")]
fn test_invalid_b2sum_length_option_too_large(#[case] len: &str) {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("testf", "foobar\n");

scene
.ccmd("b2sum")
.arg("--length=513")
.arg("--length")
.arg(len)
.arg(at.subdir.join("testf"))
.fails_with_code(1);
.fails_with_code(1)
.no_stdout()
.stderr_contains(format!("b2sum: invalid length: '{len}'"))
.stderr_contains("b2sum: maximum digest length for 'BLAKE2b' is 512 bits");
}

#[test]
Expand Down
Loading