xref: /freebsd/usr.bin/hexdump/tests/hexdump_test.sh (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2017 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# $FreeBSD$
28
29ENDIAN=el
30ret=$(echo I | tr -d "[:space:]" | od -to2 | head -n1 | awk '{print $2}' | cut -c6)
31if [ "$ret" = "0" ]; then
32        ENDIAN=eb
33fi
34
35atf_test_case b_flag
36b_flag_head()
37{
38	atf_set "descr" "Verify -b output"
39}
40b_flag_body()
41{
42	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_bflag_a.out" \
43	    hexdump -b "$(atf_get_srcdir)/d_hexdump_a.in"
44	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_bflag_b.out" \
45	    hexdump -b "$(atf_get_srcdir)/d_hexdump_b.in"
46	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_bflag_c.out" \
47	    hexdump -b "$(atf_get_srcdir)/d_hexdump_c.in"
48}
49
50atf_test_case c_flag
51c_flag_head()
52{
53	atf_set "descr" "Verify -c output"
54}
55c_flag_body()
56{
57	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_cflag_a.out" \
58	    hexdump -c "$(atf_get_srcdir)/d_hexdump_a.in"
59	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_cflag_b.out" \
60	    hexdump -c "$(atf_get_srcdir)/d_hexdump_b.in"
61	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_cflag_c.out" \
62	    hexdump -c "$(atf_get_srcdir)/d_hexdump_c.in"
63}
64
65atf_test_case C_flag
66C_flag_head()
67{
68	atf_set "descr" "Verify -C output"
69}
70C_flag_body()
71{
72	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_a.out" \
73	    hexdump -C "$(atf_get_srcdir)/d_hexdump_a.in"
74	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_b.out" \
75	    hexdump -C "$(atf_get_srcdir)/d_hexdump_b.in"
76	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_c.out" \
77	    hexdump -C "$(atf_get_srcdir)/d_hexdump_c.in"
78	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_bug118723.out" \
79	    hexdump -C "$(atf_get_srcdir)/d_hexdump_bug118723.in"
80	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCvflag_bug118723.out" \
81	    hexdump -Cv "$(atf_get_srcdir)/d_hexdump_bug118723.in"
82}
83
84atf_test_case hd_name
85hd_name_head()
86{
87	atf_set "descr" "Verify hd output matching -C output"
88}
89hd_name_body()
90{
91	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_a.out" \
92	    hd "$(atf_get_srcdir)/d_hexdump_a.in"
93	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_b.out" \
94	    hd "$(atf_get_srcdir)/d_hexdump_b.in"
95	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_c.out" \
96	    hd "$(atf_get_srcdir)/d_hexdump_c.in"
97	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_bug118723.out" \
98	    hd "$(atf_get_srcdir)/d_hexdump_bug118723.in"
99	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCvflag_bug118723.out" \
100	    hd -v "$(atf_get_srcdir)/d_hexdump_bug118723.in"
101}
102
103atf_test_case d_flag
104d_flag_head()
105{
106	atf_set "descr" "Verify -d output"
107}
108d_flag_body()
109{
110	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_dflag_a_$ENDIAN.out" \
111	    hexdump -d "$(atf_get_srcdir)/d_hexdump_a.in"
112	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_dflag_b_$ENDIAN.out" \
113	    hexdump -d "$(atf_get_srcdir)/d_hexdump_b.in"
114	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_dflag_c_$ENDIAN.out" \
115	    hexdump -d "$(atf_get_srcdir)/d_hexdump_c.in"
116}
117
118atf_test_case n_flag
119n_flag_head()
120{
121	atf_set "descr" "Check -n functionality"
122}
123n_flag_body()
124{
125	atf_check -o empty hexdump -bn 0 "$(atf_get_srcdir)/d_hexdump_a.in"
126	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_nflag_a.out" \
127	    hexdump -bn 1 "$(atf_get_srcdir)/d_hexdump_a.in"
128}
129
130atf_test_case o_flag
131o_flag_head()
132{
133	atf_set "descr" "Verify -o output"
134}
135o_flag_body()
136{
137	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_oflag_a_$ENDIAN.out" \
138	    hexdump -o "$(atf_get_srcdir)/d_hexdump_a.in"
139	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_oflag_b_$ENDIAN.out" \
140	    hexdump -o "$(atf_get_srcdir)/d_hexdump_b.in"
141	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_oflag_c_$ENDIAN.out" \
142	    hexdump -o "$(atf_get_srcdir)/d_hexdump_c.in"
143}
144
145atf_test_case s_flag
146s_flag_head()
147{
148	atf_set "descr" "Verify -s output"
149}
150s_flag_body()
151{
152	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_sflag_a.out" \
153	    hexdump -bs 4 "$(atf_get_srcdir)/d_hexdump_a.in"
154
155	atf_check -o not-empty hexdump -n 100 -s 1024 /dev/random
156}
157
158atf_test_case v_flag
159v_flag_head()
160{
161	atf_set "descr" "Verify -v functionality"
162}
163v_flag_body()
164{
165	for i in $(seq 0 7); do
166		atf_check -o match:"^\*$" \
167		    hexdump -s ${i} "$(atf_get_srcdir)/d_hexdump_c.in"
168		atf_check -o not-match:"^\*$" \
169		    hexdump -vs ${i} "$(atf_get_srcdir)/d_hexdump_c.in"
170	done
171
172	atf_check -o not-match:"^\*$" \
173	    hexdump -s 8 "$(atf_get_srcdir)/d_hexdump_c.in"
174	atf_check -o not-match:"^\*$" \
175	    hexdump -vs 8 "$(atf_get_srcdir)/d_hexdump_c.in"
176}
177
178atf_test_case x_flag
179x_flag_head()
180{
181	atf_set "descr" "Verify -x output"
182}
183x_flag_body()
184{
185	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_xflag_a_$ENDIAN.out" \
186	    hexdump -x "$(atf_get_srcdir)/d_hexdump_a.in"
187	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_xflag_b_$ENDIAN.out" \
188	    hexdump -x "$(atf_get_srcdir)/d_hexdump_b.in"
189	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_xflag_c_$ENDIAN.out" \
190	    hexdump -x "$(atf_get_srcdir)/d_hexdump_c.in"
191}
192
193atf_test_case no_conv_err
194no_conv_err()
195{
196	atf_set "descr" "Verify missing conversion char error handling"
197}
198no_conv_err_body()
199{
200	atf_check -s exit:1 -e ignore \
201	    hexdump -e '"%"'
202	atf_check -s exit:1 -e ignore \
203	    hexdump -e '4/2 "%"'
204}
205
206atf_init_test_cases()
207{
208	atf_add_test_case b_flag
209	atf_add_test_case c_flag
210	atf_add_test_case C_flag
211	atf_add_test_case hd_name
212	atf_add_test_case d_flag
213	atf_add_test_case n_flag
214	atf_add_test_case o_flag
215	atf_add_test_case s_flag
216	atf_add_test_case v_flag
217	atf_add_test_case x_flag
218	atf_add_test_case no_conv_err
219}
220