Skip to content

Commit

Permalink
test(cksum): add multiple tests
Browse files Browse the repository at this point in the history
test(cksum): add test for blake length gessing
test(cksum): add test for hexa/base64 confusion
test(cksum): add test for error handling on incorrectly formatted checksum
test(cksum): add test for trailing spaces making a line improperly formatted
  • Loading branch information
RenjiSann committed Aug 31, 2024
1 parent 65b8e31 commit a5e11b4
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,29 @@ fn test_cksum_check_space() {
.stderr_contains("line is improperly formatted");
}

#[test]
fn test_cksum_check_trailing_space() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.touch("f");
at.write("CHECKSUM",
&[
"SHA384 (f) = 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b\n",
"BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n",
"BLAKE2b-384 (f) = b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100\n",
// This line is improperly formatted because it has trailing spaces
"SM3 (f) = 1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b \n"
].join(""));
scene
.ucmd()
.arg("--check")
.arg("CHECKSUM")
.succeeds()
.stdout_contains("f: OK\nf: OK\nf: OK\n")
.stderr_contains("line is improperly formatted");
}

#[test]
fn test_cksum_check_leading_info() {
let scene = TestScenario::new(util_name!());
Expand Down Expand Up @@ -1301,3 +1324,122 @@ fn test_non_utf8_comment() {
.succeeds()
.stdout_is("empty: OK\nempty: OK\nempty: OK\n");
}

#[test]
fn test_check_blake_length_guess() {
let correct_lines = [
// Correct: The length is not explicit, but the checksum's size
// matches the default parameter.
"BLAKE2b (foo.dat) = ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d",
// Correct: The length is explicitly given, and the checksum's size
// matches the length.
"BLAKE2b-512 (foo.dat) = ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d",
// Correct: the checksum size is not default but
// the length is explicitly given.
"BLAKE2b-48 (foo.dat) = 171cdfdf84ed",
];
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("foo.dat", "foo");

for line in correct_lines {
at.write("foo.sums", line);
scene
.ucmd()
.arg("--check")
.arg(at.subdir.join("foo.sums"))
.succeeds()
.stdout_is("foo.dat: OK\n");
}

// Incorrect lines

// This is incorrect because the algorithm provides no length,
// and the checksum length is not default.
let incorrect = "BLAKE2b (foo.dat) = 171cdfdf84ed";
at.write("foo.sums", &incorrect);
scene
.ucmd()
.arg("--check")
.arg(at.subdir.join("foo.sums"))
.fails()
.stderr_contains("foo.sums: no properly formatted checksum lines found");
}

#[test]
fn test_check_confusing_base64() {
let cksum = "BLAKE2b-48 (foo.dat) = fc1f97C4";

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("foo.dat", "esq");
at.write("foo.sums", cksum);

scene
.ucmd()
.arg("--check")
.arg(at.subdir.join("foo.sums"))
.succeeds()
.stdout_is("foo.dat: OK\n");
}

/// This test checks that when a file contains several checksum lines
/// with different encoding, the decoding still works.
#[test]
fn test_check_mix_hexa_base64() {
let b64 = "BLAKE2b-128 (foo1.dat) = BBNuJPhdRwRlw9tm5Y7VbA==";
let hex = "BLAKE2b-128 (foo2.dat) = 04136e24f85d470465c3db66e58ed56c";

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("foo1.dat", "foo");
at.write("foo2.dat", "foo");

at.write("hex_b64", &format!("{}\n{}", hex, b64));
at.write("b64_hex", &format!("{}\n{}", b64, hex));

scene
.ucmd()
.arg("--check")
.arg(at.subdir.join("hex_b64"))
.succeeds()
.stdout_is("foo2.dat: OK\nfoo1.dat: OK\n")
.no_stderr();

scene
.ucmd()
.arg("--check")
.arg(at.subdir.join("b64_hex"))
.succeeds()
.stdout_is("foo1.dat: OK\nfoo2.dat: OK\n")
.no_stderr();
}

#[test]
fn test_check_incorrectly_formatted_checksum_does_not_stop_processing() {
// The first line contains an incorrectly formatted checksum that can't be
// correctly decoded. This must not prevent the program from looking at the
// rest of the file.
let lines = [
"BLAKE2b-56 (foo1) = GFYEQ7HhAw=", // Should be 2 '=' at the end
"BLAKE2b-56 (foo2) = 18560443b1e103", // OK
];

let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;

at.write("foo1", "foo");
at.write("foo2", "foo");
at.write("sum", &lines.join("\n"));

scene
.ucmd()
.arg("--check")
.arg(at.subdir.join("sum"))
.succeeds()
.stderr_contains("1 line is improperly formatted")
.stdout_contains("foo2: OK");
}

0 comments on commit a5e11b4

Please sign in to comment.