cksum/hashsum: manage the '*' start correctly

This commit is contained in:
Sylvestre Ledru 2024-05-28 23:51:12 +02:00
parent 84d90fcbdf
commit 1cf6700023
2 changed files with 32 additions and 7 deletions

View file

@ -308,7 +308,7 @@ const ALGO_BASED_REGEX: &str = r"^\s*\\?(?P<algo>(?:[A-Z0-9]+|BLAKE2b))(?:-(?P<b
const DOUBLE_SPACE_REGEX: &str = r"^(?P<checksum>[a-fA-F0-9]+)\s{2}(?P<filename>.*)$";
// In this case, we ignore the *
const SINGLE_SPACE_REGEX: &str = r"^(?P<checksum>[a-fA-F0-9]+)\s(?P<binary>\*?)(?P<filename>.*)$";
const SINGLE_SPACE_REGEX: &str = r"^(?P<checksum>[a-fA-F0-9]+)\s(?P<filename>\*?.*)$";
/// Determines the appropriate regular expression to use based on the provided lines.
fn determine_regex(filename: &OsStr, lines: &[String]) -> UResult<(Regex, bool)> {
@ -336,6 +336,7 @@ fn determine_regex(filename: &OsStr, lines: &[String]) -> UResult<(Regex, bool)>
)
.into())
}
/***
* Do the checksum validation (can be strict or not)
*/
@ -390,7 +391,15 @@ where
if let Some(caps) = chosen_regex.captures(&line) {
properly_formatted = true;
let filename_to_check = caps.name("filename").unwrap().as_str();
// Get the filename to check and remove the leading asterisk if present
let mut filename_to_check = caps.name("filename").unwrap().as_str();
if filename_to_check.starts_with('*')
&& i == 0
&& chosen_regex.as_str() == SINGLE_SPACE_REGEX
{
// Remove the leading asterisk if present - only for the first line
filename_to_check = &filename_to_check[1..];
}
let expected_checksum = caps.name("checksum").unwrap().as_str();
@ -866,7 +875,7 @@ mod tests {
),
(
"f5b61709718c1ecf8db1aea8547d4698 *c",
Some(("f5b61709718c1ecf8db1aea8547d4698", "c")),
Some(("f5b61709718c1ecf8db1aea8547d4698", "*c")),
),
(
"b064a020db8018f18ff5ae367d01b212 dd",

View file

@ -378,7 +378,7 @@ fn test_check_md5sum_only_one_space() {
"check.md5sum",
"60b725f10c9c85c70d97880dfe8191b3 a\n\
bf35d7536c785cf06730d5a40301eba2 b\n\
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 *c\n",
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 c\n",
);
scene
.ccmd("md5sum")
@ -758,9 +758,9 @@ fn test_check_space_star_or_not() {
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stdout_contains("a: FAILED")
.stdout_contains("*c: FAILED");
.succeeds()
.stdout_contains("a: OK")
.stderr_contains("WARNING: 1 line is improperly formatted");
}
#[test]
@ -868,3 +868,19 @@ fn test_check_quiet() {
.stdout_contains("f: FAILED")
.stderr_contains("WARNING: 1 computed checksum did NOT match");
}
#[test]
fn test_star_to_start() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e */dev/null\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stderr_is("")
.stdout_is("/dev/null: OK\n");
}