2 # Copyright 2014, Google Inc. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * 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 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 # Helper function that is always used to create and fill stderr.txt for these
36 # The first argument is a command.
37 # The second is just a string.
39 creat) > stderr.txt ;;
40 print) [ "${2}" ] && \
41 printf "%s\n" "${2}" >> stderr.txt ;;
45 # Helper function that create the file stderr.txt that contains the string
46 # passed in as the first argument.
49 _custom_create_file creat
50 _custom_create_file print "${1}"
53 # Helper function that create the file stderr.txt that contains the expected
54 # truncate utility usage message.
55 create_stderr_usage_file()
57 _custom_create_file creat
58 _custom_create_file print "${1}"
59 _custom_create_file print \
60 "usage: truncate [-c] -s [+|-]size[K|k|M|m|G|g|T|t] file ..."
61 _custom_create_file print " truncate [-c] -r rfile file ..."
64 atf_test_case illegal_option
67 atf_set "descr" "Verifies that truncate exits >0 when passed an" \
68 "invalid command line option"
72 create_stderr_usage_file 'truncate: illegal option -- 7'
74 # We expect the error message, with no new files.
75 atf_check -s not-exit:0 -e file:stderr.txt truncate -7 -s0 output.txt
76 [ ! -e output.txt ] || atf_fail "output.txt should not exist"
79 atf_test_case illegal_size
82 atf_set "descr" "Verifies that truncate exits >0 when passed an" \
83 "invalid power of two convention"
87 create_stderr_file "truncate: invalid size argument \`+1L'"
89 # We expect the error message, with no new files.
90 atf_check -s not-exit:0 -e file:stderr.txt truncate -s+1L output.txt
91 [ ! -e output.txt ] || atf_fail "output.txt should not exist"
94 atf_test_case too_large_size
97 atf_set "descr" "Verifies that truncate exits >0 when passed an" \
98 "a size that is INT64_MAX < size <= UINT64_MAX"
100 too_large_size_body()
102 create_stderr_file "truncate: invalid size argument \`8388608t'"
104 # We expect the error message, with no new files.
105 atf_check -s not-exit:0 -e file:stderr.txt \
106 truncate -s8388608t output.txt
107 [ ! -e output.txt ] || atf_fail "output.txt should not exist"
113 atf_set "descr" "Verifies that -c prevents creation of new files"
117 # No new files and truncate returns 0 as if this is a success.
118 atf_check truncate -c -s 0 doesnotexist.txt
119 [ ! -e output.txt ] || atf_fail "doesnotexist.txt should not exist"
121 atf_check truncate -c -r reference doesnotexist.txt
122 [ ! -e output.txt ] || atf_fail "doesnotexist.txt should not exist"
126 # The existing file will be altered by truncate.
128 atf_check -e file:stderr.txt truncate -c -s1 exists.txt
129 [ -s exists.txt ] || atf_fail "exists.txt be larger than zero bytes"
135 atf_set "descr" "Verifies that truncate command line flags" \
136 "-s and -r cannot be specifed together"
140 create_stderr_usage_file
142 # Force an error due to the use of both -s and -r.
144 atf_check -s not-exit:0 -e file:stderr.txt truncate -s0 -r afile afile
147 atf_test_case no_files
150 atf_set "descr" "Verifies that truncate needs a list of files on" \
155 create_stderr_usage_file
157 # A list of files must be present on the command line.
158 atf_check -s not-exit:0 -e file:stderr.txt truncate -s1
161 atf_test_case bad_refer
164 atf_set "descr" "Verifies that truncate detects a non-existent" \
169 create_stderr_file "truncate: afile: No such file or directory"
171 # The reference file must exist before you try to use it.
172 atf_check -s not-exit:0 -e file:stderr.txt truncate -r afile afile
173 [ ! -e afile ] || atf_fail "afile should not exist"
176 atf_test_case bad_truncate cleanup
179 atf_set "descr" "Verifies that truncate reports an error during" \
184 create_stderr_file "truncate: exists.txt: Operation not permitted"
186 # Trying to get the ftruncate() call to return -1.
188 atf_check chflags uimmutable exists.txt
190 atf_check -s not-exit:0 -e file:stderr.txt truncate -s1 exists.txt
192 bad_truncate_cleanup()
197 atf_test_case new_absolute_grow
198 new_absolute_grow_head()
200 atf_set "descr" "Verifies truncate can make and grow a new 1m file"
202 new_absolute_grow_body()
206 # Create a new file and grow it to 1024 bytes.
207 atf_check -s exit:0 -e file:stderr.txt truncate -s1k output.txt
208 atf_check -s exit:1 cmp -s output.txt /dev/zero
209 eval $(stat -s output.txt)
210 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
214 # Grow the existing file to 1M. We are using absolute sizes.
215 atf_check -s exit:0 -e file:stderr.txt truncate -c -s1M output.txt
216 atf_check -s exit:1 cmp -s output.txt /dev/zero
217 eval $(stat -s output.txt)
218 [ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
221 atf_test_case new_absolute_shrink
222 new_absolute_shrink_head()
224 atf_set "descr" "Verifies that truncate can make and" \
225 "shrink a new 1m file"
227 new_absolute_shrink_body()
231 # Create a new file and grow it to 1048576 bytes.
232 atf_check -s exit:0 -e file:stderr.txt truncate -s1M output.txt
233 atf_check -s exit:1 cmp -s output.txt /dev/zero
234 eval $(stat -s output.txt)
235 [ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
239 # Shrink the existing file to 1k. We are using absolute sizes.
240 atf_check -s exit:0 -e file:stderr.txt truncate -s1k output.txt
241 atf_check -s exit:1 cmp -s output.txt /dev/zero
242 eval $(stat -s output.txt)
243 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
246 atf_test_case new_relative_grow
247 new_relative_grow_head()
249 atf_set "descr" "Verifies truncate can make and grow a new 1m file" \
250 "using relative sizes"
252 new_relative_grow_body()
256 # Create a new file and grow it to 1024 bytes.
257 atf_check -s exit:0 -e file:stderr.txt truncate -s+1k output.txt
258 atf_check -s exit:1 cmp -s output.txt /dev/zero
259 eval $(stat -s output.txt)
260 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
264 # Grow the existing file to 1M. We are using relative sizes.
265 atf_check -s exit:0 -e file:stderr.txt truncate -s+1047552 output.txt
266 atf_check -s exit:1 cmp -s output.txt /dev/zero
267 eval $(stat -s output.txt)
268 [ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
271 atf_test_case new_relative_shrink
272 new_relative_shrink_head()
274 atf_set "descr" "Verifies truncate can make and shrink a new 1m file" \
275 "using relative sizes"
277 new_relative_shrink_body()
281 # Create a new file and grow it to 1049600 bytes.
282 atf_check -s exit:0 -e file:stderr.txt truncate -s+1049600 output.txt
283 atf_check -s exit:1 cmp -s output.txt /dev/zero
284 eval $(stat -s output.txt)
285 [ ${st_size} -eq 1049600 ] || atf_fail "expected file size of 1m"
289 # Shrink the existing file to 1k. We are using relative sizes.
290 atf_check -s exit:0 -e file:stderr.txt truncate -s-1M output.txt
291 atf_check -s exit:1 cmp -s output.txt /dev/zero
292 eval $(stat -s output.txt)
293 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
296 atf_test_case cannot_open
299 atf_set "descr" "Verifies truncate handles open failures correctly" \
301 atf_set "require.user" "unprivileged"
305 # Create three files -- the middle file cannot allow writes.
309 atf_check chmod 0000 0000
311 create_stderr_file "truncate: 0000: Permission denied"
313 # Create a new file and grow it to 1024 bytes.
314 atf_check -s not-exit:0 -e file:stderr.txt \
315 truncate -c -s1k before 0000 after
316 eval $(stat -s before)
317 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
318 eval $(stat -s after)
319 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
321 [ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
324 atf_test_case reference
327 atf_set "descr" "Verifies that truncate can use a reference file"
331 # Create a 4 byte reference file.
332 printf "123\n" > reference
333 eval $(stat -s reference)
334 [ ${st_size} -eq 4 ] || atf_fail "reference file should be 4 bytes"
338 # Create a new file and grow it to 4 bytes.
339 atf_check -e file:stderr.txt truncate -r reference afile
340 eval $(stat -s afile)
341 [ ${st_size} -eq 4 ] || atf_fail "new file should also be 4 bytes"
344 atf_test_case new_zero
347 atf_set "descr" "Verifies truncate can make and grow zero byte file"
353 # Create a new file and grow it to zero bytes.
354 atf_check -s exit:0 -e file:stderr.txt truncate -s0 output.txt
355 eval $(stat -s output.txt)
356 [ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
358 # Pretend to grow the file.
359 atf_check -s exit:0 -e file:stderr.txt truncate -s+0 output.txt
360 eval $(stat -s output.txt)
361 [ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
364 atf_test_case negative
367 atf_set "descr" "Verifies truncate treats negative sizes as zero"
371 # Create a 5 byte file.
372 printf "abcd\n" > afile
373 eval $(stat -s afile)
374 [ ${st_size} -eq 5 ] || atf_fail "afile file should be 5 bytes"
378 # Create a new file and do a 100 byte negative relative shrink.
379 atf_check -e file:stderr.txt truncate -s-100 afile
380 eval $(stat -s afile)
381 [ ${st_size} -eq 0 ] || atf_fail "new file should now be zero bytes"
384 atf_init_test_cases()
386 atf_add_test_case illegal_option
387 atf_add_test_case illegal_size
388 atf_add_test_case too_large_size
389 atf_add_test_case opt_c
390 atf_add_test_case opt_rs
391 atf_add_test_case no_files
392 atf_add_test_case bad_refer
393 atf_add_test_case bad_truncate
394 atf_add_test_case cannot_open
395 atf_add_test_case new_absolute_grow
396 atf_add_test_case new_absolute_shrink
397 atf_add_test_case new_relative_grow
398 atf_add_test_case new_relative_shrink
399 atf_add_test_case reference
400 atf_add_test_case new_zero
401 atf_add_test_case negative