Skip to content

Commit aaa0610

Browse files
authored
Merge pull request #9569 from RenjiSann/cksum-fix-b2sum
hashsum: Fix length processing to fix last GNU test
2 parents 3108c45 + 00d9070 commit aaa0610

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/uu/hashsum/src/hashsum.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ use std::num::ParseIntError;
1111
use std::path::Path;
1212

1313
use clap::builder::ValueParser;
14-
use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
14+
use clap::{Arg, ArgAction, ArgMatches, Command};
1515

1616
use uucore::checksum::compute::{
1717
ChecksumComputeOptions, figure_out_output_format, perform_checksum_computation,
1818
};
1919
use uucore::checksum::validate::{
2020
ChecksumValidateOptions, ChecksumVerbose, perform_checksum_validation,
2121
};
22-
use uucore::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, calculate_blake2b_length};
22+
use uucore::checksum::{AlgoKind, ChecksumError, SizedAlgoKind, calculate_blake2b_length_str};
2323
use uucore::error::UResult;
2424
use uucore::line_ending::LineEnding;
2525
use uucore::{format_usage, translate};
@@ -139,14 +139,14 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
139139
// least somewhat better from a user's perspective.
140140
let matches = uucore::clap_localization::handle_clap_result(command, args)?;
141141

142-
let input_length: Option<&usize> = if binary_name == "b2sum" {
143-
matches.get_one::<usize>(options::LENGTH)
142+
let input_length: Option<&String> = if binary_name == "b2sum" {
143+
matches.get_one::<String>(options::LENGTH)
144144
} else {
145145
None
146146
};
147147

148148
let length = match input_length {
149-
Some(length) => calculate_blake2b_length(*length)?,
149+
Some(length) => calculate_blake2b_length_str(length)?,
150150
None => None,
151151
};
152152

@@ -378,7 +378,6 @@ fn uu_app_opt_length(command: Command) -> Command {
378378
command.arg(
379379
Arg::new(options::LENGTH)
380380
.long(options::LENGTH)
381-
.value_parser(value_parser!(usize))
382381
.short('l')
383382
.help(translate!("hashsum-help-length"))
384383
.overrides_with(options::LENGTH)

src/uucore/src/lib/features/checksum/mod.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ impl SizedAlgoKind {
289289
}
290290
// [`calculate_blake2b_length`] expects a length in bits but we
291291
// have a length in bytes.
292-
(ak::Blake2b, Some(l)) => Ok(Self::Blake2b(calculate_blake2b_length(8 * l)?)),
292+
(ak::Blake2b, Some(l)) => Ok(Self::Blake2b(calculate_blake2b_length_str(
293+
&(8 * l).to_string(),
294+
)?)),
293295
(ak::Blake2b, None) => Ok(Self::Blake2b(None)),
294296

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

445-
/// Calculates the length of the digest.
446-
pub fn calculate_blake2b_length(bit_length: usize) -> UResult<Option<usize>> {
447-
calculate_blake2b_length_str(bit_length.to_string().as_str())
448-
}
449-
450447
/// Calculates the length of the digest.
451448
pub fn calculate_blake2b_length_str(bit_length: &str) -> UResult<Option<usize>> {
452449
// Blake2b's length is parsed in an u64.
@@ -596,10 +593,10 @@ mod tests {
596593

597594
#[test]
598595
fn test_calculate_blake2b_length() {
599-
assert_eq!(calculate_blake2b_length(0).unwrap(), None);
600-
assert!(calculate_blake2b_length(10).is_err());
601-
assert!(calculate_blake2b_length(520).is_err());
602-
assert_eq!(calculate_blake2b_length(512).unwrap(), None);
603-
assert_eq!(calculate_blake2b_length(256).unwrap(), Some(32));
596+
assert_eq!(calculate_blake2b_length_str("0").unwrap(), None);
597+
assert!(calculate_blake2b_length_str("10").is_err());
598+
assert!(calculate_blake2b_length_str("520").is_err());
599+
assert_eq!(calculate_blake2b_length_str("512").unwrap(), None);
600+
assert_eq!(calculate_blake2b_length_str("256").unwrap(), Some(32));
604601
}
605602
}

tests/by-util/test_hashsum.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6+
use rstest::rstest;
7+
68
use uutests::new_ucmd;
79
use uutests::util::TestScenario;
810
use uutests::util_name;
@@ -250,21 +252,30 @@ fn test_invalid_b2sum_length_option_not_multiple_of_8() {
250252
.ccmd("b2sum")
251253
.arg("--length=9")
252254
.arg(at.subdir.join("testf"))
253-
.fails_with_code(1);
255+
.fails_with_code(1)
256+
.stderr_contains("b2sum: invalid length: '9'")
257+
.stderr_contains("b2sum: length is not a multiple of 8");
254258
}
255259

256-
#[test]
257-
fn test_invalid_b2sum_length_option_too_large() {
260+
#[rstest]
261+
#[case("513")]
262+
#[case("1024")]
263+
#[case("18446744073709552000")]
264+
fn test_invalid_b2sum_length_option_too_large(#[case] len: &str) {
258265
let scene = TestScenario::new(util_name!());
259266
let at = &scene.fixtures;
260267

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

263270
scene
264271
.ccmd("b2sum")
265-
.arg("--length=513")
272+
.arg("--length")
273+
.arg(len)
266274
.arg(at.subdir.join("testf"))
267-
.fails_with_code(1);
275+
.fails_with_code(1)
276+
.no_stdout()
277+
.stderr_contains(format!("b2sum: invalid length: '{len}'"))
278+
.stderr_contains("b2sum: maximum digest length for 'BLAKE2b' is 512 bits");
268279
}
269280

270281
#[test]

0 commit comments

Comments
 (0)