Debian Codegraph @

tests/by-util/test_cksum.rs

raw
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 // spell-checker:ignore (words) asdf algo algos asha mgmt xffname hexa GFYEQ HYQK Yqxb dont checkfile
7
8 use rstest::rstest;
9 use rstest_reuse::{apply, template};
10
11 use uutests::at_and_ucmd;
12 use uutests::new_ucmd;
13 use uutests::util::TestScenario;
14 use uutests::util::UCommand;
15 use uutests::util::log_info;
16 use uutests::util_name;
17
18 #[template]
19 #[rstest]
20 #[case::sysv("sysv")]
21 #[case::bsd("bsd")]
22 #[case::crc("crc")]
23 #[case::md5("md5")]
24 #[case::sha1("sha1")]
25 #[case::sha224("sha224")]
26 #[case::sha256("sha256")]
27 #[case::sha384("sha384")]
28 #[case::sha512("sha512")]
29 #[case::blake2b("blake2b")]
30 #[case::blake3("blake3")]
31 #[case::sm3("sm3")]
32 fn test_all_algos(#[case] algo: &str) {}
33
34 #[template]
35 #[rstest]
36 fn test_sha(#[values("sha2", "sha3")] algo: &str, #[values(224, 256, 384, 512)] len: u32) {}
37
38 fn sha_fixture_name(algo: &str, len: u32, prefix: &str, suffix: &str) -> String {
39 // assume algo is always "sha2" or "sha3"
40 match algo {
41 "sha2" => format!("{prefix}sha{len}{suffix}"),
42 _ => format!("{prefix}sha3_{len}{suffix}"),
43 }
44 }
45
46 #[test]
47 #[cfg(feature = "openssl")]
48 fn test_openssl_link() {
49 let Ok(out) = std::process::Command::new("ldd")
50 .arg(uutests::util::get_tests_binary())
51 .output()
52 else {
53 return; // missing ldd
54 };
55 let dynamic = String::from_utf8_lossy(&out.stdout).contains("crypto"); // covering MSYS/MinGW too
56 assert_eq!(
57 std::env::var("OPENSSL_NO_VENDOR") == Ok("1".to_string()),
58 dynamic
59 );
60 }
61
62 #[test]
63 fn test_invalid_arg() {
64 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
65 }
66
67 #[test]
68 fn test_single_file() {
69 new_ucmd!()
70 .arg("lorem_ipsum.txt")
71 .succeeds()
72 .stdout_is_fixture("crc_single_file.expected");
73 }
74
75 #[test]
76 fn test_multiple_files() {
77 new_ucmd!()
78 .arg("lorem_ipsum.txt")
79 .arg("alice_in_wonderland.txt")
80 .succeeds()
81 .stdout_is_fixture("crc_multiple_files.expected");
82 }
83
84 #[test]
85 fn test_stdin() {
86 new_ucmd!()
87 .pipe_in_fixture("lorem_ipsum.txt")
88 .succeeds()
89 .stdout_is_fixture("crc_stdin.expected");
90 }
91
92 #[test]
93 fn test_stdin_with_dash_directory() {
94 let (at, mut ucmd) = at_and_ucmd!();
95 at.mkdir("-");
96
97 ucmd.pipe_in_fixture("lorem_ipsum.txt")
98 .succeeds()
99 .stdout_is_fixture("crc_stdin.expected");
100 }
101
102 #[test]
103 fn test_empty_file() {
104 let (at, mut ucmd) = at_and_ucmd!();
105
106 at.touch("a");
107
108 ucmd.arg("a")
109 .succeeds()
110 .no_stderr()
111 .normalized_newlines_stdout_is("4294967295 0 a\n");
112 }
113
114 #[test]
115 fn test_arg_overrides_stdin() {
116 let (at, mut ucmd) = at_and_ucmd!();
117 let input = "foobarfoobar"; // spell-checker:disable-line
118
119 at.touch("a");
120
121 ucmd.arg("a")
122 .pipe_in(input.as_bytes())
123 // the command might have exited before all bytes have been pipe in.
124 // in that case, we don't care about the error (broken pipe)
125 .ignore_stdin_write_error()
126 .succeeds()
127 .no_stderr()
128 .normalized_newlines_stdout_is("4294967295 0 a\n");
129 }
130
131 #[test]
132 fn test_nonexisting_file() {
133 let file_name = "asdf";
134
135 new_ucmd!()
136 .arg(file_name)
137 .fails_with_code(1)
138 .no_stdout()
139 .stderr_contains(format!("cksum: {file_name}: No such file or directory"));
140 }
141
142 #[test]
143 fn test_nonexisting_file_out() {
144 let (at, mut ucmd) = at_and_ucmd!();
145 at.write(
146 "f",
147 "MD5 (nonexistent) = e5773576fc75ff0f8eba14f61587ae28\n",
148 );
149
150 ucmd.arg("-c")
151 .arg("f")
152 .fails()
153 .stdout_contains("nonexistent: FAILED open or read")
154 .stderr_contains("cksum: nonexistent: No such file or directory");
155 }
156
157 #[test]
158 fn test_one_nonexisting_file() {
159 let (at, mut ucmd) = at_and_ucmd!();
160
161 at.touch("abc.txt");
162 at.touch("xyz.txt");
163
164 ucmd.arg("abc.txt")
165 .arg("asdf.txt")
166 .arg("xyz.txt")
167 .fails()
168 .stdout_contains_line("4294967295 0 xyz.txt")
169 .stderr_contains("asdf.txt: No such file or directory")
170 .stdout_contains_line("4294967295 0 abc.txt");
171 }
172
173 // Make sure crc is correct for files larger than 32 bytes
174 // but <128 bytes (1 fold pclmul) // spell-checker:disable-line
175 #[test]
176 fn test_crc_for_bigger_than_32_bytes() {
177 let result = new_ucmd!().arg("chars.txt").succeeds();
178
179 let mut stdout_split = result.stdout_str().split(' ');
180
181 let cksum: i64 = stdout_split.next().unwrap().parse().unwrap();
182 let bytes_cnt: i64 = stdout_split.next().unwrap().parse().unwrap();
183
184 assert_eq!(cksum, 586_047_089);
185 assert_eq!(bytes_cnt, 16);
186 }
187
188 #[test]
189 fn test_stdin_larger_than_128_bytes() {
190 let result = new_ucmd!().arg("larger_than_2056_bytes.txt").succeeds();
191
192 let mut stdout_split = result.stdout_str().split(' ');
193
194 let cksum: i64 = stdout_split.next().unwrap().parse().unwrap();
195 let bytes_cnt: i64 = stdout_split.next().unwrap().parse().unwrap();
196
197 assert_eq!(cksum, 945_881_979);
198 assert_eq!(bytes_cnt, 2058);
199 }
200
201 #[test]
202 fn test_repeated_flags() {
203 new_ucmd!()
204 .arg("-a")
205 .arg("sha1")
206 .arg("--algo=sha256")
207 .arg("-a=md5")
208 .arg("lorem_ipsum.txt")
209 .succeeds()
210 .stdout_is_fixture("md5_single_file.expected");
211 }
212
213 #[test]
214 fn test_tag_after_untagged() {
215 new_ucmd!()
216 .arg("--untagged")
217 .arg("--tag")
218 .arg("-a=md5")
219 .arg("lorem_ipsum.txt")
220 .succeeds()
221 .stdout_is_fixture("md5_single_file.expected");
222 }
223
224 #[apply(test_all_algos)]
225 fn test_algorithm_single_file(#[case] algo: &str) {
226 for option in ["-a", "--algorithm"] {
227 new_ucmd!()
228 .arg(format!("{option}={algo}"))
229 .arg("lorem_ipsum.txt")
230 .succeeds()
231 .stdout_is_fixture(format!("{algo}_single_file.expected"));
232 }
233 }
234
235 #[apply(test_all_algos)]
236 fn test_algorithm_multiple_files(#[case] algo: &str) {
237 for option in ["-a", "--algorithm"] {
238 new_ucmd!()
239 .arg(format!("{option}={algo}"))
240 .arg("lorem_ipsum.txt")
241 .arg("alice_in_wonderland.txt")
242 .succeeds()
243 .stdout_is_fixture(format!("{algo}_multiple_files.expected"));
244 }
245 }
246
247 #[apply(test_all_algos)]
248 fn test_algorithm_stdin(#[case] algo: &str) {
249 for option in ["-a", "--algorithm"] {
250 new_ucmd!()
251 .arg(format!("{option}={algo}"))
252 .pipe_in_fixture("lorem_ipsum.txt")
253 .succeeds()
254 .stdout_is_fixture(format!("{algo}_stdin.expected"));
255 }
256 }
257
258 #[test]
259 fn test_untagged_single_file() {
260 new_ucmd!()
261 .arg("--untagged")
262 .arg("lorem_ipsum.txt")
263 .succeeds()
264 .stdout_is_fixture("untagged/crc_single_file.expected");
265 }
266
267 #[test]
268 fn test_untagged_multiple_files() {
269 new_ucmd!()
270 .arg("--untagged")
271 .arg("lorem_ipsum.txt")
272 .arg("alice_in_wonderland.txt")
273 .succeeds()
274 .stdout_is_fixture("untagged/crc_multiple_files.expected");
275 }
276
277 #[test]
278 fn test_untagged_stdin() {
279 new_ucmd!()
280 .arg("--untagged")
281 .pipe_in_fixture("lorem_ipsum.txt")
282 .succeeds()
283 .stdout_is_fixture("untagged/crc_stdin.expected");
284 }
285
286 #[apply(test_all_algos)]
287 fn test_untagged_algorithm_single_file(#[case] algo: &str) {
288 new_ucmd!()
289 .arg("--untagged")
290 .arg(format!("--algorithm={algo}"))
291 .arg("lorem_ipsum.txt")
292 .succeeds()
293 .stdout_is_fixture(format!("untagged/{algo}_single_file.expected"));
294 }
295
296 #[test]
297 fn test_tag_short() {
298 new_ucmd!()
299 .arg("-t")
300 .arg("--untagged")
301 .arg("--algorithm=md5")
302 .arg("lorem_ipsum.txt")
303 .succeeds()
304 .stdout_is("cd724690f7dc61775dfac400a71f2caa lorem_ipsum.txt\n");
305 }
306
307 #[test]
308 fn test_untagged_algorithm_after_tag() {
309 new_ucmd!()
310 .arg("--tag")
311 .arg("--untagged")
312 .arg("--algorithm=md5")
313 .arg("lorem_ipsum.txt")
314 .succeeds()
315 .stdout_is_fixture("untagged/md5_single_file.expected");
316 }
317
318 #[apply(test_all_algos)]
319 fn test_untagged_algorithm_multiple_files(#[case] algo: &str) {
320 new_ucmd!()
321 .arg("--untagged")
322 .arg(format!("--algorithm={algo}"))
323 .arg("lorem_ipsum.txt")
324 .arg("alice_in_wonderland.txt")
325 .succeeds()
326 .stdout_is_fixture(format!("untagged/{algo}_multiple_files.expected"));
327 }
328
329 #[apply(test_all_algos)]
330 fn test_untagged_algorithm_stdin(#[case] algo: &str) {
331 new_ucmd!()
332 .arg("--untagged")
333 .arg(format!("--algorithm={algo}"))
334 .pipe_in_fixture("lorem_ipsum.txt")
335 .succeeds()
336 .stdout_is_fixture(format!("untagged/{algo}_stdin.expected"));
337 }
338
339 #[test]
340 fn test_sha_length_invalid() {
341 for algo in ["sha2", "sha3"] {
342 for l in ["0", "00", "13", "56", "99999999999999999999999999"] {
343 new_ucmd!()
344 .arg("--algorithm")
345 .arg(algo)
346 .arg("--length")
347 .arg(l)
348 .arg("/dev/null")
349 .fails_with_code(1)
350 .no_stdout()
351 .stderr_contains(format!("invalid length: '{l}'"))
352 .stderr_contains(format!(
353 "digest length for '{}' must be 224, 256, 384, or 512",
354 algo.to_ascii_uppercase()
355 ));
356
357 // Also fails with --check
358 new_ucmd!()
359 .arg("--algorithm")
360 .arg(algo)
361 .arg("--length")
362 .arg(l)
363 .arg("/dev/null")
364 .arg("--check")
365 .fails_with_code(1)
366 .no_stdout()
367 .stderr_contains(format!("invalid length: '{l}'"))
368 .stderr_contains(format!(
369 "digest length for '{}' must be 224, 256, 384, or 512",
370 algo.to_ascii_uppercase()
371 ));
372 }
373
374 // Different error for NaNs
375 for l in ["512x", "x512", "512x512"] {
376 new_ucmd!()
377 .arg("--algorithm")
378 .arg(algo)
379 .arg("--length")
380 .arg(l)
381 .arg("/dev/null")
382 .fails_with_code(1)
383 .no_stdout()
384 .stderr_contains(format!("invalid length: '{l}'"));
385
386 // Also fails with --check
387 new_ucmd!()
388 .arg("--algorithm")
389 .arg(algo)
390 .arg("--length")
391 .arg(l)
392 .arg("/dev/null")
393 .arg("--check")
394 .fails_with_code(1)
395 .no_stdout()
396 .stderr_contains(format!("invalid length: '{l}'"));
397 }
398 }
399 }
400
401 #[test]
402 fn test_sha_missing_length() {
403 for algo in ["sha2", "sha3"] {
404 new_ucmd!()
405 .arg("--algorithm")
406 .arg(algo)
407 .arg("lorem_ipsum.txt")
408 .fails_with_code(1)
409 .no_stdout()
410 .stderr_contains(format!(
411 "--algorithm={algo} requires specifying --length 224, 256, 384, or 512"
412 ));
413 }
414 }
415
416 fn sha_cmd(algo: &str, len: u32) -> UCommand {
417 let mut ucmd = new_ucmd!();
418 ucmd.arg(format!("--algorithm={algo}"))
419 .arg(format!("--length={len}"));
420 ucmd
421 }
422
423 #[apply(test_sha)]
424 fn test_sha_single_file(algo: &str, len: u32) {
425 sha_cmd(algo, len)
426 .arg("lorem_ipsum.txt")
427 .succeeds()
428 .stdout_is_fixture(sha_fixture_name(algo, len, "", "_single_file.expected"));
429 }
430
431 #[apply(test_sha)]
432 fn test_sha_multiple_files(algo: &str, len: u32) {
433 sha_cmd(algo, len)
434 .arg("lorem_ipsum.txt")
435 .arg("alice_in_wonderland.txt")
436 .succeeds()
437 .stdout_is_fixture(sha_fixture_name(algo, len, "", "_multiple_files.expected"));
438 }
439
440 #[apply(test_sha)]
441 fn test_sha_stdin(algo: &str, len: u32) {
442 sha_cmd(algo, len)
443 .pipe_in_fixture("lorem_ipsum.txt")
444 .succeeds()
445 .stdout_is_fixture(sha_fixture_name(algo, len, "", "_stdin.expected"));
446 }
447
448 #[apply(test_sha)]
449 fn test_untagged_sha_single_file(algo: &str, len: u32) {
450 sha_cmd(algo, len)
451 .arg("--untagged")
452 .arg("lorem_ipsum.txt")
453 .succeeds()
454 .stdout_is_fixture(sha_fixture_name(
455 algo,
456 len,
457 "untagged/",
458 "_single_file.expected",
459 ));
460 }
461
462 #[apply(test_sha)]
463 fn test_untagged_sha_multiple_files(algo: &str, len: u32) {
464 sha_cmd(algo, len)
465 .arg("--untagged")
466 .arg("lorem_ipsum.txt")
467 .arg("alice_in_wonderland.txt")
468 .succeeds()
469 .stdout_is_fixture(sha_fixture_name(
470 algo,
471 len,
472 "untagged/",
473 "_multiple_files.expected",
474 ));
475 }
476
477 #[apply(test_sha)]
478 fn test_untagged_sha_stdin(algo: &str, len: u32) {
479 sha_cmd(algo, len)
480 .arg("--untagged")
481 .pipe_in_fixture("lorem_ipsum.txt")
482 .succeeds()
483 .stdout_is_fixture(sha_fixture_name(algo, len, "untagged/", "_stdin.expected"));
484 }
485
486 #[apply(test_sha)]
487 fn test_check_tagged_sha_single_file(algo: &str, len: u32) {
488 new_ucmd!()
489 .arg("--check")
490 .arg(sha_fixture_name(algo, len, "", "_single_file.expected"))
491 .succeeds()
492 .stdout_is("lorem_ipsum.txt: OK\n");
493 }
494
495 #[apply(test_sha)]
496 fn test_check_tagged_sha_multiple_files(algo: &str, len: u32) {
497 new_ucmd!()
498 .arg("--check")
499 .arg(sha_fixture_name(algo, len, "", "_multiple_files.expected"))
500 .succeeds()
501 .stdout_contains("lorem_ipsum.txt: OK\n")
502 .stdout_contains("alice_in_wonderland.txt: OK\n");
503 }
504
505 // When checking sha2/sha3 in untagged mode, the length is automatically
506 // deduced from the length of the digest.
507 #[apply(test_sha)]
508 fn test_check_untagged_sha_single_file(algo: &str, len: u32) {
509 new_ucmd!()
510 .arg("--check")
511 .arg(format!("--algorithm={algo}"))
512 .arg(sha_fixture_name(
513 algo,
514 len,
515 "untagged/",
516 "_single_file.expected",
517 ))
518 .succeeds()
519 .stdout_is("lorem_ipsum.txt: OK\n");
520 }
521
522 #[apply(test_sha)]
523 fn test_check_untagged_sha_multiple_files(algo: &str, len: u32) {
524 new_ucmd!()
525 .arg("--check")
526 .arg(format!("--algorithm={algo}"))
527 .arg(sha_fixture_name(
528 algo,
529 len,
530 "untagged/",
531 "_multiple_files.expected",
532 ))
533 .succeeds()
534 .stdout_contains("lorem_ipsum.txt: OK\n")
535 .stdout_contains("alice_in_wonderland.txt: OK\n");
536 }
537
538 #[rstest]
539 #[case::sha2("sha2", &[
540 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
541 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
542 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
543 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
544 ])]
545 #[case::sha3("sha3", &[
546 "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7",
547 "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a",
548 "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004",
549 "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26"
550 ])]
551 fn test_check_untagged_sha_invalid_length(#[case] algo: &str, #[case] digests: &[&str; 4]) {
552 // When checking with --algorithm sha2/sha3, Guess the length from the provided
553 // digest. Raise "improperly formatted" if the digest is not af adequate
554 // size (224, 256, 384 or 512 bits).
555
556 let (at, mut ucmd) = at_and_ucmd!();
557 at.touch("a");
558 at.touch("b");
559 at.touch("c");
560 at.touch("d");
561 at.touch("e");
562
563 let invalid = "xxxx e";
564
565 ucmd.arg("-a")
566 .arg(algo)
567 .arg("-c")
568 .pipe_in(format!(
569 "{} a\n{} b\n{} c\n{} d\n{invalid}",
570 digests[0], digests[1], digests[2], digests[3]
571 ))
572 .succeeds()
573 .stdout_contains("a: OK")
574 .stdout_contains("b: OK")
575 .stdout_contains("c: OK")
576 .stdout_contains("d: OK")
577 .stdout_does_not_contain("e: FAILED")
578 .stderr_contains("improperly formatted");
579 }
580
581 #[test]
582 fn test_check_sha2_tagged_variant() {
583 let scene = TestScenario::new(util_name!());
584 let at = &scene.fixtures;
585 at.touch("f");
586
587 // SHA2-xxx is an alias to SHAxxx we don't output but we still recognize.
588 let checksum_lines = [
589 (
590 "SHA224",
591 "SHA2-224",
592 "(f) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
593 ),
594 (
595 "SHA256",
596 "SHA2-256",
597 "(f) = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
598 ),
599 (
600 "SHA384",
601 "SHA2-384",
602 "(f) = 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b",
603 ),
604 (
605 "SHA512",
606 "SHA2-512",
607 "(f) = cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
608 ),
609 ];
610
611 for (basic, variant, digest) in checksum_lines {
612 let stdin = format!("{basic} {digest}");
613 log_info("stdin is: ", &stdin);
614 scene
615 .ucmd()
616 .arg("--check")
617 .arg("--algorithm=sha2")
618 .pipe_in(stdin)
619 .succeeds()
620 .stdout_is("f: OK\n");
621
622 // Check that the variant works the same
623 let stdin = format!("{variant} {digest}");
624 log_info("stdin is: ", &stdin);
625 scene
626 .ucmd()
627 .arg("--check")
628 .arg("--algorithm=sha2")
629 .pipe_in(stdin)
630 .succeeds()
631 .stdout_is("f: OK\n");
632 }
633 }
634
635 #[test]
636 fn test_check_sha2_tagged_missing_hint() {
637 // SHA2 (<file>) = <digest>
638 //
639 // should fail and raise "improperly formatted" because SHA2 expects a
640 // mandatory length hint.
641
642 let (at, mut ucmd) = at_and_ucmd!();
643 at.touch("a");
644 at.touch("b");
645
646 // valid digest but missing hint
647 let invalid_sha224 = "SHA2 (a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
648 // invalid digest
649 let invalid = "SHA2 (b) = xxxx";
650
651 ucmd.arg("-c")
652 .pipe_in(format!("{invalid_sha224}\n{invalid}"))
653 .fails()
654 .stderr_contains("no properly formatted checksum lines found");
655 }
656
657 #[test]
658 fn test_check_tagged_missing_algo() {
659 // When checking tagged lines, if the algorithm is missing, print an "improperly
660 // formatted" message rather than panic.
661
662 let (at, mut ucmd) = at_and_ucmd!();
663 at.touch("a");
664
665 let invalid1 = "(a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
666 let invalid2 = "a) = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
667 let invalid3 = "(a = d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f";
668
669 ucmd.arg("-c")
670 .pipe_in(format!("{invalid1}\n{invalid2}\n{invalid3}"))
671 .fails()
672 .stderr_contains("no properly formatted checksum lines found");
673 }
674
675 #[rstest]
676 #[case::md5("md5", "d41d8cd98f00b204e9800998ecf8427e")]
677 #[case::sha1("sha1", "da39a3ee5e6b4b0d3255bfef95601890afd80709")]
678 #[case::sha224("sha224", "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f")]
679 #[case::sha256(
680 "sha256",
681 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
682 )]
683 #[case::sha384(
684 "sha384",
685 "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b"
686 )]
687 #[case::sha512(
688 "sha512",
689 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"
690 )]
691 #[case::sm3(
692 "sm3",
693 "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b"
694 )]
695 fn test_check_untagged_with_invalid_length(#[case] algo: &str, #[case] digest: &str) {
696 // issue #11202
697
698 // Ensures that when checking untagged lines, if the provided algorithm is
699 // one of `sha(224|256|384|512)`, digests whose length mismatches the
700 // length of the given algorithm will report as "improperly formatted"
701 // rather than a FAILED check.
702
703 let (at, mut ucmd) = at_and_ucmd!();
704 at.touch("a");
705 at.touch("b");
706
707 let good_checksum = format!("{digest} a");
708 let invalid_checksum = "e3b0 b";
709
710 ucmd.arg("-a")
711 .arg(algo)
712 .arg("-c")
713 .pipe_in(format!("{invalid_checksum}\n{good_checksum}"))
714 .succeeds()
715 .stdout_contains("a: OK")
716 .stdout_does_not_contain("b: FAILED")
717 .stderr_contains("WARNING: 1 line is improperly formatted");
718 }
719
720 #[test]
721 fn test_check_algo() {
722 for algo in ["bsd", "sysv", "crc", "crc32b"] {
723 new_ucmd!()
724 .arg("-a")
725 .arg(algo)
726 .arg("--check")
727 .arg("lorem_ipsum.txt")
728 .fails()
729 .no_stdout()
730 .stderr_contains(
731 "cksum: --check is not supported with --algorithm={bsd,sysv,crc,crc32b}",
732 );
733 }
734 }
735
736 #[test]
737 fn test_length_with_wrong_algorithm() {
738 new_ucmd!()
739 .arg("--length=16")
740 .arg("--algorithm=md5")
741 .arg("lorem_ipsum.txt")
742 .fails_with_code(1)
743 .no_stdout()
744 .stderr_contains(
745 "cksum: --length is only supported with --algorithm blake2b, sha2, or sha3",
746 );
747
748 new_ucmd!()
749 .arg("--length=16")
750 .arg("--algorithm=md5")
751 .arg("-c")
752 .arg("foo.sums")
753 .fails_with_code(1)
754 .no_stdout()
755 .stderr_contains(
756 "cksum: --length is only supported with --algorithm blake2b, sha2, or sha3",
757 );
758 }
759
760 /// Giving --length to a wrong algorithm doesn't fail if the length is zero
761 #[test]
762 fn test_length_is_zero_with_wrong_algorithm() {
763 for algo in ["md5", "crc", "sha1", "sha224", "sha256", "sha384", "sha512"] {
764 new_ucmd!()
765 .arg("--length=0")
766 .args(&["-a", algo])
767 .arg("lorem_ipsum.txt")
768 .succeeds()
769 .no_stderr()
770 .stdout_is_fixture(format!("{algo}_single_file.expected"));
771 }
772 }
773
774 #[test]
775 fn test_length_not_supported() {
776 new_ucmd!()
777 .arg("--length=15")
778 .arg("lorem_ipsum.txt")
779 .fails_with_code(1)
780 .no_stdout()
781 .stderr_contains(
782 "cksum: --length is only supported with --algorithm blake2b, sha2, or sha3",
783 );
784
785 new_ucmd!()
786 .arg("-l")
787 .arg("158")
788 .arg("-c")
789 .arg("-a")
790 .arg("crc")
791 .arg("/tmp/xxx")
792 .fails_with_code(1)
793 .no_stdout()
794 .stderr_contains(
795 "cksum: --length is only supported with --algorithm blake2b, sha2, or sha3",
796 );
797 }
798
799 #[test]
800 fn test_blake2b_length() {
801 new_ucmd!()
802 .arg("--length=16")
803 .arg("--algorithm=blake2b")
804 .arg("lorem_ipsum.txt")
805 .arg("alice_in_wonderland.txt")
806 .succeeds()
807 .stdout_contains(
808 "BLAKE2b-16 (lorem_ipsum.txt) = 7e2f\nBLAKE2b-16 (alice_in_wonderland.txt) = a546",
809 );
810 }
811
812 #[test]
813 fn test_blake2b_length_greater_than_512() {
814 for l in ["513", "1024", "73786976294838206464"] {
815 new_ucmd!()
816 .arg("--algorithm=blake2b")
817 .arg("--length")
818 .arg(l)
819 .arg("lorem_ipsum.txt")
820 .fails_with_code(1)
821 .no_stdout()
822 .stderr_contains(format!("invalid length: '{l}'"))
823 .stderr_contains("maximum digest length for 'BLAKE2b' is 512 bits");
824 }
825 }
826
827 #[test]
828 fn test_blake2b_length_nan() {
829 for l in ["foo", "512x", "x512", "0xff"] {
830 new_ucmd!()
831 .arg("--algorithm=blake2b")
832 .arg("--length")
833 .arg(l)
834 .arg("lorem_ipsum.txt")
835 .fails_with_code(1)
836 .no_stdout()
837 .stderr_contains(format!("invalid length: '{l}'"));
838 }
839 }
840
841 #[test]
842 fn test_blake2b_length_is_zero() {
843 new_ucmd!()
844 .arg("--length=0")
845 .arg("--algorithm=blake2b")
846 .arg("lorem_ipsum.txt")
847 .arg("alice_in_wonderland.txt")
848 .succeeds()
849 .no_stderr()
850 .stdout_is_fixture("length_is_zero.expected");
851 }
852
853 #[test]
854 fn test_blake2b_length_repeated() {
855 new_ucmd!()
856 .arg("--length=10")
857 .arg("--length=123456")
858 .arg("--length=0")
859 .arg("--algorithm=blake2b")
860 .arg("lorem_ipsum.txt")
861 .arg("alice_in_wonderland.txt")
862 .succeeds()
863 .no_stderr()
864 .stdout_is_fixture("length_is_zero.expected");
865 }
866
867 #[test]
868 fn test_blake2b_length_invalid() {
869 for len in [
870 "1", "01", // Odd
871 "",
872 ] {
873 new_ucmd!()
874 .arg("--length")
875 .arg(len)
876 .arg("--algorithm=blake2b")
877 .arg("lorem_ipsum.txt")
878 .arg("alice_in_wonderland.txt")
879 .fails_with_code(1)
880 .stderr_contains(format!("invalid length: '{len}'"));
881 }
882 }
883
884 #[apply(test_all_algos)]
885 fn test_raw_single_file(#[case] algo: &str) {
886 new_ucmd!()
887 .arg("--raw")
888 .arg("lorem_ipsum.txt")
889 .arg(format!("--algorithm={algo}"))
890 .succeeds()
891 .no_stderr()
892 .stdout_is_fixture_bytes(format!("raw/{algo}_single_file.expected"));
893 }
894
895 #[test]
896 fn test_raw_multiple_files() {
897 new_ucmd!()
898 .arg("--raw")
899 .arg("lorem_ipsum.txt")
900 .arg("alice_in_wonderland.txt")
901 .fails_with_code(1)
902 .no_stdout()
903 .stderr_contains("cksum: the --raw option is not supported with multiple files");
904 }
905
906 #[test]
907 fn test_base64_raw_conflicts() {
908 new_ucmd!()
909 .arg("--base64")
910 .arg("--raw")
911 .arg("lorem_ipsum.txt")
912 .fails_with_code(1)
913 .no_stdout()
914 .stderr_contains("--base64")
915 .stderr_contains("cannot be used with")
916 .stderr_contains("--raw");
917 }
918
919 #[apply(test_all_algos)]
920 fn test_base64_single_file(#[case] algo: &str) {
921 new_ucmd!()
922 .arg("--base64")
923 .arg("lorem_ipsum.txt")
924 .arg(format!("--algorithm={algo}"))
925 .succeeds()
926 .no_stderr()
927 .stdout_is_fixture_bytes(format!("base64/{algo}_single_file.expected"));
928 }
929
930 #[test]
931 fn test_base64_multiple_files() {
932 new_ucmd!()
933 .arg("--base64")
934 .arg("--algorithm=md5")
935 .arg("lorem_ipsum.txt")
936 .arg("alice_in_wonderland.txt")
937 .succeeds()
938 .no_stderr()
939 .stdout_is_fixture_bytes("base64/md5_multiple_files.expected");
940 }
941
942 #[test]
943 fn test_fail_on_folder() {
944 let (at, mut ucmd) = at_and_ucmd!();
945
946 let folder_name = "a_folder";
947 at.mkdir(folder_name);
948
949 ucmd.arg(folder_name)
950 .fails()
951 .no_stdout()
952 .stderr_contains(format!("cksum: {folder_name}: Is a directory"));
953 }
954
955 #[apply(test_all_algos)]
956 fn test_all_algorithms_fail_on_folder(#[case] algo: &str) {
957 let scene = TestScenario::new(util_name!());
958
959 let at = &scene.fixtures;
960
961 let folder_name = "a_folder";
962 at.mkdir(folder_name);
963
964 scene
965 .ucmd()
966 .arg(format!("--algorithm={algo}"))
967 .arg(folder_name)
968 .fails()
969 .no_stdout()
970 .stderr_contains(format!("cksum: {folder_name}: Is a directory"));
971 }
972
973 #[cfg(unix)]
974 #[test]
975 fn test_check_error_incorrect_format() {
976 let scene = TestScenario::new(util_name!());
977 let at = &scene.fixtures;
978 at.write("checksum", "e5773576fc75ff0f8eba14f61587ae28 README.md");
979
980 scene
981 .ucmd()
982 .arg("-c")
983 .arg("checksum")
984 .fails()
985 .stderr_contains("no properly formatted checksum lines found");
986 }
987
988 #[cfg(unix)]
989 #[test]
990 fn test_dev_null() {
991 let scene = TestScenario::new(util_name!());
992
993 scene
994 .ucmd()
995 .arg("--tag")
996 .arg("--untagged")
997 .arg("--algorithm=md5")
998 .arg("/dev/null")
999 .succeeds()
1000 .stdout_contains("d41d8cd98f00b204e9800998ecf8427e ");
1001 }
1002
1003 #[cfg(unix)]
1004 #[test]
1005 fn test_blake2b_default_length() {
1006 let scene = TestScenario::new(util_name!());
1007 let at = &scene.fixtures;
1008
1009 at.touch("f");
1010
1011 scene
1012 .ucmd()
1013 .arg("-a")
1014 .arg("blake2b")
1015 .arg("-l512")
1016 .arg("f")
1017 .succeeds()
1018 .stdout_contains("BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce");
1019
1020 // test also the read
1021 at.write("checksum", "BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce");
1022 scene
1023 .ucmd()
1024 .arg("--check")
1025 .arg("checksum")
1026 .succeeds()
1027 .stdout_contains("f: OK");
1028
1029 scene
1030 .ucmd()
1031 .arg("--status")
1032 .arg("--check")
1033 .arg("checksum")
1034 .succeeds()
1035 .no_output();
1036 }
1037
1038 #[test]
1039 fn test_reset_binary() {
1040 let scene = TestScenario::new(util_name!());
1041 let at = &scene.fixtures;
1042
1043 at.touch("f");
1044
1045 scene
1046 .ucmd()
1047 .arg("--binary")
1048 .arg("--tag")
1049 .arg("--untagged")
1050 .arg("--algorithm=md5")
1051 .arg(at.subdir.join("f"))
1052 .succeeds()
1053 .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
1054 }
1055
1056 #[test]
1057 fn test_reset_binary_but_set() {
1058 let scene = TestScenario::new(util_name!());
1059 let at = &scene.fixtures;
1060
1061 at.touch("f");
1062
1063 scene
1064 .ucmd()
1065 .arg("--binary")
1066 .arg("--tag")
1067 .arg("--untagged")
1068 .arg("--binary")
1069 .arg("--algorithm=md5")
1070 .arg(at.subdir.join("f"))
1071 .succeeds()
1072 .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
1073 }
1074
1075 mod output_format {
1076 use super::*;
1077
1078 #[test]
1079 fn test_text_tag() {
1080 let (at, mut ucmd) = at_and_ucmd!();
1081 at.touch("f");
1082
1083 ucmd.arg("--text")
1084 .arg("--tag")
1085 .args(&["-a", "md5"])
1086 .arg(at.subdir.join("f"))
1087 .fails();
1088 }
1089
1090 #[test]
1091 fn test_text_no_untagged() {
1092 let (at, mut ucmd) = at_and_ucmd!();
1093 at.touch("f");
1094
1095 // --text without --untagged fails
1096 ucmd.arg("--text")
1097 .args(&["-a", "md5"])
1098 .arg(at.subdir.join("f"))
1099 .fails_with_code(1)
1100 .stderr_contains("--text mode is only supported with --untagged");
1101 }
1102
1103 #[test]
1104 fn test_text_binary() {
1105 let (at, mut ucmd) = at_and_ucmd!();
1106 at.touch("f");
1107
1108 // --binary overwrites --text, thus no error is raised
1109 ucmd.arg("--text")
1110 .arg("--binary")
1111 .args(&["-a", "md5"])
1112 .arg(at.subdir.join("f"))
1113 .succeeds()
1114 // No --untagged, tagged output is used
1115 .stdout_contains("f) = d41d8cd98f00b204e9800998ecf8427e");
1116 }
1117
1118 #[test]
1119 fn test_text_binary_untagged() {
1120 let (at, mut ucmd) = at_and_ucmd!();
1121 at.touch("f");
1122
1123 // --binary overwrites --text
1124 ucmd.arg("--text")
1125 .arg("--binary")
1126 .arg("--untagged")
1127 .args(&["-a", "md5"])
1128 .arg(at.subdir.join("f"))
1129 .succeeds()
1130 // Untagged output is used
1131 .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
1132 }
1133 }
1134
1135 #[test]
1136 fn test_binary_file() {
1137 let scene = TestScenario::new(util_name!());
1138 let at = &scene.fixtures;
1139
1140 at.touch("f");
1141
1142 scene
1143 .ucmd()
1144 .arg("--untagged")
1145 .arg("-b")
1146 .arg("--algorithm=md5")
1147 .arg(at.subdir.join("f"))
1148 .succeeds()
1149 .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
1150
1151 scene
1152 .ucmd()
1153 .arg("--tag")
1154 .arg("--untagged")
1155 .arg("--binary")
1156 .arg("--algorithm=md5")
1157 .arg(at.subdir.join("f"))
1158 .succeeds()
1159 .stdout_contains("d41d8cd98f00b204e9800998ecf8427e *");
1160
1161 scene
1162 .ucmd()
1163 .arg("--tag")
1164 .arg("--untagged")
1165 .arg("--binary")
1166 .arg("--algorithm=md5")
1167 .arg("raw/blake2b_single_file.expected")
1168 .succeeds()
1169 .stdout_contains("7e297c07ed8e053600092f91bdd1dad7 *");
1170
1171 new_ucmd!()
1172 .arg("--tag")
1173 .arg("--untagged")
1174 .arg("--binary")
1175 .arg("--algorithm=md5")
1176 .arg("lorem_ipsum.txt")
1177 .succeeds()
1178 .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n");
1179
1180 new_ucmd!()
1181 .arg("--untagged")
1182 .arg("--binary")
1183 .arg("--algorithm=md5")
1184 .arg("lorem_ipsum.txt")
1185 .succeeds()
1186 .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n");
1187
1188 new_ucmd!()
1189 .arg("--binary")
1190 .arg("--untagged")
1191 .arg("--algorithm=md5")
1192 .arg("lorem_ipsum.txt")
1193 .succeeds()
1194 .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n");
1195
1196 new_ucmd!()
1197 .arg("-a")
1198 .arg("md5")
1199 .arg("--binary")
1200 .arg("--untagged")
1201 .arg("lorem_ipsum.txt")
1202 .succeeds()
1203 .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n");
1204
1205 new_ucmd!()
1206 .arg("-a")
1207 .arg("md5")
1208 .arg("--binary")
1209 .arg("--tag")
1210 .arg("--untagged")
1211 .arg("lorem_ipsum.txt")
1212 .succeeds()
1213 .stdout_is("cd724690f7dc61775dfac400a71f2caa *lorem_ipsum.txt\n");
1214 }
1215
1216 #[test]
1217 fn test_folder_and_file() {
1218 let scene = TestScenario::new(util_name!());
1219
1220 let at = &scene.fixtures;
1221
1222 let folder_name = "a_folder";
1223 at.mkdir(folder_name);
1224
1225 scene
1226 .ucmd()
1227 .arg(folder_name)
1228 .arg("lorem_ipsum.txt")
1229 .fails()
1230 .stderr_contains(format!("cksum: {folder_name}: Is a directory"))
1231 .stdout_is_fixture("crc_single_file.expected");
1232 }
1233
1234 #[test]
1235 fn test_conflicting_options() {
1236 let scene = TestScenario::new(util_name!());
1237
1238 let at = &scene.fixtures;
1239
1240 at.touch("f");
1241
1242 scene
1243 .ucmd()
1244 .arg("--binary")
1245 .arg("--check")
1246 .arg("f")
1247 .fails_with_code(1)
1248 .no_stdout()
1249 .stderr_contains(
1250 "cksum: the --binary and --text options are meaningless when verifying checksums",
1251 );
1252
1253 scene
1254 .ucmd()
1255 .arg("--tag")
1256 .arg("-c")
1257 .arg("-a")
1258 .arg("md5")
1259 .fails_with_code(1)
1260 .no_stdout()
1261 .stderr_contains("cksum: the --tag option is meaningless when verifying checksums");
1262 }
1263
1264 #[test]
1265 fn test_check_algo_err() {
1266 let scene = TestScenario::new(util_name!());
1267
1268 let at = &scene.fixtures;
1269
1270 at.touch("f");
1271
1272 scene
1273 .ucmd()
1274 .arg("-a")
1275 .arg("sm3")
1276 .arg("--check")
1277 .arg("f")
1278 .fails_with_code(1)
1279 .no_stdout()
1280 .stderr_contains("cksum: f: no properly formatted checksum lines found");
1281 }
1282
1283 #[test]
1284 fn test_check_pipe() {
1285 let scene = TestScenario::new(util_name!());
1286
1287 let at = &scene.fixtures;
1288
1289 at.touch("f");
1290
1291 scene
1292 .ucmd()
1293 .arg("--check")
1294 .arg("-")
1295 .pipe_in("f")
1296 .fails_with_code(1)
1297 .no_stdout()
1298 .stderr_contains("cksum: 'standard input': no properly formatted checksum lines found");
1299 }
1300
1301 #[test]
1302 fn test_cksum_check_empty_line() {
1303 let scene = TestScenario::new(util_name!());
1304 let at = &scene.fixtures;
1305
1306 at.touch("f");
1307 at.write("CHECKSUM", "\
1308 SHA384 (f) = 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b\n\
1309 BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n\
1310 BLAKE2b-384 (f) = b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100\n\
1311 SM3 (f) = 1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b\n\n");
1312 scene
1313 .ucmd()
1314 .arg("--check")
1315 .arg("CHECKSUM")
1316 .succeeds()
1317 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n")
1318 .stderr_does_not_contain("line is improperly formatted");
1319 }
1320
1321 #[test]
1322 fn test_cksum_check_space() {
1323 let scene = TestScenario::new(util_name!());
1324 let at = &scene.fixtures;
1325
1326 at.touch("f");
1327 at.write("CHECKSUM", "\
1328 SHA384 (f) = 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b\n\
1329 BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n\
1330 BLAKE2b-384 (f) = b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100\n\
1331 SM3 (f) = 1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b\n \n");
1332 scene
1333 .ucmd()
1334 .arg("--check")
1335 .arg("CHECKSUM")
1336 .succeeds()
1337 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n")
1338 .stderr_contains("line is improperly formatted");
1339 }
1340
1341 #[test]
1342 fn test_cksum_check_leading_info() {
1343 let scene = TestScenario::new(util_name!());
1344 let at = &scene.fixtures;
1345
1346 at.touch("f");
1347 at.write("CHECKSUM", "\
1348 \\SHA384 (f) = 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b\n\
1349 \\BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n\
1350 \\BLAKE2b-384 (f) = b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100\n\
1351 \\SM3 (f) = 1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b\n");
1352 scene
1353 .ucmd()
1354 .arg("--check")
1355 .arg("CHECKSUM")
1356 .succeeds()
1357 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n");
1358 }
1359
1360 #[test]
1361 fn test_cksum_check() {
1362 let scene = TestScenario::new(util_name!());
1363 let at = &scene.fixtures;
1364
1365 at.touch("f");
1366 at.write("CHECKSUM", "\
1367 SHA384 (f) = 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b\n\
1368 BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n\
1369 BLAKE2b-384 (f) = b32811423377f52d7862286ee1a72ee540524380fda1724a6f25d7978c6fd3244a6caf0498812673c5e05ef583825100\n\
1370 SM3 (f) = 1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b\n");
1371 scene
1372 .ucmd()
1373 .arg("--check")
1374 .arg("CHECKSUM")
1375 .succeeds()
1376 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n")
1377 .stderr_does_not_contain("line is improperly formatted");
1378 scene
1379 .ucmd()
1380 .arg("--check")
1381 .arg("--strict")
1382 .arg("CHECKSUM")
1383 .succeeds()
1384 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n")
1385 .stderr_does_not_contain("line is improperly formatted");
1386 // inject invalid content
1387 at.append("CHECKSUM", "incorrect data");
1388 scene
1389 .ucmd()
1390 .arg("--check")
1391 .arg("CHECKSUM")
1392 .succeeds()
1393 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n")
1394 .stderr_contains("line is improperly formatted");
1395 scene
1396 .ucmd()
1397 .arg("--check")
1398 .arg("--strict")
1399 .arg("CHECKSUM")
1400 .fails()
1401 .stdout_contains("f: OK\nf: OK\nf: OK\nf: OK\n")
1402 .stderr_contains("line is improperly formatted");
1403 }
1404
1405 #[test]
1406 fn test_cksum_check_case() {
1407 let scene = TestScenario::new(util_name!());
1408 let at = &scene.fixtures;
1409
1410 at.touch("f");
1411 at.write(
1412 "CHECKSUM",
1413 "Sha1 (f) = da39a3ee5e6b4b0d3255bfef95601890afd80709\n",
1414 );
1415 scene.ucmd().arg("--check").arg("CHECKSUM").fails();
1416 }
1417
1418 #[test]
1419 fn test_cksum_check_invalid() {
1420 let scene = TestScenario::new(util_name!());
1421 let at = &scene.fixtures;
1422 let commands = [vec!["-a", "sha384"]];
1423 at.touch("f");
1424 at.touch("CHECKSUM");
1425 for command in &commands {
1426 let result = scene.ucmd().args(command).arg("f").succeeds();
1427 at.append("CHECKSUM", result.stdout_str());
1428 }
1429 // inject invalid content
1430 at.append("CHECKSUM", "again incorrect data\naze\n");
1431 scene
1432 .ucmd()
1433 .arg("--check")
1434 .arg("--strict")
1435 .arg("CHECKSUM")
1436 .fails()
1437 .stdout_contains("f: OK\n")
1438 .stderr_contains("2 lines");
1439
1440 // without strict, it passes
1441 scene
1442 .ucmd()
1443 .arg("--check")
1444 .arg("CHECKSUM")
1445 .succeeds()
1446 .stdout_contains("f: OK\n")
1447 .stderr_contains("2 lines");
1448 }
1449
1450 #[test]
1451 fn test_cksum_check_failed() {
1452 let scene = TestScenario::new(util_name!());
1453 let at = &scene.fixtures;
1454 let commands = [vec!["-a", "sha384"]];
1455 at.touch("f");
1456 at.touch("CHECKSUM");
1457 for command in &commands {
1458 let result = scene.ucmd().args(command).arg("f").succeeds();
1459 at.append("CHECKSUM", result.stdout_str());
1460 }
1461 // inject invalid content
1462 at.append("CHECKSUM", "again incorrect data\naze\nSM3 (input) = 7cfb120d4fabea2a904948538a438fdb57c725157cb40b5aee8d937b8351477e\n");
1463
1464 let result = scene
1465 .ucmd()
1466 .arg("--check")
1467 .arg("--strict")
1468 .arg("CHECKSUM")
1469 .fails();
1470
1471 assert!(
1472 result
1473 .stderr_str()
1474 .contains("input: No such file or directory")
1475 );
1476 assert!(
1477 result
1478 .stderr_str()
1479 .contains("2 lines are improperly formatted\n")
1480 );
1481 assert!(
1482 result
1483 .stderr_str()
1484 .contains("1 listed file could not be read\n")
1485 );
1486 assert!(result.stdout_str().contains("f: OK\n"));
1487
1488 // without strict
1489 let result = scene.ucmd().arg("--check").arg("CHECKSUM").fails();
1490
1491 assert!(
1492 result
1493 .stderr_str()
1494 .contains("input: No such file or directory")
1495 );
1496 assert!(
1497 result
1498 .stderr_str()
1499 .contains("2 lines are improperly formatted\n")
1500 );
1501 assert!(
1502 result
1503 .stderr_str()
1504 .contains("1 listed file could not be read\n")
1505 );
1506 assert!(result.stdout_str().contains("f: OK\n"));
1507
1508 // tests with two files
1509 at.touch("CHECKSUM2");
1510 at.write("f2", "42");
1511 for command in &commands {
1512 let result = scene.ucmd().args(command).arg("f2").succeeds();
1513 at.append("CHECKSUM2", result.stdout_str());
1514 }
1515 // inject invalid content
1516 at.append("CHECKSUM2", "again incorrect data\naze\nSM3 (input2) = 7cfb120d4fabea2a904948538a438fdb57c725157cb40b5aee8d937b8351477e\n");
1517 at.append("CHECKSUM2", "again incorrect data\naze\nSM3 (input2) = 7cfb120d4fabea2a904948538a438fdb57c725157cb40b5aee8d937b8351477e\n");
1518
1519 let result = scene
1520 .ucmd()
1521 .arg("--check")
1522 .arg("CHECKSUM")
1523 .arg("CHECKSUM2")
1524 .fails();
1525 println!("result.stderr_str() {}", result.stderr_str());
1526 println!("result.stdout_str() {}", result.stdout_str());
1527 assert!(
1528 result
1529 .stderr_str()
1530 .contains("input2: No such file or directory")
1531 );
1532 assert!(
1533 result
1534 .stderr_str()
1535 .contains("4 lines are improperly formatted\n")
1536 );
1537 assert!(
1538 result
1539 .stderr_str()
1540 .contains("2 listed files could not be read\n")
1541 );
1542 assert!(result.stdout_str().contains("f: OK\n"));
1543 assert!(result.stdout_str().contains("2: OK\n"));
1544 }
1545
1546 #[test]
1547 fn test_check_md5_format() {
1548 let scene = TestScenario::new(util_name!());
1549
1550 let at = &scene.fixtures;
1551 at.touch("empty");
1552 at.write("f", "d41d8cd98f00b204e9800998ecf8427e *empty\n");
1553
1554 scene
1555 .ucmd()
1556 .arg("-a")
1557 .arg("md5")
1558 .arg("--check")
1559 .arg("f")
1560 .succeeds()
1561 .stdout_contains("empty: OK");
1562
1563 // with a second file
1564 at.write("not-empty", "42");
1565 at.write("f2", "a1d0c6e83f027327d8461063f4ac58a6 *not-empty\n");
1566
1567 scene
1568 .ucmd()
1569 .arg("-a")
1570 .arg("md5")
1571 .arg("--check")
1572 .arg("f")
1573 .arg("f2")
1574 .succeeds()
1575 .stdout_contains("empty: OK")
1576 .stdout_contains("not-empty: OK");
1577 }
1578
1579 // The digest in check mode must be computed over the raw bytes of the file:
1580 // on Windows, no CRLF -> LF conversion must happen (matching generation).
1581 #[test]
1582 fn test_check_crlf_file_hashed_as_raw_bytes() {
1583 let scene = TestScenario::new(util_name!());
1584 let at = &scene.fixtures;
1585
1586 // md5 of the raw bytes b"abc\r\nd\r"; hashing with CRLF converted to LF
1587 // would yield b7597fc3635cfc9b646eee54636dfa5a instead.
1588 at.write_bytes("f", b"abc\r\nd\r");
1589 at.write("CHECKSUM", "e5bd2e073913d2cb78174d5be11ab1e9 f\n");
1590
1591 scene
1592 .ucmd()
1593 .arg("-a")
1594 .arg("md5")
1595 .arg("--check")
1596 .arg("CHECKSUM")
1597 .succeeds()
1598 .stdout_contains("f: OK");
1599
1600 // Same with the binary marker `*`.
1601 at.write("CHECKSUM_BINARY", "e5bd2e073913d2cb78174d5be11ab1e9 *f\n");
1602
1603 scene
1604 .ucmd()
1605 .arg("-a")
1606 .arg("md5")
1607 .arg("--check")
1608 .arg("CHECKSUM_BINARY")
1609 .succeeds()
1610 .stdout_contains("f: OK");
1611 }
1612
1613 // Manage the mixed behavior
1614 // cksum --check -a sm3 CHECKSUMS
1615 // when CHECKSUM contains among other lines:
1616 // SHA384 (input) = f392fd0ae43879ced890c665a1d47179116b5eddf6fb5b49f4982746418afdcbd54ba5eedcd422af3592f57f666da285
1617 #[test]
1618 fn test_cksum_mixed() {
1619 let scene = TestScenario::new(util_name!());
1620 let at = &scene.fixtures;
1621 let commands = [
1622 vec!["-a", "sha384"],
1623 vec!["-a", "blake2b"],
1624 vec!["-a", "blake2b", "-l", "384"],
1625 vec!["-a", "sm3"],
1626 ];
1627 at.touch("f");
1628 at.touch("CHECKSUM");
1629 for command in &commands {
1630 let result = scene.ucmd().args(command).arg("f").succeeds();
1631 at.append("CHECKSUM", result.stdout_str());
1632 }
1633 println!("Content of CHECKSUM:\n{}", at.read("CHECKSUM"));
1634 let result = scene
1635 .ucmd()
1636 .arg("--check")
1637 .arg("-a")
1638 .arg("sm3")
1639 .arg("CHECKSUM")
1640 .succeeds();
1641
1642 println!("result.stderr_str() {}", result.stderr_str());
1643 println!("result.stdout_str() {}", result.stdout_str());
1644 assert!(result.stdout_str().contains("f: OK"));
1645 assert!(
1646 result
1647 .stderr_str()
1648 .contains("3 lines are improperly formatted")
1649 );
1650 }
1651
1652 #[test]
1653 fn test_cksum_garbage() {
1654 let scene = TestScenario::new(util_name!());
1655 let at = &scene.fixtures;
1656
1657 // Incorrect data at the start
1658 at.write(
1659 "check-file",
1660 "garbage MD5 (README.md) = e5773576fc75ff0f8eba14f61587ae28",
1661 );
1662 scene
1663 .ucmd()
1664 .arg("--check")
1665 .arg("check-file")
1666 .fails()
1667 .stderr_contains("check-file: no properly formatted checksum lines found");
1668
1669 // Incorrect data at the end
1670 at.write(
1671 "check-file",
1672 "MD5 (README.md) = e5773576fc75ff0f8eba14f61587ae28 garbage",
1673 );
1674 scene
1675 .ucmd()
1676 .arg("--check")
1677 .arg("check-file")
1678 .fails()
1679 .stderr_contains("check-file: no properly formatted checksum lines found");
1680 }
1681
1682 #[test]
1683 fn test_md5_bits() {
1684 let (at, mut ucmd) = at_and_ucmd!();
1685 at.write(
1686 "f",
1687 "MD5-65536 (README.md) = e5773576fc75ff0f8eba14f61587ae28\n",
1688 );
1689
1690 ucmd.arg("-c")
1691 .arg("f")
1692 .fails()
1693 .stderr_contains("f: no properly formatted checksum lines found");
1694 }
1695
1696 #[test]
1697 fn test_blake2b_check_digest_too_long() {
1698 let (at, mut ucmd) = at_and_ucmd!();
1699 // The referenced file must exist: the digest length is only used once the
1700 // line is accepted and the file is about to be hashed.
1701 at.write("f1", "content\n");
1702 // 65 bytes, above the 64 bytes BLAKE2b maximum.
1703 at.write("sums", &format!("{} f1\n", "a".repeat(130)));
1704
1705 ucmd.args(&["-a", "blake2b", "-c", "sums"])
1706 .fails_with_code(1)
1707 .stderr_contains("sums: no properly formatted checksum lines found");
1708 }
1709
1710 #[test]
1711 fn test_blake2b_bits() {
1712 let (at, mut ucmd) = at_and_ucmd!();
1713 at.write(
1714 "f",
1715 "BLAKE2b-257 (README.md) = f9a984b70cf9a7549920864860fd1131c9fb6c0552def0b6dcce1d87b4ec4c5d\n"
1716 );
1717
1718 ucmd.arg("-c")
1719 .arg("f")
1720 .fails()
1721 .stderr_contains("f: no properly formatted checksum lines found");
1722 }
1723
1724 #[test]
1725 fn test_bsd_case() {
1726 let scene = TestScenario::new(util_name!());
1727 let at = &scene.fixtures;
1728 at.write("f", "bSD (README.md) = 0000\n");
1729
1730 scene
1731 .ucmd()
1732 .arg("-c")
1733 .arg("f")
1734 .fails()
1735 .stderr_contains("f: no properly formatted checksum lines found");
1736 at.write("f", "BsD (README.md) = 0000\n");
1737
1738 scene
1739 .ucmd()
1740 .arg("-c")
1741 .arg("f")
1742 .fails()
1743 .stderr_contains("f: no properly formatted checksum lines found");
1744 }
1745
1746 #[test]
1747 fn test_blake2d_tested_with_sha1() {
1748 let (at, mut ucmd) = at_and_ucmd!();
1749 at.write(
1750 "f",
1751 "BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n"
1752 );
1753
1754 ucmd.arg("-a")
1755 .arg("sha1")
1756 .arg("-c")
1757 .arg("f")
1758 .fails()
1759 .stderr_contains("f: no properly formatted checksum lines found");
1760 }
1761
1762 #[test]
1763 fn test_unknown_sha() {
1764 let (at, mut ucmd) = at_and_ucmd!();
1765 at.write("f", "SHA4 (README.md) = 00000000\n");
1766
1767 ucmd.arg("-c")
1768 .arg("f")
1769 .fails()
1770 .stderr_contains("f: no properly formatted checksum lines found");
1771 }
1772
1773 #[test]
1774 fn test_check_directory_error() {
1775 let (at, mut ucmd) = at_and_ucmd!();
1776
1777 at.mkdir("d");
1778 at.write(
1779 "f",
1780 "BLAKE2b (d) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n"
1781 );
1782 #[cfg(not(windows))]
1783 let err_msg = "cksum: d: Is a directory\n";
1784 #[cfg(windows)]
1785 let err_msg = "cksum: d: Permission denied\n";
1786 ucmd.arg("--check")
1787 .arg(at.subdir.join("f"))
1788 .fails()
1789 .stderr_contains(err_msg);
1790 }
1791
1792 #[test]
1793 fn test_check_base64_hashes() {
1794 let hashes = "MD5 (empty) = 1B2M2Y8AsgTpgAmY7PhCfg==\nSHA256 (empty) = 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\nBLAKE2b (empty) = eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg==\n";
1795
1796 let scene = TestScenario::new(util_name!());
1797 let at = &scene.fixtures;
1798
1799 at.touch("empty");
1800 at.write("check", hashes);
1801
1802 scene
1803 .ucmd()
1804 .arg("--check")
1805 .arg(at.subdir.join("check"))
1806 .succeeds()
1807 .stdout_is("empty: OK\nempty: OK\nempty: OK\n");
1808 }
1809
1810 #[test]
1811 fn test_several_files_error_mgmt() {
1812 let scene = TestScenario::new(util_name!());
1813 let at = &scene.fixtures;
1814
1815 // don't exist
1816 scene
1817 .ucmd()
1818 .arg("--check")
1819 .arg("empty")
1820 .arg("incorrect")
1821 .fails()
1822 .stderr_contains("empty: No such file ")
1823 .stderr_contains("incorrect: No such file ");
1824
1825 at.touch("empty");
1826 at.touch("incorrect");
1827
1828 // exists but incorrect
1829 scene
1830 .ucmd()
1831 .arg("--check")
1832 .arg("empty")
1833 .arg("incorrect")
1834 .fails()
1835 .stderr_contains("empty: no properly ")
1836 .stderr_contains("incorrect: no properly ");
1837 }
1838
1839 #[test]
1840 fn test_check_unknown_checksum_file() {
1841 let scene = TestScenario::new(util_name!());
1842
1843 scene
1844 .ucmd()
1845 .arg("--check")
1846 .arg("missing")
1847 .fails()
1848 .stderr_only("cksum: missing: No such file or directory\n");
1849 }
1850
1851 #[test]
1852 fn test_check_comment_line() {
1853 // A comment in a checksum file shall be discarded unnoticed.
1854
1855 let scene = TestScenario::new(util_name!());
1856 let at = &scene.fixtures;
1857
1858 at.write("foo", "foo-content\n");
1859 at.write(
1860 "CHECKSUM-sha1",
1861 "\
1862 # This is a comment\n\
1863 SHA1 (foo) = 058ab38dd3603703b3a7063cf95dc51a4286b6fe\n\
1864 # next comment is empty\n#",
1865 );
1866
1867 scene
1868 .ucmd()
1869 .arg("--check")
1870 .arg("CHECKSUM-sha1")
1871 .succeeds()
1872 .stdout_contains("foo: OK")
1873 .no_stderr();
1874 }
1875
1876 #[test]
1877 fn test_check_comment_only() {
1878 // A file only filled with comments is equivalent to an empty file,
1879 // and therefore produces an error.
1880
1881 let scene = TestScenario::new(util_name!());
1882 let at = &scene.fixtures;
1883
1884 at.write("CHECKSUM", "# This is a comment\n");
1885
1886 scene
1887 .ucmd()
1888 .arg("--check")
1889 .arg("CHECKSUM")
1890 .fails()
1891 .stderr_contains("no properly formatted checksum lines found");
1892 }
1893
1894 #[test]
1895 fn test_check_comment_leading_space() {
1896 // A comment must have its '#' in first position on the line.
1897 // A space before it will raise a warning for improperly formatted line.
1898
1899 let scene = TestScenario::new(util_name!());
1900 let at = &scene.fixtures;
1901
1902 at.write("foo", "foo-content\n");
1903 at.write(
1904 "CHECKSUM-sha1",
1905 " # This is a comment\n\
1906 SHA1 (foo) = 058ab38dd3603703b3a7063cf95dc51a4286b6fe\n",
1907 );
1908
1909 scene
1910 .ucmd()
1911 .arg("--check")
1912 .arg("CHECKSUM-sha1")
1913 .succeeds()
1914 .stdout_contains("foo: OK")
1915 .stderr_contains("WARNING: 1 line is improperly formatted");
1916 }
1917
1918 /// This test checks alignment with GNU's error handling.
1919 /// Windows has a different logic and is guarded by [`test_check_directory_error`].
1920 #[cfg(not(windows))]
1921 #[test]
1922 fn test_check_failed_to_read() {
1923 // check `cksum`'s behavior when encountering directories or non existing files
1924
1925 let scene = TestScenario::new(util_name!());
1926 let at = &scene.fixtures;
1927
1928 at.write(
1929 "CHECKSUM",
1930 "SHA1 (dir) = ffffffffffffffffffffffffffffffffffffffff\n\
1931 SHA1 (not-file) = ffffffffffffffffffffffffffffffffffffffff\n",
1932 );
1933 at.mkdir("dir");
1934
1935 scene
1936 .ucmd()
1937 .arg("--check")
1938 .arg("CHECKSUM")
1939 .fails()
1940 .stdout_is(
1941 "dir: FAILED open or read\n\
1942 not-file: FAILED open or read\n",
1943 )
1944 .stderr_contains("cksum: WARNING: 2 listed files could not be read");
1945
1946 // check with `--ignore-missing`
1947 scene
1948 .ucmd()
1949 .arg("--check")
1950 .arg("CHECKSUM")
1951 .arg("--ignore-missing")
1952 .fails()
1953 .stdout_is("dir: FAILED open or read\n")
1954 .stderr_contains("cksum: WARNING: 1 listed file could not be read");
1955 }
1956
1957 #[test]
1958 fn test_zero_multiple_file() {
1959 new_ucmd!()
1960 .arg("-z")
1961 .arg("alice_in_wonderland.txt")
1962 .arg("lorem_ipsum.txt")
1963 .succeeds()
1964 .stdout_is_fixture("zero_multiple_file.expected");
1965 }
1966
1967 #[test]
1968 fn test_zero_single_file() {
1969 new_ucmd!()
1970 .arg("--zero")
1971 .arg("alice_in_wonderland.txt")
1972 .succeeds()
1973 .stdout_is_fixture("zero_single_file.expected");
1974 }
1975
1976 #[test]
1977 fn test_check_trailing_space_fails() {
1978 // If a checksum line has trailing spaces after the digest,
1979 // it shall be considered improperly formatted.
1980
1981 let scene = TestScenario::new(util_name!());
1982 let at = &scene.fixtures;
1983
1984 at.write("foo", "foo-content\n");
1985 at.write(
1986 "CHECKSUM",
1987 "SHA1 (foo) = 058ab38dd3603703b3a7063cf95dc51a4286b6fe \n",
1988 );
1989
1990 scene
1991 .ucmd()
1992 .arg("--check")
1993 .arg("CHECKSUM")
1994 .fails()
1995 .no_stdout()
1996 .stderr_contains("CHECKSUM: no properly formatted checksum lines found");
1997 }
1998
1999 /// Regroup tests related to the handling of non-utf-8 content
2000 /// in checksum files.
2001 /// These tests are excluded from Windows because it does not provide any safe
2002 /// conversion between `OsString` and byte sequences for non-utf-8 strings.
2003 mod check_encoding {
2004
2005 // This test should pass on linux and macos.
2006 #[cfg(not(windows))]
2007 #[test]
2008 fn test_check_non_utf8_comment() {
2009 use super::*;
2010 let hashes =
2011 b"MD5 (empty) = 1B2M2Y8AsgTpgAmY7PhCfg==\n\
2012 # Comment with a non utf8 char: >>\xff<<\n\
2013 SHA256 (empty) = 47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=\n\
2014 BLAKE2b (empty) = eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg==\n"
2015 ;
2016
2017 let (at, mut cmd) = at_and_ucmd!();
2018
2019 at.touch("empty");
2020 at.write_bytes("check", hashes);
2021
2022 cmd.arg("--check")
2023 .arg(at.subdir.join("check"))
2024 .succeeds()
2025 .stdout_is("empty: OK\nempty: OK\nempty: OK\n")
2026 .no_stderr();
2027 }
2028
2029 // This test should pass on linux. Windows and macos will fail to
2030 // create a file which name contains '\xff'.
2031 #[cfg(target_os = "linux")]
2032 #[test]
2033 fn test_check_non_utf8_filename() {
2034 use super::*;
2035 use std::{ffi::OsString, os::unix::ffi::OsStringExt};
2036
2037 let scene = TestScenario::new(util_name!());
2038 let at = &scene.fixtures;
2039 let filename: OsString = OsStringExt::from_vec(b"funky\xffname".to_vec());
2040 at.touch(filename);
2041
2042 // Checksum match
2043 at.write_bytes("check",
2044 b"SHA256 (funky\xffname) = e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855\n");
2045 scene
2046 .ucmd()
2047 .arg("--check")
2048 .arg(at.subdir.join("check"))
2049 .succeeds()
2050 .stdout_is_bytes(b"'funky'$'\\377''name': OK\n")
2051 .no_stderr();
2052
2053 // Checksum mismatch
2054 at.write_bytes("check",
2055 b"SHA256 (funky\xffname) = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n");
2056 scene
2057 .ucmd()
2058 .arg("--check")
2059 .arg(at.subdir.join("check"))
2060 .fails()
2061 .stdout_is_bytes(b"'funky'$'\\377''name': FAILED\n")
2062 .stderr_contains("1 computed checksum did NOT match");
2063
2064 // file not found
2065 at.write_bytes("check",
2066 b"SHA256 (flakey\xffname) = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n");
2067 scene
2068 .ucmd()
2069 .arg("--check")
2070 .arg(at.subdir.join("check"))
2071 .fails()
2072 .stdout_is_bytes(b"'flakey'$'\\377''name': FAILED open or read\n")
2073 .stderr_contains("1 listed file could not be read");
2074 }
2075
2076 #[cfg(target_os = "linux")]
2077 #[test]
2078 fn test_quoting_in_stderr() {
2079 use super::*;
2080 use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
2081
2082 let (at, mut cmd) = at_and_ucmd!();
2083
2084 at.mkdir(<OsStr as OsStrExt>::from_bytes(b"FFF\xffDIR"));
2085 at.write_bytes(
2086 "check",
2087 b"SHA256 (FFF\xffFFF) = 29953405eaa3dcc41c37d1621d55b6a47eee93e05613e439e73295029740b10c\nSHA256 (FFF\xffDIR) = 29953405eaa3dcc41c37d1621d55b6a47eee93e05613e439e73295029740b10c\n",
2088 );
2089
2090 cmd.arg("-c")
2091 .arg("check")
2092 .fails_with_code(1)
2093 .stdout_contains_bytes(b"'FFF'$'\\377''FFF': FAILED open or read")
2094 .stdout_contains_bytes(b"'FFF'$'\\377''DIR': FAILED open or read")
2095 .stderr_contains("'FFF'$'\\377''FFF': No such file or directory")
2096 .stderr_contains("'FFF'$'\\377''DIR': Is a directory");
2097 }
2098 }
2099
2100 #[test]
2101 fn test_check_blake_length_guess() {
2102 let correct_lines = [
2103 // Correct: The length is not explicit, but the checksum's size
2104 // matches the default parameter.
2105 "BLAKE2b (foo.dat) = ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d",
2106 // Correct: The length is explicitly given, and the checksum's size
2107 // matches the length.
2108 "BLAKE2b-512 (foo.dat) = ca002330e69d3e6b84a46a56a6533fd79d51d97a3bb7cad6c2ff43b354185d6dc1e723fb3db4ae0737e120378424c714bb982d9dc5bbd7a0ab318240ddd18f8d",
2109 // Correct: the checksum size is not default but
2110 // the length is explicitly given.
2111 "BLAKE2b-48 (foo.dat) = 171cdfdf84ed",
2112 ];
2113 let scene = TestScenario::new(util_name!());
2114 let at = &scene.fixtures;
2115
2116 at.write("foo.dat", "foo");
2117
2118 for line in correct_lines {
2119 at.write("foo.sums", line);
2120 scene
2121 .ucmd()
2122 .arg("--check")
2123 .arg(at.subdir.join("foo.sums"))
2124 .succeeds()
2125 .stdout_is("foo.dat: OK\n");
2126 }
2127
2128 // Incorrect lines
2129
2130 // This is incorrect because the algorithm provides no length,
2131 // and the checksum length is not default.
2132 let incorrect = "BLAKE2b (foo.dat) = 171cdfdf84ed";
2133 at.write("foo.sums", incorrect);
2134 scene
2135 .ucmd()
2136 .arg("--check")
2137 .arg(at.subdir.join("foo.sums"))
2138 .fails()
2139 .stderr_contains("foo.sums: no properly formatted checksum lines found");
2140
2141 // This is incorrect because the length hint provided doesn't match the
2142 // checksum length.
2143 let length_mismatch = "BLAKE2b-8 (foo.dat) = 171cdfdf84ed";
2144 at.write("foo.sums", length_mismatch);
2145 scene
2146 .ucmd()
2147 .arg("--check")
2148 .arg(at.subdir.join("foo.sums"))
2149 .fails()
2150 .stderr_contains("foo.sums: no properly formatted checksum lines found");
2151
2152 // In this case, validation should fail because even though the checksum
2153 // length matches the hint, it is above BLAKE2b's max (512).
2154 let too_long = "BLAKE2b-520 (/dev/null) = 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
2155 at.write("foo.sums", too_long);
2156 scene
2157 .ucmd()
2158 .arg("--check")
2159 .arg(at.subdir.join("foo.sums"))
2160 .fails()
2161 .stderr_contains("foo.sums: no properly formatted checksum lines found");
2162 }
2163
2164 #[test]
2165 fn test_check_confusing_base64() {
2166 let cksum = "BLAKE2b-48 (foo.dat) = fc1f97C4";
2167
2168 let scene = TestScenario::new(util_name!());
2169 let at = &scene.fixtures;
2170
2171 at.write("foo.dat", "esq");
2172 at.write("foo.sums", cksum);
2173
2174 scene
2175 .ucmd()
2176 .arg("--check")
2177 .arg(at.subdir.join("foo.sums"))
2178 .succeeds()
2179 .stdout_is("foo.dat: OK\n");
2180 }
2181
2182 /// This test checks that when a file contains several checksum lines
2183 /// with different encoding, the decoding still works.
2184 #[test]
2185 fn test_check_mix_hex_base64() {
2186 let b64 = "BLAKE2b-128 (foo1.dat) = BBNuJPhdRwRlw9tm5Y7VbA==";
2187 let hex = "BLAKE2b-128 (foo2.dat) = 04136e24f85d470465c3db66e58ed56c";
2188
2189 let scene = TestScenario::new(util_name!());
2190 let at = &scene.fixtures;
2191
2192 at.write("foo1.dat", "foo");
2193 at.write("foo2.dat", "foo");
2194
2195 at.write("hex_b64", &format!("{hex}\n{b64}"));
2196 at.write("b64_hex", &format!("{b64}\n{hex}"));
2197
2198 scene
2199 .ucmd()
2200 .arg("--check")
2201 .arg(at.subdir.join("hex_b64"))
2202 .succeeds()
2203 .stdout_only("foo2.dat: OK\nfoo1.dat: OK\n");
2204
2205 scene
2206 .ucmd()
2207 .arg("--check")
2208 .arg(at.subdir.join("b64_hex"))
2209 .succeeds()
2210 .stdout_only("foo1.dat: OK\nfoo2.dat: OK\n");
2211 }
2212
2213 /// This test ensures that an improperly formatted base64 checksum in a file
2214 /// does not interrupt the processing of next lines.
2215 #[test]
2216 fn test_check_incorrectly_formatted_checksum_keeps_processing_b64() {
2217 let scene = TestScenario::new(util_name!());
2218 let at = &scene.fixtures;
2219 at.touch("f");
2220
2221 let good_ck = "MD5 (f) = 1B2M2Y8AsgTpgAmY7PhCfg=="; // OK
2222 let bad_ck = "MD5 (f) = 1B2M2Y8AsgTpgAmY7PhCfg="; // Missing last '='
2223
2224 // Good then Bad
2225 scene
2226 .ucmd()
2227 .arg("--check")
2228 .pipe_in([good_ck, bad_ck].join("\n").as_bytes().to_vec())
2229 .succeeds()
2230 .stdout_contains("f: OK")
2231 .stderr_contains("cksum: WARNING: 1 line is improperly formatted");
2232
2233 // Bad then Good
2234 scene
2235 .ucmd()
2236 .arg("--check")
2237 .pipe_in([bad_ck, good_ck].join("\n").as_bytes().to_vec())
2238 .succeeds()
2239 .stdout_contains("f: OK")
2240 .stderr_contains("cksum: WARNING: 1 line is improperly formatted");
2241 }
2242
2243 /// This test ensures that an improperly formatted hexadecimal checksum in a
2244 /// file does not interrupt the processing of next lines.
2245 #[test]
2246 fn test_check_incorrectly_formatted_checksum_keeps_processing_hex() {
2247 let scene = TestScenario::new(util_name!());
2248 let at = &scene.fixtures;
2249 at.touch("f");
2250
2251 let good_ck = "MD5 (f) = d41d8cd98f00b204e9800998ecf8427e"; // OK
2252 let bad_ck = "MD5 (f) = d41d8cd98f00b204e9800998ecf8427"; // Missing last
2253
2254 // Good then Bad
2255 scene
2256 .ucmd()
2257 .arg("--check")
2258 .pipe_in([good_ck, bad_ck].join("\n").as_bytes().to_vec())
2259 .succeeds()
2260 .stdout_contains("f: OK")
2261 .stderr_contains("cksum: WARNING: 1 line is improperly formatted");
2262
2263 // Bad then Good
2264 scene
2265 .ucmd()
2266 .arg("--check")
2267 .pipe_in([bad_ck, good_ck].join("\n").as_bytes().to_vec())
2268 .succeeds()
2269 .stdout_contains("f: OK")
2270 .stderr_contains("cksum: WARNING: 1 line is improperly formatted");
2271 }
2272
2273 /// Tests for cksum with base64 output encoding.
2274 mod cksum_base64_encoding {
2275 use super::*;
2276 use uutests::util::log_info;
2277
2278 const PAIRS: [(&str, &str); 12] = [
2279 ("sysv", "0 0 f"),
2280 ("bsd", "00000 0 f"),
2281 ("crc", "4294967295 0 f"),
2282 ("crc32b", "0 0 f"),
2283 ("md5", "1B2M2Y8AsgTpgAmY7PhCfg=="),
2284 ("sha1", "2jmj7l5rSw0yVb/vlWAYkK/YBwk="),
2285 ("sha224", "0UoCjCo6K8lHYQK7KII0xBWisB+CjqYqxbPkLw=="),
2286 ("sha256", "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="),
2287 (
2288 "sha384",
2289 "OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb",
2290 ),
2291 (
2292 "sha512",
2293 "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg==",
2294 ),
2295 (
2296 "blake2b",
2297 "eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg==",
2298 ),
2299 ("sm3", "GrIdg1XPoX+OYRlIMegajyK+yMco/vt0ftA161CCqis="),
2300 ];
2301
2302 fn make_scene() -> TestScenario {
2303 let scene = TestScenario::new(util_name!());
2304 let at = &scene.fixtures;
2305 at.touch("f");
2306
2307 scene
2308 }
2309
2310 fn output_format(algo: &str, digest: &str) -> String {
2311 if ["sysv", "bsd", "crc", "crc32b"].contains(&algo) {
2312 digest.to_string()
2313 } else {
2314 format!("{} (f) = {digest}", algo.to_uppercase()).replace("BLAKE2B", "BLAKE2b")
2315 }
2316 }
2317
2318 #[test]
2319 fn test_cksum_base64_generating() {
2320 // Ensure that each algorithm works with `--base64`.
2321 let scene = make_scene();
2322
2323 for (algo, digest) in PAIRS {
2324 log_info("ALGORITHM", algo);
2325 scene
2326 .ucmd()
2327 .arg("--base64")
2328 .arg("-a")
2329 .arg(algo)
2330 .arg("f")
2331 .succeeds()
2332 .stdout_only(format!("{}\n", output_format(algo, digest)));
2333 }
2334 }
2335
2336 #[test]
2337 fn test_cksum_base64_verify() {
2338 // For each algorithm that accepts `--check`,
2339 // ensure that it works with base64 digests.
2340 let scene = make_scene();
2341
2342 for (algo, digest) in PAIRS {
2343 if ["sysv", "bsd", "crc", "crc32b"].contains(&algo) {
2344 // These algorithms do not accept `--check`
2345 scene
2346 .ucmd()
2347 .arg("--check")
2348 .arg("-a")
2349 .arg(algo)
2350 .fails()
2351 .stderr_only(
2352 "cksum: --check is not supported with --algorithm={bsd,sysv,crc,crc32b}\n",
2353 );
2354 continue;
2355 }
2356
2357 let line = output_format(algo, digest);
2358 scene
2359 .ucmd()
2360 .arg("--check")
2361 .arg("--strict")
2362 .pipe_in(line)
2363 .succeeds()
2364 .stdout_only("f: OK\n");
2365 }
2366 }
2367
2368 #[test]
2369 fn test_cksum_base64_verify_truncated_eq1() {
2370 // For digests ending with '=', ensure `--check` fails if '=' is removed.
2371 let scene = make_scene();
2372
2373 for (algo, digest) in PAIRS {
2374 if !digest.ends_with('=') {
2375 continue;
2376 }
2377
2378 let mut line = output_format(algo, digest);
2379 if line.ends_with('=') {
2380 line.pop();
2381 }
2382
2383 log_info(format!("ALGORITHM: {algo}, STDIN: '{line}'"), "");
2384 scene
2385 .ucmd()
2386 .arg("--check")
2387 .pipe_in(line)
2388 .fails()
2389 .no_stdout()
2390 .stderr_contains("no properly formatted checksum lines found");
2391 }
2392 }
2393
2394 #[test]
2395 fn test_cksum_base64_verify_truncated_eq2() {
2396 // For digests ending with '==',
2397 // ensure `--check` fails if '==' is removed.
2398 let scene = make_scene();
2399
2400 for (algo, digest) in PAIRS {
2401 if !digest.ends_with("==") {
2402 continue;
2403 }
2404
2405 let line = output_format(algo, digest);
2406 let line = line.trim_end_matches("==");
2407
2408 log_info(format!("ALGORITHM: {algo}, STDIN: '{line}'"), "");
2409 scene
2410 .ucmd()
2411 .arg("--check")
2412 .pipe_in(line)
2413 .fails()
2414 .no_stdout()
2415 .stderr_contains("no properly formatted checksum lines found");
2416 }
2417 }
2418 }
2419
2420 /// Tests for cksum with base64 output encoding (untagged mode).
2421 mod cksum_base64_untagged_encoding {
2422 use super::*;
2423
2424 macro_rules! decl_sha_test {
2425 ($id:ident, $algo:literal, $len:expr) => {
2426 mod $id {
2427 use super::*;
2428
2429 #[test]
2430 fn check_length_guess() {
2431 let ts = TestScenario::new(util_name!());
2432 let at = &ts.fixtures;
2433
2434 at.write("inp", "test input\n");
2435
2436 let compute = ts
2437 .ucmd()
2438 .arg("-a")
2439 .arg($algo)
2440 .arg("-l")
2441 .arg(stringify!($len))
2442 .arg("--base64")
2443 .arg("--untagged")
2444 .arg("inp")
2445 .succeeds();
2446
2447 at.write_bytes("check", compute.stdout());
2448
2449 ts.ucmd()
2450 .arg("-a")
2451 .arg($algo)
2452 .arg("--check")
2453 .arg("check")
2454 .succeeds()
2455 .stdout_only("inp: OK\n");
2456
2457 at.write("check", " inp");
2458
2459 ts.ucmd()
2460 .arg("-a")
2461 .arg($algo)
2462 .arg("check")
2463 .fails()
2464 .stderr_contains(concat!(
2465 "--algorithm=",
2466 $algo,
2467 " requires specifying --length"
2468 ));
2469 }
2470 }
2471 };
2472 }
2473
2474 decl_sha_test!(sha2_224, "sha2", 224);
2475 decl_sha_test!(sha2_256, "sha2", 256);
2476 decl_sha_test!(sha2_384, "sha2", 384);
2477 decl_sha_test!(sha2_512, "sha2", 512);
2478 decl_sha_test!(sha3_224, "sha3", 224);
2479 decl_sha_test!(sha3_256, "sha3", 256);
2480 decl_sha_test!(sha3_384, "sha3", 384);
2481 decl_sha_test!(sha3_512, "sha3", 512);
2482
2483 macro_rules! decl_blake_test {
2484 ($id:ident, $len:expr) => {
2485 mod $id {
2486 use super::*;
2487
2488 #[test]
2489 fn check_length_guess() {
2490 let ts = TestScenario::new(util_name!());
2491 let at = &ts.fixtures;
2492
2493 at.write("inp", "test input\n");
2494
2495 let compute = ts
2496 .ucmd()
2497 .arg("-a")
2498 .arg("blake2b")
2499 .arg("-l")
2500 .arg(stringify!($len))
2501 .arg("--base64")
2502 .arg("--untagged")
2503 .arg("inp")
2504 .succeeds();
2505
2506 at.write_bytes("check", compute.stdout());
2507
2508 ts.ucmd()
2509 .arg("-a")
2510 .arg("blake2b")
2511 .arg("--check")
2512 .arg("check")
2513 .succeeds()
2514 .stdout_only("inp: OK\n");
2515 }
2516 }
2517 };
2518 }
2519
2520 decl_blake_test!(blake2b_8, 8);
2521 decl_blake_test!(blake2b_216, 216);
2522 decl_blake_test!(blake2b_224, 224);
2523 decl_blake_test!(blake2b_232, 232);
2524 decl_blake_test!(blake2b_248, 248);
2525 decl_blake_test!(blake2b_256, 256);
2526 decl_blake_test!(blake2b_264, 264);
2527 decl_blake_test!(blake2b_376, 376);
2528 decl_blake_test!(blake2b_384, 384);
2529 decl_blake_test!(blake2b_392, 392);
2530 decl_blake_test!(blake2b_504, 504);
2531 decl_blake_test!(blake2b_512, 512);
2532 }
2533 /// Tests for cksum check mode (-c/--check).
2534 mod cksum_check_mode {
2535 use super::*;
2536
2537 const INVALID_SUM: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaafdb57c725157cb40b5aee8d937b8351477e";
2538
2539 fn make_scene() -> TestScenario {
2540 let scene = TestScenario::new(util_name!());
2541 let at = &scene.fixtures;
2542 at.write("input", "9\n7\n1\n4\n2\n6\n3\n5\n8\n10\n");
2543
2544 let algos: &[&[&str]] = &[
2545 &["-a", "sha384"],
2546 &["-a", "blake2b"],
2547 &["-a", "blake2b", "-l", "384"],
2548 &["-a", "sm3"],
2549 ];
2550
2551 for args in algos {
2552 let result = scene.ucmd().args(args).succeeds();
2553 let stdout = result.stdout();
2554 at.append_bytes("CHECKSUMS", stdout);
2555 }
2556
2557 scene
2558 }
2559
2560 fn make_scene_with_comment() -> TestScenario {
2561 let scene = make_scene();
2562
2563 scene
2564 .fixtures
2565 .append("CHECKSUMS", "# Very important comment\n");
2566
2567 scene
2568 }
2569
2570 fn make_scene_with_invalid_line() -> TestScenario {
2571 let scene = make_scene_with_comment();
2572
2573 scene.fixtures.append("CHECKSUMS", "invalid_line\n");
2574
2575 scene
2576 }
2577
2578 #[test]
2579 fn test_tagged_invalid_length() {
2580 let (at, mut ucmd) = at_and_ucmd!();
2581
2582 at.write(
2583 "sha2-bad-length.sum",
2584 "SHA2-128 (/dev/null) = 38b060a751ac96384cd9327eb1b1e36a",
2585 );
2586
2587 ucmd.arg("--check")
2588 .arg("sha2-bad-length.sum")
2589 .fails()
2590 .stderr_contains("sha2-bad-length.sum: no properly formatted checksum lines found");
2591 }
2592
2593 #[test]
2594 #[cfg_attr(not(unix), ignore = "/dev/null is only available on UNIX")]
2595 fn test_untagged_base64_matching_tag() {
2596 let (at, mut ucmd) = at_and_ucmd!();
2597
2598 at.write("tag-prefix.sum", "SHA1+++++++++++++++++++++++= /dev/null");
2599
2600 ucmd.arg("--check")
2601 .arg("-a")
2602 .arg("sha1")
2603 .arg("tag-prefix.sum")
2604 .fails()
2605 .stderr_contains("WARNING: 1 computed checksum did NOT match");
2606 }
2607
2608 #[test]
2609 #[cfg_attr(windows, ignore = "Awkward filename is not supported on windows")]
2610 fn test_awkward_filename() {
2611 let ts = TestScenario::new(util_name!());
2612 let at = &ts.fixtures;
2613
2614 let awkward_file = "abc (f) = abc";
2615
2616 at.touch(awkward_file);
2617
2618 let result = ts.ucmd().arg("-a").arg("sha1").arg(awkward_file).succeeds();
2619
2620 at.write_bytes("tag-awkward.sum", result.stdout());
2621
2622 ts.ucmd().arg("-c").arg("tag-awkward.sum").succeeds();
2623 }
2624
2625 #[test]
2626 fn test_signed_checksums() {
2627 let ts = TestScenario::new(util_name!());
2628 let at = &ts.fixtures;
2629
2630 let filename = "test_file";
2631 at.touch(filename);
2632
2633 let checksum_result = ts.ucmd().arg("-a").arg("sha256").arg(filename).succeeds();
2634 let valid_checksum_line = checksum_result.stdout_str().trim();
2635
2636 let signed_content = format!(
2637 "-----BEGIN PGP SIGNED MESSAGE-----\n\
2638 Hash: SHA256\n\
2639 \n\
2640 # This is a comment that should be ignored\n\
2641 {valid_checksum_line}\n\
2642 -----BEGIN PGP SIGNATURE-----\n\
2643 \n\
2644 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\
2645 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\
2646 =PlaceHolder\n\
2647 -----END PGP SIGNATURE-----"
2648 );
2649
2650 at.write("signed_CHECKSUMS", &signed_content);
2651
2652 ts.ucmd().arg("--check").arg("signed_CHECKSUMS").succeeds();
2653 }
2654
2655 #[test]
2656 fn test_check_individual_digests_in_mixed_file() {
2657 let scene = make_scene();
2658
2659 scene
2660 .ucmd()
2661 .arg("--check")
2662 .arg("-a")
2663 .arg("sm3")
2664 .arg("CHECKSUMS")
2665 .succeeds();
2666 }
2667
2668 #[test]
2669 fn test_check_against_older_non_hex_formats() {
2670 let scene = make_scene();
2671
2672 scene
2673 .ucmd()
2674 .arg("-c")
2675 .arg("-a")
2676 .arg("crc")
2677 .arg("CHECKSUMS")
2678 .fails();
2679
2680 let crc_cmd = scene.ucmd().arg("-a").arg("crc").arg("input").succeeds();
2681 let crc_cmd_out = crc_cmd.stdout();
2682 scene.fixtures.write_bytes("CHECKSUMS.crc", crc_cmd_out);
2683
2684 scene.ucmd().arg("-c").arg("CHECKSUMS.crc").fails();
2685 }
2686
2687 #[test]
2688 fn test_status() {
2689 let scene = make_scene();
2690
2691 scene
2692 .ucmd()
2693 .arg("--status")
2694 .arg("--check")
2695 .arg("CHECKSUMS")
2696 .succeeds()
2697 .no_output();
2698 }
2699
2700 #[test]
2701 fn test_status_with_comment() {
2702 let scene = make_scene_with_comment();
2703
2704 scene
2705 .ucmd()
2706 .arg("--status")
2707 .arg("--check")
2708 .arg("CHECKSUMS")
2709 .succeeds()
2710 .no_output();
2711 }
2712
2713 #[test]
2714 fn test_check_strict() {
2715 let scene = make_scene_with_invalid_line();
2716
2717 // without strict, succeeds
2718 scene
2719 .ucmd()
2720 .arg("--check")
2721 .arg("CHECKSUMS")
2722 .succeeds()
2723 .stderr_contains("1 line is improperly formatted");
2724
2725 // with strict, fails
2726 scene
2727 .ucmd()
2728 .arg("--strict")
2729 .arg("--check")
2730 .arg("CHECKSUMS")
2731 .fails()
2732 .stderr_contains("1 line is improperly formatted");
2733 }
2734
2735 fn make_scene_with_two_invalid_lines() -> TestScenario {
2736 let scene = make_scene_with_comment();
2737
2738 scene
2739 .fixtures
2740 .append("CHECKSUMS", "invalid_line\ninvalid_line\n");
2741
2742 scene
2743 }
2744
2745 #[test]
2746 fn test_check_strict_plural_checks() {
2747 let scene = make_scene_with_two_invalid_lines();
2748
2749 scene
2750 .ucmd()
2751 .arg("--strict")
2752 .arg("--check")
2753 .arg("CHECKSUMS")
2754 .fails()
2755 .stderr_contains("2 lines are improperly formatted");
2756 }
2757
2758 fn make_scene_with_incorrect_checksum() -> TestScenario {
2759 let scene = make_scene_with_two_invalid_lines();
2760
2761 scene
2762 .fixtures
2763 .append("CHECKSUMS", &format!("SM3 (input) = {INVALID_SUM}\n"));
2764
2765 scene
2766 }
2767
2768 #[test]
2769 fn test_check_with_incorrect_checksum() {
2770 let scene = make_scene_with_incorrect_checksum();
2771
2772 scene
2773 .ucmd()
2774 .arg("--check")
2775 .arg("CHECKSUMS")
2776 .fails()
2777 .stdout_contains("input: FAILED")
2778 .stderr_contains("1 computed checksum did NOT match");
2779
2780 // also fails with strict
2781 scene
2782 .ucmd()
2783 .arg("--strict")
2784 .arg("--check")
2785 .arg("CHECKSUMS")
2786 .fails()
2787 .stdout_contains("input: FAILED")
2788 .stderr_contains("1 computed checksum did NOT match");
2789 }
2790
2791 #[test]
2792 fn test_status_with_errors() {
2793 let scene = make_scene_with_incorrect_checksum();
2794
2795 scene
2796 .ucmd()
2797 .arg("--status")
2798 .arg("--check")
2799 .arg("CHECKSUMS")
2800 .fails()
2801 .no_output();
2802 }
2803
2804 #[test]
2805 fn test_check_with_non_existing_file() {
2806 let scene = make_scene();
2807 scene
2808 .fixtures
2809 .write("CHECKSUMS2", &format!("SM3 (input2) = {INVALID_SUM}\n"));
2810
2811 scene
2812 .ucmd()
2813 .arg("--check")
2814 .arg("CHECKSUMS2")
2815 .fails()
2816 .stdout_contains("input2: FAILED open or read")
2817 .stderr_contains("1 listed file could not be read");
2818
2819 // also fails with strict
2820 scene
2821 .ucmd()
2822 .arg("--strict")
2823 .arg("--check")
2824 .arg("CHECKSUMS2")
2825 .fails()
2826 .stdout_contains("input2: FAILED open or read")
2827 .stderr_contains("1 listed file could not be read");
2828 }
2829
2830 fn make_scene_with_another_improperly_formatted() -> TestScenario {
2831 let scene = make_scene_with_incorrect_checksum();
2832
2833 scene.fixtures.append(
2834 "CHECKSUMS",
2835 &format!("BLAKE2b (missing-file) = {INVALID_SUM}\n"),
2836 );
2837
2838 scene
2839 }
2840
2841 #[test]
2842 fn test_warn() {
2843 let scene = make_scene_with_another_improperly_formatted();
2844
2845 scene
2846 .ucmd()
2847 .arg("--warn")
2848 .arg("--check")
2849 .arg("CHECKSUMS")
2850 .fails()
2851 .stderr_contains("CHECKSUMS: 6: improperly formatted SM3 checksum line")
2852 .stderr_contains("CHECKSUMS: 9: improperly formatted BLAKE2b checksum line");
2853 }
2854
2855 fn make_scene_with_checksum_missing() -> TestScenario {
2856 let scene = make_scene_with_another_improperly_formatted();
2857
2858 scene.fixtures.write(
2859 "CHECKSUMS-missing",
2860 &format!("SM3 (nonexistent) = {INVALID_SUM}\n"),
2861 );
2862
2863 scene
2864 }
2865
2866 #[test]
2867 fn test_ignore_missing() {
2868 let scene = make_scene_with_checksum_missing();
2869
2870 scene
2871 .ucmd()
2872 .arg("--ignore-missing")
2873 .arg("--check")
2874 .arg("CHECKSUMS-missing")
2875 .fails()
2876 .stdout_does_not_contain("nonexistent: No such file or directory")
2877 .stdout_does_not_contain("nonexistent: FAILED open or read")
2878 .stderr_contains("CHECKSUMS-missing: no file was verified");
2879 }
2880
2881 #[test]
2882 fn test_ignore_missing_stdin() {
2883 let scene = make_scene_with_checksum_missing();
2884
2885 scene
2886 .ucmd()
2887 .arg("--ignore-missing")
2888 .arg("--check")
2889 .pipe_in_fixture("CHECKSUMS-missing")
2890 .fails()
2891 .no_stdout()
2892 .stderr_contains("'standard input': no file was verified");
2893 }
2894
2895 #[test]
2896 fn test_status_and_warn() {
2897 let scene = make_scene_with_checksum_missing();
2898
2899 // --status before --warn
2900 scene
2901 .ucmd()
2902 .arg("--status")
2903 .arg("--warn")
2904 .arg("--check")
2905 .arg("CHECKSUMS")
2906 .fails()
2907 .stderr_contains("CHECKSUMS: 9: improperly formatted BLAKE2b checksum line")
2908 .stderr_contains("WARNING: 3 lines are improperly formatted")
2909 .stderr_contains("WARNING: 1 computed checksum did NOT match");
2910
2911 // --warn before --status (status hides the results)
2912 scene
2913 .ucmd()
2914 .arg("--warn")
2915 .arg("--status")
2916 .arg("--check")
2917 .arg("CHECKSUMS")
2918 .fails()
2919 .stderr_does_not_contain("CHECKSUMS: 9: improperly formatted BLAKE2b checksum line")
2920 .stderr_does_not_contain("WARNING: 3 lines are improperly formatted")
2921 .stderr_does_not_contain("WARNING: 1 computed checksum did NOT match");
2922 }
2923
2924 #[test]
2925 fn test_status_and_ignore_missing() {
2926 let scene = make_scene_with_checksum_missing();
2927
2928 scene
2929 .ucmd()
2930 .arg("--status")
2931 .arg("--ignore-missing")
2932 .arg("--check")
2933 .arg("CHECKSUMS")
2934 .fails()
2935 .no_output();
2936 }
2937
2938 #[test]
2939 fn test_status_warn_and_ignore_missing() {
2940 let scene = make_scene_with_checksum_missing();
2941
2942 scene
2943 .ucmd()
2944 .arg("--status")
2945 .arg("--warn")
2946 .arg("--ignore-missing")
2947 .arg("--check")
2948 .arg("CHECKSUMS-missing")
2949 .fails()
2950 .stderr_contains("CHECKSUMS-missing: no file was verified")
2951 .stdout_does_not_contain("nonexistent: No such file or directory");
2952 }
2953
2954 #[test]
2955 fn test_check_several_files_dont_exist() {
2956 let scene = make_scene();
2957
2958 scene
2959 .ucmd()
2960 .arg("--check")
2961 .arg("non-existing-1")
2962 .arg("non-existing-2")
2963 .fails()
2964 .stderr_contains("non-existing-1: No such file or directory")
2965 .stderr_contains("non-existing-2: No such file or directory");
2966 }
2967
2968 #[test]
2969 fn test_check_several_files_empty() {
2970 let scene = make_scene();
2971 scene.fixtures.touch("empty-1");
2972 scene.fixtures.touch("empty-2");
2973
2974 scene
2975 .ucmd()
2976 .arg("--check")
2977 .arg("empty-1")
2978 .arg("empty-2")
2979 .fails()
2980 .stderr_contains("empty-1: no properly formatted checksum lines found")
2981 .stderr_contains("empty-2: no properly formatted checksum lines found");
2982 }
2983 }
2984
2985 /// The tests in this module check the behavior of cksum when given different
2986 /// checksum formats and algorithms in the same file, while specifying an
2987 /// algorithm on CLI or not.
2988 mod format_mix {
2989 use super::*;
2990
2991 // First line is algo-based, second one is not
2992 const INPUT_ALGO_NON_ALGO: &str = "\
2993 BLAKE2b (bar) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n\
2994 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce foo";
2995
2996 // First line is non algo-based, second one is
2997 const INPUT_NON_ALGO_ALGO: &str = "\
2998 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce foo\n\
2999 BLAKE2b (bar) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce";
3000
3001 /// Make a simple scene with foo and bar empty files
3002 fn make_scene() -> TestScenario {
3003 let scene = TestScenario::new(util_name!());
3004 let at = &scene.fixtures;
3005
3006 at.touch("foo");
3007 at.touch("bar");
3008
3009 scene
3010 }
3011
3012 #[test]
3013 fn test_check_cli_algo_non_algo() {
3014 let scene = make_scene();
3015 scene
3016 .ucmd()
3017 .arg("--check")
3018 .arg("--algo=blake2b")
3019 .pipe_in(INPUT_ALGO_NON_ALGO)
3020 .succeeds()
3021 .stdout_contains("bar: OK\nfoo: OK")
3022 .no_stderr();
3023 }
3024
3025 #[test]
3026 fn test_check_cli_non_algo_algo() {
3027 let scene = make_scene();
3028 scene
3029 .ucmd()
3030 .arg("--check")
3031 .arg("--algo=blake2b")
3032 .pipe_in(INPUT_NON_ALGO_ALGO)
3033 .succeeds()
3034 .stdout_contains("foo: OK\nbar: OK")
3035 .no_stderr();
3036 }
3037
3038 #[test]
3039 fn test_check_algo_non_algo() {
3040 let scene = make_scene();
3041 scene
3042 .ucmd()
3043 .arg("--check")
3044 .pipe_in(INPUT_ALGO_NON_ALGO)
3045 .succeeds()
3046 .stdout_contains("bar: OK")
3047 .stderr_contains("cksum: WARNING: 1 line is improperly formatted");
3048 }
3049
3050 #[test]
3051 fn test_check_non_algo_algo() {
3052 let scene = make_scene();
3053 scene
3054 .ucmd()
3055 .arg("--check")
3056 .pipe_in(INPUT_NON_ALGO_ALGO)
3057 .succeeds()
3058 .stdout_contains("bar: OK")
3059 .stderr_contains("cksum: WARNING: 1 line is improperly formatted");
3060 }
3061 }
3062
3063 #[cfg(not(target_os = "android"))]
3064 mod debug_flag {
3065 use super::*;
3066
3067 #[test]
3068 fn test_debug_flag() {
3069 // Test with default CRC algorithm - should output CPU feature detection
3070 new_ucmd!()
3071 .arg("--debug")
3072 .arg("lorem_ipsum.txt")
3073 .succeeds()
3074 .stdout_is_fixture("crc_single_file.expected")
3075 .stderr_contains("avx512")
3076 .stderr_contains("avx2")
3077 .stderr_contains("pclmul");
3078
3079 // Test with MD5 algorithm - CPU detection should be same regardless of algorithm
3080 new_ucmd!()
3081 .arg("--debug")
3082 .arg("-a")
3083 .arg("md5")
3084 .arg("lorem_ipsum.txt")
3085 .succeeds()
3086 .stdout_is_fixture("md5_single_file.expected")
3087 .stderr_contains("avx512")
3088 .stderr_contains("avx2")
3089 .stderr_contains("pclmul");
3090
3091 // Test with stdin - CPU detection should appear once
3092 new_ucmd!()
3093 .arg("--debug")
3094 .pipe_in("test")
3095 .succeeds()
3096 .stderr_contains("avx512")
3097 .stderr_contains("avx2")
3098 .stderr_contains("pclmul");
3099
3100 // Test with multiple files - CPU detection should appear once, not per file
3101 new_ucmd!()
3102 .arg("--debug")
3103 .arg("lorem_ipsum.txt")
3104 .arg("alice_in_wonderland.txt")
3105 .succeeds()
3106 .stdout_is_fixture("crc_multiple_files.expected")
3107 .stderr_str_check(|stderr| {
3108 // Verify CPU detection happens only once by checking the count of each feature line
3109 let avx512_count = stderr
3110 .lines()
3111 .filter(|line| line.contains("avx512"))
3112 .count();
3113 let avx2_count = stderr.lines().filter(|line| line.contains("avx2")).count();
3114 let pclmul_count = stderr
3115 .lines()
3116 .filter(|line| line.contains("pclmul"))
3117 .count();
3118
3119 avx512_count == 1 && avx2_count == 1 && pclmul_count == 1
3120 });
3121 }
3122
3123 #[test]
3124 fn test_debug_with_algorithms() {
3125 // Test with SHA256 - CPU detection should be same regardless of algorithm
3126 new_ucmd!()
3127 .arg("--debug")
3128 .arg("-a")
3129 .arg("sha256")
3130 .arg("lorem_ipsum.txt")
3131 .succeeds()
3132 .stderr_contains("avx512")
3133 .stderr_contains("avx2")
3134 .stderr_contains("pclmul");
3135
3136 // Test with BLAKE2b default length
3137 new_ucmd!()
3138 .arg("--debug")
3139 .arg("-a")
3140 .arg("blake2b")
3141 .arg("lorem_ipsum.txt")
3142 .succeeds()
3143 .stderr_contains("avx512")
3144 .stderr_contains("avx2")
3145 .stderr_contains("pclmul");
3146
3147 // Test with BLAKE2b custom length
3148 new_ucmd!()
3149 .arg("--debug")
3150 .arg("-a")
3151 .arg("blake2b")
3152 .arg("--length")
3153 .arg("256")
3154 .arg("lorem_ipsum.txt")
3155 .succeeds()
3156 .stderr_contains("avx512")
3157 .stderr_contains("avx2")
3158 .stderr_contains("pclmul");
3159
3160 // Test with SHA1
3161 new_ucmd!()
3162 .arg("--debug")
3163 .arg("-a")
3164 .arg("sha1")
3165 .arg("lorem_ipsum.txt")
3166 .succeeds()
3167 .stderr_contains("avx512")
3168 .stderr_contains("avx2")
3169 .stderr_contains("pclmul");
3170 }
3171 }
3172
3173 #[test]
3174 #[cfg(all(target_os = "linux", not(target_env = "musl")))]
3175 fn test_check_file_with_io_error() {
3176 // /proc/self/mem causes EIO when read without proper seeking
3177 new_ucmd!()
3178 .arg("-a")
3179 .arg("md5")
3180 .arg("--check")
3181 .pipe_in("d8e8fca2dc0f896fd7cb4cb0031ba249 /proc/self/mem\n")
3182 .fails()
3183 .stderr_contains("Input/output error")
3184 .stdout_contains("FAILED open or read");
3185 }
3186
3187 #[test]
3188 #[cfg(all(target_os = "linux", not(target_env = "musl")))]
3189 fn test_check_checkfile_with_io_error() {
3190 // /proc/self/mem causes EIO when read without proper seeking
3191 new_ucmd!()
3192 .arg("-a")
3193 .arg("md5")
3194 .arg("--check")
3195 .arg("/proc/self/mem")
3196 .fails()
3197 .stderr_contains("/proc/self/mem: read error")
3198 .no_stdout();
3199 }
3200
3201 #[rstest]
3202 #[case::default_length(
3203 &[],
3204 "ac8549b2861a151896ab721bd29d7a20c1a3d1f75b31266f786f20d963fb0fdf"
3205 )]
3206 #[case::pass_default_length(
3207 &["-l", "256"],
3208 "ac8549b2861a151896ab721bd29d7a20c1a3d1f75b31266f786f20d963fb0fdf"
3209 )]
3210 #[case::smaller_length(
3211 &["-l", "128"],
3212 "ac8549b2861a151896ab721bd29d7a20"
3213 )]
3214 #[case::bigger_length(
3215 &["-l", "264"],
3216 "ac8549b2861a151896ab721bd29d7a20c1a3d1f75b31266f786f20d963fb0fdfc2"
3217 )]
3218 #[case::length_0(
3219 &["-l", "0"],
3220 "ac8549b2861a151896ab721bd29d7a20c1a3d1f75b31266f786f20d963fb0fdf"
3221 )]
3222 #[case::length_1(
3223 &["-l", "1"],
3224 "00"
3225 )]
3226 #[case::length_2(
3227 &["-l", "2"],
3228 "00"
3229 )]
3230 #[case::length_3(
3231 &["-l", "3"],
3232 "04"
3233 )]
3234 #[case::length_4(
3235 &["-l", "4"],
3236 "0c"
3237 )]
3238 #[case::length_5(
3239 &["-l", "5"],
3240 "0c"
3241 )]
3242 #[case::length_6(
3243 &["-l", "6"],
3244 "2c"
3245 )]
3246 #[case::length_7(
3247 &["-l", "7"],
3248 "2c"
3249 )]
3250 #[case::length_8(
3251 &["-l", "8"],
3252 "ac"
3253 )]
3254 fn test_shake128(#[case] args: &[&str], #[case] expected: &str) {
3255 let bit_len = if args.is_empty() || args[1] == "0" {
3256 "256"
3257 } else {
3258 args[1]
3259 };
3260
3261 new_ucmd!()
3262 .arg("-a")
3263 .arg("shake128")
3264 .args(args)
3265 .pipe_in("xxx")
3266 .succeeds()
3267 .stdout_only(format!("SHAKE128-{bit_len} (-) = {expected}\n"));
3268 }
3269
3270 #[rstest]
3271 #[case::default_length(
3272 &[],
3273 "2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7b24958f3e3c4cc0e71f1fe6f054cd3fb28b9efb62b4f8f3fbe6d50d90f5c6eba"
3274 )]
3275 #[case::pass_default_length(
3276 &["-l", "512"],
3277 "2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7b24958f3e3c4cc0e71f1fe6f054cd3fb28b9efb62b4f8f3fbe6d50d90f5c6eba"
3278 )]
3279 #[case::smaller_length(
3280 &["-l", "128"],
3281 "2fa631503c3ea5fe85131dbfa2480518"
3282 )]
3283 #[case::bigger_length(
3284 &["-l", "1024"],
3285 "2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7b24958f3e3c4cc0e71f1fe6f054cd3fb28b9efb62b4f8f3fbe6d50d90f5c6eba18783d25f8b36d92b8607f016352b5c405945a7859a8339201728f680647324d1b8ea93a01d2ef965dadf4a1bee3ff044ed2b4bd95e4311f5e3f2cd5bae0b7c6"
3286 )]
3287 #[case::length_0(
3288 &["-l", "0"],
3289 "2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7b24958f3e3c4cc0e71f1fe6f054cd3fb28b9efb62b4f8f3fbe6d50d90f5c6eba"
3290 )]
3291 #[case::length_1(
3292 &["-l", "1"],
3293 "01"
3294 )]
3295 #[case::length_2(
3296 &["-l", "2"],
3297 "03"
3298 )]
3299 #[case::length_3(
3300 &["-l", "3"],
3301 "07"
3302 )]
3303 #[case::length_4(
3304 &["-l", "4"],
3305 "0f"
3306 )]
3307 #[case::length_5(
3308 &["-l", "5"],
3309 "0f"
3310 )]
3311 #[case::length_6(
3312 &["-l", "6"],
3313 "2f"
3314 )]
3315 #[case::length_7(
3316 &["-l", "7"],
3317 "2f"
3318 )]
3319 #[case::length_8(
3320 &["-l", "8"],
3321 "2f"
3322 )]
3323 fn test_shake256(#[case] args: &[&str], #[case] expected: &str) {
3324 let bit_len = if args.is_empty() || args[1] == "0" {
3325 "512"
3326 } else {
3327 args[1]
3328 };
3329
3330 new_ucmd!()
3331 .arg("-a")
3332 .arg("shake256")
3333 .args(args)
3334 .pipe_in("xxx")
3335 .succeeds()
3336 .stdout_only(format!("SHAKE256-{bit_len} (-) = {expected}\n"));
3337 }
3338
3339 #[test]
3340 fn test_check_shake128_no_length() {
3341 const INPUT_SHAKE128_CORRECT_LEN: &str =
3342 "SHAKE128 (bar) = ac8549b2861a151896ab721bd29d7a20c1a3d1f75b31266f786f20d963fb0fdf";
3343 const INPUT_SHAKE128_WRONG_LEN: &str = "SHAKE128 (bar) = ac8549b2861a151896ab721bd29d7a20";
3344
3345 let scene = TestScenario::new(util_name!());
3346 let at = &scene.fixtures;
3347
3348 at.write("bar", "xxx");
3349
3350 scene
3351 .ucmd()
3352 .arg("-a")
3353 .arg("shake128")
3354 .arg("-c")
3355 .pipe_in(INPUT_SHAKE128_CORRECT_LEN)
3356 .succeeds();
3357
3358 scene
3359 .ucmd()
3360 .arg("-a")
3361 .arg("shake128")
3362 .arg("-c")
3363 .pipe_in(INPUT_SHAKE128_WRONG_LEN)
3364 .fails()
3365 .stderr_only("cksum: 'standard input': no properly formatted checksum lines found\n");
3366 }
3367
3368 #[test]
3369 fn test_check_shake256_no_length() {
3370 const INPUT_SHAKE256_CORRECT_LEN: &str = "SHAKE256 (bar) = 2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7b24958f3e3c4cc0e71f1fe6f054cd3fb28b9efb62b4f8f3fbe6d50d90f5c6eba";
3371 const INPUT_SHAKE256_WRONG_LEN: &str =
3372 "SHAKE256 (bar) = 2fa631503c3ea5fe85131dbfa24805185474740e6dcb5f2a64f69d932bcb55f7";
3373
3374 let scene = TestScenario::new(util_name!());
3375 let at = &scene.fixtures;
3376
3377 at.write("bar", "xxx");
3378
3379 scene
3380 .ucmd()
3381 .arg("-c")
3382 .pipe_in(INPUT_SHAKE256_CORRECT_LEN)
3383 .succeeds();
3384
3385 scene
3386 .ucmd()
3387 .arg("-c")
3388 .pipe_in(INPUT_SHAKE256_WRONG_LEN)
3389 .fails()
3390 .stderr_only("cksum: 'standard input': no properly formatted checksum lines found\n");
3391 }
3392
3393 #[test]
3394 fn test_shake_extremely_large_length_does_not_abort() {
3395 // Regression test for #12869: an absurdly large `--length` used to
3396 // trigger an unguarded allocation that aborts the process instead of
3397 // returning a normal error.
3398 new_ucmd!()
3399 .args(&[
3400 "--algorithm",
3401 "shake128",
3402 "--length",
3403 "10011111117721172727",
3404 ])
3405 .pipe_in("")
3406 .fails();
3407 }
3408
3409 #[template]
3410 #[rstest]
3411 #[case::no_length(
3412 b"foo",
3413 "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9",
3414 None
3415 )]
3416 #[case(
3417 b"foo",
3418 "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9",
3419 Some(0)
3420 )]
3421 #[case(
3422 b"foo",
3423 "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9",
3424 Some(256)
3425 )]
3426 #[case(
3427 b"foo",
3428 "04e0bb39f30b1a3feb89f536c93be15055482df748674b00d26e5a75777702e9791074b7511b59d31c71c62f5a745689fa6c",
3429 Some(400)
3430 )]
3431 #[case(b"foo", "04e0bb39f3", Some(40))]
3432 #[case(b"foo", "04e0", Some(16))]
3433 #[case(b"foo", "04", Some(8))]
3434 fn test_blake3(#[case] input: &[u8], #[case] expected: &str, #[case] length: Option<usize>) {}
3435
3436 #[apply(test_blake3)]
3437 fn test_compute_blake3(
3438 #[case] input: &[u8],
3439 #[case] expected: &str,
3440 #[case] length: Option<usize>,
3441 ) {
3442 let length_args: &[String] = if let Some(len) = length {
3443 &["-l".into(), len.to_string()]
3444 } else {
3445 &[]
3446 };
3447
3448 new_ucmd!()
3449 .arg("-a")
3450 .arg("blake3")
3451 .args(length_args)
3452 .pipe_in(input)
3453 .succeeds()
3454 .stdout_only(format!(
3455 "BLAKE3{} (-) = {expected}\n",
3456 match length {
3457 Some(0) | None => "-256".into(),
3458 Some(i) => format!("-{i}"),
3459 }
3460 ));
3461
3462 // with --untagged
3463 new_ucmd!()
3464 .arg("-a")
3465 .arg("blake3")
3466 .arg("--untagged")
3467 .args(length_args)
3468 .pipe_in(input)
3469 .succeeds()
3470 .stdout_only(format!("{expected} -\n"));
3471 }
3472
3473 #[apply(test_blake3)]
3474 fn test_check_blake3_tagged(
3475 #[case] input: &[u8],
3476 #[case] digest: &str,
3477 #[case] opt_len: Option<usize>,
3478 ) {
3479 let (at, mut ucmd) = at_and_ucmd!();
3480 at.write_bytes("FILE", input);
3481
3482 let len = match opt_len {
3483 Some(0) => "-256".into(),
3484 Some(i) => format!("-{i}"),
3485 None => String::new(),
3486 };
3487
3488 let tagged = format!("BLAKE3{len} (FILE) = {digest}");
3489
3490 ucmd.arg("-c")
3491 .arg("-a")
3492 .arg("blake3")
3493 .pipe_in(tagged)
3494 .succeeds()
3495 .stdout_only("FILE: OK\n");
3496 }
3497
3498 #[apply(test_blake3)]
3499 #[allow(clippy::used_underscore_binding)]
3500 fn test_check_blake3_untagged(
3501 #[case] input: &[u8],
3502 #[case] digest: &str,
3503 #[case] _opt_len: Option<usize>,
3504 ) {
3505 let (at, mut ucmd) = at_and_ucmd!();
3506 at.write_bytes("FILE", input);
3507
3508 let untagged = format!("{digest} FILE");
3509
3510 ucmd.arg("-c")
3511 .arg("-a")
3512 .arg("blake3")
3513 .pipe_in(untagged)
3514 .succeeds()
3515 .stdout_only("FILE: OK\n");
3516 }
3517
3518 #[test]
3519 fn test_locale_aware_error_filename_escaping() {
3520 // 'Ã' is valid UTF-8, invalid ASCII.
3521 let filename = "file_Ã";
3522
3523 // The character is valid and not escaped in UTF-8
3524 new_ucmd!()
3525 .env("LC_ALL", "en_US.UTF-8")
3526 .arg(filename)
3527 .fails_with_code(1)
3528 .stderr_is("cksum: file_Ã: No such file or directory\n");
3529
3530 // In C locale, non-ASCII bytes are escaped
3531 new_ucmd!()
3532 .env("LC_ALL", "C")
3533 .arg(filename)
3534 .fails_with_code(1)
3535 .stderr_is("cksum: 'file_'$'\\303\\203': No such file or directory\n");
3536
3537 // Same with a directory error
3538 let dirname = "dir_Ã";
3539 let (at, mut ucmd) = at_and_ucmd!();
3540 at.mkdir(dirname);
3541 ucmd.env("LC_ALL", "C")
3542 .arg(dirname)
3543 .fails_with_code(1)
3544 .stderr_is("cksum: 'dir_'$'\\303\\203': Is a directory\n");
3545
3546 // A parenthesis is escaped in any case.
3547 for locale in ["C", "en_US.UTF-8"] {
3548 new_ucmd!()
3549 .env("LC_ALL", locale)
3550 .arg("file_x(y")
3551 .fails_with_code(1)
3552 .stderr_is("cksum: 'file_x(y': No such file or directory\n");
3553 }
3554 }