1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
|
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use crate::common::util::TestScenario;
// spell-checker:ignore checkfile, nonames, testf, ntestf
macro_rules! get_hash(
($str:expr) => (
$str.split(' ').collect::<Vec<&str>>()[0]
);
);
macro_rules! test_digest {
($($id:ident $t:ident $size:expr)*) => ($(
mod $id {
use crate::common::util::*;
static DIGEST_ARG: &'static str = concat!("--", stringify!($t));
static BITS_ARG: &'static str = concat!("--bits=", stringify!($size));
static EXPECTED_FILE: &'static str = concat!(stringify!($id), ".expected");
static CHECK_FILE: &'static str = concat!(stringify!($id), ".checkfile");
static INPUT_FILE: &'static str = "input.txt";
#[test]
fn test_single_file() {
let ts = TestScenario::new("hashsum");
assert_eq!(ts.fixtures.read(EXPECTED_FILE),
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg(INPUT_FILE).succeeds().no_stderr().stdout_str()));
}
#[test]
fn test_stdin() {
let ts = TestScenario::new("hashsum");
assert_eq!(ts.fixtures.read(EXPECTED_FILE),
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).pipe_in_fixture(INPUT_FILE).succeeds().no_stderr().stdout_str()));
}
#[test]
fn test_nonames() {
let ts = TestScenario::new("hashsum");
// EXPECTED_FILE has no newline character at the end
if DIGEST_ARG == "--b3sum" {
// Option only available on b3sum
assert_eq!(format!("{0}\n{0}\n", ts.fixtures.read(EXPECTED_FILE)),
ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--no-names").arg(INPUT_FILE).arg("-").pipe_in_fixture(INPUT_FILE)
.succeeds().no_stderr().stdout_str()
);
}
}
#[test]
fn test_check() {
let ts = TestScenario::new("hashsum");
println!("File content='{}'", ts.fixtures.read(INPUT_FILE));
println!("Check file='{}'", ts.fixtures.read(CHECK_FILE));
ts.ucmd()
.args(&[DIGEST_ARG, BITS_ARG, "--check", CHECK_FILE])
.succeeds()
.no_stderr()
.stdout_is("input.txt: OK\n");
}
#[test]
fn test_zero() {
let ts = TestScenario::new("hashsum");
assert_eq!(ts.fixtures.read(EXPECTED_FILE),
get_hash!(ts.ucmd().arg(DIGEST_ARG).arg(BITS_ARG).arg("--zero").arg(INPUT_FILE).succeeds().no_stderr().stdout_str()));
}
#[cfg(windows)]
#[test]
fn test_text_mode() {
// TODO Replace this with hard-coded files that store the
// expected output of text mode on an input file that has
// "\r\n" line endings.
let result = new_ucmd!()
.args(&[DIGEST_ARG, BITS_ARG, "-b"])
.pipe_in("a\nb\nc\n")
.succeeds();
let expected = result.no_stderr().stdout();
// Replace the "*-\n" at the end of the output with " -\n".
// The asterisk indicates that the digest was computed in
// binary mode.
let n = expected.len();
let expected = [&expected[..n - 3], b" -\n"].concat();
new_ucmd!()
.args(&[DIGEST_ARG, BITS_ARG, "-t"])
.pipe_in("a\r\nb\r\nc\r\n")
.succeeds()
.no_stderr()
.stdout_is(std::str::from_utf8(&expected).unwrap());
}
}
)*)
}
test_digest! {
md5 md5 128
sha1 sha1 160
sha224 sha224 224
sha256 sha256 256
sha384 sha384 384
sha512 sha512 512
sha3_224 sha3 224
sha3_256 sha3 256
sha3_384 sha3 384
sha3_512 sha3 512
shake128_256 shake128 256
shake256_512 shake256 512
b2sum b2sum 512
b3sum b3sum 256
}
#[test]
fn test_check_sha1() {
// To make sure that #3815 doesn't happen again
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
at.write(
"testf.sha1",
"988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n",
);
scene
.ccmd("sha1sum")
.arg("-c")
.arg(at.subdir.join("testf.sha1"))
.succeeds()
.stdout_is("testf: OK\n")
.stderr_is("");
}
#[test]
fn test_check_md5_ignore_missing() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
at.write(
"testf.sha1",
"14758f1afd44c09b7992073ccf00b43d testf\n14758f1afd44c09b7992073ccf00b43d testf2\n",
);
scene
.ccmd("md5sum")
.arg("-c")
.arg(at.subdir.join("testf.sha1"))
.fails()
.stdout_contains("testf2: FAILED open or read");
scene
.ccmd("md5sum")
.arg("-c")
.arg("--ignore-missing")
.arg(at.subdir.join("testf.sha1"))
.succeeds()
.stdout_is("testf: OK\n")
.stderr_is("");
scene
.ccmd("md5sum")
.arg("--ignore-missing")
.arg(at.subdir.join("testf.sha1"))
.fails()
.stderr_contains("the --ignore-missing option is meaningful only when verifying checksums");
}
#[test]
fn test_check_b2sum_length_option_0() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
at.write("testf.b2sum", "9e2bf63e933e610efee4a8d6cd4a9387e80860edee97e27db3b37a828d226ab1eb92a9cdd8ca9ca67a753edaf8bd89a0558496f67a30af6f766943839acf0110 testf\n");
scene
.ccmd("b2sum")
.arg("--length=0")
.arg("-c")
.arg(at.subdir.join("testf.b2sum"))
.succeeds()
.stdout_only("testf: OK\n");
}
#[test]
fn test_check_b2sum_length_duplicate() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
scene
.ccmd("b2sum")
.arg("--length=123")
.arg("--length=128")
.arg("testf")
.succeeds()
.stdout_contains("d6d45901dec53e65d2b55fb6e2ab67b0");
}
#[test]
fn test_check_b2sum_length_option_8() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
at.write("testf.b2sum", "6a testf\n");
scene
.ccmd("b2sum")
.arg("--length=8")
.arg("-c")
.arg(at.subdir.join("testf.b2sum"))
.succeeds()
.stdout_only("testf: OK\n");
}
#[test]
fn test_invalid_b2sum_length_option_not_multiple_of_8() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
scene
.ccmd("b2sum")
.arg("--length=9")
.arg(at.subdir.join("testf"))
.fails_with_code(1);
}
#[test]
fn test_invalid_b2sum_length_option_too_large() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
scene
.ccmd("b2sum")
.arg("--length=513")
.arg(at.subdir.join("testf"))
.fails_with_code(1);
}
#[test]
fn test_check_b2sum_tag_output() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
scene
.ccmd("b2sum")
.arg("--length=0")
.arg("--tag")
.arg("f")
.succeeds()
.stdout_only("BLAKE2b (f) = 786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce\n");
scene
.ccmd("b2sum")
.arg("--length=128")
.arg("--tag")
.arg("f")
.succeeds()
.stdout_only("BLAKE2b-128 (f) = cae66941d9efbd404e4d88758ea67670\n");
}
#[test]
fn test_check_b2sum_verify() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("a", "a\n");
scene
.ccmd("b2sum")
.arg("--tag")
.arg("a")
.succeeds()
.stdout_only("BLAKE2b (a) = bedfbb90d858c2d67b7ee8f7523be3d3b54004ef9e4f02f2ad79a1d05bfdfe49b81e3c92ebf99b504102b6bf003fa342587f5b3124c205f55204e8c4b4ce7d7c\n");
scene
.ccmd("b2sum")
.arg("--tag")
.arg("-l")
.arg("128")
.arg("a")
.succeeds()
.stdout_only("BLAKE2b-128 (a) = b93e0fc7bb21633c08bba07c5e71dc00\n");
}
#[test]
fn test_check_file_not_found_warning() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("testf", "foobar\n");
at.write(
"testf.sha1",
"988881adc9fc3655077dc2d4d757d480b5ea0e11 testf\n",
);
at.remove("testf");
scene
.ccmd("sha1sum")
.arg("-c")
.arg(at.subdir.join("testf.sha1"))
.fails()
.stdout_is("testf: FAILED open or read\n")
.stderr_is("sha1sum: testf: No such file or directory\nsha1sum: WARNING: 1 listed file could not be read\n");
}
// Asterisk `*` is a reserved paths character on win32, nor the path can end with a whitespace.
// ref: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
#[test]
fn test_check_md5sum() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
#[cfg(not(windows))]
{
for f in &["a", " b", "*c", "dd", " "] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"60b725f10c9c85c70d97880dfe8191b3 a\n\
bf35d7536c785cf06730d5a40301eba2 b\n\
f5b61709718c1ecf8db1aea8547d4698 *c\n\
b064a020db8018f18ff5ae367d01b212 dd\n\
d784fa8b6d98d27699781bd9a7cf19f0 ",
);
scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5sum")
.succeeds()
.stdout_is("a: OK\n b: OK\n*c: OK\ndd: OK\n : OK\n")
.stderr_is("");
}
#[cfg(windows)]
{
for f in &["a", " b", "dd"] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"60b725f10c9c85c70d97880dfe8191b3 a\n\
bf35d7536c785cf06730d5a40301eba2 b\n\
b064a020db8018f18ff5ae367d01b212 dd",
);
scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5sum")
.succeeds()
.stdout_is("a: OK\n b: OK\ndd: OK\n")
.stderr_is("");
}
}
// GNU also supports one line sep
#[test]
fn test_check_md5sum_only_one_space() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
for f in ["a", " b", "c"] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"60b725f10c9c85c70d97880dfe8191b3 a\n\
bf35d7536c785cf06730d5a40301eba2 b\n\
2cd6ee2c70b0bde53fbe6cac3c8b8bb1 c\n",
);
scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5sum")
.succeeds()
.stdout_only("a: OK\n b: OK\nc: OK\n");
}
#[test]
fn test_check_md5sum_reverse_bsd() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
#[cfg(not(windows))]
{
for f in &["a", " b", "*c", "dd", " "] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"60b725f10c9c85c70d97880dfe8191b3 a\n\
bf35d7536c785cf06730d5a40301eba2 b\n\
f5b61709718c1ecf8db1aea8547d4698 *c\n\
b064a020db8018f18ff5ae367d01b212 dd\n\
d784fa8b6d98d27699781bd9a7cf19f0 ",
);
scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5sum")
.succeeds()
.stdout_is("a: OK\n b: OK\n*c: OK\ndd: OK\n : OK\n")
.stderr_is("");
}
#[cfg(windows)]
{
for f in &["a", " b", "dd"] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"60b725f10c9c85c70d97880dfe8191b3 a\n\
bf35d7536c785cf06730d5a40301eba2 b\n\
b064a020db8018f18ff5ae367d01b212 dd",
);
scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5sum")
.succeeds()
.stdout_is("a: OK\n b: OK\ndd: OK\n")
.stderr_is("");
}
}
#[test]
fn test_check_md5sum_mixed_format() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
#[cfg(not(windows))]
{
for f in &[" b", "*c", "dd", " "] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"bf35d7536c785cf06730d5a40301eba2 b\n\
f5b61709718c1ecf8db1aea8547d4698 *c\n\
b064a020db8018f18ff5ae367d01b212 dd\n\
d784fa8b6d98d27699781bd9a7cf19f0 ",
);
}
#[cfg(windows)]
{
for f in &[" b", "dd"] {
at.write(f, &format!("{f}\n"));
}
at.write(
"check.md5sum",
"bf35d7536c785cf06730d5a40301eba2 b\n\
b064a020db8018f18ff5ae367d01b212 dd",
);
}
scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5sum")
.fails_with_code(1);
}
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
}
#[test]
fn test_conflicting_arg() {
new_ucmd!()
.arg("--tag")
.arg("--check")
.arg("--md5")
.fails_with_code(1);
new_ucmd!()
.arg("--tag")
.arg("--text")
.arg("--md5")
.fails_with_code(1);
}
#[test]
fn test_tag() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("foobar", "foo bar\n");
scene
.ccmd("sha256sum")
.arg("--tag")
.arg("foobar")
.succeeds()
.stdout_is(
"SHA256 (foobar) = 1f2ec52b774368781bed1d1fb140a92e0eb6348090619c9291f9a5a3c8e8d151\n",
);
}
#[test]
#[cfg(not(windows))]
fn test_with_escape_filename() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let filename = "a\nb";
at.touch(filename);
let result = scene.ccmd("md5sum").arg("--text").arg(filename).succeeds();
let stdout = result.stdout_str();
println!("stdout {stdout}");
assert!(stdout.starts_with('\\'));
assert!(stdout.trim().ends_with("a\\nb"));
}
#[test]
#[cfg(not(windows))]
fn test_with_escape_filename_zero_text() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let filename = "a\nb";
at.touch(filename);
let result = scene
.ccmd("md5sum")
.arg("--text")
.arg("--zero")
.arg(filename)
.succeeds();
let stdout = result.stdout_str();
println!("stdout {stdout}");
assert!(!stdout.starts_with('\\'));
assert!(stdout.contains("a\nb"));
}
#[test]
fn test_check_empty_line() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write(
"in.md5",
"d41d8cd98f00b204e9800998ecf8427e f\n\nd41d8cd98f00b204e9800998ecf8427e f\ninvalid\n\n",
);
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stderr_contains("WARNING: 1 line is improperly formatted");
}
#[test]
#[cfg(not(windows))]
fn test_check_with_escape_filename() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let filename = "a\nb";
at.touch(filename);
let result = scene.ccmd("md5sum").arg("--tag").arg(filename).succeeds();
let stdout = result.stdout_str();
println!("stdout {stdout}");
assert!(stdout.starts_with("\\MD5"));
assert!(stdout.contains("a\\nb"));
at.write("check.md5", stdout);
let result = scene
.ccmd("md5sum")
.arg("--strict")
.arg("-c")
.arg("check.md5")
.succeeds();
result.stdout_is("\\a\\nb: OK\n");
}
#[test]
fn test_check_strict_error() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write(
"in.md5",
"ERR\nERR\nd41d8cd98f00b204e9800998ecf8427e f\nERR\n",
);
scene
.ccmd("md5sum")
.arg("--check")
.arg("--strict")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_contains("WARNING: 3 lines are improperly formatted");
}
#[test]
fn test_check_warn() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write(
"in.md5",
"d41d8cd98f00b204e9800998ecf8427e f\nd41d8cd98f00b204e9800998ecf8427e f\ninvalid\n",
);
scene
.ccmd("md5sum")
.arg("--check")
.arg("--warn")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stderr_contains("in.md5: 3: improperly formatted MD5 checksum line")
.stderr_contains("WARNING: 1 line is improperly formatted");
// with strict, we should fail the execution
scene
.ccmd("md5sum")
.arg("--check")
.arg("--strict")
.arg(at.subdir.join("in.md5"))
.fails();
}
#[test]
fn test_check_status() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "MD5(f)= d41d8cd98f00b204e9800998ecf8427f\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg("--status")
.arg(at.subdir.join("in.md5"))
.fails()
.no_output();
}
#[test]
fn test_check_status_code() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f f\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg("--status")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_is("")
.stdout_is("");
}
#[test]
fn test_sha1_with_md5sum_should_fail() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("f.sha1", "SHA1 (f) = d41d8cd98f00b204e9800998ecf8427e\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("f.sha1"))
.fails()
.stderr_contains("f.sha1: no properly formatted checksum lines found")
.stderr_does_not_contain("WARNING: 1 line is improperly formatted");
}
#[test]
// Disabled on Windows because of the "*"
#[cfg(not(windows))]
fn test_check_one_two_space_star() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("empty");
// with one space, the "*" is removed
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e *empty\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stdout_is("empty: OK\n");
// with two spaces, the "*" is not removed
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e *empty\n");
// First should fail as *empty doesn't exit
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stdout_is("*empty: FAILED open or read\n");
at.touch("*empty");
// Should pass as we have the file
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stdout_is("*empty: OK\n");
}
#[test]
// Disabled on Windows because of the "*"
#[cfg(not(windows))]
fn test_check_space_star_or_not() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("a");
at.touch("*c");
// with one space, the "*" is removed
at.write(
"in.md5",
"d41d8cd98f00b204e9800998ecf8427e *c\n
d41d8cd98f00b204e9800998ecf8427e a\n",
);
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stdout_contains("c: FAILED")
.stdout_does_not_contain("a: FAILED")
.stderr_contains("WARNING: 1 line is improperly formatted");
at.write(
"in.md5",
"d41d8cd98f00b204e9800998ecf8427e a\n
d41d8cd98f00b204e9800998ecf8427e *c\n",
);
// First should fail as *empty doesn't exit
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stdout_contains("a: OK")
.stderr_contains("WARNING: 1 line is improperly formatted");
}
#[test]
fn test_check_no_backslash_no_space() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "MD5(f)= d41d8cd98f00b204e9800998ecf8427e\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stdout_is("f: OK\n");
}
#[test]
fn test_incomplete_format() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "MD5 (\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_contains("no properly formatted checksum lines found");
}
#[test]
fn test_start_error() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "ERR\nd41d8cd98f00b204e9800998ecf8427e f\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg("--strict")
.arg(at.subdir.join("in.md5"))
.fails()
.stdout_is("f: OK\n")
.stderr_contains("WARNING: 1 line is improperly formatted");
}
#[test]
fn test_check_check_ignore_no_file() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f missing\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg("--ignore-missing")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_contains("in.md5: no file was verified");
}
#[test]
fn test_check_directory_error() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.mkdir("d");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f d\n");
#[cfg(not(windows))]
let err_msg = "md5sum: d: Is a directory\n";
#[cfg(windows)]
let err_msg = "md5sum: d: Permission denied\n";
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_contains(err_msg);
}
#[test]
fn test_check_quiet() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e f\n");
scene
.ccmd("md5sum")
.arg("--quiet")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.no_output();
// incorrect md5
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427f f\n");
scene
.ccmd("md5sum")
.arg("--quiet")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.fails()
.stdout_contains("f: FAILED")
.stderr_contains("WARNING: 1 computed checksum did NOT match");
scene
.ccmd("md5sum")
.arg("--quiet")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_contains("md5sum: the --quiet option is meaningful only when verifying checksums");
scene
.ccmd("md5sum")
.arg("--strict")
.arg(at.subdir.join("in.md5"))
.fails()
.stderr_contains("md5sum: the --strict option is meaningful only when verifying checksums");
}
#[test]
fn test_star_to_start() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
at.write("in.md5", "d41d8cd98f00b204e9800998ecf8427e *f\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg(at.subdir.join("in.md5"))
.succeeds()
.stdout_only("f: OK\n");
}
#[test]
fn test_check_b2sum_strict_check() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.touch("f");
let checksums = [
"2e f\n",
"e4a6a0577479b2b4 f\n",
"cae66941d9efbd404e4d88758ea67670 f\n",
"246c0442cd564aced8145b8b60f1370aa7 f\n",
"0e5751c026e543b2e8ab2eb06099daa1d1e5df47778f7787faab45cdf12fe3a8 f\n",
"4ded8c5fc8b12f3273f877ca585a44ad6503249a2b345d6d9c0e67d85bcb700db4178c0303e93b8f4ad758b8e2c9fd8b3d0c28e585f1928334bb77d36782e8 f\n",
"786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce f\n",
];
at.write("ck", &checksums.join(""));
let output = "f: OK\n".to_string().repeat(checksums.len());
scene
.ccmd("b2sum")
.arg("-c")
.arg(at.subdir.join("ck"))
.succeeds()
.stdout_only(&output);
scene
.ccmd("b2sum")
.arg("--strict")
.arg("-c")
.arg(at.subdir.join("ck"))
.succeeds()
.stdout_only(&output);
}
#[test]
fn test_check_md5_comment_line() {
// A comment in a checksum file shall be discarded unnoticed.
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("foo", "foo-content\n");
at.write(
"MD5SUM",
"\
# This is a comment\n\
8411029f3f5b781026a93db636aca721 foo\n\
# next comment is empty\n#",
);
scene
.ccmd("md5sum")
.arg("--check")
.arg("MD5SUM")
.succeeds()
.stdout_contains("foo: OK")
.no_stderr();
}
#[test]
fn test_check_md5_comment_only() {
// A file only filled with comments is equivalent to an empty file,
// and therefore produces an error.
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("foo", "foo-content\n");
at.write("MD5SUM", "# This is a comment\n");
scene
.ccmd("md5sum")
.arg("--check")
.arg("MD5SUM")
.fails()
.stderr_contains("no properly formatted checksum lines found");
}
#[test]
fn test_check_md5_comment_leading_space() {
// A file only filled with comments is equivalent to an empty file,
// and therefore produces an error.
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
at.write("foo", "foo-content\n");
at.write(
"MD5SUM",
" # This is a comment\n\
8411029f3f5b781026a93db636aca721 foo\n",
);
scene
.ccmd("md5sum")
.arg("--check")
.arg("MD5SUM")
.succeeds()
.stdout_contains("foo: OK")
.stderr_contains("WARNING: 1 line is improperly formatted");
}
#[test]
fn test_sha256_binary() {
let ts = TestScenario::new(util_name!());
assert_eq!(
ts.fixtures.read("binary.sha256.expected"),
get_hash!(ts
.ucmd()
.arg("--sha256")
.arg("--bits=256")
.arg("binary.png")
.succeeds()
.no_stderr()
.stdout_str())
);
}
#[test]
fn test_sha256_stdin_binary() {
let ts = TestScenario::new(util_name!());
assert_eq!(
ts.fixtures.read("binary.sha256.expected"),
get_hash!(ts
.ucmd()
.arg("--sha256")
.arg("--bits=256")
.pipe_in_fixture("binary.png")
.succeeds()
.no_stderr()
.stdout_str())
);
}
#[test]
fn test_check_sha256_binary() {
let ts = TestScenario::new(util_name!());
ts.ucmd()
.args(&[
"--sha256",
"--bits=256",
"--check",
"binary.sha256.checkfile",
])
.succeeds()
.no_stderr()
.stdout_is("binary.png: OK\n");
}
|