2023-05-11 06:03:12 +00:00
|
|
|
// spell-checker:ignore (words) asdf algo algos
|
2021-05-30 05:10:54 +00:00
|
|
|
|
2023-03-20 13:51:19 +00:00
|
|
|
use crate::common::util::TestScenario;
|
2015-11-16 05:25:01 +00:00
|
|
|
|
2023-05-11 06:03:12 +00:00
|
|
|
const ALGOS: [&str; 11] = [
|
|
|
|
"sysv", "bsd", "crc", "md5", "sha1", "sha224", "sha256", "sha384", "sha512", "blake2b", "sm3",
|
|
|
|
];
|
|
|
|
|
2022-09-10 16:38:14 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_arg() {
|
|
|
|
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
|
|
|
|
}
|
|
|
|
|
2015-11-16 05:25:01 +00:00
|
|
|
#[test]
|
|
|
|
fn test_single_file() {
|
2020-04-13 18:36:03 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg("lorem_ipsum.txt")
|
|
|
|
.succeeds()
|
2023-05-11 06:03:12 +00:00
|
|
|
.stdout_is_fixture("crc_single_file.expected");
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_multiple_files() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.arg("lorem_ipsum.txt")
|
|
|
|
.arg("alice_in_wonderland.txt")
|
2020-04-13 18:36:03 +00:00
|
|
|
.succeeds()
|
2023-05-11 06:03:12 +00:00
|
|
|
.stdout_is_fixture("crc_multiple_files.expected");
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stdin() {
|
2016-08-23 11:52:43 +00:00
|
|
|
new_ucmd!()
|
2016-07-29 21:26:32 +00:00
|
|
|
.pipe_in_fixture("lorem_ipsum.txt")
|
2020-04-13 18:36:03 +00:00
|
|
|
.succeeds()
|
2023-05-11 06:03:12 +00:00
|
|
|
.stdout_is_fixture("crc_stdin.expected");
|
2015-11-16 05:25:01 +00:00
|
|
|
}
|
2021-04-05 20:21:21 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-05-10 07:48:32 +00:00
|
|
|
fn test_empty_file() {
|
2021-04-05 20:21:21 +00:00
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
at.touch("a");
|
|
|
|
|
2021-04-17 12:01:52 +00:00
|
|
|
ucmd.arg("a")
|
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.normalized_newlines_stdout_is("4294967295 0 a\n");
|
2021-04-05 20:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_arg_overrides_stdin() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
2021-05-30 05:10:54 +00:00
|
|
|
let input = "foobarfoobar"; // spell-checker:disable-line
|
2021-04-05 20:21:21 +00:00
|
|
|
|
|
|
|
at.touch("a");
|
|
|
|
|
2021-04-17 12:01:52 +00:00
|
|
|
ucmd.arg("a")
|
2021-04-09 09:08:31 +00:00
|
|
|
.pipe_in(input.as_bytes())
|
|
|
|
// the command might have exited before all bytes have been pipe in.
|
|
|
|
// in that case, we don't care about the error (broken pipe)
|
|
|
|
.ignore_stdin_write_error()
|
2021-04-17 12:01:52 +00:00
|
|
|
.succeeds()
|
|
|
|
.no_stderr()
|
|
|
|
.normalized_newlines_stdout_is("4294967295 0 a\n");
|
2021-04-05 20:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-05-10 07:48:32 +00:00
|
|
|
fn test_nonexisting_file() {
|
|
|
|
let file_name = "asdf";
|
2021-04-17 12:01:52 +00:00
|
|
|
|
2023-05-10 07:48:32 +00:00
|
|
|
new_ucmd!()
|
|
|
|
.arg(file_name)
|
2021-04-17 12:01:52 +00:00
|
|
|
.fails()
|
|
|
|
.no_stdout()
|
2023-05-10 07:48:32 +00:00
|
|
|
.stderr_contains(format!("cksum: {file_name}: No such file or directory"));
|
|
|
|
}
|
2021-04-17 23:33:52 +00:00
|
|
|
|
2023-05-10 07:48:32 +00:00
|
|
|
#[test]
|
|
|
|
fn test_folder() {
|
|
|
|
let (at, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
|
|
|
let folder_name = "a_folder";
|
2021-04-17 12:01:52 +00:00
|
|
|
at.mkdir(folder_name);
|
2023-05-10 07:48:32 +00:00
|
|
|
|
|
|
|
ucmd.arg(folder_name)
|
2021-12-26 20:46:57 +00:00
|
|
|
.succeeds()
|
2023-05-10 07:48:32 +00:00
|
|
|
.stdout_only(format!("4294967295 0 {folder_name}\n"));
|
2021-04-05 20:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure crc is correct for files larger than 32 bytes
|
2021-05-30 05:10:54 +00:00
|
|
|
// but <128 bytes (1 fold pclmul) // spell-checker:disable-line
|
2021-04-05 20:21:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_crc_for_bigger_than_32_bytes() {
|
|
|
|
let (_, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
2021-04-17 12:01:52 +00:00
|
|
|
let result = ucmd.arg("chars.txt").succeeds();
|
2021-04-05 20:21:21 +00:00
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
let mut stdout_split = result.stdout_str().split(' ');
|
2021-04-05 20:21:21 +00:00
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
let cksum: i64 = stdout_split.next().unwrap().parse().unwrap();
|
|
|
|
let bytes_cnt: i64 = stdout_split.next().unwrap().parse().unwrap();
|
2021-04-05 20:21:21 +00:00
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
assert_eq!(cksum, 586_047_089);
|
2021-04-05 20:21:21 +00:00
|
|
|
assert_eq!(bytes_cnt, 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_stdin_larger_than_128_bytes() {
|
|
|
|
let (_, mut ucmd) = at_and_ucmd!();
|
|
|
|
|
2021-04-17 12:01:52 +00:00
|
|
|
let result = ucmd.arg("larger_than_2056_bytes.txt").succeeds();
|
2021-04-05 20:21:21 +00:00
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
let mut stdout_split = result.stdout_str().split(' ');
|
2021-04-05 20:21:21 +00:00
|
|
|
|
2021-05-30 05:10:54 +00:00
|
|
|
let cksum: i64 = stdout_split.next().unwrap().parse().unwrap();
|
|
|
|
let bytes_cnt: i64 = stdout_split.next().unwrap().parse().unwrap();
|
2021-04-05 20:21:21 +00:00
|
|
|
|
2021-05-29 12:32:35 +00:00
|
|
|
assert_eq!(cksum, 945_881_979);
|
2021-04-05 20:21:21 +00:00
|
|
|
assert_eq!(bytes_cnt, 2058);
|
|
|
|
}
|
2023-02-13 03:40:16 +00:00
|
|
|
|
|
|
|
#[test]
|
2023-05-11 06:03:12 +00:00
|
|
|
fn test_algorithm_single_file() {
|
|
|
|
for algo in ALGOS {
|
|
|
|
for option in ["-a", "--algorithm"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{option}={algo}"))
|
|
|
|
.arg("lorem_ipsum.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture(format!("{algo}_single_file.expected"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_algorithm_multiple_files() {
|
|
|
|
for algo in ALGOS {
|
|
|
|
for option in ["-a", "--algorithm"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{option}={algo}"))
|
|
|
|
.arg("lorem_ipsum.txt")
|
|
|
|
.arg("alice_in_wonderland.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture(format!("{algo}_multiple_files.expected"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_algorithm_stdin() {
|
|
|
|
for algo in ALGOS {
|
|
|
|
for option in ["-a", "--algorithm"] {
|
|
|
|
new_ucmd!()
|
|
|
|
.arg(format!("{option}={algo}"))
|
|
|
|
.pipe_in_fixture("lorem_ipsum.txt")
|
|
|
|
.succeeds()
|
|
|
|
.stdout_is_fixture(format!("{algo}_stdin.expected"));
|
|
|
|
}
|
|
|
|
}
|
2023-02-13 03:40:16 +00:00
|
|
|
}
|