xref: /freebsd/usr.bin/truncate/tests/truncate_test.sh (revision fc96358cdd560beb65578d169c1aefd39d0c54a2)
1*fc96358cSJilles Tjoelker#
2*fc96358cSJilles Tjoelker# Copyright 2014, Google Inc. All rights reserved.
3*fc96358cSJilles Tjoelker#
4*fc96358cSJilles Tjoelker# Redistribution and use in source and binary forms, with or without
5*fc96358cSJilles Tjoelker# modification, are permitted provided that the following conditions are
6*fc96358cSJilles Tjoelker# met:
7*fc96358cSJilles Tjoelker#
8*fc96358cSJilles Tjoelker# * Redistributions of source code must retain the above copyright
9*fc96358cSJilles Tjoelker#   notice, this list of conditions and the following disclaimer.
10*fc96358cSJilles Tjoelker# * Redistributions in binary form must reproduce the above copyright
11*fc96358cSJilles Tjoelker#   notice, this list of conditions and the following disclaimer in the
12*fc96358cSJilles Tjoelker#   documentation and/or other materials provided with the distribution.
13*fc96358cSJilles Tjoelker# * Neither the name of Google Inc. nor the names of its
14*fc96358cSJilles Tjoelker#   contributors may be used to endorse or promote products derived from
15*fc96358cSJilles Tjoelker#   this software without specific written permission.
16*fc96358cSJilles Tjoelker#
17*fc96358cSJilles Tjoelker# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*fc96358cSJilles Tjoelker# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*fc96358cSJilles Tjoelker# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*fc96358cSJilles Tjoelker# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*fc96358cSJilles Tjoelker# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*fc96358cSJilles Tjoelker# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*fc96358cSJilles Tjoelker# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*fc96358cSJilles Tjoelker# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*fc96358cSJilles Tjoelker# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*fc96358cSJilles Tjoelker# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*fc96358cSJilles Tjoelker# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*fc96358cSJilles Tjoelker#
29*fc96358cSJilles Tjoelker# $FreeBSD$
30*fc96358cSJilles Tjoelker#
31*fc96358cSJilles Tjoelker
32*fc96358cSJilles Tjoelker# Helper function that is always used to create and fill stderr.txt for these
33*fc96358cSJilles Tjoelker# tests.
34*fc96358cSJilles Tjoelker_custom_create_file()
35*fc96358cSJilles Tjoelker{
36*fc96358cSJilles Tjoelker	# The first argument is a command.
37*fc96358cSJilles Tjoelker	# The second is just a string.
38*fc96358cSJilles Tjoelker	case "${1}" in
39*fc96358cSJilles Tjoelker		creat) > stderr.txt ;;
40*fc96358cSJilles Tjoelker		print) [ "${2}" ] && \
41*fc96358cSJilles Tjoelker		    printf "%s\n" "${2}" >> stderr.txt ;;
42*fc96358cSJilles Tjoelker	esac
43*fc96358cSJilles Tjoelker}
44*fc96358cSJilles Tjoelker
45*fc96358cSJilles Tjoelker# Helper function that create the file stderr.txt that contains the string
46*fc96358cSJilles Tjoelker# passed in as the first argument.
47*fc96358cSJilles Tjoelkercreate_stderr_file()
48*fc96358cSJilles Tjoelker{
49*fc96358cSJilles Tjoelker	_custom_create_file creat
50*fc96358cSJilles Tjoelker	_custom_create_file print "${1}"
51*fc96358cSJilles Tjoelker}
52*fc96358cSJilles Tjoelker
53*fc96358cSJilles Tjoelker# Helper function that create the file stderr.txt that contains the expected
54*fc96358cSJilles Tjoelker# truncate utility usage message.
55*fc96358cSJilles Tjoelkercreate_stderr_usage_file()
56*fc96358cSJilles Tjoelker{
57*fc96358cSJilles Tjoelker	_custom_create_file creat
58*fc96358cSJilles Tjoelker	_custom_create_file print "${1}"
59*fc96358cSJilles Tjoelker	_custom_create_file print \
60*fc96358cSJilles Tjoelker	    "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g|T|t] file ..."
61*fc96358cSJilles Tjoelker	_custom_create_file print "       truncate [-c] -r rfile file ..."
62*fc96358cSJilles Tjoelker}
63*fc96358cSJilles Tjoelker
64*fc96358cSJilles Tjoelkeratf_test_case illegal_option
65*fc96358cSJilles Tjoelkerillegal_option_head()
66*fc96358cSJilles Tjoelker{
67*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate exits >0 when passed an" \
68*fc96358cSJilles Tjoelker	    "invalid command line option"
69*fc96358cSJilles Tjoelker}
70*fc96358cSJilles Tjoelkerillegal_option_body()
71*fc96358cSJilles Tjoelker{
72*fc96358cSJilles Tjoelker	create_stderr_usage_file 'truncate: illegal option -- 7'
73*fc96358cSJilles Tjoelker
74*fc96358cSJilles Tjoelker	# We expect the error message, with no new files.
75*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt truncate -7 -s0 output.txt
76*fc96358cSJilles Tjoelker	[ ! -e output.txt ] || atf_fail "output.txt should not exist"
77*fc96358cSJilles Tjoelker}
78*fc96358cSJilles Tjoelker
79*fc96358cSJilles Tjoelkeratf_test_case illegal_size
80*fc96358cSJilles Tjoelkerillegal_size_head()
81*fc96358cSJilles Tjoelker{
82*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate exits >0 when passed an" \
83*fc96358cSJilles Tjoelker	    "invalid power of two convention"
84*fc96358cSJilles Tjoelker}
85*fc96358cSJilles Tjoelkerillegal_size_body()
86*fc96358cSJilles Tjoelker{
87*fc96358cSJilles Tjoelker	create_stderr_file "truncate: invalid size argument \`+1L'"
88*fc96358cSJilles Tjoelker
89*fc96358cSJilles Tjoelker	# We expect the error message, with no new files.
90*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt truncate -s+1L output.txt
91*fc96358cSJilles Tjoelker	[ ! -e output.txt ] || atf_fail "output.txt should not exist"
92*fc96358cSJilles Tjoelker}
93*fc96358cSJilles Tjoelker
94*fc96358cSJilles Tjoelkeratf_test_case too_large_size
95*fc96358cSJilles Tjoelkertoo_large_size_head()
96*fc96358cSJilles Tjoelker{
97*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate exits >0 when passed an" \
98*fc96358cSJilles Tjoelker	    "a size that is INT64_MAX < size <= UINT64_MAX"
99*fc96358cSJilles Tjoelker}
100*fc96358cSJilles Tjoelkertoo_large_size_body()
101*fc96358cSJilles Tjoelker{
102*fc96358cSJilles Tjoelker	create_stderr_file "truncate: invalid size argument \`8388608t'"
103*fc96358cSJilles Tjoelker
104*fc96358cSJilles Tjoelker	# We expect the error message, with no new files.
105*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt \
106*fc96358cSJilles Tjoelker	    truncate -s8388608t output.txt
107*fc96358cSJilles Tjoelker	[ ! -e output.txt ] || atf_fail "output.txt should not exist"
108*fc96358cSJilles Tjoelker}
109*fc96358cSJilles Tjoelker
110*fc96358cSJilles Tjoelkeratf_test_case opt_c
111*fc96358cSJilles Tjoelkeropt_c_head()
112*fc96358cSJilles Tjoelker{
113*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that -c prevents creation of new files"
114*fc96358cSJilles Tjoelker}
115*fc96358cSJilles Tjoelkeropt_c_body()
116*fc96358cSJilles Tjoelker{
117*fc96358cSJilles Tjoelker	# No new files and truncate returns 0 as if this is a success.
118*fc96358cSJilles Tjoelker	atf_check truncate -c -s 0 doesnotexist.txt
119*fc96358cSJilles Tjoelker	[ ! -e output.txt ] || atf_fail "doesnotexist.txt should not exist"
120*fc96358cSJilles Tjoelker	> reference
121*fc96358cSJilles Tjoelker	atf_check truncate -c -r reference doesnotexist.txt
122*fc96358cSJilles Tjoelker	[ ! -e output.txt ] || atf_fail "doesnotexist.txt should not exist"
123*fc96358cSJilles Tjoelker
124*fc96358cSJilles Tjoelker	create_stderr_file
125*fc96358cSJilles Tjoelker
126*fc96358cSJilles Tjoelker	# The existing file will be altered by truncate.
127*fc96358cSJilles Tjoelker	> exists.txt
128*fc96358cSJilles Tjoelker	atf_check -e file:stderr.txt truncate -c -s1 exists.txt
129*fc96358cSJilles Tjoelker	[ -s exists.txt ] || atf_fail "exists.txt be larger than zero bytes"
130*fc96358cSJilles Tjoelker}
131*fc96358cSJilles Tjoelker
132*fc96358cSJilles Tjoelkeratf_test_case opt_rs
133*fc96358cSJilles Tjoelkeropt_rs_head()
134*fc96358cSJilles Tjoelker{
135*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate command line flags" \
136*fc96358cSJilles Tjoelker	    "-s and -r cannot be specifed together"
137*fc96358cSJilles Tjoelker}
138*fc96358cSJilles Tjoelkeropt_rs_body()
139*fc96358cSJilles Tjoelker{
140*fc96358cSJilles Tjoelker	create_stderr_usage_file
141*fc96358cSJilles Tjoelker
142*fc96358cSJilles Tjoelker	# Force an error due to the use of both -s and -r.
143*fc96358cSJilles Tjoelker	> afile
144*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt truncate -s0 -r afile afile
145*fc96358cSJilles Tjoelker}
146*fc96358cSJilles Tjoelker
147*fc96358cSJilles Tjoelkeratf_test_case no_files
148*fc96358cSJilles Tjoelkerno_files_head()
149*fc96358cSJilles Tjoelker{
150*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate needs a list of files on" \
151*fc96358cSJilles Tjoelker	    "the command line"
152*fc96358cSJilles Tjoelker}
153*fc96358cSJilles Tjoelkerno_files_body()
154*fc96358cSJilles Tjoelker{
155*fc96358cSJilles Tjoelker	create_stderr_usage_file
156*fc96358cSJilles Tjoelker
157*fc96358cSJilles Tjoelker	# A list of files must be present on the command line.
158*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt truncate -s1
159*fc96358cSJilles Tjoelker}
160*fc96358cSJilles Tjoelker
161*fc96358cSJilles Tjoelkeratf_test_case bad_refer
162*fc96358cSJilles Tjoelkerbad_refer_head()
163*fc96358cSJilles Tjoelker{
164*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate detects a non-existent" \
165*fc96358cSJilles Tjoelker	    "reference file"
166*fc96358cSJilles Tjoelker}
167*fc96358cSJilles Tjoelkerbad_refer_body()
168*fc96358cSJilles Tjoelker{
169*fc96358cSJilles Tjoelker	create_stderr_file "truncate: afile: No such file or directory"
170*fc96358cSJilles Tjoelker
171*fc96358cSJilles Tjoelker	# The reference file must exist before you try to use it.
172*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt truncate -r afile afile
173*fc96358cSJilles Tjoelker	[ ! -e afile ] || atf_fail "afile should not exist"
174*fc96358cSJilles Tjoelker}
175*fc96358cSJilles Tjoelker
176*fc96358cSJilles Tjoelkeratf_test_case bad_truncate cleanup
177*fc96358cSJilles Tjoelkerbad_truncate_head()
178*fc96358cSJilles Tjoelker{
179*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate reports an error during" \
180*fc96358cSJilles Tjoelker	    "truncation"
181*fc96358cSJilles Tjoelker}
182*fc96358cSJilles Tjoelkerbad_truncate_body()
183*fc96358cSJilles Tjoelker{
184*fc96358cSJilles Tjoelker	create_stderr_file "truncate: exists.txt: Operation not permitted"
185*fc96358cSJilles Tjoelker
186*fc96358cSJilles Tjoelker	# Trying to get the ftruncate() call to return -1.
187*fc96358cSJilles Tjoelker	> exists.txt
188*fc96358cSJilles Tjoelker	atf_check chflags uimmutable exists.txt
189*fc96358cSJilles Tjoelker
190*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt truncate -s1 exists.txt
191*fc96358cSJilles Tjoelker}
192*fc96358cSJilles Tjoelkerbad_truncate_cleanup()
193*fc96358cSJilles Tjoelker{
194*fc96358cSJilles Tjoelker	chflags 0 exists.txt
195*fc96358cSJilles Tjoelker}
196*fc96358cSJilles Tjoelker
197*fc96358cSJilles Tjoelkeratf_test_case new_absolute_grow
198*fc96358cSJilles Tjoelkernew_absolute_grow_head()
199*fc96358cSJilles Tjoelker{
200*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies truncate can make and grow a new 1m file"
201*fc96358cSJilles Tjoelker}
202*fc96358cSJilles Tjoelkernew_absolute_grow_body()
203*fc96358cSJilles Tjoelker{
204*fc96358cSJilles Tjoelker	create_stderr_file
205*fc96358cSJilles Tjoelker
206*fc96358cSJilles Tjoelker	# Create a new file and grow it to 1024 bytes.
207*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s1k output.txt
208*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
209*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
210*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
211*fc96358cSJilles Tjoelker
212*fc96358cSJilles Tjoelker	create_stderr_file
213*fc96358cSJilles Tjoelker
214*fc96358cSJilles Tjoelker	# Grow the existing file to 1M.  We are using absolute sizes.
215*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -c -s1M output.txt
216*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
217*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
218*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
219*fc96358cSJilles Tjoelker}
220*fc96358cSJilles Tjoelker
221*fc96358cSJilles Tjoelkeratf_test_case new_absolute_shrink
222*fc96358cSJilles Tjoelkernew_absolute_shrink_head()
223*fc96358cSJilles Tjoelker{
224*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate can make and" \
225*fc96358cSJilles Tjoelker	    "shrink a new 1m file"
226*fc96358cSJilles Tjoelker}
227*fc96358cSJilles Tjoelkernew_absolute_shrink_body()
228*fc96358cSJilles Tjoelker{
229*fc96358cSJilles Tjoelker	create_stderr_file
230*fc96358cSJilles Tjoelker
231*fc96358cSJilles Tjoelker	# Create a new file and grow it to 1048576 bytes.
232*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s1M output.txt
233*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
234*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
235*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
236*fc96358cSJilles Tjoelker
237*fc96358cSJilles Tjoelker	create_stderr_file
238*fc96358cSJilles Tjoelker
239*fc96358cSJilles Tjoelker	# Shrink the existing file to 1k.  We are using absolute sizes.
240*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s1k output.txt
241*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
242*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
243*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
244*fc96358cSJilles Tjoelker}
245*fc96358cSJilles Tjoelker
246*fc96358cSJilles Tjoelkeratf_test_case new_relative_grow
247*fc96358cSJilles Tjoelkernew_relative_grow_head()
248*fc96358cSJilles Tjoelker{
249*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies truncate can make and grow a new 1m file" \
250*fc96358cSJilles Tjoelker	    "using relative sizes"
251*fc96358cSJilles Tjoelker}
252*fc96358cSJilles Tjoelkernew_relative_grow_body()
253*fc96358cSJilles Tjoelker{
254*fc96358cSJilles Tjoelker	create_stderr_file
255*fc96358cSJilles Tjoelker
256*fc96358cSJilles Tjoelker	# Create a new file and grow it to 1024 bytes.
257*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s+1k output.txt
258*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
259*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
260*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
261*fc96358cSJilles Tjoelker
262*fc96358cSJilles Tjoelker	create_stderr_file
263*fc96358cSJilles Tjoelker
264*fc96358cSJilles Tjoelker	# Grow the existing file to 1M.  We are using relative sizes.
265*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s+1047552 output.txt
266*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
267*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
268*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
269*fc96358cSJilles Tjoelker}
270*fc96358cSJilles Tjoelker
271*fc96358cSJilles Tjoelkeratf_test_case new_relative_shrink
272*fc96358cSJilles Tjoelkernew_relative_shrink_head()
273*fc96358cSJilles Tjoelker{
274*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies truncate can make and shrink a new 1m file" \
275*fc96358cSJilles Tjoelker	    "using relative sizes"
276*fc96358cSJilles Tjoelker}
277*fc96358cSJilles Tjoelkernew_relative_shrink_body()
278*fc96358cSJilles Tjoelker{
279*fc96358cSJilles Tjoelker	create_stderr_file
280*fc96358cSJilles Tjoelker
281*fc96358cSJilles Tjoelker	# Create a new file and grow it to 1049600 bytes.
282*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s+1049600 output.txt
283*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
284*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
285*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1049600 ] || atf_fail "expected file size of 1m"
286*fc96358cSJilles Tjoelker
287*fc96358cSJilles Tjoelker	create_stderr_file
288*fc96358cSJilles Tjoelker
289*fc96358cSJilles Tjoelker	# Shrink the existing file to 1k.  We are using relative sizes.
290*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s-1M output.txt
291*fc96358cSJilles Tjoelker	atf_check -s exit:1 cmp -s output.txt /dev/zero
292*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
293*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
294*fc96358cSJilles Tjoelker}
295*fc96358cSJilles Tjoelker
296*fc96358cSJilles Tjoelkeratf_test_case cannot_open
297*fc96358cSJilles Tjoelkercannot_open_head()
298*fc96358cSJilles Tjoelker{
299*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies truncate handles open failures correctly" \
300*fc96358cSJilles Tjoelker	    "in a list of files"
301*fc96358cSJilles Tjoelker	atf_set "require.user" "unprivileged"
302*fc96358cSJilles Tjoelker}
303*fc96358cSJilles Tjoelkercannot_open_body()
304*fc96358cSJilles Tjoelker{
305*fc96358cSJilles Tjoelker	# Create three files -- the middle file cannot allow writes.
306*fc96358cSJilles Tjoelker	> before
307*fc96358cSJilles Tjoelker	> 0000
308*fc96358cSJilles Tjoelker	> after
309*fc96358cSJilles Tjoelker	atf_check chmod 0000 0000
310*fc96358cSJilles Tjoelker
311*fc96358cSJilles Tjoelker	create_stderr_file "truncate: 0000: Permission denied"
312*fc96358cSJilles Tjoelker
313*fc96358cSJilles Tjoelker	# Create a new file and grow it to 1024 bytes.
314*fc96358cSJilles Tjoelker	atf_check -s not-exit:0 -e file:stderr.txt \
315*fc96358cSJilles Tjoelker	truncate -c -s1k before 0000 after
316*fc96358cSJilles Tjoelker	eval $(stat -s before)
317*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
318*fc96358cSJilles Tjoelker	eval $(stat -s after)
319*fc96358cSJilles Tjoelker	[ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
320*fc96358cSJilles Tjoelker	eval $(stat -s 0000)
321*fc96358cSJilles Tjoelker	[ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
322*fc96358cSJilles Tjoelker}
323*fc96358cSJilles Tjoelker
324*fc96358cSJilles Tjoelkeratf_test_case reference
325*fc96358cSJilles Tjoelkerreference_head()
326*fc96358cSJilles Tjoelker{
327*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies that truncate can use a reference file"
328*fc96358cSJilles Tjoelker}
329*fc96358cSJilles Tjoelkerreference_body()
330*fc96358cSJilles Tjoelker{
331*fc96358cSJilles Tjoelker	# Create a 4 byte reference file.
332*fc96358cSJilles Tjoelker	printf "123\n" > reference
333*fc96358cSJilles Tjoelker	eval $(stat -s reference)
334*fc96358cSJilles Tjoelker	[ ${st_size} -eq 4 ] || atf_fail "reference file should be 4 bytes"
335*fc96358cSJilles Tjoelker
336*fc96358cSJilles Tjoelker	create_stderr_file
337*fc96358cSJilles Tjoelker
338*fc96358cSJilles Tjoelker	# Create a new file and grow it to 4 bytes.
339*fc96358cSJilles Tjoelker	atf_check -e file:stderr.txt truncate -r reference afile
340*fc96358cSJilles Tjoelker	eval $(stat -s afile)
341*fc96358cSJilles Tjoelker	[ ${st_size} -eq 4 ] || atf_fail "new file should also be 4 bytes"
342*fc96358cSJilles Tjoelker}
343*fc96358cSJilles Tjoelker
344*fc96358cSJilles Tjoelkeratf_test_case new_zero
345*fc96358cSJilles Tjoelkernew_zero_head()
346*fc96358cSJilles Tjoelker{
347*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies truncate can make and grow zero byte file"
348*fc96358cSJilles Tjoelker}
349*fc96358cSJilles Tjoelkernew_zero_body()
350*fc96358cSJilles Tjoelker{
351*fc96358cSJilles Tjoelker	create_stderr_file
352*fc96358cSJilles Tjoelker
353*fc96358cSJilles Tjoelker	# Create a new file and grow it to zero bytes.
354*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s0 output.txt
355*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
356*fc96358cSJilles Tjoelker	[ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
357*fc96358cSJilles Tjoelker
358*fc96358cSJilles Tjoelker	# Pretend to grow the file.
359*fc96358cSJilles Tjoelker	atf_check -s exit:0 -e file:stderr.txt truncate -s+0 output.txt
360*fc96358cSJilles Tjoelker	eval $(stat -s output.txt)
361*fc96358cSJilles Tjoelker	[ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
362*fc96358cSJilles Tjoelker}
363*fc96358cSJilles Tjoelker
364*fc96358cSJilles Tjoelkeratf_test_case negative
365*fc96358cSJilles Tjoelkernegative_head()
366*fc96358cSJilles Tjoelker{
367*fc96358cSJilles Tjoelker	atf_set "descr" "Verifies truncate treats negative sizes as zero"
368*fc96358cSJilles Tjoelker}
369*fc96358cSJilles Tjoelkernegative_body()
370*fc96358cSJilles Tjoelker{
371*fc96358cSJilles Tjoelker	# Create a 5 byte file.
372*fc96358cSJilles Tjoelker	printf "abcd\n" > afile
373*fc96358cSJilles Tjoelker	eval $(stat -s afile)
374*fc96358cSJilles Tjoelker	[ ${st_size} -eq 5 ] || atf_fail "afile file should be 5 bytes"
375*fc96358cSJilles Tjoelker
376*fc96358cSJilles Tjoelker	create_stderr_file
377*fc96358cSJilles Tjoelker
378*fc96358cSJilles Tjoelker	# Create a new file and do a 100 byte negative relative shrink.
379*fc96358cSJilles Tjoelker	atf_check -e file:stderr.txt truncate -s-100 afile
380*fc96358cSJilles Tjoelker	eval $(stat -s afile)
381*fc96358cSJilles Tjoelker	[ ${st_size} -eq 0 ] || atf_fail "new file should now be zero bytes"
382*fc96358cSJilles Tjoelker}
383*fc96358cSJilles Tjoelker
384*fc96358cSJilles Tjoelkeratf_init_test_cases()
385*fc96358cSJilles Tjoelker{
386*fc96358cSJilles Tjoelker	atf_add_test_case illegal_option
387*fc96358cSJilles Tjoelker	atf_add_test_case illegal_size
388*fc96358cSJilles Tjoelker	atf_add_test_case too_large_size
389*fc96358cSJilles Tjoelker	atf_add_test_case opt_c
390*fc96358cSJilles Tjoelker	atf_add_test_case opt_rs
391*fc96358cSJilles Tjoelker	atf_add_test_case no_files
392*fc96358cSJilles Tjoelker	atf_add_test_case bad_refer
393*fc96358cSJilles Tjoelker	atf_add_test_case bad_truncate
394*fc96358cSJilles Tjoelker	atf_add_test_case cannot_open
395*fc96358cSJilles Tjoelker	atf_add_test_case new_absolute_grow
396*fc96358cSJilles Tjoelker	atf_add_test_case new_absolute_shrink
397*fc96358cSJilles Tjoelker	atf_add_test_case new_relative_grow
398*fc96358cSJilles Tjoelker	atf_add_test_case new_relative_shrink
399*fc96358cSJilles Tjoelker	atf_add_test_case reference
400*fc96358cSJilles Tjoelker	atf_add_test_case new_zero
401*fc96358cSJilles Tjoelker	atf_add_test_case negative
402*fc96358cSJilles Tjoelker}
403