| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
use uutests::new_ucmd; |
| 7 |
use uutests::util::TestScenario; |
| 8 |
use uutests::util_name; |
| 9 |
// spell-checker:ignore checkfile, testf, ntestf, heic |
| 10 |
macro_rules! get_hash( |
| 11 |
($str:expr) => ( |
| 12 |
$str.split(' ').collect::<Vec<&str>>()[0] |
| 13 |
); |
| 14 |
); |
| 15 |
|
| 16 |
macro_rules! test_digest { |
| 17 |
($id:ident) => { |
| 18 |
mod $id { |
| 19 |
use uutests::util::*; |
| 20 |
use uutests::util_name; |
| 21 |
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected"); |
| 22 |
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile"); |
| 23 |
static INPUT_FILE: &'static str = "input.txt"; |
| 24 |
|
| 25 |
#[test] |
| 26 |
fn test_single_file() { |
| 27 |
let ts = TestScenario::new(util_name!()); |
| 28 |
assert_eq!( |
| 29 |
ts.fixtures.read(EXPECTED_FILE), |
| 30 |
get_hash!( |
| 31 |
ts.ucmd() |
| 32 |
.arg(INPUT_FILE) |
| 33 |
.succeeds() |
| 34 |
.no_stderr() |
| 35 |
.stdout_str() |
| 36 |
) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
fn test_stdin() { |
| 42 |
let ts = TestScenario::new(util_name!()); |
| 43 |
assert_eq!( |
| 44 |
ts.fixtures.read(EXPECTED_FILE), |
| 45 |
get_hash!( |
| 46 |
ts.ucmd() |
| 47 |
.pipe_in_fixture(INPUT_FILE) |
| 48 |
.succeeds() |
| 49 |
.no_stderr() |
| 50 |
.stdout_str() |
| 51 |
) |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn test_stdin_with_dash_directory() { |
| 57 |
let ts = TestScenario::new(util_name!()); |
| 58 |
ts.fixtures.mkdir("-"); |
| 59 |
assert_eq!( |
| 60 |
ts.fixtures.read(EXPECTED_FILE), |
| 61 |
get_hash!( |
| 62 |
ts.ucmd() |
| 63 |
.pipe_in_fixture(INPUT_FILE) |
| 64 |
.succeeds() |
| 65 |
.no_stderr() |
| 66 |
.stdout_str() |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_check() { |
| 73 |
let ts = TestScenario::new(util_name!()); |
| 74 |
println!("File content='{}'", ts.fixtures.read(INPUT_FILE)); |
| 75 |
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE)); |
| 76 |
|
| 77 |
ts.ucmd() |
| 78 |
.args(&["--check", CHECK_FILE]) |
| 79 |
.succeeds() |
| 80 |
.no_stderr() |
| 81 |
.stdout_is("input.txt: OK\n"); |
| 82 |
} |
| 83 |
|
| 84 |
#[test] |
| 85 |
fn test_zero() { |
| 86 |
let ts = TestScenario::new(util_name!()); |
| 87 |
assert_eq!( |
| 88 |
ts.fixtures.read(EXPECTED_FILE), |
| 89 |
get_hash!( |
| 90 |
ts.ucmd() |
| 91 |
.arg("--zero") |
| 92 |
.arg(INPUT_FILE) |
| 93 |
.succeeds() |
| 94 |
.no_stderr() |
| 95 |
.stdout_str() |
| 96 |
) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn test_missing_file() { |
| 102 |
let ts = TestScenario::new(util_name!()); |
| 103 |
let at = &ts.fixtures; |
| 104 |
|
| 105 |
at.write("a", "file1\n"); |
| 106 |
at.write("c", "file3\n"); |
| 107 |
|
| 108 |
ts.ucmd() |
| 109 |
.args(&["a", "b", "c"]) |
| 110 |
.fails() |
| 111 |
.stdout_contains("a\n") |
| 112 |
.stdout_contains("c\n") |
| 113 |
.stderr_contains("b: No such file or directory"); |
| 114 |
} |
| 115 |
} |
| 116 |
}; |
| 117 |
} |
| 118 |
|
| 119 |
test_digest! {sha256} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_invalid_arg() { |
| 123 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_conflicting_arg() { |
| 128 |
new_ucmd!().arg("--tag").arg("--check").fails_with_code(1); |
| 129 |
new_ucmd!().arg("--tag").arg("--text").fails_with_code(1); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn test_tag() { |
| 134 |
let scene = TestScenario::new(util_name!()); |
| 135 |
let at = &scene.fixtures; |
| 136 |
|
| 137 |
at.write("foobar", "foo bar\n"); |
| 138 |
scene |
| 139 |
.ccmd("sha256sum") |
| 140 |
.arg("--tag") |
| 141 |
.arg("foobar") |
| 142 |
.succeeds() |
| 143 |
.stdout_is( |
| 144 |
"SHA256 (foobar) = 1f2ec52b774368781bed1d1fb140a92e0eb6348090619c9291f9a5a3c8e8d151\n", |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_sha256_binary() { |
| 150 |
let ts = TestScenario::new(util_name!()); |
| 151 |
assert_eq!( |
| 152 |
ts.fixtures.read("binary.sha256.expected"), |
| 153 |
get_hash!( |
| 154 |
ts.ucmd() |
| 155 |
.arg("binary.png") |
| 156 |
.succeeds() |
| 157 |
.no_stderr() |
| 158 |
.stdout_str() |
| 159 |
) |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn test_sha256_stdin_binary() { |
| 165 |
let ts = TestScenario::new(util_name!()); |
| 166 |
assert_eq!( |
| 167 |
ts.fixtures.read("binary.sha256.expected"), |
| 168 |
get_hash!( |
| 169 |
ts.ucmd() |
| 170 |
.pipe_in_fixture("binary.png") |
| 171 |
.succeeds() |
| 172 |
.no_stderr() |
| 173 |
.stdout_str() |
| 174 |
) |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn test_check_sha256_binary() { |
| 180 |
new_ucmd!() |
| 181 |
.args(&["--check", "binary.sha256.checkfile"]) |
| 182 |
.succeeds() |
| 183 |
.no_stderr() |
| 184 |
.stdout_is("binary.png: OK\n"); |
| 185 |
} |
| 186 |
|
| 187 |
// Regression test for https://github.com/uutils/coreutils/issues/6655 |
| 188 |
// On Windows, `sha256sum --check` applied CRLF -> LF conversion while |
| 189 |
// generation hashed raw bytes, so verifying any binary file containing |
| 190 |
// "\r\n" (HEIC, TTF, PNG, ...) against a checksum file generated by the |
| 191 |
// same tool always reported FAILED. |
| 192 |
#[test] |
| 193 |
fn test_check_binary_files_with_crlf_bytes() { |
| 194 |
let ts = TestScenario::new(util_name!()); |
| 195 |
let at = &ts.fixtures; |
| 196 |
|
| 197 |
// Bytes around offset 43134 of a real TTF file, taken from the issue. |
| 198 |
at.write_bytes("a.fake-ttf", b"\x00\x00\r\n\x00\x00"); |
| 199 |
at.write_bytes("b.fake-heic", b"HEIC\r\n\xff\x00\r\n*"); |
| 200 |
|
| 201 |
let untagged = ts.ucmd().args(&["a.fake-ttf", "b.fake-heic"]).succeeds(); |
| 202 |
untagged.stdout_is( |
| 203 |
"1c671d7322d49cd2726475f4b8a8b50f27b454789e23a31c6ac14014740d8e58 a.fake-ttf\n\ |
| 204 |
a54c776c4b43597b7f043ff59cd0a36753764eb6edec3c00a99dc54adcfbccbc b.fake-heic\n", |
| 205 |
); |
| 206 |
at.write_bytes("hash.256", untagged.stdout()); |
| 207 |
|
| 208 |
let tagged = ts |
| 209 |
.ucmd() |
| 210 |
.args(&["--tag", "a.fake-ttf", "b.fake-heic"]) |
| 211 |
.succeeds(); |
| 212 |
at.write_bytes("hash-tag.256", tagged.stdout()); |
| 213 |
|
| 214 |
for checksum_file in ["hash.256", "hash-tag.256"] { |
| 215 |
ts.ucmd() |
| 216 |
.args(&["--check", checksum_file]) |
| 217 |
.succeeds() |
| 218 |
.stdout_only("a.fake-ttf: OK\nb.fake-heic: OK\n"); |
| 219 |
} |
| 220 |
} |