]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.bin/du/tests/du_test.sh
MFC r319850:
[FreeBSD/stable/10.git] / usr.bin / du / tests / du_test.sh
1 #
2 # Copyright (c) 2017 Ngie Cooper <ngie@FreeBSD.org>
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
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27
28 atf_test_case A_flag
29 A_flag_head()
30 {
31         atf_set "descr" "Verify -A behavior"
32 }
33 A_flag_body()
34 {
35         # XXX: compressed volumes?
36         atf_check truncate -s 10g sparse.file
37         atf_check -o inline:'1\tsparse.file\n' du -g sparse.file
38         atf_check -o inline:'10\tsparse.file\n' du -A -g sparse.file
39 }
40
41 atf_test_case H_flag
42 H_flag_head()
43 {
44         atf_set "descr" "Verify -H behavior"
45 }
46 H_flag_body()
47 {
48         local paths1='testdir/A/B testdir/A testdir/C testdir'
49         local paths2='testdir/A/B testdir/A testdir/C testdir'
50         local sep='\n[0-9]+\t'
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 "[0-9]+\t$(echo $paths1 | tr ' ' "$sep")\n" du.out
57         atf_check -o save:du_C.out du -aAH testdir/C
58         atf_check egrep -q "[0-9]+\t$(echo $paths2 | tr ' ' "$sep")\n" du_C.out
59 }
60
61 atf_test_case I_flag
62 I_flag_head()
63 {
64         atf_set "descr" "Verify -I behavior"
65 }
66 I_flag_body()
67 {
68         paths_sans_foo_named="a/motley/fool/of/sorts fool/parts/with/their/cache bar baz"
69         paths_foo_named="foo foobar"
70         paths="$paths_sans_foo_named $paths_foo_named"
71
72         # cd'ing to testdir helps ensure that files from atf/kyua don't
73         # pollute the results.
74         atf_check -x "mkdir testdir && cd testdir && mkdir -p $paths"
75         atf_check -o save:du.out -x "cd testdir && du -s $paths_sans_foo_named"
76         atf_check -o save:du_I.out -x "cd testdir && du -I '*foo*' -s $paths"
77
78         atf_check diff -u du.out du_I.out
79 }
80
81 atf_test_case c_flag
82 c_flag_head()
83 {
84         atf_set "descr" "Verify -c output"
85 }
86 c_flag_body()
87 {
88         atf_check truncate -s 0 foo bar
89 }
90
91 atf_test_case g_flag
92 g_flag_head()
93 {
94         atf_set "descr" "Verify -g output"
95 }
96 g_flag_body()
97 {
98         atf_check truncate -s 1k A
99         atf_check truncate -s 1m B
100         atf_check truncate -s 1g C
101         atf_check truncate -s 1t D
102         atf_check -o inline:'1\tA\n1\tB\n1\tC\n1024\tD\n' du -Ag A B C D
103 }
104
105 atf_test_case h_flag
106 h_flag_head()
107 {
108         atf_set "descr" "Verify -h output"
109 }
110 h_flag_body()
111 {
112         atf_check truncate -s 1k A
113         atf_check truncate -s 1m B
114         atf_check truncate -s 1g C
115         atf_check truncate -s 1t D
116         atf_check -o inline:'1.0K\tA\n1.0M\tB\n1.0G\tC\n1.0T\tD\n' du -Ah A B C D
117 }
118
119 atf_test_case k_flag
120 k_flag_head()
121 {
122         atf_set "descr" "Verify -k output"
123 }
124 k_flag_body()
125 {
126         atf_check truncate -s 1k A
127         atf_check truncate -s 1m B
128         atf_check -o inline:'1\tA\n1024\tB\n' du -Ak A B
129 }
130
131 atf_test_case m_flag
132 m_flag_head()
133 {
134         atf_set "descr" "Verify -m output"
135 }
136 m_flag_body()
137 {
138         atf_check truncate -s 1k A
139         atf_check truncate -s 1m B
140         atf_check truncate -s 1g C
141         atf_check -o inline:'1\tA\n1\tB\n1024\tC\n' du -Am A B C
142 }
143
144 atf_init_test_cases()
145 {
146         atf_add_test_case A_flag
147         atf_add_test_case H_flag
148         atf_add_test_case I_flag
149         atf_add_test_case g_flag
150         atf_add_test_case h_flag
151         atf_add_test_case k_flag
152         atf_add_test_case m_flag
153 }