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
test(cksum): Re-implement GNU test 'cksum-base64' in the testsuite
  • Loading branch information
RenjiSann committed Oct 27, 2024
1 parent 10c49ee commit 598537a
Showing 1 changed file with 258 additions and 1 deletion.
259 changes: 258 additions & 1 deletion tests/by-util/test_cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (words) asdf algo algos asha mgmt xffname
// spell-checker:ignore (words) asdf algo algos asha mgmt xffname hexa GFYEQ HYQK Yqxb

use crate::common::util::TestScenario;

Expand Down Expand Up @@ -1502,3 +1502,260 @@ mod check_utf8 {
.stderr_contains("1 listed file could not be read");
}
}

#[ignore = "not yet implemented"]
#[test]
fn test_check_blake_length_guess() {
let correct_lines = [

Check warning on line 1509 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1508-L1509

Added lines #L1508 - L1509 were not covered by tests
// 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;

Check warning on line 1521 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1520-L1521

Added lines #L1520 - L1521 were not covered by tests

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

Check warning on line 1523 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1523

Added line #L1523 was not covered by tests

for line in correct_lines {
at.write("foo.sums", line);
scene

Check warning on line 1527 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1526-L1527

Added lines #L1526 - L1527 were not covered by tests
.ucmd()
.arg("--check")
.arg(at.subdir.join("foo.sums"))

Check warning on line 1530 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1530

Added line #L1530 was not covered by tests
.succeeds()
.stdout_is("foo.dat: OK\n");
}

Check warning on line 1533 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1532-L1533

Added lines #L1532 - L1533 were not covered by tests

// 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

Check warning on line 1541 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1539-L1541

Added lines #L1539 - L1541 were not covered by tests
.ucmd()
.arg("--check")
.arg(at.subdir.join("foo.sums"))

Check warning on line 1544 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1544

Added line #L1544 was not covered by tests
.fails()
.stderr_contains("foo.sums: no properly formatted checksum lines found");
}

Check warning on line 1547 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1546-L1547

Added lines #L1546 - L1547 were not covered by tests

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

Check warning on line 1552 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1551-L1552

Added lines #L1551 - L1552 were not covered by tests

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

Check warning on line 1555 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1554-L1555

Added lines #L1554 - L1555 were not covered by tests

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

Check warning on line 1558 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1557-L1558

Added lines #L1557 - L1558 were not covered by tests

scene

Check warning on line 1560 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1560

Added line #L1560 was not covered by tests
.ucmd()
.arg("--check")
.arg(at.subdir.join("foo.sums"))

Check warning on line 1563 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1563

Added line #L1563 was not covered by tests
.succeeds()
.stdout_is("foo.dat: OK\n");
}

Check warning on line 1566 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1565-L1566

Added lines #L1565 - L1566 were not covered by tests

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

Check warning on line 1574 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1572-L1574

Added lines #L1572 - L1574 were not covered by tests

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

Check warning on line 1577 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1576-L1577

Added lines #L1576 - L1577 were not covered by tests

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

Check warning on line 1580 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1579-L1580

Added lines #L1579 - L1580 were not covered by tests

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

Check warning on line 1583 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1582-L1583

Added lines #L1582 - L1583 were not covered by tests

scene

Check warning on line 1585 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1585

Added line #L1585 was not covered by tests
.ucmd()
.arg("--check")
.arg(at.subdir.join("hex_b64"))

Check warning on line 1588 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1588

Added line #L1588 was not covered by tests
.succeeds()
.stdout_only("foo2.dat: OK\nfoo1.dat: OK\n");

Check warning on line 1590 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1590

Added line #L1590 was not covered by tests

scene

Check warning on line 1592 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1592

Added line #L1592 was not covered by tests
.ucmd()
.arg("--check")
.arg(at.subdir.join("b64_hex"))

Check warning on line 1595 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1595

Added line #L1595 was not covered by tests
.succeeds()
.stdout_only("foo1.dat: OK\nfoo2.dat: OK\n");
}

Check warning on line 1598 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1597-L1598

Added lines #L1597 - L1598 were not covered by tests

