Skip to content

Commit f3d5f38

Browse files
committed
cksum: Add tests to fix issue with cksum parser
The 'cksum' (sha256sum, etc...) parser panic when parsing files in the "tagged output format If it encounters a line that does not contain an algorithm name (MD5, SHA256, etc...) Fix this, so that it reports a syntax error like GNU does, and test for it.
1 parent d154c21 commit f3d5f38

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ mod tests {
10271027
#[test]
10281028
fn test_algo_based_parser() {
10291029
#[allow(clippy::type_complexity)]
1030+
// Tuple format: (input line, option(algo name, option(algo bit size), filename, checksum)
10301031
let test_cases: &[(&[u8], Option<(&[u8], Option<&[u8]>, &[u8], &[u8])>)] = &[
10311032
(b"SHA256 (example.txt) = d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2", Some((b"SHA256", None, b"example.txt", b"d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2d2"))),
10321033
// cspell:disable
@@ -1044,6 +1045,10 @@ mod tests {
10441045
(b" MD5(weirdfilename6) = ) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
10451046
(b" MD5 (weirdfilename7)= )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
10461047
(b" MD5 (weirdfilename8) = )= fds65dsf46as5df4d6f54asds5d7f7g9", None),
1048+
// test that missing algorithm
1049+
(b"(filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
1050+
(b"filename) = fds65dsf46as5df4d6f54asds5d7f7g9", None),
1051+
(b"filename = fds65dsf46as5df4d6f54asds5d7f7g9", None),
10471052
];
10481053

10491054
// cspell:enable

tests/by-util/test_cksum.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,24 @@ fn test_check_sha2_tagged_missing_hint() {
627627
.stderr_contains("no properly formatted checksum lines found");
628628
}
629629

630+
#[test]
631+
fn test_check_tagged_missing_algo() {
632+
// When checking tagged lines, if the algorithm is missing, raise "improperly
633+
// formatted" rather than a failed check.
634+
635+
let (at, mut ucmd) = at_and_ucmd!();
636+
at.touch("a");
637+
638+
let invalid1 = "(a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
639+
let invalid2 = "a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
640+
let invalid3 = "(a = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
641+
642+
ucmd.arg("-c")
643+
.pipe_in(format!("{invalid1}\n{invalid2}\n{invalid3}"))
644+
.fails()
645+
.stderr_contains("no properly formatted checksum lines found");
646+
}
647+
630648
#[rstest]
631649
#[case::md5("md5", "d41d8cd98f00b204e9800998ecf8427e")]
632650
#[case::sha1("sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]

0 commit comments

Comments
 (0)