xref: /illumos-gate/usr/src/test/util-tests/tests/du/dutest.ksh (revision 43c208b5f450eb6cd6064cb062ac44d41549e785)
1#! /usr/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2025 Edgecast Cloud LLC
15#
16
17. "$(dirname $0)/du.kshlib"
18
19du="/bin/du"
20
21function fail {
22	echo "FAIL $@"
23	((fail++))
24}
25
26function pass {
27	echo "PASS $@"
28}
29
30function A_flag
31{
32	typeset size
33	typeset fn="$0"
34	typeset errors=$fail
35
36	require_sparse_file_support
37
38	truncate -s 10g sparse.file
39
40	set -- $($du -s sparse.file)
41	size=$1
42
43	if [[ "$size" != "1" ]]; then
44		fail "$fn unexpected size: \"$size\""
45	fi
46
47	set -- $($du -Ah sparse.file)
48	size="$1"
49
50	if [[ "$size" != "10G" ]]; then
51		fail "$fn unexpected size \"$size\""
52	fi
53	rm sparse.file
54	(( errors == fail )) && pass $fn
55}
56
57function H_flag
58{
59	typeset paths1='testdir/A/B testdir/A testdir/C testdir'
60        typeset paths2='testdir/C/B testdir/C'
61	typeset lineprefix=$'^[0-9]+\t'
62	typeset sep="\$\n${lineprefix}"
63	typeset fn="$0"
64	typeset errors=$fail
65
66	mkdir testdir
67	(cd testdir && mkdir A && touch A/B && ln -s A C)
68	$du -aAH testdir > du.out
69	egrep "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")\$" \
70	    du.out > egrep.out
71	if ! cmp -s du.out egrep.out
72	then
73		fail "$fn unexpected output"
74	fi
75	# Check that the output doesn't contain any lines (i.e. paths) that we
76	# did not expect it to contain from $paths1.
77	egrep -v "${lineprefix}$(echo $paths1 | sed -e "s/ /$sep/g")\$" \
78	    du.out > egrep.out
79	if [[ -s egrep.out ]]
80	then
81		fail "$fn unexpected output"
82	fi
83	$du -aAH testdir/C > du.out
84
85	egrep "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")\$" \
86	    du.out > egrep.out
87	if ! cmp -s du.out egrep.out
88	then
89		fail "$fn unexpected output"
90	fi
91	# Check that the output doesn't contain any lines (i.e. paths) that we
92	# did not expect it to contain from $paths2.
93	egrep -v "${lineprefix}$(echo $paths2 | sed -e "s/ /$sep/g")\$" \
94	    du.out > egrep.out
95	if [[ -s egrep.out ]]
96	then
97		fail "$fn unexpected output"
98	fi
99	rm -rf testdir du.out egrep.out
100	(( errors == fail )) && pass $fn
101}
102
103function L_flag
104{
105	typeset fn="$0"
106	typeset errors=$fail
107
108	mkdir testdir
109	truncate -s 8192 testdir/A
110	ln -s A testdir/B
111
112	if [[ $($du -A testdir) != $(printf "17\ttestdir\n") ]]; then
113		fail "$fn unexpected size"
114	fi
115	if [[ $($du -AL testdir) != $(printf "17\ttestdir\n") ]]; then
116		fail "$fn unexpected size"
117	fi
118	rm -rf testdir
119	(( errors == fail )) && pass $fn
120}
121
122function a_flag
123{
124	typeset fn="$0"
125	typeset errors=$fail
126
127	mkdir testdir
128	truncate -s 0 testdir/A
129
130	if [[ $($du -A testdir) != $(printf "1\ttestdir\n") ]]; then
131		fail "$fn unexpected size"
132	fi
133	if [[ $($du -Aa testdir) != $(printf "0\ttestdir/A\n1\ttestdir\n") ]]
134	then
135		fail "$fn unexpected size"
136	fi
137
138	rm -rf testdir
139	(( errors == fail )) && pass $fn
140}
141
142function h_flag
143{
144	typeset fn="$0"
145	typeset errors=$fail
146
147	require_sparse_file_support
148
149	truncate -s 1k A
150	truncate -s 1m B
151	truncate -s 1g C
152	truncate -s 1t D
153
154	if [[ $($du -Ah A B C D) != $(printf "1K\tA\n1M\tB\n1G\tC\n1T\tD\n") ]]
155	then
156		fail "$fn unexpected size"
157	fi
158
159	rm A B C D
160	(( errors == fail )) && pass $fn
161}
162
163function k_flag
164{
165	typeset fn="$0"
166	typeset errors=$fail
167
168	require_sparse_file_support
169
170	truncate -s 1k A
171	truncate -s 1m B
172
173	if [[ $($du -Ak A B) != $(printf "1\tA\n1024\tB\n") ]]; then
174		fail "$fn unexpected size"
175	fi
176
177	rm A B
178	(( errors == fail )) && pass $fn
179}
180
181function m_flag
182{
183	typeset fn="$0"
184	typeset errors=$fail
185
186	require_sparse_file_support
187
188	truncate -s 1k A
189	truncate -s 1m B
190	truncate -s 1g C
191
192	if [[ $($du -Am A B C) != $(printf "1\tA\n1\tB\n1024\tC\n") ]]; then
193		fail "$fn unexpected size"
194	fi
195
196	rm A B C
197	(( errors == fail )) && pass $fn
198}
199
200function s_flag
201{
202	typeset fn="$0"
203	typeset errors=$fail
204
205	require_sparse_file_support
206
207	mkdir -p testdir/testdir1
208	truncate -s 0 testdir/testdir1/A testdir/testdir1/B
209
210	if [[ $($du -As testdir) != $(printf "1\ttestdir\n") ]]; then
211		fail "$fn unexpected size"
212	fi
213	if [[ $($du -As testdir/A) != $(printf "0\ttestdir/A\n") ]]; then
214		fail "$fn unexpected size"
215	fi
216	if [[ $($du -As testdir/testdir1) != \
217	    $(printf "1\ttestdir/testdir1\n") ]]; then
218		fail "$fn unexpected size"
219	fi
220	if [[ $($du -As testdir/testdir1/B) != \
221	    $(printf "0\ttestdir/testdir1/B\n") ]]; then
222		fail "$fn unexpected size"
223	fi
224
225	rm -rf testdir
226	(( errors == fail )) && pass $fn
227}
228
229mkdir -p $test_dir
230cd $test_dir
231
232A_flag
233H_flag
234L_flag
235a_flag
236h_flag
237k_flag
238m_flag
239s_flag
240
241cd -
242rm -rf $test_dir
243
244exit $fail
245