xref: /freebsd/usr.bin/hexdump/tests/hexdump_test.sh (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1#
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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}
79
80atf_test_case hd_name
81hd_name_head()
82{
83	atf_set "descr" "Verify hd output matching -C output"
84}
85hd_name_body()
86{
87	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_a.out" \
88	    hd "$(atf_get_srcdir)/d_hexdump_a.in"
89	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_b.out" \
90	    hd "$(atf_get_srcdir)/d_hexdump_b.in"
91	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_UCflag_c.out" \
92	    hd "$(atf_get_srcdir)/d_hexdump_c.in"
93}
94
95atf_test_case d_flag
96d_flag_head()
97{
98	atf_set "descr" "Verify -d output"
99}
100d_flag_body()
101{
102	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_dflag_a_$ENDIAN.out" \
103	    hexdump -d "$(atf_get_srcdir)/d_hexdump_a.in"
104	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_dflag_b_$ENDIAN.out" \
105	    hexdump -d "$(atf_get_srcdir)/d_hexdump_b.in"
106	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_dflag_c_$ENDIAN.out" \
107	    hexdump -d "$(atf_get_srcdir)/d_hexdump_c.in"
108}
109
110atf_test_case n_flag
111n_flag_head()
112{
113	atf_set "descr" "Check -n functionality"
114}
115n_flag_body()
116{
117	atf_check -o empty hexdump -bn 0 "$(atf_get_srcdir)/d_hexdump_a.in"
118	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_nflag_a.out" \
119	    hexdump -bn 1 "$(atf_get_srcdir)/d_hexdump_a.in"
120}
121
122atf_test_case o_flag
123o_flag_head()
124{
125	atf_set "descr" "Verify -o output"
126}
127o_flag_body()
128{
129	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_oflag_a_$ENDIAN.out" \
130	    hexdump -o "$(atf_get_srcdir)/d_hexdump_a.in"
131	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_oflag_b_$ENDIAN.out" \
132	    hexdump -o "$(atf_get_srcdir)/d_hexdump_b.in"
133	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_oflag_c_$ENDIAN.out" \
134	    hexdump -o "$(atf_get_srcdir)/d_hexdump_c.in"
135}
136
137atf_test_case s_flag
138s_flag_head()
139{
140	atf_set "descr" "Verify -s output"
141}
142s_flag_body()
143{
144	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_sflag_a.out" \
145	    hexdump -bs 4 "$(atf_get_srcdir)/d_hexdump_a.in"
146
147	atf_check -o not-empty hexdump -n 100 -s 1024 /dev/random
148}
149
150atf_test_case v_flag
151v_flag_head()
152{
153	atf_set "descr" "Verify -v functionality"
154}
155v_flag_body()
156{
157	for i in $(seq 0 7); do
158		atf_check -o match:"^\*$" \
159		    hexdump -s ${i} "$(atf_get_srcdir)/d_hexdump_c.in"
160		atf_check -o not-match:"^\*$" \
161		    hexdump -vs ${i} "$(atf_get_srcdir)/d_hexdump_c.in"
162	done
163
164	atf_check -o not-match:"^\*$" \
165	    hexdump -s 8 "$(atf_get_srcdir)/d_hexdump_c.in"
166	atf_check -o not-match:"^\*$" \
167	    hexdump -vs 8 "$(atf_get_srcdir)/d_hexdump_c.in"
168}
169
170atf_test_case x_flag
171x_flag_head()
172{
173	atf_set "descr" "Verify -x output"
174}
175x_flag_body()
176{
177	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_xflag_a_$ENDIAN.out" \
178	    hexdump -x "$(atf_get_srcdir)/d_hexdump_a.in"
179	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_xflag_b_$ENDIAN.out" \
180	    hexdump -x "$(atf_get_srcdir)/d_hexdump_b.in"
181	atf_check -o file:"$(atf_get_srcdir)/d_hexdump_xflag_c_$ENDIAN.out" \
182	    hexdump -x "$(atf_get_srcdir)/d_hexdump_c.in"
183}
184
185atf_test_case no_conv_err
186no_conv_err()
187{
188	atf_set "descr" "Verify missing conversion char error handling"
189}
190no_conv_err_body()
191{
192	atf_check -s exit:1 -e ignore \
193	    hexdump -e '"%"'
194	atf_check -s exit:1 -e ignore \
195	    hexdump -e '4/2 "%"'
196}
197
198atf_init_test_cases()
199{
200	atf_add_test_case b_flag
201	atf_add_test_case c_flag
202	atf_add_test_case C_flag
203	atf_add_test_case hd_name
204	atf_add_test_case d_flag
205	atf_add_test_case n_flag
206	atf_add_test_case o_flag
207	atf_add_test_case s_flag
208	atf_add_test_case v_flag
209	atf_add_test_case x_flag
210	atf_add_test_case no_conv_err
211}
212