Skip to content

Commit ac72aa8

Browse files
RenjiSanncakebaker
authored andcommitted
test(cksum): Factorized sha2/sha3 tests
1 parent 06bc9ad commit ac72aa8

1 file changed

Lines changed: 115 additions & 236 deletions

File tree

tests/by-util/test_cksum.rs

Lines changed: 115 additions & 236 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ use rstest_reuse::{apply, template};
1010
use uutests::at_and_ucmd;
1111
use uutests::new_ucmd;
1212
use uutests::util::TestScenario;
13+
use uutests::util::UCommand;
1314
use uutests::util::log_info;
1415
use uutests::util_name;
1516

16-
const SHA_LENGTHS: [u32; 4] = [224, 256, 384, 512];
17-
1817
#[template]
1918
#[rstest]
2019
#[case::sysv("sysv")]
@@ -31,6 +30,18 @@ const SHA_LENGTHS: [u32; 4] = [224, 256, 384, 512];
3130
#[case::sm3("sm3")]
3231
fn test_all_algos(#[case] algo: &str) {}
3332

33+
#[template]
34+
#[rstest]
35+
fn test_sha(#[values("sha2", "sha3")] algo: &str, #[values(224, 256, 384, 512)] len: u32) {}
36+
37+
fn sha_fixture_name(algo: &str, len: u32, prefix: &str, suffix: &str) -> String {
38+
// assume algo is always "sha2" or "sha3"
39+
match algo {
40+
"sha2" => format!("{prefix}sha{len}{suffix}"),
41+
_ => format!("{prefix}sha3_{len}{suffix}"),
42+
}
43+
}
44+
3445
#[test]
3546
fn test_invalid_arg() {
3647
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
@@ -375,131 +386,126 @@ fn test_sha_missing_length() {
375386
}
376387
}
377388

378-
#[test]
379-
fn test_sha2_single_file() {
380-
for l in SHA_LENGTHS {
381-
new_ucmd!()
382-
.arg("--algorithm=sha2")
383-
.arg(format!("--length={l}"))
384-
.arg("lorem_ipsum.txt")
385-
.succeeds()
386-
.stdout_is_fixture(format!("sha{l}_single_file.expected"));
387-
}
389+
fn sha_cmd(algo: &str, len: u32) -> UCommand {
390+
let mut ucmd = new_ucmd!();
391+
ucmd.arg(format!("--algorithm={algo}"))
392+
.arg(format!("--length={len}"));
393+
ucmd
388394
}
389395

390-
#[test]
391-
fn test_sha2_multiple_files() {
392-
for l in SHA_LENGTHS {
393-
new_ucmd!()
394-
.arg("--algorithm=sha2")
395-
.arg(format!("--length={l}"))
396-
.arg("lorem_ipsum.txt")
397-
.arg("alice_in_wonderland.txt")
398-
.succeeds()
399-
.stdout_is_fixture(format!("sha{l}_multiple_files.expected"));
400-
}
396+
#[apply(test_sha)]
397+
fn test_sha_single_file(algo: &str, len: u32) {
398+
sha_cmd(algo, len)
399+
.arg("lorem_ipsum.txt")
400+
.succeeds()
401+
.stdout_is_fixture(sha_fixture_name(algo, len, "", "_single_file.expected"));
401402
}
402403

403-
#[test]
404-
fn test_sha2_stdin() {
405-
for l in SHA_LENGTHS {
406-
new_ucmd!()
407-
.arg("--algorithm=sha2")
408-
.arg(format!("--length={l}"))
409-
.pipe_in_fixture("lorem_ipsum.txt")
410-
.succeeds()
411-
.stdout_is_fixture(format!("sha{l}_stdin.expected"));
412-
}
404+
#[apply(test_sha)]
405+
fn test_sha_multiple_files(algo: &str, len: u32) {
406+
sha_cmd(algo, len)
407+
.arg("lorem_ipsum.txt")
408+
.arg("alice_in_wonderland.txt")
409+
.succeeds()
410+
.stdout_is_fixture(sha_fixture_name(algo, len, "", "_multiple_files.expected"));
413411
}
414412

415-
#[test]
416-
fn test_untagged_sha2_single_file() {
417-
for l in SHA_LENGTHS {
418-
new_ucmd!()
419-
.arg("--untagged")
420-
.arg("--algorithm=sha2")
421-
.arg(format!("--length={l}"))
422-
.arg("lorem_ipsum.txt")
423-
.succeeds()
424-
.stdout_is_fixture(format!("untagged/sha{l}_single_file.expected"));
425-
}
413+
#[apply(test_sha)]
414+
fn test_sha_stdin(algo: &str, len: u32) {
415+
sha_cmd(algo, len)
416+
.pipe_in_fixture("lorem_ipsum.txt")
417+
.succeeds()
418+
.stdout_is_fixture(sha_fixture_name(algo, len, "", "_stdin.expected"));
426419
}
427420

428-
#[test]
429-
fn test_untagged_sha2_multiple_files() {
430-
for l in SHA_LENGTHS {
431-
new_ucmd!()
432-
.arg("--untagged")
433-
.arg("--algorithm=sha2")
434-
.arg(format!("--length={l}"))
435-
.arg("lorem_ipsum.txt")
436-
.arg("alice_in_wonderland.txt")
437-
.succeeds()
438-
.stdout_is_fixture(format!("untagged/sha{l}_multiple_files.expected"));
439-
}
421+
#[apply(test_sha)]
422+
fn test_untagged_sha_single_file(algo: &str, len: u32) {
423+
sha_cmd(algo, len)
424+
.arg("--untagged")
425+
.arg("lorem_ipsum.txt")
426+
.succeeds()
427+
.stdout_is_fixture(sha_fixture_name(
428+
algo,
429+
len,
430+
"untagged/",
431+
"_single_file.expected",
432+
));
440433
}
441434

442-
#[test]
443-
fn test_untagged_sha2_stdin() {
444-
for l in SHA_LENGTHS {
445-
new_ucmd!()
446-
.arg("--untagged")
447-
.arg("--algorithm=sha2")
448-
.arg(format!("--length={l}"))
449-
.pipe_in_fixture("lorem_ipsum.txt")
450-
.succeeds()
451-
.stdout_is_fixture(format!("untagged/sha{l}_stdin.expected"));
452-
}
435+
#[apply(test_sha)]
436+
fn test_untagged_sha_multiple_files(algo: &str, len: u32) {
437+
sha_cmd(algo, len)
438+
.arg("--untagged")
439+
.arg("lorem_ipsum.txt")
440+
.arg("alice_in_wonderland.txt")
441+
.succeeds()
442+
.stdout_is_fixture(sha_fixture_name(
443+
algo,
444+
len,
445+
"untagged/",
446+
"_multiple_files.expected",
447+
));
453448
}
454449

455-
#[test]
456-
fn test_check_tagged_sha2_single_file() {
457-
for l in SHA_LENGTHS {
458-
new_ucmd!()
459-
.arg("--check")
460-
.arg(format!("sha{l}_single_file.expected"))
461-
.succeeds()
462-
.stdout_is("lorem_ipsum.txt: OK\n");
463-
}
450+
#[apply(test_sha)]
451+
fn test_untagged_sha_stdin(algo: &str, len: u32) {
452+
sha_cmd(algo, len)
453+
.arg("--untagged")
454+
.pipe_in_fixture("lorem_ipsum.txt")
455+
.succeeds()
456+
.stdout_is_fixture(sha_fixture_name(algo, len, "untagged/", "_stdin.expected"));
464457
}
465458

466-
#[test]
467-
fn test_check_tagged_sha2_multiple_files() {
468-
for l in SHA_LENGTHS {
469-
new_ucmd!()
470-
.arg("--check")
471-
.arg(format!("sha{l}_multiple_files.expected"))
472-
.succeeds()
473-
.stdout_contains("lorem_ipsum.txt: OK\n")
474-
.stdout_contains("alice_in_wonderland.txt: OK\n");
475-
}
459+
#[apply(test_sha)]
460+
fn test_check_tagged_sha_single_file(algo: &str, len: u32) {
461+
new_ucmd!()
462+
.arg("--check")
463+
.arg(sha_fixture_name(algo, len, "", "_single_file.expected"))
464+
.succeeds()
465+
.stdout_is("lorem_ipsum.txt: OK\n");
476466
}
477467

478-
// When checking sha2 in untagged mode, the length is automatically deduced
479-
// from the length of the digest.
480-
#[test]
481-
fn test_check_untagged_sha2_single_file() {
482-
for l in SHA_LENGTHS {
483-
new_ucmd!()
484-
.arg("--check")
485-
.arg("--algorithm=sha2")
486-
.arg(format!("untagged/sha{l}_single_file.expected"))
487-
.succeeds()
488-
.stdout_is("lorem_ipsum.txt: OK\n");
489-
}
468+
#[apply(test_sha)]
469+
fn test_check_tagged_sha_multiple_files(algo: &str, len: u32) {
470+
new_ucmd!()
471+
.arg("--check")
472+
.arg(sha_fixture_name(algo, len, "", "_multiple_files.expected"))
473+
.succeeds()
474+
.stdout_contains("lorem_ipsum.txt: OK\n")
475+
.stdout_contains("alice_in_wonderland.txt: OK\n");
490476
}
491477

492-
#[test]
493-
fn test_check_untagged_sha2_multiple_files() {
494-
for l in SHA_LENGTHS {
495-
new_ucmd!()
496-
.arg("--check")
497-
.arg("--algorithm=sha2")
498-
.arg(format!("untagged/sha{l}_multiple_files.expected"))
499-
.succeeds()
500-
.stdout_contains("lorem_ipsum.txt: OK\n")
501-
.stdout_contains("alice_in_wonderland.txt: OK\n");
502-
}
478+
// When checking sha2/sha3 in untagged mode, the length is automatically
479+
// deduced from the length of the digest.
480+
#[apply(test_sha)]
481+
fn test_check_untagged_sha_single_file(algo: &str, len: u32) {
482+
new_ucmd!()
483+
.arg("--check")
484+
.arg(format!("--algorithm={algo}"))
485+
.arg(sha_fixture_name(
486+
algo,
487+
len,
488+
"untagged/",
489+
"_single_file.expected",
490+
))
491+
.succeeds()
492+
.stdout_is("lorem_ipsum.txt: OK\n");
493+
}
494+
495+
#[apply(test_sha)]
496+
fn test_check_untagged_sha_multiple_files(algo: &str, len: u32) {
497+
new_ucmd!()
498+
.arg("--check")
499+
.arg(format!("--algorithm={algo}"))
500+
.arg(sha_fixture_name(
501+
algo,
502+
len,
503+
"untagged/",
504+
"_multiple_files.expected",
505+
))
506+
.succeeds()
507+
.stdout_contains("lorem_ipsum.txt: OK\n")
508+
.stdout_contains("alice_in_wonderland.txt: OK\n");
503509
}
504510

505511
#[test]
@@ -556,133 +562,6 @@ fn test_check_sha2_tagged_variant() {
556562
}
557563
}
558564

559-
#[test]
560-
fn test_sha3_single_file() {
561-
for l in SHA_LENGTHS {
562-
new_ucmd!()
563-
.arg("--algorithm=sha3")
564-
.arg(format!("--length={l}"))
565-
.arg("lorem_ipsum.txt")
566-
.succeeds()
567-
.stdout_is_fixture(format!("sha3_{l}_single_file.expected"));
568-
}
569-
}
570-
571-
#[test]
572-
fn test_sha3_multiple_files() {
573-
for l in SHA_LENGTHS {
574-
new_ucmd!()
575-
.arg("--algorithm=sha3")
576-
.arg(format!("--length={l}"))
577-
.arg("lorem_ipsum.txt")
578-
.arg("alice_in_wonderland.txt")
579-
.succeeds()
580-
.stdout_is_fixture(format!("sha3_{l}_multiple_files.expected"));
581-
}
582-
}
583-
584-
#[test]
585-
fn test_sha3_stdin() {
586-
for l in SHA_LENGTHS {
587-
new_ucmd!()
588-
.arg("--algorithm=sha3")
589-
.arg(format!("--length={l}"))
590-
.pipe_in_fixture("lorem_ipsum.txt")
591-
.succeeds()
592-
.stdout_is_fixture(format!("sha3_{l}_stdin.expected"));
593-
}
594-
}
595-
596-
#[test]
597-
fn test_untagged_sha3_single_file() {
598-
for l in SHA_LENGTHS {
599-
new_ucmd!()
600-
.arg("--untagged")
601-
.arg("--algorithm=sha3")
602-
.arg(format!("--length={l}"))
603-
.arg("lorem_ipsum.txt")
604-
.succeeds()
605-
.stdout_is_fixture(format!("untagged/sha3_{l}_single_file.expected"));
606-
}
607-
}
608-
609-
#[test]
610-
fn test_untagged_sha3_multiple_files() {
611-
for l in SHA_LENGTHS {
612-
new_ucmd!()
613-
.arg("--untagged")
614-
.arg("--algorithm=sha3")
615-
.arg(format!("--length={l}"))
616-
.arg("lorem_ipsum.txt")
617-
.arg("alice_in_wonderland.txt")
618-
.succeeds()
619-
.stdout_is_fixture(format!("untagged/sha3_{l}_multiple_files.expected"));
620-
}
621-
}
622-
623-
#[test]
624-
fn test_untagged_sha3_stdin() {
625-
for l in SHA_LENGTHS {
626-
new_ucmd!()
627-
.arg("--untagged")
628-
.arg("--algorithm=sha3")
629-
.arg(format!("--length={l}"))
630-
.pipe_in_fixture("lorem_ipsum.txt")
631-
.succeeds()
632-
.stdout_is_fixture(format!("untagged/sha3_{l}_stdin.expected"));
633-
}
634-
}
635-
636-
#[test]
637-
fn test_check_tagged_sha3_single_file() {
638-
for l in SHA_LENGTHS {
639-
new_ucmd!()
640-
.arg("--check")
641-
.arg(format!("sha3_{l}_single_file.expected"))
642-
.succeeds()
643-
.stdout_is("lorem_ipsum.txt: OK\n");
644-
}
645-
}
646-
647-
#[test]
648-
fn test_check_tagged_sha3_multiple_files() {
649-
for l in SHA_LENGTHS {
650-
new_ucmd!()
651-
.arg("--check")
652-
.arg(format!("sha3_{l}_multiple_files.expected"))
653-
.succeeds()
654-
.stdout_contains("lorem_ipsum.txt: OK\n")
655-
.stdout_contains("alice_in_wonderland.txt: OK\n");
656-
}
657-
}
658-
659-
// When checking sha3 in untagged mode, the length is automatically deduced
660-
// from the length of the digest.
661-
#[test]
662-
fn test_check_untagged_sha3_single_file() {
663-
for l in SHA_LENGTHS {
664-
new_ucmd!()
665-
.arg("--check")
666-
.arg("--algorithm=sha3")
667-
.arg(format!("untagged/sha3_{l}_single_file.expected"))
668-
.succeeds()
669-
.stdout_is("lorem_ipsum.txt: OK\n");
670-
}
671-
}
672-
673-
#[test]
674-
fn test_check_untagged_sha3_multiple_files() {
675-
for l in SHA_LENGTHS {
676-
new_ucmd!()
677-
.arg("--check")
678-
.arg("--algorithm=sha3")
679-
.arg(format!("untagged/sha3_{l}_multiple_files.expected"))
680-
.succeeds()
681-
.stdout_contains("lorem_ipsum.txt: OK\n")
682-
.stdout_contains("alice_in_wonderland.txt: OK\n");
683-
}
684-
}
685-
686565
#[test]
687566
fn test_check_algo() {
688567
for algo in ["bsd", "sysv", "crc", "crc32b"] {

0 commit comments

Comments
 (0)