#[ignore = "not yet implemented"]
#[test]
fn test_check_incorrectly_formatted_checksum_does_not_stop_processing() {

Check warning on line 1602 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1602

Added line #L1602 was not covered by tests
// 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 = [

Check warning on line 1606 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1606

Added line #L1606 was not covered by tests
"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;

Check warning on line 1612 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1611-L1612

Added lines #L1611 - L1612 were not covered by tests

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

Check warning on line 1616 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1614-L1616

Added lines #L1614 - L1616 were not covered by tests

scene

Check warning on line 1618 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1618

Added line #L1618 was not covered by tests
.ucmd()
.arg("--check")
.arg(at.subdir.join("sum"))

Check warning on line 1621 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1621

Added line #L1621 was not covered by tests
.succeeds()
.stderr_contains("1 line is improperly formatted")
.stdout_contains("foo2: OK");
}

Check warning on line 1625 in tests/by-util/test_cksum.rs

View check run for this annotation

Codecov / codecov/patch

tests/by-util/test_cksum.rs#L1624-L1625

Added lines #L1624 - L1625 were not covered by tests

/// This module reimplements the cksum-base64.pl GNU test.
mod cksum_base64 {
use super::*;
use crate::common::util::log_info;

const PAIRS: [(&str, &str); 11] = [
("sysv", "0 0 f"),
("bsd", "00000 0 f"),
("crc", "4294967295 0 f"),
("md5", "1B2M2Y8AsgTpgAmY7PhCfg=="),
("sha1", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="),
("sha224", "0UoCjCo6K8lHYQK7KII0xBWisB+CjqYqxbPkLw=="),
("sha256", "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="),
(
"sha384",
"OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb",
),
(
"sha512",
"z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg=="
),
(
"blake2b",
"eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg=="
),
("sm3", "GrIdg1XPoX+OYRlIMegajyK+yMco/vt0ftA161CCqis="),
];

fn make_scene() -> TestScenario {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");

scene
}

fn output_format(algo: &str, digest: &str) -> String {
if ["sysv", "bsd", "crc"].contains(&algo) {
digest.to_string()
} else {
format!("{} (f) = {}", algo.to_uppercase(), digest).replace("BLAKE2B", "BLAKE2b")
}
}

#[test]
fn test_generating() {
// Ensure that each algorithm works with `--base64`.
let scene = make_scene();

for (algo, digest) in PAIRS {
scene
.ucmd()
.arg("--base64")
.arg("-a")
.arg(algo)
.arg("f")
.succeeds()
.stdout_only(format!("{}\n", output_format(algo, digest)));
}
}

#[test]
fn test_chk() {
// For each algorithm that accepts `--check`,
// ensure that it works with base64 digests.
let scene = make_scene();

for (algo, digest) in PAIRS {
if ["sysv", "bsd", "crc"].contains(&algo) {
// These algorithms do not accept `--check`
continue;
}

let line = output_format(algo, digest);
scene
.ucmd()
.arg("--check")
.arg("--strict")
.pipe_in(line)
.succeeds()
.stdout_only("f: OK\n");
}
}

#[test]
fn test_chk_eq1() {
// For digests ending with '=', ensure `--check` fails if '=' is removed.
let scene = make_scene();

for (algo, digest) in PAIRS {
if !digest.ends_with('=') {
continue;
}

let mut line = output_format(algo, digest);
if line.ends_with('=') {
line.pop();
}

log_info(format!("ALGORITHM: {algo}, STDIN: '{line}'"), "");
scene
.ucmd()
.arg("--check")
.pipe_in(line)
.fails()
.no_stdout()
.stderr_contains("no properly formatted checksum lines found");
}
}

#[test]
fn test_chk_eq2() {
// For digests ending with '==',
// ensure `--check` fails if '==' is removed.
let scene = make_scene();

for (algo, digest) in PAIRS {
if !digest.ends_with("==") {
continue;
}

let line = output_format(algo, digest);
let line = line.trim_end_matches("==");

log_info(format!("ALGORITHM: {algo}, STDIN: '{line}'"), "");
scene
.ucmd()
.arg("--check")
.pipe_in(line)
.fails()
.no_stdout()
.stderr_contains("no properly formatted checksum lines found");
}
}
}

0 comments on commit 598537a

Please sign in to comment.