xref: /freebsd/usr.bin/du/tests/du_test.sh (revision 19261079)
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# $FreeBSD$
26
27require_sparse_file_support()
28{
29	if ! getconf MIN_HOLE_SIZE "$(pwd)"; then
30		echo "getconf MIN_HOLE_SIZE $(pwd) failed; sparse files " \
31		    "probably not supported by file system"
32		mount
33		atf_skip "Test's work directory does not support sparse files;" \
34		    "try with a different TMPDIR?"
35	fi
36}
37
38atf_test_case A_flag
39A_flag_head()
40{
41	atf_set "descr" "Verify -A behavior"
42}
43A_flag_body()
44{
45	require_sparse_file_support
46	# XXX: compressed volumes?
47	atf_check truncate -s 10g sparse.file
48	atf_check -o inline:'1\tsparse.file\n' du -g sparse.file
49	atf_check -o inline:'10\tsparse.file\n' du -A -g sparse.file
50}
51
52atf_test_case H_flag
53H_flag_head()
54{
55	atf_set "descr" "Verify -H behavior"
56}
57H_flag_body()
58{
59	local paths1='testdir/A/B testdir/A testdir/C testdir'
60	local paths2='testdir/C/B testdir/C'
61	local lineprefix=$'^[0-9]+\t'
62	local sep="\$\n${lineprefix}"
63
64	atf_check mkdir testdir
65	atf_check -x "cd testdir && mkdir A && touch A/B && ln -s A C"
66
67	atf_check -o save:du.out du -aAH testdir
68	atf_check egrep -q "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")$" du.out
69	# Check that the output doesn't contain any lines (i.e. paths) that we
70	# did not expect it to contain from $paths1.
71	atf_check -s exit:1 egrep -vq "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")$" du.out
72
73	atf_check -o save:du_C.out du -aAH testdir/C
74	atf_check egrep -q "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")$" du_C.out
75
76	# Check that the output doesn't contain any lines (i.e. paths) that we
77	# did not expect it to contain from $paths2.
78	atf_check -s exit:1 egrep -vq "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")$" du_C.out
79}
80
81atf_test_case I_flag
82I_flag_head()
83{
84	atf_set "descr" "Verify -I behavior"
85}
86I_flag_body()
87{
88	paths_sans_foo_named="a/motley/fool/of/sorts fool/parts/with/their/cache bar baz"
89	paths_foo_named="foo foobar"
90	paths="$paths_sans_foo_named $paths_foo_named"
91
92	# cd'ing to testdir helps ensure that files from atf/kyua don't
93	# pollute the results.
94	atf_check -x "mkdir testdir && cd testdir && mkdir -p $paths"
95	atf_check -o save:du.out -x "cd testdir && du -s $paths_sans_foo_named"
96	atf_check -o save:du_I.out -x "cd testdir && du -I '*foo*' -s $paths"
97
98	atf_check diff -u du.out du_I.out
99}
100
101atf_test_case c_flag
102c_flag_head()
103{
104	atf_set	"descr" "Verify -c output"
105}
106c_flag_body()
107{
108	atf_check truncate -s 0 foo bar
109}
110
111atf_test_case g_flag
112g_flag_head()
113{
114	atf_set "descr" "Verify -g output"
115}
116g_flag_body()
117{
118	require_sparse_file_support
119	atf_check truncate -s 1k A
120	atf_check truncate -s 1m B
121	atf_check truncate -s 1g C
122	atf_check truncate -s 1t D
123	atf_check -o inline:'1\tA\n1\tB\n1\tC\n1024\tD\n' du -Ag A B C D
124}
125
126atf_test_case h_flag
127h_flag_head()
128{
129	atf_set	"descr" "Verify -h output"
130}
131h_flag_body()
132{
133	require_sparse_file_support
134	atf_check truncate -s 1k A
135	atf_check truncate -s 1m B
136	atf_check truncate -s 1g C
137	atf_check truncate -s 1t D
138	atf_check -o inline:'1.0K\tA\n1.0M\tB\n1.0G\tC\n1.0T\tD\n' du -Ah A B C D
139}
140
141atf_test_case k_flag
142k_flag_head()
143{
144	atf_set "descr" "Verify -k output"
145}
146k_flag_body()
147{
148	atf_check truncate -s 1k A
149	atf_check truncate -s 1m B
150	atf_check -o inline:'1\tA\n1024\tB\n' du -Ak A B
151}
152
153atf_test_case m_flag
154m_flag_head()
155{
156	atf_set "descr" "Verify -m output"
157}
158m_flag_body()
159{
160	atf_check truncate -s 1k A
161	atf_check truncate -s 1m B
162	atf_check truncate -s 1g C
163	atf_check -o inline:'1\tA\n1\tB\n1024\tC\n' du -Am A B C
164}
165
166atf_test_case si_flag
167si_flag_head()
168{
169	atf_set "descr" "Verify --si output"
170}
171si_flag_body()
172{
173	atf_check truncate -s 1500000 A
174	atf_check truncate -s 1572864 B
175
176	atf_check -o inline:'1.4M\tA\n1.5M\tB\n' du -Ah A B
177	atf_check -o inline:'1.5M\tA\n1.6M\tB\n' du -A --si A B
178}
179
180atf_init_test_cases()
181{
182	atf_add_test_case A_flag
183	atf_add_test_case H_flag
184	atf_add_test_case I_flag
185	atf_add_test_case g_flag
186	atf_add_test_case h_flag
187	atf_add_test_case k_flag
188	atf_add_test_case m_flag
189	atf_add_test_case si_flag
190}
191