Skip to content

cksum/hashsum: Support for commented lines in checksum files #6782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/uucore/src/lib/features/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
67 changes: 67 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
68 changes: 68 additions & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading