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