xref: /freebsd/usr.bin/du/tests/du_test.sh (revision 3e5550d25c6d614f422506373f7068c6c366fa85)
1#
2# Copyright (c) 2017 Enji Cooper <ngie@FreeBSD.org>
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23# SUCH DAMAGE.
24#
25
26require_sparse_file_support()
27{
28	if ! getconf MIN_HOLE_SIZE "$(pwd)"; then
29		echo "getconf MIN_HOLE_SIZE $(pwd) failed; sparse files " \
30		    "probably not supported by file system"
31		mount
32		atf_skip "Test's work directory does not support sparse files;" \
33		    "try with a different TMPDIR?"
34	fi
35}
36
37atf_test_case A_flag
38A_flag_head()
39{
40	atf_set "descr" "Verify -A behavior"
41}
42A_flag_body()
43{
44	require_sparse_file_support
45	# XXX: compressed volumes?
46	atf_check truncate -s 10g sparse.file
47	atf_check -o inline:'1\tsparse.file\n' du -g sparse.file
48	atf_check -o inline:'10\tsparse.file\n' du -A -g sparse.file
49}
50
51atf_test_case H_flag
52H_flag_head()
53{
54	atf_set "descr" "Verify -H behavior"
55}
56H_flag_body()
57{
58	local paths1='testdir/A/B testdir/A testdir/C testdir'
59	local paths2='testdir/C/B testdir/C'
60	local lineprefix=$'^[0-9]+\t'
61	local sep="\$\n${lineprefix}"
62
63	atf_check mkdir testdir
64	atf_check -x "cd testdir && mkdir A && touch A/B && ln -s A C"
65
66	atf_check -o save:du.out du -aAH testdir
67	atf_check egrep -q "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")$" du.out
68	# Check that the output doesn't contain any lines (i.e. paths) that we
69	# did not expect it to contain from $paths1.
70	atf_check -s exit:1 egrep -vq "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")$" du.out
71
72	atf_check -o save:du_C.out du -aAH testdir/C
73	atf_check egrep -q "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")$" du_C.out
74
75	# Check that the output doesn't contain any lines (i.e. paths) that we
76	# did not expect it to contain from $paths2.
77	atf_check -s exit:1 egrep -vq "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")$" du_C.out
78}
79
80atf_test_case I_flag
81I_flag_head()
82{
83	atf_set "descr" "Verify -I behavior"
84}
85I_flag_body()
86{
87	paths_sans_foo_named="a/motley/fool/of/sorts fool/parts/with/their/cache bar baz"
88	paths_foo_named="foo foobar"
89	paths="$paths_sans_foo_named $paths_foo_named"
90
91	# cd'ing to testdir helps ensure that files from atf/kyua don't
92	# pollute the results.
93	atf_check -x "mkdir testdir && cd testdir && mkdir -p $paths"
94	atf_check -o save:du.out -x "cd testdir && du -s $paths_sans_foo_named"
95	atf_check -o save:du_I.out -x "cd testdir && du -I '*foo*' -s $paths"
96
97	atf_check diff -u du.out du_I.out
98}
99
100atf_test_case L_flag
101L_flag_head()
102{
103	atf_set "descr" "Verify -L behavior"
104}
105L_flag_body()
106{
107	atf_check mkdir -p testdir1
108	atf_check truncate -s 8192 testdir1/A
109	atf_check ln -s A testdir1/B
110	atf_check -o inline:'9\ttestdir1\n' du -A testdir1
111	atf_check -o inline:'17\ttestdir1\n' du -AL testdir1
112}
113
114atf_test_case P_flag
115P_flag_head()
116{
117	atf_set "descr" "Verify -P behavior"
118}
119P_flag_body()
120{
121	atf_check mkdir -p testdir1
122	atf_check truncate -s 8192 testdir1/A
123	atf_check ln -s A testdir1/B
124	atf_check -o inline:'9\ttestdir1\n' du -A testdir1
125	atf_check -o inline:'9\ttestdir1\n' du -AP testdir1
126}
127
128atf_test_case a_flag
129a_flag_head()
130{
131	atf_set "descr" "Verify -a behavior"
132}
133a_flag_body()
134{
135	atf_check mkdir -p testdir1
136	atf_check truncate -s 0 testdir1/A
137	atf_check -o inline:'1\ttestdir1\n' du -A testdir1
138	atf_check -o inline:'0\ttestdir1/A\n1\ttestdir1\n' du -Aa testdir1
139}
140
141atf_test_case c_flag
142c_flag_head()
143{
144	atf_set	"descr" "Verify -c output"
145}
146c_flag_body()
147{
148	atf_check truncate -s 8192 foo bar
149	atf_check -o inline:'8\tfoo\n8\tbar\n' du -A foo bar
150	atf_check -o inline:'8\tfoo\n8\tbar\n16\ttotal\n' du -Ac foo bar
151}
152
153atf_test_case d_flag
154d_flag_head()
155{
156	atf_set "descr" "Verify -d behavior"
157}
158d_flag_body()
159{
160	atf_check mkdir -p d0/d1/d2
161	atf_check -o inline:'2\td0\n' du -A -d 0 d0
162	atf_check -o inline:'1\td0/d1\n2\td0\n' du -A -d 1 d0
163	atf_check -o inline:'1\td0/d1/d2\n1\td0/d1\n2\td0\n' du -A -d 2 d0
164	atf_check -o inline:'1\td0/d1/d2\n1\td0/d1\n2\td0\n' du -A -d 3 d0
165}
166
167atf_test_case g_flag
168g_flag_head()
169{
170	atf_set "descr" "Verify -g output"
171}
172g_flag_body()
173{
174	require_sparse_file_support
175	atf_check truncate -s 1k A
176	atf_check truncate -s 1m B
177	atf_check truncate -s 1g C
178	atf_check truncate -s 1t D
179	atf_check -o inline:'1\tA\n1\tB\n1\tC\n1024\tD\n' du -Ag A B C D
180}
181
182atf_test_case h_flag
183h_flag_head()
184{
185	atf_set	"descr" "Verify -h output"
186}
187h_flag_body()
188{
189	require_sparse_file_support
190	atf_check truncate -s 1k A
191	atf_check truncate -s 1m B
192	atf_check truncate -s 1g C
193	atf_check truncate -s 1t D
194	atf_check -o inline:'1.0K\tA\n1.0M\tB\n1.0G\tC\n1.0T\tD\n' du -Ah A B C D
195}
196
197atf_test_case k_flag
198k_flag_head()
199{
200	atf_set "descr" "Verify -k output"
201}
202k_flag_body()
203{
204	atf_check truncate -s 1k A
205	atf_check truncate -s 1m B
206	atf_check -o inline:'1\tA\n1024\tB\n' du -Ak A B
207}
208
209atf_test_case l_flag
210l_flag_head()
211{
212	atf_set "descr" "Verify -l output"
213}
214l_flag_body()
215{
216	atf_check truncate -s 0 A
217	atf_check ln A B
218	atf_check -o inline:'0\tA\n' du -A A B
219	atf_check -o inline:'0\tA\n0\tB\n' du -Al A B
220}
221
222atf_test_case m_flag
223m_flag_head()
224{
225	atf_set "descr" "Verify -m output"
226}
227m_flag_body()
228{
229	atf_check truncate -s 1k A
230	atf_check truncate -s 1m B
231	atf_check truncate -s 1g C
232	atf_check -o inline:'1\tA\n1\tB\n1024\tC\n' du -Am A B C
233}
234
235atf_test_case n_flag
236n_flag_head()
237{
238	atf_set "descr" "Verify -n output"
239}
240n_flag_body()
241{
242	atf_check truncate -s 0 A
243	atf_check truncate -s 0 B
244	atf_check -o inline:'0\tA\n0\tB\n' du -An A B
245	atf_check chflags nodump B
246	atf_check -o inline:'0\tA\n' du -An A B
247}
248
249atf_test_case s_flag
250s_flag_head()
251{
252	atf_set "descr" "Verify -s behavior"
253}
254s_flag_body()
255{
256	atf_check mkdir -p testdir1/testdir2
257	atf_check truncate -s 0 testdir1/A testdir1/testdir2/B
258	atf_check -o inline:'1\ttestdir1\n' du -As testdir1
259	atf_check -o inline:'0\ttestdir1/A\n' du -As testdir1/A
260	atf_check -o inline:'1\ttestdir1/testdir2\n' du -As testdir1/testdir2
261	atf_check -o inline:'0\ttestdir1/testdir2/B\n' \
262	    du -As testdir1/testdir2/B
263}
264
265atf_test_case si_flag
266si_flag_head()
267{
268	atf_set "descr" "Verify --si output"
269}
270si_flag_body()
271{
272	atf_check truncate -s 1500000 A
273	atf_check truncate -s 1572864 B
274
275	atf_check -o inline:'1.4M\tA\n1.5M\tB\n' du -Ah A B
276	atf_check -o inline:'1.5M\tA\n1.6M\tB\n' du -A --si A B
277}
278
279atf_add_test_case t_flag
280t_flag_head()
281{
282	atf_set "descr" "Verify -t output"
283}
284t_flag_body()
285{
286	atf_check mkdir -p testdir1/testdir2
287	atf_check truncate -s 8192 testdir1/A testdir1/testdir2/B
288
289	atf_check -o inline:'17\ttestdir1\n' du -At 10240 testdir1
290	atf_check -o inline:'9\ttestdir1/testdir2\n' du -At -10240 testdir1
291	atf_check -o inline:'9\ttestdir1/testdir2\n17\ttestdir1\n' \
292	    du -Aat 9216 testdir1
293	atf_check -o save:du.out du -Aat -9216 testdir1
294	atf_check -o inline:'8\ttestdir1/A\n8\ttestdir1/testdir2/B\n' \
295	    sort du.out
296}
297
298atf_init_test_cases()
299{
300	atf_add_test_case A_flag
301	atf_add_test_case H_flag
302	atf_add_test_case I_flag
303	atf_add_test_case L_flag
304	atf_add_test_case P_flag
305	atf_add_test_case a_flag
306	atf_add_test_case c_flag
307	atf_add_test_case d_flag
308	atf_add_test_case g_flag
309	atf_add_test_case h_flag
310	atf_add_test_case k_flag
311	atf_add_test_case l_flag
312	atf_add_test_case m_flag
313	atf_add_test_case n_flag
314	atf_add_test_case s_flag
315	atf_add_test_case si_flag
316	atf_add_test_case t_flag
317}
318