Skip to content

Commit 812eb64

Browse files
RenjiSanncakebaker
authored andcommitted
test(cksum): Add multiple tests on check length digest validation
1 parent 97e69f0 commit 812eb64

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

tests/by-util/test_cksum.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,49 @@ fn test_check_untagged_sha_multiple_files(algo: &str, len: u32) {
508508
.stdout_contains("alice_in_wonderland.txt: OK\n");
509509
}
510510

511+
#[rstest]
512+
#[case::sha2("sha2", &[
513+
"d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
514+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
515+
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
516+
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
517+
])]
518+
#[case::sha3("sha3", &[
519+
"6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7",
520+
"a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
521+
"0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004",
522+
"a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"
523+
])]
524+
fn test_check_untagged_sha_invalid_length(#[case] algo: &str, #[case] digests: &[&str; 4]) {
525+
// When checking with --algorithm sha2/sha3, Guess the length from the provided
526+
// digest. Raise "improperly formatted" if the digest is not af adequate
527+
// size (224, 256, 384 or 512 bits).
528+
529+
let (at, mut ucmd) = at_and_ucmd!();
530+
at.touch("a");
531+
at.touch("b");
532+
at.touch("c");
533+
at.touch("d");
534+
at.touch("e");
535+
536+
let invalid = "xxxx e";
537+
538+
ucmd.arg("-a")
539+
.arg(algo)
540+
.arg("-c")
541+
.pipe_in(format!(
542+
"{} a\n{} b\n{} c\n{} d\n{invalid}",
543+
digests[0], digests[1], digests[2], digests[3]
544+
))
545+
.succeeds()
546+
.stdout_contains("a: OK")
547+
.stdout_contains("b: OK")
548+
.stdout_contains("c: OK")
549+
.stdout_contains("d: OK")
550+
.stdout_does_not_contain("e: FAILED")
551+
.stderr_contains("improperly formatted");
552+
}
553+
511554
#[test]
512555
fn test_check_sha2_tagged_variant() {
513556
let scene = TestScenario::new(util_name!());
@@ -562,6 +605,73 @@ fn test_check_sha2_tagged_variant() {
562605
}
563606
}
564607

608+
#[test]
609+
fn test_check_sha2_tagged_missing_hint() {
610+
// SHA2 (<file>) = <digest>
611+
//
612+
// should fail and raise "improperly formatted" because SHA2 expects a
613+
// mandatory length hint.
614+
615+
let (at, mut ucmd) = at_and_ucmd!();
616+
at.touch("a");
617+
at.touch("b");
618+
619+
// valid digest but missing hint
620+
let invalid_sha224 = "SHA2 (a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
621+
// invalid digest
622+
let invalid = "SHA2 (b) = xxxx";
623+
624+
ucmd.arg("-c")
625+
.pipe_in(format!("{invalid_sha224}\n{invalid}"))
626+
.fails()
627+
.stderr_contains("no properly formatted checksum lines found");
628+
}
629+
630+
#[rstest]
631+
#[case::md5("md5", "d41d8cd98f00b204e9800998ecf8427e")]
632+
#[case::sha1("sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]
633+
#[case::sha224("sha224", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")]
634+
#[case::sha256(
635+
"sha256",
636+
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
637+
)]
638+
#[case::sha384(
639+
"sha384",
640+
"38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"
641+
)]
642+
#[case::sha512(
643+
"sha512",
644+
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
645+
)]
646+
#[case::sm3(
647+
"sm3",
648+
"1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b"
649+
)]
650+
fn test_check_untagged_with_invalid_length(#[case] algo: &str, #[case] digest: &str) {
651+
// issue #11202
652+
653+
// Ensures that when checking untagged lines, if the provided algorithm is
654+
// one of `sha(224|256|384|512)`, digests whose length mismatches the
655+
// length of the given algorithm will report as "improperly formatted"
656+
// rather than a FAILED check.
657+
658+
let (at, mut ucmd) = at_and_ucmd!();
659+
at.touch("a");
660+
at.touch("b");
661+
662+
let good_checksum = format!("{digest} a");
663+
let invalid_checksum = "e3b0 b";
664+
665+
ucmd.arg("-a")
666+
.arg(algo)
667+
.arg("-c")
668+
.pipe_in(format!("{invalid_checksum}\n{good_checksum}"))
669+
.succeeds()
670+
.stdout_contains("a: OK")
671+
.stdout_does_not_contain("b: FAILED")
672+
.stderr_contains("WARNING: 1 line is improperly formatted");
673+
}
674+
565675
#[test]
566676
fn test_check_algo() {
567677
for algo in ["bsd", "sysv", "crc", "crc32b"] {

0 commit comments

Comments
 (0)