1
0
mirror of git://git.sv.gnu.org/coreutils.git synced 2026-04-17 00:56:34 +02:00
Files
coreutils/tests/misc/cksum-base64.pl
Jim Meyering b319685c6e cksum: accept new option: --base64 (-b)
* src/digest.c [HASH_ALGO_CKSUM]: Include "base64.h"
[HASH_ALGO_CKSUM] (base64_digest): New global.
[HASH_ALGO_CKSUM] (enum BASE64_DIGEST_OPTION): New enum.
[HASH_ALGO_CKSUM] (long_options): Add "base64".
(valid_digits): Rename from hex_digits, now taking an input length argument.
Adjust callers.
(bsd_split_3): Rename arg from hex_digits to digest.
Add new *d_len parameter for length of extracted digest.
Move "i" declaration down to first use.
(split_3): Rename arg from hex_digits to digest.
Add new *d_len parameter for length of extracted digest.
Instead of relying on "known" length of digest to find the following
must-be-whitespace byte, search for the first whitespace byte.
[HASH_ALGO_CKSUM] (output_file): Handle base64_digest.
[HASH_ALGO_CKSUM] (main): Set base64_digest.
[HASH_ALGO_CKSUM] (b64_equal): New function.
(hex_equal): New function, factored out of digest_check.
(digest_check) Factored part into b64_equal and hex_equal.
Rename local hex_digest to digest.
* tests/misc/cksum-base64.pl: Add tests.
* tests/local.mk (all_tests): Add to the list.
* cfg.mk (_cksum): Define.
(exclude_file_name_regexp--sc_prohibit_test_backticks): Exempt new test.
(exclude_file_name_regexp--sc_long_lines): Likewise.
* doc/coreutils.texi (cksum invocation): Document it.
(md5sum invocation) [--check]: Mention digest encoding auto-detect.
* NEWS (New Features): Mention this.
2023-01-31 18:24:28 -08:00

100 lines
3.8 KiB
Perl
Executable File

#!/usr/bin/perl
# Exercise cksum's --base64 option.
# Copyright (C) 2023 Free Software Foundation, Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
use strict;
(my $program_name = $0) =~ s|.*/||;
# Turn off localization of executable's output.
@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
# Pairs of hash,degenerate_output, given file name of "f":
my @pairs =
(
['sysv', "0 0 f"],
['bsd', "00000 0 f"],
['crc', "4294967295 0 f"],
['md5', "1B2M2Y8AsgTpgAmY7PhCfg=="],
['sha1', "2jmj7l5rSw0yVb/vlWAYkK/YBwk="],
['sha224', "0UoCjCo6K8lHYQK7KII0xBWisB+CjqYqxbPkLw=="],
['sha256', "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="],
['sha384', "OLBgp1GsljhM2TJ+sbHjaiH9txEUvgdDTAzHv2P24donTt6/529l+9Ua0vFImLlb"],
['sha512', "z4PhNX7vuL3xVChQ1m2AB9Yg5AULVxXcg/SpIdNs6c5H0NE8XYXysP+DGNKHfuwvY7kxvUdBeoGlODJ6+SfaPg=="],
['blake2b', "eGoC90IBWQPGxv2FJVLScpEvR0DhWEdhiobiF/cfVBnSXhAxr+5YUxOJZESTTrBLkDpoWxRIt1XVb3Aa/pvizg=="],
['sm3', "GrIdg1XPoX+OYRlIMegajyK+yMco/vt0ftA161CCqis="],
);
# Return the formatted output for a given hash name/value pair.
# Use the hard-coded "f" as file name.
sub fmt ($$) {
my ($h, $v) = @_;
$h !~ m{^(sysv|bsd|crc)$} and $v = uc($h). " (f) = $v";
# BLAKE2b is inconsistent:
$v =~ s{BLAKE2B}{BLAKE2b};
return "$v"
}
my @Tests =
(
# Ensure that each of the above works with -b:
(map {my ($h,$v)= @$_; my $o=fmt $h,$v;
[$h, "-ba $h", {IN=>{f=>''}}, {OUT=>"$o\n"}]} @pairs),
# For each that accepts --check, ensure that works with base64 digests:
(map {my ($h,$v)= @$_; my $o=fmt $h,$v;
["chk-".$h, "--check --strict", {IN=>$o},
{AUX=>{f=>''}}, {OUT=>"f: OK\n"}]}
grep { $_->[0] !~ m{^(sysv|bsd|crc)$} } @pairs),
# For digests ending in "=", ensure --check fails if any "=" is removed.
(map {my ($h,$v)= @$_; my $o=fmt $h,$v;
["chk-eq1-".$h, "--check", {IN=>$o}, {AUX=>{f=>''}},
{ERR_SUBST=>"s/.*: //"}, {OUT=>''}, {EXIT=>1},
{ERR=>"no properly formatted checksum lines found\n"}]}
( map {my ($h,$v)=@$_; $v =~ s/=$//; [$h,$v] }
grep { $_->[1] =~ m{=$} } @pairs)),
# Same as above, but for those ending in "==":
(map {my ($h,$v)= @$_; my $o=fmt $h,$v;
["chk-eq2-".$h, "--check", {IN=>$o}, {AUX=>{f=>''}},
{ERR_SUBST=>"s/.*: //"}, {OUT=>''}, {EXIT=>1},
{ERR=>"no properly formatted checksum lines found\n"}]}
( map {my ($h,$v)=@$_; $v =~ s/==$//; [$h,$v] }
grep { $_->[1] =~ m{==$} } @pairs)),
# Trigger a read-buffer-overrun error in an early (not committed)
# version of the --base64-adding patch.
['nul', '-a sha1 --check', {IN=>'\0\0\0'},
{ERR=>"no properly formatted checksum lines found\n"},
{ERR_SUBST=>"s/.*: //"}, {OUT=>''}, {EXIT=>1}],
);
my $save_temps = $ENV{DEBUG};
my $verbose = $ENV{VERBOSE};
my $prog = 'cksum';
my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
# Ensure hash names from cksum --help match those in @pairs above.
my $help_algs = join ' ', map { m{^ ([[:alpha:]]\S+)} }
grep { m{^ ([[:alpha:]]\S+)} } split ('\n', `cksum --help`);
my $test_algs = join ' ', map {$_->[0]} @pairs;
$help_algs eq $test_algs or die "$help_algs not equal to\n$test_algs";
exit $fail;