diff --git a/src/uucore/src/lib/features/checksum.rs b/src/uucore/src/lib/features/checksum.rs index c55d56393..27fbc7bf1 100644 --- a/src/uucore/src/lib/features/checksum.rs +++ b/src/uucore/src/lib/features/checksum.rs @@ -575,8 +575,8 @@ where res.failed_cksum += 1; } } else { - if line.is_empty() { - // Don't show any warning for empty lines + if line.is_empty() || line.starts_with("#") { + // Don't show any warning for empty or commented lines. continue; } if warn { diff --git a/tests/by-util/test_cksum.rs b/tests/by-util/test_cksum.rs index 117e54e1e..507c2dffb 100644 --- a/tests/by-util/test_cksum.rs +++ b/tests/by-util/test_cksum.rs @@ -1277,3 +1277,70 @@ fn test_non_utf8_filename() { .stdout_is_bytes(b"SHA256 (funky\xffname) = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n") .no_stderr(); } + +#[test] +fn test_check_comment_line() { + // A comment in a checksum file shall be discarded unnoticed. + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("foo", "foo-content\n"); + at.write( + "CHECKSUM-sha1", + "\ + # This is a comment\n\ + SHA1 (foo) = 058ab38dd3603703b3a7063cf95dc51a4286b6fe\n\ + # next comment is empty\n#", + ); + + scene + .ucmd() + .arg("--check") + .arg("CHECKSUM-sha1") + .succeeds() + .stdout_contains("foo: OK") + .no_stderr(); +} + +#[test] +fn test_check_comment_only() { + // A file only filled with comments is equivalent to an empty file, + // and therefore produces an error. + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("CHECKSUM", "# This is a comment\n"); + + scene + .ucmd() + .arg("--check") + .arg("CHECKSUM") + .fails() + .stderr_contains("no properly formatted checksum lines found"); +} + +#[test] +fn test_check_comment_leading_space() { + // A comment must have its '#' in first position on the line. + // A space before it will raise a warning for improperly formatted line. + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("foo", "foo-content\n"); + at.write( + "CHECKSUM-sha1", + " # This is a comment\n\ + SHA1 (foo) = 058ab38dd3603703b3a7063cf95dc51a4286b6fe\n", + ); + + scene + .ucmd() + .arg("--check") + .arg("CHECKSUM-sha1") + .succeeds() + .stdout_contains("foo: OK") + .stderr_contains("WARNING: 1 line is improperly formatted"); +} diff --git a/tests/by-util/test_hashsum.rs b/tests/by-util/test_hashsum.rs index 28a27adae..533f0b7e7 100644 --- a/tests/by-util/test_hashsum.rs +++ b/tests/by-util/test_hashsum.rs @@ -939,3 +939,71 @@ fn test_check_b2sum_strict_check() { .succeeds() .stdout_only(&output); } + +#[test] +fn test_check_md5_comment_line() { + // A comment in a checksum file shall be discarded unnoticed. + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("foo", "foo-content\n"); + at.write( + "MD5SUM", + "\ + # This is a comment\n\ + 8411029f3f5b781026a93db636aca721 foo\n\ + # next comment is empty\n#", + ); + + scene + .ccmd("md5sum") + .arg("--check") + .arg("MD5SUM") + .succeeds() + .stdout_contains("foo: OK") + .no_stderr(); +} + +#[test] +fn test_check_md5_comment_only() { + // A file only filled with comments is equivalent to an empty file, + // and therefore produces an error. + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("foo", "foo-content\n"); + at.write("MD5SUM", "# This is a comment\n"); + + scene + .ccmd("md5sum") + .arg("--check") + .arg("MD5SUM") + .fails() + .stderr_contains("no properly formatted checksum lines found"); +} + +#[test] +fn test_check_md5_comment_leading_space() { + // A file only filled with comments is equivalent to an empty file, + // and therefore produces an error. + + let scene = TestScenario::new(util_name!()); + let at = &scene.fixtures; + + at.write("foo", "foo-content\n"); + at.write( + "MD5SUM", + " # This is a comment\n\ + 8411029f3f5b781026a93db636aca721 foo\n", + ); + + scene + .ccmd("md5sum") + .arg("--check") + .arg("MD5SUM") + .succeeds() + .stdout_contains("foo: OK") + .stderr_contains("WARNING: 1 line is improperly formatted"); +}