xref: /freebsd/usr.bin/cmp/tests/cmp_test2.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# Copyright (c) 2017 Alan Somers
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24# POSSIBILITY OF SUCH DAMAGE.
25#
26
27atf_test_case special
28special_head() {
29	atf_set "descr" "Test cmp(1)'s handling of non-regular files"
30}
31special_body() {
32	echo 0123456789abcdef > a
33	echo 0123456789abcdeg > b
34	atf_check -s exit:0 -o empty -e empty cmp a - <a
35	atf_check -s exit:0 -o empty -e empty cmp - a <a
36	atf_check -s exit:1 -o not-empty -e empty cmp a - <b
37	atf_check -s exit:1 -o not-empty -e empty cmp - a <b
38
39	atf_check -s exit:0 -o empty -e empty cmp a a <&-
40}
41
42atf_test_case symlink
43symlink_head() {
44	atf_set "descr" "Test cmp(1)'s handling of symlinks"
45}
46symlink_body() {
47	echo 0123456789abcdef > a
48	echo 0123456789abcdeg > b
49	ln -s a a.lnk
50	ln -s b b.lnk
51	ln -s a a2.lnk
52	cp a adup
53	ln -s adup adup.lnk
54	atf_check -s exit:0 cmp a a.lnk
55	atf_check -s exit:0 cmp a.lnk a
56	atf_check -s not-exit:0 -o ignore cmp a b.lnk
57	atf_check -s not-exit:0 -o ignore cmp b.lnk a
58	atf_check -s not-exit:0 -o ignore -e ignore cmp -h a a.lnk
59	atf_check -s not-exit:0 -o ignore -e ignore cmp -h a.lnk a
60	atf_check -s exit:0 cmp -h a.lnk a2.lnk
61	atf_check -s not-exit:0 -o ignore -e ignore cmp -h a.lnk adup.lnk
62}
63
64atf_test_case pr252542
65pr252542_head()
66{
67	atf_set "descr" "Test cmp(1) -s with file offset skips"
68}
69pr252542_body()
70{
71	echo -n '1234567890' > a
72	echo -n 'abc567890' > b
73	echo -n 'xbc567890' > c
74	atf_check -s exit:0 cmp -s a b 4 3
75	atf_check -s exit:0 cmp -i 4:3 -s a b
76	atf_check -s exit:0 cmp -i 1 -s b c
77	atf_check -s exit:1 -o ignore cmp -z a b 4 3
78	atf_check -s exit:1 -o ignore cmp -i 4:3 -z a b
79	atf_check -s exit:1 -o ignore cmp -i 1 -z a b
80}
81
82atf_test_case skipsuff
83skipsuff_head()
84{
85	atf_set "descr" "Test cmp(1) accepting SI suffixes on skips"
86}
87skipsuff_body()
88{
89
90	jot -nb a -s '' 1028 > a
91	jot -nb b -s '' 1024 > b
92	jot -nb a -s '' 4 >> b
93
94	atf_check -s exit:1 -o ignore cmp -s a b
95	atf_check -s exit:0 cmp -s a b 1k 1k
96}
97
98atf_test_case limit
99limit_head()
100{
101	atf_set "descr" "Test cmp(1) -n (limit)"
102}
103limit_body()
104{
105	echo -n "aaaabbbb" > a
106	echo -n "aaaaxxxx" > b
107
108	atf_check -s exit:1 -o ignore cmp -s a b
109	atf_check -s exit:0 cmp -sn 4 a b
110	atf_check -s exit:0 cmp -sn 3 a b
111	atf_check -s exit:1 -o ignore cmp -sn 5 a b
112
113	# Test special, too.  The implementation for link is effectively
114	# identical.
115	atf_check -s exit:0 -e empty cmp -sn 4 b - <a
116	atf_check -s exit:0 -e empty cmp -sn 3 b - <a
117	atf_check -s exit:1 -o ignore cmp -sn 5 b - <a
118}
119
120atf_test_case bflag
121bflag_head()
122{
123	atf_set "descr" "Test cmp(1) -b (print bytes)"
124}
125bflag_body()
126{
127	echo -n "abcd" > a
128	echo -n "abdd" > b
129
130	atf_check -s exit:1 -o file:$(atf_get_srcdir)/b_flag.out \
131	    cmp -b a b
132	atf_check -s exit:1 -o file:$(atf_get_srcdir)/bl_flag.out \
133	    cmp -bl a b
134}
135
136# Helper for stdout test case
137atf_check_stdout()
138{
139	(
140		trap "" PIPE
141		sleep 1
142		cmp "$@" 2>stderr
143		echo $? >result
144	) | true
145	atf_check -o inline:"2\n" cat result
146	atf_check -o match:"stdout" cat stderr
147}
148
149atf_test_case stdout
150stdout_head()
151{
152	atf_set descr "Failure to write to stdout"
153}
154stdout_body()
155{
156	echo a >a
157	echo b >b
158	atf_check_stdout a b
159	atf_check_stdout - b <a
160	atf_check_stdout a - <b
161	ln -s a alnk
162	ln -s b blnk
163	atf_check_stdout -h alnk blnk
164}
165
166atf_init_test_cases()
167{
168	atf_add_test_case special
169	atf_add_test_case symlink
170	atf_add_test_case pr252542
171	atf_add_test_case skipsuff
172	atf_add_test_case limit
173	atf_add_test_case bflag
174	atf_add_test_case stdout
175}
176