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