t_tar.sh revision 1.3 1 # $NetBSD: t_tar.sh,v 1.3 2024/04/28 07:27:40 rillig Exp $
2 #
3 # Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 # POSSIBILITY OF SUCH DAMAGE.
26 #
27
28 atf_test_case append
29 append_head() {
30 atf_set "descr" "Ensure that appending a file to an archive" \
31 "produces the same results as if the file" \
32 "had been there during the archive's creation"
33 }
34 append_body() {
35 touch foo bar
36
37 # store both foo and bar into file1.tar
38 atf_check -s exit:0 -o empty -e empty tar -cf file1.tar foo bar
39
40 # store foo into file2.tar, then append bar to file2.tar
41 atf_check -s exit:0 -o empty -e empty tar -cf file2.tar foo
42 atf_check -s exit:0 -o empty -e empty tar -rf file2.tar bar
43
44 # ensure that file1.tar and file2.tar are equal
45 atf_check -s exit:0 -o empty -e empty cmp file1.tar file2.tar
46 }
47
48 atf_test_case rd_base256_size
49 rd_base256_size_head() {
50 atf_set "descr" "Test extracting an archive whose member size" \
51 "is encoded as base-256 number (GNU style)"
52 }
53 rd_base256_size_body() {
54 # prepare random file data for comparison
55 # take 0x1200CF bytes in order to test that we:
56 # - handle multiple bytes of size field correctly
57 # - do not fail on NUL bytes
58 # - do not fail on char values > 0x80 (with signed char)
59 dd if=/dev/urandom of=reference.bin bs=1179855 count=1
60 # write test archive header
61 # - filename
62 printf 'output.bin' > test.tar
63 # - pad to 100 octets
64 head -c 90 /dev/zero >> test.tar
65 # - mode, uid, gid
66 printf '%07d\0%07d\0%07d\0' 644 177776 177775 >> test.tar
67 # - size (base-256)
68 printf '\x80\0\0\0\0\0\0\0\0\x12\x00\xCF' >> test.tar
69 # - timestamp, checksum
70 printf '%011d\0%06d\0 0' 13377546642 12460 >> test.tar
71 # - pad empty linkname (100 octets)
72 head -c 100 /dev/zero >> test.tar
73 # - magic, user name
74 printf 'ustar \0nobody' >> test.tar
75 # - pad user name field to 32 bytes
76 head -c 26 /dev/zero >> test.tar
77 # - group name
78 printf 'nogroup' >> test.tar
79 # - pad to full block
80 head -c 208 /dev/zero >> test.tar
81 # append file data to the test archive
82 cat reference.bin >> test.tar
83 # pad to full block + append two terminating null blocks
84 head -c 1450 /dev/zero >> test.tar
85
86 # test extracting the test archive
87 atf_check -s exit:0 -o empty -e empty tar -xf test.tar
88
89 # ensure that output.bin is equal to reference.bin
90 atf_check -s exit:0 -o empty -e empty cmp output.bin reference.bin
91 }
92
93 atf_init_test_cases()
94 {
95 atf_add_test_case append
96 atf_add_test_case rd_base256_size
97 }
98