diff options
| author | Pádraig Brady <P@draigBrady.com> | 2023-02-03 16:34:18 +0000 |
|---|---|---|
| committer | Pádraig Brady <P@draigBrady.com> | 2023-02-06 13:09:40 +0000 |
| commit | ead07bb3d461389bb52336109be7858458e49c38 (patch) | |
| tree | 8d905a8a307dec94d422783162fc241dc659cc48 /tests | |
| parent | build: uptime: avoid issues on systems without utmp.h (diff) | |
| download | coreutils-ead07bb3d461389bb52336109be7858458e49c38.tar.gz coreutils-ead07bb3d461389bb52336109be7858458e49c38.zip | |
cksum: add --raw option to output a binary digest
--raw output is the most composable format, and also is a
robust way to discard the file name without parsing (escaped) output.
Examples:
$ cksum --raw -a crc "$afile" | basenc --base16
4ACFC4F0
$ cksum --raw -a crc "$afile" | basenc --base2msbf
01001010110011111100010011110000
$ cksum --raw -a sha256 "$bfile" | basenc --base32
AAAAAAAADHLGRHAILLQWLAY6SNH7OY5OI2RKNQLSWPY3MCUM4JXQ====
* doc/coreutils.texi (cksum invocation): Describe the new feature.
* src/digest.c (output_file): Inspect the new RAW_DIGEST global,
and output the bytes directly if set.
* src/cksum.c (output_crc): Likewise.
* src/sum.c (output_bsd, output_sysv): Likewise.
* tests/misc/cksum-raw.sh: A new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the new feature.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/local.mk | 1 | ||||
| -rwxr-xr-x | tests/misc/cksum-raw.sh | 60 |
2 files changed, 61 insertions, 0 deletions
diff --git a/tests/local.mk b/tests/local.mk index 70a8f6e73..4c40fd115 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -294,6 +294,7 @@ all_tests = \ tests/misc/cksum-a.sh \ tests/misc/cksum-c.sh \ tests/misc/cksum-base64.pl \ + tests/misc/cksum-raw.sh \ tests/misc/comm.pl \ tests/misc/csplit.sh \ tests/misc/csplit-1000.sh \ diff --git a/tests/misc/cksum-raw.sh b/tests/misc/cksum-raw.sh new file mode 100755 index 000000000..a5f14be88 --- /dev/null +++ b/tests/misc/cksum-raw.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# Validate cksum --raw operation + +# 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/>. + +. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src +print_ver_ cksum date od + +cat > digest_types <<\EOF || framework_failure_ +bsd u2 +sysv u2 +crc u4 +md5 x1 +sha1 x1 +sha224 x1 +sha256 x1 +sha384 x1 +sha512 x1 +blake2b x1 +sm3 x1 +EOF + +date > file.in || framework_failure_ + +while read algo type; do + # Binary converted back to text + cksum --raw --algorithm $algo file.in > digest.bin || fail=1 + d='digest.bin.txt' + od --endian=big -An -w1024 -t$type < digest.bin | tr -d ' ' \ + > "$d" || framework_failure_ + # Pad the bsd checksum with leading 0's, if needed. + case $algo in bsd) n=$(cat "$d"); printf '%05d\n' "$n" > "$d" ;; esac + + # Standard text output + cksum --untagged --algorithm $algo < file.in | cut -d ' ' -f1 \ + > digest.txt || fail=1 + + compare digest.txt "$d" || fail=1 +done < digest_types + +# Ensure --base64 and --raw not used together +returns_ 1 cksum --base64 --raw </dev/null || fail=1 + +# Ensure --raw not supported with multiple files +returns_ 1 cksum --raw /dev/null /dev/null || fail=1 + +Exit $fail |
