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
179 atf_set "descr" "Verifies that truncate reports an error during" \
181 atf_set "require.user" "unprivileged"
185 create_stderr_file "truncate: exists.txt: Permission denied"
187 # Trying to get the ftruncate() call to return -1.
189 atf_check chmod 444 exists.txt
191 atf_check -s not-exit:0 -e file:stderr.txt truncate -s1 exists.txt
194 atf_test_case new_absolute_grow
195 new_absolute_grow_head()
197 atf_set "descr" "Verifies truncate can make and grow a new 1m file"
199 new_absolute_grow_body()
203 # Create a new file and grow it to 1024 bytes.
204 atf_check -s exit:0 -e file:stderr.txt truncate -s1k output.txt
205 atf_check -s exit:1 cmp -s output.txt /dev/zero
206 eval $(stat -s output.txt)
207 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
211 # Grow the existing file to 1M. We are using absolute sizes.
212 atf_check -s exit:0 -e file:stderr.txt truncate -c -s1M output.txt
213 atf_check -s exit:1 cmp -s output.txt /dev/zero
214 eval $(stat -s output.txt)
215 [ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
218 atf_test_case new_absolute_shrink
219 new_absolute_shrink_head()
221 atf_set "descr" "Verifies that truncate can make and" \
222 "shrink a new 1m file"
224 new_absolute_shrink_body()
228 # Create a new file and grow it to 1048576 bytes.
229 atf_check -s exit:0 -e file:stderr.txt truncate -s1M output.txt
230 atf_check -s exit:1 cmp -s output.txt /dev/zero
231 eval $(stat -s output.txt)
232 [ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
236 # Shrink the existing file to 1k. We are using absolute sizes.
237 atf_check -s exit:0 -e file:stderr.txt truncate -s1k output.txt
238 atf_check -s exit:1 cmp -s output.txt /dev/zero
239 eval $(stat -s output.txt)
240 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
243 atf_test_case new_relative_grow
244 new_relative_grow_head()
246 atf_set "descr" "Verifies truncate can make and grow a new 1m file" \
247 "using relative sizes"
249 new_relative_grow_body()
253 # Create a new file and grow it to 1024 bytes.
254 atf_check -s exit:0 -e file:stderr.txt truncate -s+1k output.txt
255 atf_check -s exit:1 cmp -s output.txt /dev/zero
256 eval $(stat -s output.txt)
257 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
261 # Grow the existing file to 1M. We are using relative sizes.
262 atf_check -s exit:0 -e file:stderr.txt truncate -s+1047552 output.txt
263 atf_check -s exit:1 cmp -s output.txt /dev/zero
264 eval $(stat -s output.txt)
265 [ ${st_size} -eq 1048576 ] || atf_fail "expected file size of 1m"
268 atf_test_case new_relative_shrink
269 new_relative_shrink_head()
271 atf_set "descr" "Verifies truncate can make and shrink a new 1m file" \
272 "using relative sizes"
274 new_relative_shrink_body()
278 # Create a new file and grow it to 1049600 bytes.
279 atf_check -s exit:0 -e file:stderr.txt truncate -s+1049600 output.txt
280 atf_check -s exit:1 cmp -s output.txt /dev/zero
281 eval $(stat -s output.txt)
282 [ ${st_size} -eq 1049600 ] || atf_fail "expected file size of 1m"
286 # Shrink the existing file to 1k. We are using relative sizes.
287 atf_check -s exit:0 -e file:stderr.txt truncate -s-1M output.txt
288 atf_check -s exit:1 cmp -s output.txt /dev/zero
289 eval $(stat -s output.txt)
290 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
293 atf_test_case cannot_open
296 atf_set "descr" "Verifies truncate handles open failures correctly" \
298 atf_set "require.user" "unprivileged"
302 # Create three files -- the middle file cannot allow writes.
306 atf_check chmod 0000 0000
308 create_stderr_file "truncate: 0000: Permission denied"
310 # Create a new file and grow it to 1024 bytes.
311 atf_check -s not-exit:0 -e file:stderr.txt \
312 truncate -c -s1k before 0000 after
313 eval $(stat -s before)
314 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
315 eval $(stat -s after)
316 [ ${st_size} -eq 1024 ] || atf_fail "expected file size of 1k"
318 [ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
321 atf_test_case reference
324 atf_set "descr" "Verifies that truncate can use a reference file"
328 # Create a 4 byte reference file.
329 printf "123\n" > reference
330 eval $(stat -s reference)
331 [ ${st_size} -eq 4 ] || atf_fail "reference file should be 4 bytes"
335 # Create a new file and grow it to 4 bytes.
336 atf_check -e file:stderr.txt truncate -r reference afile
337 eval $(stat -s afile)
338 [ ${st_size} -eq 4 ] || atf_fail "new file should also be 4 bytes"
341 atf_test_case new_zero
344 atf_set "descr" "Verifies truncate can make and grow zero byte file"
350 # Create a new file and grow it to zero bytes.
351 atf_check -s exit:0 -e file:stderr.txt truncate -s0 output.txt
352 eval $(stat -s output.txt)
353 [ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
355 # Pretend to grow the file.
356 atf_check -s exit:0 -e file:stderr.txt truncate -s+0 output.txt
357 eval $(stat -s output.txt)
358 [ ${st_size} -eq 0 ] || atf_fail "expected file size of zero"
361 atf_test_case negative
364 atf_set "descr" "Verifies truncate treats negative sizes as zero"
368 # Create a 5 byte file.
369 printf "abcd\n" > afile
370 eval $(stat -s afile)
371 [ ${st_size} -eq 5 ] || atf_fail "afile file should be 5 bytes"
375 # Create a new file and do a 100 byte negative relative shrink.
376 atf_check -e file:stderr.txt truncate -s-100 afile
377 eval $(stat -s afile)
378 [ ${st_size} -eq 0 ] || atf_fail "new file should now be zero bytes"
381 atf_init_test_cases()
383 atf_add_test_case illegal_option
384 atf_add_test_case illegal_size
385 atf_add_test_case too_large_size
386 atf_add_test_case opt_c
387 atf_add_test_case opt_rs
388 atf_add_test_case no_files
389 atf_add_test_case bad_refer
390 atf_add_test_case bad_truncate
391 atf_add_test_case cannot_open
392 atf_add_test_case new_absolute_grow
393 atf_add_test_case new_absolute_shrink
394 atf_add_test_case new_relative_grow
395 atf_add_test_case new_relative_shrink
396 atf_add_test_case reference
397 atf_add_test_case new_zero
398 atf_add_test_case negative