Skip to content

Commit abd438f

Browse files
committed
test(cksum): rework test for improperly formatted keeps processing
1 parent 32cf264 commit abd438f

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

tests/by-util/test_cksum.rs

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,12 +1403,13 @@ fn test_check_trailing_space_fails() {
14031403
/// in checksum files.
14041404
/// These tests are excluded from Windows because it does not provide any safe
14051405
/// conversion between `OsString` and byte sequences for non-utf-8 strings.
1406-
#[cfg(not(windows))]
14071406
mod check_utf8 {
1408-
use super::*;
14091407

1408+
// This test should pass on linux and macos.
1409+
#[cfg(not(windows))]
14101410
#[test]
14111411
fn test_check_non_utf8_comment() {
1412+
use super::*;
14121413
let hashes =
14131414
b"MD5 (empty) = 1B2M2Y8AsgTpgAmY7PhCfg==\n\
14141415
# Comment with a non utf8 char: >>\xff<<\n\
@@ -1431,8 +1432,12 @@ mod check_utf8 {
14311432
.no_stderr();
14321433
}
14331434

1435+
// This test should pass on linux. Windows and macos will fail to
1436+
// create a file which name contains '\xff'.
1437+
#[cfg(target_os = "linux")]
14341438
#[test]
14351439
fn test_check_non_utf8_filename() {
1440+
use super::*;
14361441
use std::{ffi::OsString, os::unix::ffi::OsStringExt};
14371442

14381443
let scene = TestScenario::new(util_name!());
@@ -1569,35 +1574,68 @@ fn test_check_mix_hex_base64() {
15691574
.stdout_only("foo1.dat: OK\nfoo2.dat: OK\n");
15701575
}
15711576

1572-
#[ignore = "not yet implemented"]
1577+
/// This test ensures that an improperly formatted base64 checksum in a file
1578+
/// does not interrupt the processing of next lines.
15731579
#[test]
1574-
fn test_check_incorrectly_formatted_checksum_does_not_stop_processing() {
1575-
// The first line contains an incorrectly formatted checksum that can't be
1576-
// correctly decoded. This must not prevent the program from looking at the
1577-
// rest of the file.
1578-
let lines = [
1579-
"BLAKE2b-56 (foo1) = GFYEQ7HhAw=", // Should be 2 '=' at the end
1580-
"BLAKE2b-56 (foo2) = 18560443b1e103", // OK
1581-
];
1580+
fn test_check_incorrectly_formatted_checksum_keeps_processing_b64() {
1581+
let scene = TestScenario::new(util_name!());
1582+
let at = &scene.fixtures;
1583+
at.touch("f");
1584+
1585+
let good_ck = "MD5 (f) = 1B2M2Y8AsgTpgAmY7PhCfg=="; // OK
1586+
let bad_ck = "MD5 (f) = 1B2M2Y8AsgTpgAmY7PhCfg="; // Missing last '='
1587+
1588+
// Good then Bad
1589+
scene
1590+
.ucmd()
1591+
.arg("--check")
1592+
.pipe_in([good_ck, bad_ck].join("\n").as_bytes().to_vec())
1593+
.succeeds()
1594+
.stdout_contains("f: OK")
1595+
.stderr_contains("cksum: WARNING: 1 line is improperly formatted");
15821596

1597+
// Bad then Good
1598+
scene
1599+
.ucmd()
1600+
.arg("--check")
1601+
.pipe_in([bad_ck, good_ck].join("\n").as_bytes().to_vec())
1602+
.succeeds()
1603+
.stdout_contains("f: OK")
1604+
.stderr_contains("cksum: WARNING: 1 line is improperly formatted");
1605+
}
1606+
1607+
/// This test ensures that an improperly formatted hexadecimal checksum in a
1608+
/// file does not interrupt the processing of next lines.
1609+
#[test]
1610+
fn test_check_incorrectly_formatted_checksum_keeps_processing_hex() {
15831611
let scene = TestScenario::new(util_name!());
15841612
let at = &scene.fixtures;
1613+
at.touch("f");
1614+
1615+
let good_ck = "MD5 (f) = d41d8cd98f00b204e9800998ecf8427e"; // OK
1616+
let bad_ck = "MD5 (f) = d41d8cd98f00b204e9800998ecf8427"; // Missing last
15851617

1586-
at.write("foo1", "foo");
1587-
at.write("foo2", "foo");
1588-
at.write("sum", &lines.join("\n"));
1618+
// Good then Bad
1619+
scene
1620+
.ucmd()
1621+
.arg("--check")
1622+
.pipe_in([good_ck, bad_ck].join("\n").as_bytes().to_vec())
1623+
.succeeds()
1624+
.stdout_contains("f: OK")
1625+
.stderr_contains("cksum: WARNING: 1 line is improperly formatted");
15891626

1627+
// Bad then Good
15901628
scene
15911629
.ucmd()
15921630
.arg("--check")
1593-
.arg(at.subdir.join("sum"))
1631+
.pipe_in([bad_ck, good_ck].join("\n").as_bytes().to_vec())
15941632
.succeeds()
1595-
.stderr_contains("1 line is improperly formatted")
1596-
.stdout_contains("foo2: OK");
1633+
.stdout_contains("f: OK")
1634+
.stderr_contains("cksum: WARNING: 1 line is improperly formatted");
15971635
}
15981636

15991637
/// This module reimplements the cksum-base64.pl GNU test.
1600-
mod cksum_base64 {
1638+
mod gnu_cksum_base64 {
16011639
use super::*;
16021640
use crate::common::util::log_info;
16031641

0 commit comments

Comments
 (0)