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