Skip to content
Closed
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
8 changes: 8 additions & 0 deletions src/uucore/src/lib/features/checksum/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ impl LineFormat {
// tagged format does not put a space before (filename)

let par_idx = rest.iter().position(|&b| b == b'(')?;
if par_idx < 1 {
return None;
}
let sub_case = if rest[par_idx - 1] == b' ' {
SubCase::Posix
} else {
Expand Down Expand Up @@ -1024,6 +1027,7 @@ mod tests {
#[test]
fn test_algo_based_parser() {
#[allow(clippy::type_complexity)]
// Tuple format: (input line, option(algo name, option(algo bit size), filename, checksum)
let test_cases: &[(&[u8], Option<(&[u8], Option<&[u8]>, &[u8], &[u8])>)] = &[
(b"SHA256 (example.txt) = d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2", Some((b"SHA256", None, b"example.txt", b"d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2"))),
// cspell:disable
Expand All @@ -1041,6 +1045,10 @@ mod tests {
(b" MD5(weirdfilename6) = ) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b" MD5 (weirdfilename7)= )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b" MD5 (weirdfilename8) = )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
// test for missing algorithm
(b"(filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b"filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
(b"filename = fds65dsf46as5df4d6f54asds5d7f7g9", None),
];

// cspell:enable
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,24 @@ fn test_check_sha2_tagged_missing_hint() {
.stderr_contains("no properly formatted checksum lines found");
}

#[test]
fn test_check_tagged_missing_algo() {
// When checking tagged lines, if the algorithm is missing, raise "improperly
// formatted" rather than a failed check.

let (at, mut ucmd) = at_and_ucmd!();
at.touch("a");

let invalid1 = "(a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
let invalid2 = "a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
let invalid3 = "(a = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";

ucmd.arg("-c")
.pipe_in(format!("{invalid1}\n{invalid2}\n{invalid3}"))
.fails()
.stderr_contains("no properly formatted checksum lines found");
}

#[rstest]
#[case::md5("md5", "d41d8cd98f00b204e9800998ecf8427e")]
#[case::sha1("sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]
Expand Down
Loading