1#!/bin/sh
2# 'md5sum' tests for generation and checking of
3# BSD traditional and alternate formats (md5 [-r])
4
5# Copyright (C) 2011-2023 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21print_ver_ md5sum
22
23## BSD alternate format tests ##
24
25# Ensure we can --check BSD alternate format.
26# Note we start this list with a name
27# that's unambiguous in BSD format.
28# I.e., one not starting with ' ' or '*'
29for i in 'a' ' b' '*c' 'dd' ' '; do
30  echo "$i" > "$i"
31  md5sum --text "$i" >> check.md5sum || fail=1
32done
33sed 's/  / /' check.md5sum > check.md5
34
35# Note only a single format is supported per run
36md5sum --strict -c check.md5sum || fail=1
37md5sum --strict -c check.md5 || fail=1
38
39# Ensure we don't trigger BSD reversed format with GPG headers etc.
40{ echo '____not_all_hex_so_no_match_____ blah';
41  cat check.md5sum; } > check2.md5sum || framework_failure_
42md5sum -c check2.md5sum 2>check2.err || fail=1
43echo 'md5sum: WARNING: 1 line is improperly formatted' >check2.exp
44compare check2.exp check2.err || fail=1
45
46# If we skip the first entry in the BSD format checksums
47# then it'll be detected as standard format and error.
48# This unlikely caveat was thought better than mandating
49# an option to avoid the ambiguity.
50tail -n+2 check.md5 | returns_ 1 md5sum --strict -c || fail=1
51
52
53## BSD traditional format tests (--tag option) ##
54
55# Ensure --tag and --check are mutually exclusive
56returns_ 1 md5sum --tag --check /dev/null || fail=1
57
58# Ensure --tag and --text are mutually exclusive
59# We don't support --text with BSD tradition format,
60# as that would complicate the output format,
61# while providing little benefit over --text processing
62# available with the default md5sum output format.
63returns_ 1 md5sum --tag --text /dev/null || fail=1
64
65# Ensure we can --check BSD traditional format we produce
66rm check.md5
67for i in 'a' ' b' '*c' 'dd' ' '; do
68  echo "$i" > "$i"
69  md5sum --tag "$i" >> check.md5 || fail=1
70done
71md5sum --strict -c check.md5 || fail=1
72
73if : > 'backslash\is\not\dir\sep'; then
74  # Ensure we can --check BSD traditional format we produce
75  # with the GNU extension of escaped newlines
76  nl='
77'
78  t='	'
79  rm check.md5
80  for i in 'a\b' 'a\' '\a' "a${nl}b" "a${t}b"; do
81    : > "$i"
82    md5sum --tag "$i" >> check.md5 || fail=1
83  done
84  md5sum --strict -c check.md5 > out || fail=1
85  printf '%s: OK\n' '\a\\b' '\a\\' '\\\a' '\a\nb' "a${t}b" > exp ||
86    framework_failure_
87  compare exp out || fail=1
88
89
90  # Ensure BSD traditional format with GNU extension escapes
91  # is in the expected format
92  ex_file='test
93\\file'
94  ex_output='\MD5 (test\n\\\\file) = d41d8cd98f00b204e9800998ecf8427e'
95  touch "$ex_file"
96  printf "%s\n" "$ex_output" > exp
97  md5sum --tag "$ex_file" > out || fail=1
98  compare exp out || fail=1
99fi
100
101Exit $fail
102