xref: /freebsd/usr.bin/cmp/tests/cmp_test2.sh (revision 81ad6265)
1# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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# $FreeBSD$
27
28atf_test_case special
29special_head() {
30	atf_set "descr" "Test cmp(1)'s handling of non-regular files"
31}
32special_body() {
33	echo 0123456789abcdef > a
34	echo 0123456789abcdeg > b
35	atf_check -s exit:0 -o empty -e empty -x "cat a | cmp a -"
36	atf_check -s exit:0 -o empty -e empty -x "cat a | cmp - a"
37	atf_check -s exit:1 -o not-empty -e empty -x "cat b | cmp a -"
38	atf_check -s exit:1 -o not-empty -e empty -x "cat b | cmp - a"
39
40	atf_check -s exit:0 -o empty -e empty -x "cmp a a <&-"
41}
42
43atf_test_case symlink
44symlink_head() {
45	atf_set "descr" "Test cmp(1)'s handling of symlinks"
46}
47symlink_body() {
48	echo 0123456789abcdef > a
49	echo 0123456789abcdeg > b
50	ln -s a a.lnk
51	ln -s b b.lnk
52	ln -s a a2.lnk
53	cp a adup
54	ln -s adup adup.lnk
55	atf_check -s exit:0 cmp a a.lnk
56	atf_check -s exit:0 cmp a.lnk a
57	atf_check -s not-exit:0 -o ignore cmp a b.lnk
58	atf_check -s not-exit:0 -o ignore cmp b.lnk a
59	atf_check -s not-exit:0 -o ignore -e ignore cmp -h a a.lnk
60	atf_check -s not-exit:0 -o ignore -e ignore cmp -h a.lnk a
61	atf_check -s exit:0 cmp -h a.lnk a2.lnk
62	atf_check -s not-exit:0 -o ignore -e ignore cmp -h a.lnk adup.lnk
63}
64
65atf_test_case pr252542
66pr252542_head()
67{
68	atf_set "descr" "Test cmp(1) -s with file offset skips"
69}
70pr252542_body()
71{
72	echo -n '1234567890' > a
73	echo -n 'abc567890' > b
74	echo -n 'xbc567890' > c
75	atf_check -s exit:0 cmp -s a b 4 3
76	atf_check -s exit:0 cmp -i 4:3 -s a b
77	atf_check -s exit:0 cmp -i 1 -s b c
78	atf_check -s exit:1 -o ignore cmp -z a b 4 3
79	atf_check -s exit:1 -o ignore cmp -i 4:3 -z a b
80	atf_check -s exit:1 -o ignore cmp -i 1 -z a b
81}
82
83atf_test_case skipsuff
84skipsuff_head()
85{
86	atf_set "descr" "Test cmp(1) accepting SI suffixes on skips"
87}
88skipsuff_body()
89{
90
91	jot -nb a -s '' 1028 > a
92	jot -nb b -s '' 1024 > b
93	jot -nb a -s '' 4 >> b
94
95	atf_check -s exit:1 -o ignore cmp -s a b
96	atf_check -s exit:0 cmp -s a b 1k 1k
97}
98
99atf_test_case limit
100limit_head()
101{
102	atf_set "descr" "Test cmp(1) -n (limit)"
103}
104limit_body()
105{
106	echo -n "aaaabbbb" > a
107	echo -n "aaaaxxxx" > b
108
109	atf_check -s exit:1 -o ignore cmp -s a b
110	atf_check -s exit:0 cmp -sn 4 a b
111	atf_check -s exit:0 cmp -sn 3 a b
112	atf_check -s exit:1 -o ignore cmp -sn 5 a b
113
114	# Test special, too.  The implementation for link is effectively
115	# identical.
116	atf_check -s exit:0 -e empty -x "cat a | cmp -sn 4 b -"
117	atf_check -s exit:0 -e empty -x "cat a | cmp -sn 3 b -"
118	atf_check -s exit:1 -o ignore -x "cat a | cmp -sn 5 b -"
119}
120
121atf_test_case bflag
122bflag_head()
123{
124	atf_set "descr" "Test cmp(1) -b (print bytes)"
125}
126bflag_body()
127{
128	echo -n "abcd" > a
129	echo -n "abdd" > b
130
131	atf_check -s exit:1 -o file:$(atf_get_srcdir)/b_flag.out \
132	    cmp -b a b
133	atf_check -s exit:1 -o file:$(atf_get_srcdir)/bl_flag.out \
134	    cmp -bl a b
135}
136
137atf_init_test_cases()
138{
139	atf_add_test_case special
140	atf_add_test_case symlink
141	atf_add_test_case pr252542
142	atf_add_test_case skipsuff
143	atf_add_test_case limit
144	atf_add_test_case bflag
145}
146