1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

hashsum: support --check for algorithms with variable output length (#2583)

* hashsum: support --check for var. length outputs

Add the ability for `hashsum --check` to work with algorithms with
variable output length. Previously, the program would terminate with an
error due to constructing an invalid regular expression.

* fixup! hashsum: support --check for var. length outputs
This commit is contained in:
jfinkels 2021-08-23 12:35:19 -04:00 committed by GitHub
parent b828e922e5
commit bdc0f4b7c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 40 additions and 4 deletions

View file

@ -473,15 +473,26 @@ where
}); });
if options.check { if options.check {
// Set up Regexes for line validation and parsing // Set up Regexes for line validation and parsing
//
// First, we compute the number of bytes we expect to be in
// the digest string. If the algorithm has a variable number
// of output bits, then we use the `+` modifier in the
// regular expression, otherwise we use the `{n}` modifier,
// where `n` is the number of bytes.
let bytes = options.digest.output_bits() / 4; let bytes = options.digest.output_bits() / 4;
let modifier = if bytes > 0 {
format!("{{{}}}", bytes)
} else {
"+".to_string()
};
let gnu_re = safe_unwrap!(Regex::new(&format!( let gnu_re = safe_unwrap!(Regex::new(&format!(
r"^(?P<digest>[a-fA-F0-9]{{{}}}) (?P<binary>[ \*])(?P<fileName>.*)", r"^(?P<digest>[a-fA-F0-9]{}) (?P<binary>[ \*])(?P<fileName>.*)",
bytes modifier,
))); )));
let bsd_re = safe_unwrap!(Regex::new(&format!( let bsd_re = safe_unwrap!(Regex::new(&format!(
r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{{{digest_size}}})", r"^{algorithm} \((?P<fileName>.*)\) = (?P<digest>[a-fA-F0-9]{digest_size})",
algorithm = options.algoname, algorithm = options.algoname,
digest_size = bytes digest_size = modifier,
))); )));
let buffer = file; let buffer = file;

View file

@ -1,3 +1,4 @@
// spell-checker:ignore checkfile
macro_rules! get_hash( macro_rules! get_hash(
($str:expr) => ( ($str:expr) => (
$str.split(' ').collect::<Vec<&str>>()[0] $str.split(' ').collect::<Vec<&str>>()[0]
@ -12,6 +13,7 @@ macro_rules! test_digest {
static DIGEST_ARG: &'static str = concat!("--", stringify!($t)); static DIGEST_ARG: &'static str = concat!("--", stringify!($t));
static BITS_ARG: &'static str = concat!("--bits=", stringify!($size)); static BITS_ARG: &'static str = concat!("--bits=", stringify!($size));
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected"); static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
#[test] #[test]
fn test_single_file() { fn test_single_file() {
@ -26,6 +28,16 @@ macro_rules! test_digest {
assert_eq!(ts.fixtures.read(EXPECTED_FILE), assert_eq!(ts.fixtures.read(EXPECTED_FILE),
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str())); get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture("input.txt").succeeds().no_stderr().stdout_str()));
} }
#[test]
fn test_check() {
let ts = TestScenario::new("hashsum");
ts.ucmd()
.args(&[DIGEST_ARG, BITS_ARG, "--check", CHECK_FILE])
.succeeds()
.no_stderr()
.stdout_is("input.txt: OK\n");
}
} }
)*) )*)
} }

View file

@ -0,0 +1 @@
7355dd5276c21cfe0c593b5063b96af3f96a454b33216f58314f44c3ade92e9cd6cec4210a0836246780e9baf927cc50b9a3d7073e8f9bd12780fddbcb930c6d input.txt

1
tests/fixtures/hashsum/md5.checkfile vendored Normal file
View file

@ -0,0 +1 @@
e4d7f1b4ed2e42d15898f4b27b019da4 input.txt

1
tests/fixtures/hashsum/sha1.checkfile vendored Normal file
View file

@ -0,0 +1 @@
b7e23ec29af22b0b4e41da31e868d57226121c84 input.txt

View file

@ -0,0 +1 @@
6e1a93e32fb44081a401f3db3ef2e6e108b7bbeeb5705afdaf01fb27 input.txt

View file

@ -0,0 +1 @@
09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b input.txt

View file

@ -0,0 +1 @@
1fcdb6059ce05172a26bbe2a3ccc88ed5a8cd5fc53edfd9053304d429296a6da23b1cd9e5c9ed3bb34f00418a70cdb7e input.txt

View file

@ -0,0 +1 @@
927b362eaf84a75785bbec3370d1c9711349e93f1104eda060784221 input.txt

View file

@ -0,0 +1 @@
bfb3959527d7a3f2f09def2f6915452d55a8f122df9e164d6f31c7fcf6093e14 input.txt

View file

@ -0,0 +1 @@
fbd0c5931195aaa9517869972b372f717bb69f7f9f72bfc0884ed0531c36a16fc2db5dd6d82131968b23ffe0e90757e5 input.txt

View file

@ -0,0 +1 @@
2ed3a863a12e2f8ff140aa86232ff3603a7f24af62f0e2ca74672494ade175a9a3de42a351b5019d931a1deae0499609038d9b47268779d76198e1d410d20974 input.txt

View file

@ -0,0 +1 @@
8710339dcb6814d0d9d2290ef422285c9322b7163951f9a0ca8f883d3305286f44139aa374848e4174f5aada663027e4548637b6d19894aec4fb6c46a139fbf9 input.txt

View file

@ -0,0 +1 @@
83d41db453072caa9953f2f316480fbbcb84a5f3505460a18b3a36a814ae8e9e input.txt

View file

@ -0,0 +1 @@
7c9896ea84a2a1b80b2183a3f2b4e43cd59b7d48471dc213bcedaccb699d6e6f7ad5d304928ab79329f1fc62f6db072d95b51209eb807683f5c9371872a2dd4e input.txt