| 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 rstest::rstest; |
| 7 |
|
| 8 |
use uutests::new_ucmd; |
| 9 |
use uutests::util::TestScenario; |
| 10 |
use uutests::util_name; |
| 11 |
// spell-checker:ignore checkfile, testf, ntestf |
| 12 |
macro_rules! get_hash( |
| 13 |
($str:expr) => ( |
| 14 |
$str.split(' ').collect::<Vec<&str>>()[0] |
| 15 |
); |
| 16 |
); |
| 17 |
|
| 18 |
macro_rules! test_digest_with_len { |
| 19 |
($id:ident, $size:expr) => { |
| 20 |
mod $id { |
| 21 |
use uutests::util::*; |
| 22 |
use uutests::util_name; |
| 23 |
static LENGTH_ARG: &'static str = concat!("--length=", stringify!($size)); |
| 24 |
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected"); |
| 25 |
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile"); |
| 26 |
static INPUT_FILE: &'static str = "input.txt"; |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_single_file() { |
| 30 |
let ts = TestScenario::new(util_name!()); |
| 31 |
assert_eq!( |
| 32 |
ts.fixtures.read(EXPECTED_FILE), |
| 33 |
get_hash!( |
| 34 |
ts.ucmd() |
| 35 |
.arg(LENGTH_ARG) |
| 36 |
.arg(INPUT_FILE) |
| 37 |
.succeeds() |
| 38 |
.no_stderr() |
| 39 |
.stdout_str() |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn test_stdin() { |
| 46 |
let ts = TestScenario::new(util_name!()); |
| 47 |
assert_eq!( |
| 48 |
ts.fixtures.read(EXPECTED_FILE), |
| 49 |
get_hash!( |
| 50 |
ts.ucmd() |
| 51 |
.arg(LENGTH_ARG) |
| 52 |
.pipe_in_fixture(INPUT_FILE) |
| 53 |
.succeeds() |
| 54 |
.no_stderr() |
| 55 |
.stdout_str() |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_stdin_with_dash_directory() { |
| 62 |
let ts = TestScenario::new(util_name!()); |
| 63 |
ts.fixtures.mkdir("-"); |
| 64 |
assert_eq!( |
| 65 |
ts.fixtures.read(EXPECTED_FILE), |
| 66 |
get_hash!( |
| 67 |
ts.ucmd() |
| 68 |
.arg(LENGTH_ARG) |
| 69 |
.pipe_in_fixture(INPUT_FILE) |
| 70 |
.succeeds() |
| 71 |
.no_stderr() |
| 72 |
.stdout_str() |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_check() { |
| 79 |
let ts = TestScenario::new(util_name!()); |
| 80 |
println!("File content='{}'", ts.fixtures.read(INPUT_FILE)); |
| 81 |
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE)); |
| 82 |
|
| 83 |
ts.ucmd() |
| 84 |
.args(&[LENGTH_ARG, "--check", CHECK_FILE]) |
| 85 |
.succeeds() |
| 86 |
.no_stderr() |
| 87 |
.stdout_is("input.txt: OK\n"); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_zero() { |
| 92 |
let ts = TestScenario::new(util_name!()); |
| 93 |
assert_eq!( |
| 94 |
ts.fixtures.read(EXPECTED_FILE), |
| 95 |
get_hash!( |
| 96 |
ts.ucmd() |
| 97 |
.arg(LENGTH_ARG) |
| 98 |
.arg("--zero") |
| 99 |
.arg(INPUT_FILE) |
| 100 |
.succeeds() |
| 101 |
.no_stderr() |
| 102 |
.stdout_str() |
| 103 |
) |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_missing_file() { |
| 109 |
let ts = TestScenario::new(util_name!()); |
| 110 |
let at = &ts.fixtures; |
| 111 |
|
| 112 |
at.write("a", "file1\n"); |
| 113 |
at.write("c", "file3\n"); |
| 114 |
|
| 115 |
ts.ucmd() |
| 116 |
.args(&[LENGTH_ARG, "a", "b", "c"]) |
| 117 |
.fails() |
| 118 |
.stdout_contains("a\n") |
| 119 |
.stdout_contains("c\n") |
| 120 |
.stderr_contains("b: No such file or directory"); |
| 121 |
} |
| 122 |
} |
| 123 |
}; |
| 124 |
} |
| 125 |
|
| 126 |
test_digest_with_len! {b2sum, 512} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_check_b2sum_length_option_0() { |
| 130 |
let scene = TestScenario::new(util_name!()); |
| 131 |
let at = &scene.fixtures; |
| 132 |
|
| 133 |
at.write("testf", "foobar\n"); |
| 134 |
at.write("testf.b2sum", "9e2bf63e933e610efee4a8d6cd4a9387e80860edee97e27db3b37a828d226ab1eb92a9cdd8ca9ca67a753edaf8bd89a0558496f67a30af6f766943839acf0110 testf\n"); |
| 135 |
|
| 136 |
scene |
| 137 |
.ccmd("b2sum") |
| 138 |
.arg("--length=0") |
| 139 |
.arg("-c") |
| 140 |
.arg(at.subdir.join("testf.b2sum")) |
| 141 |
.succeeds() |
| 142 |
.stdout_only("testf: OK\n"); |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn test_check_b2sum_length_duplicate() { |
| 147 |
let scene = TestScenario::new(util_name!()); |
| 148 |
let at = &scene.fixtures; |
| 149 |
|
| 150 |
at.write("testf", "foobar\n"); |
| 151 |
|
| 152 |
scene |
| 153 |
.ccmd("b2sum") |
| 154 |
.arg("--length=123") |
| 155 |
.arg("--length=128") |
| 156 |
.arg("testf") |
| 157 |
.succeeds() |
| 158 |
.stdout_contains("d6d45901dec53e65d2b55fb6e2ab67b0"); |
| 159 |
} |
| 160 |
|
| 161 |
#[test] |
| 162 |
fn test_check_b2sum_length_option_8() { |
| 163 |
let scene = TestScenario::new(util_name!()); |
| 164 |
let at = &scene.fixtures; |
| 165 |
|
| 166 |
at.write("testf", "foobar\n"); |
| 167 |
at.write("testf.b2sum", "6a testf\n"); |
| 168 |
|
| 169 |
scene |
| 170 |
.ccmd("b2sum") |
| 171 |
.arg("--length=8") |
| 172 |
.arg("-c") |
| 173 |
.arg(at.subdir.join("testf.b2sum")) |
| 174 |
.succeeds() |
| 175 |
.stdout_only("testf: OK\n"); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn test_invalid_b2sum_length_option_not_multiple_of_8() { |
| 180 |
let scene = TestScenario::new(util_name!()); |
| 181 |
let at = &scene.fixtures; |
| 182 |
|
| 183 |
at.write("testf", "foobar\n"); |
| 184 |
|
| 185 |
scene |
| 186 |
.ccmd("b2sum") |
| 187 |
.arg("--length=9") |
| 188 |
.arg(at.subdir.join("testf")) |
| 189 |
.fails_with_code(1) |
| 190 |
.stderr_contains("b2sum: invalid length: '9'") |
| 191 |
.stderr_contains("b2sum: length is not a multiple of 8"); |
| 192 |
} |
| 193 |
|
| 194 |
#[rstest] |
| 195 |
#[case("513")] |
| 196 |
#[case("1024")] |
| 197 |
#[case("18446744073709552000")] |
| 198 |
fn test_invalid_b2sum_length_option_too_large(#[case] len: &str) { |
| 199 |
let scene = TestScenario::new(util_name!()); |
| 200 |
let at = &scene.fixtures; |
| 201 |
|
| 202 |
at.write("testf", "foobar\n"); |
| 203 |
|
| 204 |
scene |
| 205 |
.ccmd("b2sum") |
| 206 |
.arg("--length") |
| 207 |
.arg(len) |
| 208 |
.arg(at.subdir.join("testf")) |
| 209 |
.fails_with_code(1) |
| 210 |
.no_stdout() |
| 211 |
.stderr_contains(format!("b2sum: invalid length: '{len}'")) |
| 212 |
.stderr_contains("b2sum: maximum digest length for 'BLAKE2b' is 512 bits"); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_check_b2sum_tag_output() { |
| 217 |
let scene = TestScenario::new(util_name!()); |
| 218 |
let at = &scene.fixtures; |
| 219 |
|
| 220 |
at.touch("f"); |
| 221 |
|
| 222 |
scene |
| 223 |
.ccmd("b2sum") |
| 224 |
.arg("--length=0") |
| 225 |
.arg("--tag") |
| 226 |
.arg("f") |
| 227 |
.succeeds() |
| 228 |
.stdout_only("BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n"); |
| 229 |
|
| 230 |
scene |
| 231 |
.ccmd("b2sum") |
| 232 |
.arg("--length=128") |
| 233 |
.arg("--tag") |
| 234 |
.arg("f") |
| 235 |
.succeeds() |
| 236 |
.stdout_only("BLAKE2b-128 (f) = cae66941d9efbd404e4d88758ea67670\n"); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn test_check_b2sum_verify() { |
| 241 |
let scene = TestScenario::new(util_name!()); |
| 242 |
let at = &scene.fixtures; |
| 243 |
|
| 244 |
at.write("a", "a\n"); |
| 245 |
|
| 246 |
scene |
| 247 |
.ccmd("b2sum") |
| 248 |
.arg("--tag") |
| 249 |
.arg("a") |
| 250 |
.succeeds() |
| 251 |
.stdout_only("BLAKE2b (a) = bedfbb90d858c2d67b7ee8f7523be3d3b54004ef9e4f02f2ad79a1d05bfdfe49b81e3c92ebf99b504102b6bf003fa342587f5b3124c205f55204e8c4b4ce7d7c\n"); |
| 252 |
|
| 253 |
scene |
| 254 |
.ccmd("b2sum") |
| 255 |
.arg("--tag") |
| 256 |
.arg("-l") |
| 257 |
.arg("128") |
| 258 |
.arg("a") |
| 259 |
.succeeds() |
| 260 |
.stdout_only("BLAKE2b-128 (a) = b93e0fc7bb21633c08bba07c5e71dc00\n"); |
| 261 |
} |
| 262 |
|
| 263 |
#[test] |
| 264 |
fn test_invalid_arg() { |
| 265 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 266 |
} |
| 267 |
|
| 268 |
#[test] |
| 269 |
fn test_check_b2sum_strict_check() { |
| 270 |
let scene = TestScenario::new(util_name!()); |
| 271 |
let at = &scene.fixtures; |
| 272 |
at.touch("f"); |
| 273 |
|
| 274 |
let checksums = [ |
| 275 |
"2e f\n", |
| 276 |
"e4a6a0577479b2b4 f\n", |
| 277 |
"cae66941d9efbd404e4d88758ea67670 f\n", |
| 278 |
"246c0442cd564aced8145b8b60f1370aa7 f\n", |
| 279 |
"0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 f\n", |
| 280 |
"4ded8c5fc8b12f3273f877ca585a44ad6503249a2b345d6d9c0e67d85bcb700db4178c0303e93b8f4ad758b8e2c9fd8b3d0c28e585f1928334bb77d36782e8 f\n", |
| 281 |
"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce f\n", |
| 282 |
]; |
| 283 |
|
| 284 |
at.write("ck", &checksums.join("")); |
| 285 |
|
| 286 |
let output = "f: OK\n".to_string().repeat(checksums.len()); |
| 287 |
|
| 288 |
scene |
| 289 |
.ccmd("b2sum") |
| 290 |
.arg("-c") |
| 291 |
.arg(at.subdir.join("ck")) |
| 292 |
.succeeds() |
| 293 |
.stdout_only(&output); |
| 294 |
|
| 295 |
scene |
| 296 |
.ccmd("b2sum") |
| 297 |
.arg("--strict") |
| 298 |
.arg("-c") |
| 299 |
.arg(at.subdir.join("ck")) |
| 300 |
.succeeds() |
| 301 |
.stdout_only(&output); |
| 302 |
} |