Merge pull request #9569 from RenjiSann/cksum-fix-b2sum
Some checks are pending
CICD / Style/cargo-deny (push) Waiting to run
CICD / Separate Builds (push) Waiting to run
CICD / Dependencies (push) Waiting to run
CICD / Build/Makefile (push) Blocked by required conditions
CICD / Style/deps (push) Waiting to run
CICD / Documentation/warnings (push) Waiting to run
CICD / MinRustV (push) Waiting to run
CICD / Build/stable (push) Blocked by required conditions
CICD / Build/nightly (push) Blocked by required conditions
CICD / Binary sizes (push) Blocked by required conditions
CICD / Build (push) Blocked by required conditions
CICD / Tests/BusyBox test suite (push) Blocked by required conditions
CICD / Tests/Toybox test suite (push) Blocked by required conditions
CICD / Build/SELinux (push) Blocked by required conditions
CICD / Build/SELinux-Stubs (Non-Linux) (push) Blocked by required conditions
CICD / Safe Traversal Security Check (push) Blocked by required conditions
CICD / Code Coverage (push) Waiting to run
CICD / Test all features separately (push) Blocked by required conditions
GnuTests / Run GNU tests (native) (push) Waiting to run
GnuTests / Run GNU tests (SELinux) (push) Waiting to run
GnuTests / Aggregate GNU test results (push) Blocked by required conditions
Android / Test builds (push) Waiting to run
Benchmarks / Run benchmarks (CodSpeed) (push) Waiting to run
Code Quality / Style/format (push) Waiting to run
Code Quality / Style/Python (push) Waiting to run
Devcontainer / Verify devcontainer (push) Waiting to run
FreeBSD / Tests (push) Waiting to run
Code Quality / Style/lint (push) Waiting to run
Code Quality / Style/spelling (push) Waiting to run
Code Quality / Style/toml (push) Waiting to run
Code Quality / Pre-commit hooks (push) Waiting to run
FreeBSD / Style and Lint (push) Waiting to run
OpenBSD / Style and Lint (push) Waiting to run
OpenBSD / Tests (push) Waiting to run
WSL2 / Test (push) Waiting to run

hashsum: Fix length processing to fix last GNU test
This commit is contained in:
Daniel Hofstetter 2025-12-08 14:29:38 +01:00 committed by GitHub
commit aaa061052f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 22 deletions

View file

@ -11,7 +11,7 @@ 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,
@ -19,7 +19,7 @@ use uucore::checksum::compute::{
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};
@ -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,
};
@ -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)

View file

@ -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)),
@ -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.
@ -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));
}
}

View file

@ -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;
@ -250,11 +252,16 @@ 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;
@ -262,9 +269,13 @@ fn test_invalid_b2sum_length_option_too_large() {
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]