xref: /freebsd/bin/ls/tests/ls_tests.sh (revision af71f40a983c21a3c4a5c7c3d88d566e721bae45)
1#
2# Copyright 2015 EMC Corp.
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are
7# met:
8#
9# * Redistributions of source code must retain the above copyright
10#   notice, this list of conditions and the following disclaimer.
11# * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29
30create_test_dir()
31{
32	[ -z "$ATF_TMPDIR" ] || return 0
33
34	export ATF_TMPDIR=$(pwd)
35
36	# XXX: need to nest this because of how kyua creates $TMPDIR; otherwise
37	# it will run into EPERM issues later
38	TEST_INPUTS_DIR="${ATF_TMPDIR}/test/inputs"
39
40	atf_check -e empty -s exit:0 mkdir -m 0777 -p $TEST_INPUTS_DIR
41	cd $TEST_INPUTS_DIR
42}
43
44create_test_inputs()
45{
46	create_test_dir
47
48	atf_check -e empty -s exit:0 mkdir -m 0755 -p a/b
49	atf_check -e empty -s exit:0 ln -s a/b c
50	atf_check -e empty -s exit:0 touch d
51	atf_check -e empty -s exit:0 ln d e
52	atf_check -e empty -s exit:0 touch .f
53	atf_check -e empty -s exit:0 mkdir .g
54	atf_check -e empty -s exit:0 mkfifo h
55	atf_check -e ignore -s exit:0 dd if=/dev/zero of=i count=1000 bs=1
56	atf_check -e empty -s exit:0 \
57	    sh -c "pid=${ATF_TMPDIR}/nc.pid; daemon -p \$pid nc -lU j; sleep 2; pkill -F \$pid"
58	atf_check -e empty -s exit:0 touch klmn
59	atf_check -e empty -s exit:0 touch opqr
60	atf_check -e empty -s exit:0 touch stuv
61	atf_check -e empty -s exit:0 touch wxyz
62	atf_check -e empty -s exit:0 touch 0b00000001
63	atf_check -e empty -s exit:0 touch 0b00000010
64	atf_check -e empty -s exit:0 touch 0b00000011
65	atf_check -e empty -s exit:0 touch 0b00000100
66	atf_check -e empty -s exit:0 touch 0b00000101
67	atf_check -e empty -s exit:0 touch 0b00000110
68	atf_check -e empty -s exit:0 touch 0b00000111
69	atf_check -e empty -s exit:0 touch 0b00001000
70	atf_check -e empty -s exit:0 touch 0b00001001
71	atf_check -e empty -s exit:0 touch 0b00001010
72	atf_check -e empty -s exit:0 touch 0b00001011
73	atf_check -e empty -s exit:0 touch 0b00001100
74	atf_check -e empty -s exit:0 touch 0b00001101
75	atf_check -e empty -s exit:0 touch 0b00001110
76	atf_check -e empty -s exit:0 touch 0b00001111
77}
78
79atf_test_case a_flag
80a_flag_head()
81{
82	atf_set "descr" "Verify -a support"
83}
84
85a_flag_body()
86{
87	create_test_dir
88
89	# Make sure "." and ".." show up with -a
90	atf_check -e empty -o match:'\.[[:space:]]+\.\.'  -s exit:0 ls -ax
91
92	create_test_inputs
93
94	WITH_a=$PWD/../with_a.out
95	WITHOUT_a=$PWD/../without_a.out
96
97	atf_check -e empty -o save:$WITH_a -s exit:0 ls -A
98	atf_check -e empty -o save:$WITHOUT_a -s exit:0 ls
99
100	echo "-A usage"
101	cat $WITH_a
102	echo "No -A usage"
103	cat $WITHOUT_a
104
105	for dot_path in '\.f' '\.g'; do
106		atf_check -e empty -o not-empty -s exit:0 grep "${dot_path}" \
107		    $WITH_a
108		atf_check -e empty -o empty -s not-exit:0 grep "${dot_path}" \
109		    $WITHOUT_a
110	done
111}
112
113atf_test_case A_flag
114A_flag_head()
115{
116	atf_set "descr" "Verify -A support with unprivileged users"
117}
118
119A_flag_body()
120{
121	create_test_dir
122
123	atf_check -e empty -o empty -s exit:0 ls -A
124
125	create_test_inputs
126
127	WITH_A=$PWD/../with_A.out
128	WITHOUT_A=$PWD/../without_A.out
129
130	atf_check -e empty -o save:$WITH_A -s exit:0 ls -A
131	atf_check -e empty -o save:$WITHOUT_A -s exit:0 ls
132
133	echo "-A usage"
134	cat $WITH_A
135	echo "No -A usage"
136	cat $WITHOUT_A
137
138	for dot_path in '\.f' '\.g'; do
139		atf_check -e empty -o not-empty -s exit:0 grep "${dot_path}" \
140		    $WITH_A
141		atf_check -e empty -o empty -s not-exit:0 grep "${dot_path}" \
142		    $WITHOUT_A
143	done
144}
145
146atf_test_case A_flag_implied_when_root
147A_flag_implied_when_root_head()
148{
149	atf_set "descr" "Verify that -A is implied for root"
150	atf_set "require.user" "root"
151}
152
153A_flag_implied_when_root_body()
154{
155	create_test_dir
156
157	atf_check -e empty -o empty -s exit:0 ls -A
158
159	create_test_inputs
160
161	WITH_EXPLICIT=$PWD/../with_explicit_A.out
162	WITH_IMPLIED=$PWD/../with_implied_A.out
163
164	atf_check -e empty -o save:$WITH_EXPLICIT -s exit:0 ls -A
165	atf_check -e empty -o save:$WITH_IMPLIED -s exit:0 ls
166
167	echo "Explicit -A usage"
168	cat $WITH_EXPLICIT
169	echo "Implicit -A usage"
170	cat $WITH_IMPLIED
171
172	atf_check_equal "$(cat $WITH_EXPLICIT)" "$(cat $WITH_IMPLIED)"
173}
174
175atf_test_case B_flag
176B_flag_head()
177{
178	atf_set "descr" "Verify that the output from ls -B prints out non-printable characters"
179}
180
181B_flag_body()
182{
183	atf_skip "kyua report-jenkins doesn't properly escape non-printable chars: https://github.com/jmmv/kyua/issues/136"
184
185	atf_check -e empty -o empty -s exit:0 touch "$(printf "y\013z")"
186	atf_check -e empty -o match:'y\\013z' -s exit:0 ls -B
187}
188
189atf_test_case C_flag
190C_flag_head()
191{
192	atf_set "descr" "Verify that the output from ls -C is multi-column, sorted down"
193}
194
195C_flag_body()
196{
197	create_test_inputs
198
199	WITH_C=$PWD/../with_C.out
200
201	atf_check -e empty -o save:$WITH_C -s exit:0 ls -C
202
203	echo "With -C usage"
204	cat $WITH_C
205
206	atf_check -e ignore -o not-empty -s exit:0 \
207	    egrep "0b00000001[[:space:]]+0b00000111[[:space:]]+0b00001101[[:space:]]+e[[:space:]]+stuv" $WITH_C
208	atf_check -e ignore -o not-empty -s exit:0 \
209	    egrep "0b00000010[[:space:]]+0b00001000[[:space:]]+0b00001110[[:space:]]+h[[:space:]]+wxyz" $WITH_C
210}
211
212atf_test_case I_flag
213I_flag_head()
214{
215	atf_set "descr" "Verify that the output from ls -I is the same as ls for an unprivileged user"
216}
217
218I_flag_body()
219{
220	create_test_inputs
221
222	WITH_I=$PWD/../with_I.out
223	WITHOUT_I=$PWD/../without_I.out
224
225	atf_check -e empty -o save:$WITH_I -s exit:0 ls -I
226	atf_check -e empty -o save:$WITHOUT_I -s exit:0 ls
227
228	echo "Explicit -I usage"
229	cat $WITH_I
230	echo "No -I usage"
231	cat $WITHOUT_I
232
233	atf_check_equal "$(cat $WITH_I)" "$(cat $WITHOUT_I)"
234}
235
236atf_test_case I_flag_voids_implied_A_flag_when_root
237I_flag_voids_implied_A_flag_when_root_head()
238{
239	atf_set "descr" "Verify that -I voids out implied -A for root"
240	atf_set "require.user" "root"
241}
242
243I_flag_voids_implied_A_flag_when_root_body()
244{
245	create_test_inputs
246
247	atf_check -o not-match:'\.f' -s exit:0 ls -I
248	atf_check -o not-match:'\.g' -s exit:0 ls -I
249
250	atf_check -o match:'\.f' -s exit:0 ls -A -I
251	atf_check -o match:'\.g' -s exit:0 ls -A -I
252}
253
254lcomma_flag_head()
255{
256	atf_set "descr" "Verify that -l, prints out the size with , delimiters"
257}
258
259lcomma_flag_body()
260{
261	create_test_inputs
262
263	atf_check \
264	    -o match:'\-rw\-r\-\-r\-\-[[:space:]]+.+[[:space:]]+1,000[[:space:]]+.+i' \
265	    env LC_ALL=en_US.ISO8859-1 ls -l, i
266}
267
268x_flag_head()
269{
270	atf_set "descr" "Verify that -x prints out one item per line"
271}
272
273x_flag_body()
274{
275	create_test_inputs
276
277	WITH_x=$PWD/../with_x.out
278
279	atf_check -e empty -o save:$WITH_x -s exit:0 ls -x
280
281	echo "With -x usage"
282	cat $WITH_x
283
284	atf_check -e ignore -o not-empty -s exit:0 \
285	    egrep "a[[:space:]]+c[[:space:]]+d[[:space:]]+e[[:space:]]+h" $WITH_x
286	atf_check -e ignore -o not-empty -s exit:0 \
287	    egrep "i[[:space:]]+j[[:space:]]+klmn[[:space:]]+opqr[[:space:]]+stuv" $WITH_x
288}
289
2901_flag_head()
291{
292	atf_set "descr" "Verify that -1 prints out one item per line"
293}
294
2951_flag_body()
296{
297	create_test_inputs
298
299	WITH_1=$PWD/../with_1.out
300	WITHOUT_1=$PWD/../without_1.out
301
302	atf_check -e empty -o save:$WITH_1 -s exit:0 ls -1
303	atf_check -e empty -o save:$WITHOUT_1 -s exit:0 \
304		sh -c 'for i in $(ls); do echo $i; done'
305
306	echo "Explicit -1 usage"
307	cat $WITH_1
308	echo "No -1 usage"
309	cat $WITHOUT_1
310
311	atf_check_equal "$(cat $WITH_1)" "$(cat $WITHOUT_1)"
312}
313
314atf_init_test_cases()
315{
316
317	atf_add_test_case A_flag
318	atf_add_test_case A_flag_implied_when_root
319	atf_add_test_case B_flag
320	atf_add_test_case C_flag
321	#atf_add_test_case D_flag
322	#atf_add_test_case F_flag
323	#atf_add_test_case G_flag
324	#atf_add_test_case H_flag
325	atf_add_test_case I_flag
326	atf_add_test_case I_flag_voids_implied_A_flag_when_root
327	#atf_add_test_case L_flag
328	#atf_add_test_case P_flag
329	#atf_add_test_case R_flag
330	#atf_add_test_case S_flag
331	#atf_add_test_case T_flag
332	#atf_add_test_case U_flag
333	#atf_add_test_case W_flag
334	#atf_add_test_case Z_flag
335	atf_add_test_case a_flag
336	#atf_add_test_case b_flag
337	#atf_add_test_case c_flag
338	#atf_add_test_case d_flag
339	#atf_add_test_case f_flag
340	#atf_add_test_case g_flag
341	#atf_add_test_case h_flag
342	#atf_add_test_case i_flag
343	#atf_add_test_case k_flag
344	#atf_add_test_case l_flag
345	atf_add_test_case lcomma_flag
346	#atf_add_test_case m_flag
347	#atf_add_test_case n_flag
348	#atf_add_test_case o_flag
349	#atf_add_test_case p_flag
350	#atf_add_test_case q_flag
351	#atf_add_test_case r_flag
352	#atf_add_test_case s_flag
353	#atf_add_test_case t_flag
354	#atf_add_test_case u_flag
355	#atf_add_test_case w_flag
356	atf_add_test_case x_flag
357	#atf_add_test_case y_flag
358	atf_add_test_case 1_flag
359}
360