tests: cksum: extend with --status, --ignore-missing and --warn
[coreutils.git] / tests / cksum / cksum-c.sh
blob3ead1450440f2664de8f588a28e3f6d6a2f4f70c
1 #!/bin/sh
2 # Validate cksum --check dynamic operation
4 # Copyright (C) 2021-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ cksum shuf
22 shuf -i 1-10 > input || framework_failure_
24 for args in '-a sha384' '-a blake2b' '-a blake2b -l 384' '-a sm3'; do
25 cksum $args 'input' >> CHECKSUMS || fail=1
26 done
27 cksum --strict --check CHECKSUMS || fail=1
29 # Ensure leading whitespace and \ ignored
30 sed 's/^/ \\/' CHECKSUMS | cksum --strict -c || fail=1
32 # Check common signed checksums format works in non strict mode
33 cat >> signed_CHECKSUMS <<\EOF
34 -----BEGIN PGP SIGNED MESSAGE-----
35 Hash: SHA384
37 # ignored comment
38 EOF
39 cat CHECKSUMS >> signed_CHECKSUMS
40 cat >> signed_CHECKSUMS <<\EOF
41 -----BEGIN PGP SIGNATURE-----
43 # Note base64 doesn't have ambiguous delimiters in its charset
44 SHA384+BCAAdFiEEjFummQvbJuGfKhqAEWGuaUVxmjkFAmCDId0ACgkQEWGuaUVx
45 BLAKE2b/00001EuON62pTEnqrJ5lav61QxRByiuDp/6VODrRL2JWM6Stxu1Myws=
46 =AWU7
47 -----END PGP SIGNATURE-----
48 EOF
49 cksum --check signed_CHECKSUMS || fail=1
51 # Can check individual digests in a mixed file
52 cksum --check -a sm3 CHECKSUMS || fail=1
54 # Checks against older (non hex) checksum formats not supported
55 returns_ 1 cksum -a crc -c CHECKSUMS || fail=1
56 cksum -a crc 'input' > CHECKSUMS.crc || fail=1
57 returns_ 1 cksum -c CHECKSUMS.crc || fail=1
59 # Test --status
60 cksum --status --check CHECKSUMS >out 2>&1 || fail=1
61 # Should be empty
62 compare /dev/null out || fail=1
64 # Check for the error mgmt
65 echo 'invalid line' >> CHECKSUMS
66 # Exit code is 0 in this case
67 cksum --check CHECKSUMS >out 2>&1 || fail=1
68 grep '1 line is improperly formatted' out || fail=1
69 # But not with --strict
70 cksum --strict --check CHECKSUMS >out 2>&1 && fail=1
71 grep '1 line is improperly formatted' out || fail=1
72 echo "invalid line" >> CHECKSUMS
73 # plurial checks
74 cksum --strict --check CHECKSUMS >out 2>&1 && fail=1
75 grep '2 lines are improperly formatted' out || fail=1
77 # Inject an incorrect checksum
78 invalid_sum='aaaaaaaaaaaaaaaaaaaaaaaaaaaaafdb57c725157cb40b5aee8d937b8351477e'
79 echo "SM3 (input) = $invalid_sum" >> CHECKSUMS
80 cksum --check CHECKSUMS >out 2>&1 && fail=1
81 # with --strict (won't change the result)
82 grep '1 computed checksum did NOT match' out || fail=1
83 grep 'input: FAILED' out || fail=1
84 cksum --check --strict CHECKSUMS >out 2>&1 && fail=1
86 # With a non existing file
87 echo "SM3 (input2) = $invalid_sum" >> CHECKSUMS2
88 cksum --check CHECKSUMS2 >out 2>&1 && fail=1
89 grep 'input2: FAILED open or read' out || fail=1
90 grep '1 listed file could not be read' out || fail=1
91 # with --strict (won't change the result)
92 cksum --check --strict CHECKSUMS2 >out 2>&1 && fail=1
94 # With errors
95 cksum --status --check CHECKSUMS >out 2>&1 && fail=1
96 compare /dev/null out || fail=1
98 # Test --warn
99 echo "BLAKE2b (missing-file) = $invalid_sum" >> CHECKSUMS
100 cksum --warn --check CHECKSUMS > out 2>&1
101 # check that the incorrect lines are correctly reported with --warn
102 grep 'CHECKSUMS: 5: improperly formatted SM3 checksum line' out || fail=1
103 grep 'CHECKSUMS: 8: improperly formatted BLAKE2b checksum line' out || fail=1
105 # Test --ignore-missing
107 echo "SM3 (nonexistent) = $invalid_sum" >> CHECKSUMS-missing
108 # we have output on both stdout and stderr
109 cksum --check CHECKSUMS-missing > stdout 2> stderr && fail=1
110 grep 'nonexistent: FAILED open or read' stdout || fail=1
111 grep 'nonexistent: No such file or directory' stderr || fail=1
112 grep '1 listed file could not be read' stderr || fail=1
114 cksum --ignore-missing --check CHECKSUMS-missing > stdout 2> stderr && fail=1
115 # We should not get these errors
116 grep -v 'nonexistent: No such file or directory' stdout && fail=1
117 grep -v 'nonexistent: FAILED open or read' stdout && fail=1
118 grep 'CHECKSUMS-missing: no file was verified' stderr || fail=1
120 # Combination of --status and --warn
121 cksum --status --warn --check CHECKSUMS > out 2>&1 && fail=1
123 grep 'CHECKSUMS: 8: improperly formatted BLAKE2b checksum line' out || fail=1
124 grep 'WARNING: 3 lines are improperly formatted' out || fail=1
125 grep 'WARNING: 1 computed checksum did NOT match' out || fail=1
127 # The order matters. --status will hide the results
128 cksum --warn --status --check CHECKSUMS > out 2>&1 && fail=1
129 grep -v 'CHECKSUMS: 8: improperly formatted BLAKE2b checksum line' out && fail=1
130 grep -v 'WARNING: 3 lines are improperly formatted' out && fail=1
131 grep -v 'WARNING: 1 computed checksum did NOT match' out && fail=1
133 # Combination of --status and --ignore-missing
134 cksum --status --ignore-missing --check CHECKSUMS > out 2>&1 && fail=1
135 # should be empty
136 compare /dev/null out || fail=1
138 # Combination of all three
139 cksum --status --warn --ignore-missing --check \
140 CHECKSUMS-missing > out 2>&1 && fail=1
141 # Not empty
142 test -s out || fail=1
143 grep 'CHECKSUMS-missing: no file was verified' out || fail=1
144 grep -v 'nonexistent: No such file or directory' stdout && fail=1
146 Exit $fail