2 # Exercise cksum's --base64 option.
4 # Copyright (C) 2023-2025 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/>.
21 (my $program_name = $0) =~ s
|.*/||;
23 # Turn off localization of executable's output.
24 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x
3;
26 # Pairs of hash,degenerate_output, given file name of "f":
31 ['crc', "4294967295 0 f"],
33 ['md5', "1B2M2Y8AsgTpgAmY7PhCfg=="],
34 ['sha1', "2jmj7l5rSw0yVb/vlWAYkK/YBwk="],
35 ['sha224', "0UoCjCo6K8lHYQK7KII0xBWisB+CjqYqxbPkLw=="],
36 ['sha256', "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="],
37 ['sha384', "OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb"],
38 ['sha512', "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg=="],
39 ['blake2b', "eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg=="],
40 ['sm3', "GrIdg1XPoX+OYRlIMegajyK+yMco/vt0ftA161CCqis="],
43 # Return the formatted output for a given hash name/value pair.
44 # Use the hard-coded "f" as file name.
47 $h !~ m{^(sysv|bsd|crc|crc32b)$} and $v = uc($h). " (f) = $v";
48 # BLAKE2b is inconsistent:
49 $v =~ s{BLAKE2B}{BLAKE2b};
55 # Ensure that each of the above works with --base64:
56 (map {my ($h,$v)= @
$_; my $o=fmt
$h,$v;
57 [$h, "--base64 -a $h", {IN
=>{f
=>''}}, {OUT
=>"$o\n"}]} @pairs),
59 # For each that accepts --check, ensure that works with base64 digests:
60 (map {my ($h,$v)= @
$_; my $o=fmt
$h,$v;
61 ["chk-".$h, "--check --strict", {IN
=>$o},
62 {AUX
=>{f
=>''}}, {OUT
=>"f: OK\n"}]}
63 grep { $_->[0] !~ m{^(sysv|bsd|crc|crc32b)$} } @pairs),
65 # For digests ending in "=", ensure --check fails if any "=" is removed.
66 (map {my ($h,$v)= @
$_; my $o=fmt
$h,$v;
67 ["chk-eq1-".$h, "--check", {IN
=>$o}, {AUX
=>{f
=>''}},
68 {ERR_SUBST
=>"s/.*: //"}, {OUT
=>''}, {EXIT
=>1},
69 {ERR
=>"no properly formatted checksum lines found\n"}]}
70 ( map {my ($h,$v)=@
$_; $v =~ s/=$//; [$h,$v] }
71 grep { $_->[1] =~ m{=$} } @pairs)),
73 # Same as above, but for those ending in "==":
74 (map {my ($h,$v)= @
$_; my $o=fmt
$h,$v;
75 ["chk-eq2-".$h, "--check", {IN
=>$o}, {AUX
=>{f
=>''}},
76 {ERR_SUBST
=>"s/.*: //"}, {OUT
=>''}, {EXIT
=>1},
77 {ERR
=>"no properly formatted checksum lines found\n"}]}
78 ( map {my ($h,$v)=@
$_; $v =~ s/==$//; [$h,$v] }
79 grep { $_->[1] =~ m{==$} } @pairs)),
81 # Trigger a read-buffer-overrun error in an early (not committed)
82 # version of the --base64-adding patch.
83 ['nul', '-a sha1 --check', {IN
=>'\0\0\0'},
84 {ERR
=>"no properly formatted checksum lines found\n"},
85 {ERR_SUBST
=>"s/.*: //"}, {OUT
=>''}, {EXIT
=>1}],
88 my $save_temps = $ENV{DEBUG
};
89 my $verbose = $ENV{VERBOSE
};
92 my $fail = run_tests
($program_name, $prog, \
@Tests, $save_temps, $verbose);
94 # Ensure hash names from cksum --help match those in @pairs above.
95 my $help_algs = join ' ', map { m{^ ([[:alpha:]]\S+)} }
96 grep { m{^ ([[:alpha:]]\S+)} } split ('\n', `cksum --help`);
97 my $test_algs = join ' ', map {$_->[0]} @pairs;
98 $help_algs eq $test_algs or die "$help_algs not equal to\n$test_algs";