xref: /freebsd/sbin/md5/tests/md5_test.sh (revision 9768746b)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2022 Kyle Evans <kevans@FreeBSD.org>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28atf_test_case sum_bflag
29sum_bflag_body()
30{
31	cp $(atf_get_srcdir)/sum_a.in a
32	cp $(atf_get_srcdir)/sum_a.in b
33
34	(sha256 -q a | tr -d '\n'; echo " *a") > expected
35	(sha256 -q b | tr -d '\n'; echo " *b") >> expected
36
37	atf_check -o file:expected sha256sum -b a b
38}
39
40atf_test_case sum_cflag
41sum_cflag_body()
42{
43
44	# Verify that the *sum -c mode works even if some files are missing.
45	# PR 267722 identified that we would never advance past the first record
46	# to check against.  As a result, things like checking the published
47	# checksums for the install media became a more manual process again if
48	# you didn't download all of the images.
49	for combo in "a b c" "b c" "a c" "a b" "a" "b" "c" ""; do
50		rm -f a b c
51		:> out
52		cnt=0
53		for f in ${combo}; do
54			cp $(atf_get_srcdir)/sum_${f}.in ${f}
55			printf "${f}: OK\n" >> out
56			cnt=$((cnt + 1))
57		done
58
59		err=0
60		[ "$cnt" -eq 3 ] || err=1
61		atf_check -o file:out -e ignore -s exit:${err} \
62		    sha256sum -c $(atf_get_srcdir)/sum_sums.digest
63	done
64
65}
66
67atf_test_case sum_tflag
68sum_tflag_body()
69{
70	cp $(atf_get_srcdir)/sum_a.in a
71
72	# -t is a nop, not a time trial, when used with the *sum versions
73	(sha256 -q a | tr -d '\n'; echo "  a") > expected
74	atf_check -o file:expected sha256sum -t a
75}
76
77atf_init_test_cases()
78{
79	atf_add_test_case sum_bflag
80	atf_add_test_case sum_cflag
81	atf_add_test_case sum_tflag
82}
83