summaryrefslogtreecommitdiff
path: root/testsuite/tar.tests (plain)
blob: 4929f4e499a851583db0af0f3aaf032e37270981
1#!/bin/sh
2# Copyright 2009 by Denys Vlasenko
3# Licensed under GPLv2, see file LICENSE in this source tree.
4
5. ./testing.sh
6
7unset LANG
8unset LANGUAGE
9unset LC_COLLATE
10unset LC_ALL
11umask 022
12
13rm -rf tar.tempdir 2>/dev/null
14mkdir tar.tempdir && cd tar.tempdir || exit 1
15
16# testing "test name" "script" "expected result" "file input" "stdin"
17
18testing "Empty file is not a tarball" '\
19tar xvf - 2>&1; echo $?
20' "\
21tar: short read
221
23" \
24"" ""
25SKIP=
26
27optional FEATURE_SEAMLESS_GZ
28# In NOMMU case, "invalid magic" message comes from gunzip child process.
29# Otherwise, it comes from tar.
30# Need to fix output up to avoid false positive.
31testing "Empty file is not a tarball.tar.gz" '\
32{ tar xvzf - 2>&1; echo $?; } | grep -Fv "invalid magic"
33' "\
34tar: short read
351
36" \
37"" ""
38SKIP=
39
40testing "Two zeroed blocks is a ('truncated') empty tarball" '\
41dd if=/dev/zero bs=512 count=2 2>/dev/null | tar xvf - 2>&1; echo $?
42' "\
430
44" \
45"" ""
46SKIP=
47
48testing "Twenty zeroed blocks is an empty tarball" '\
49dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $?
50' "\
510
52" \
53"" ""
54SKIP=
55
56optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
57testing "tar hardlinks and repeated files" '\
58rm -rf input_* test.tar 2>/dev/null
59>input_hard1
60ln input_hard1 input_hard2
61mkdir input_dir
62>input_dir/file
63chmod -R 644 *
64chmod 755 input_dir
65tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
66tar tvf test.tar | sed "s/.*[0-9] input/input/"
67tar xf test.tar 2>&1
68echo Ok: $?
69ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
70' "\
71input
72input_dir/
73input_dir/file
74input_hard1
75input_hard2 -> input_hard1
76input_hard1 -> input_hard1
77input_dir/
78input_dir/file
79input
80Ok: 0
81-rw-r--r-- input_dir/file
82drwxr-xr-x input_dir
83-rw-r--r-- input_hard1
84-rw-r--r-- input_hard2
85" \
86"" ""
87SKIP=
88
89optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
90testing "tar hardlinks mode" '\
91rm -rf input_* test.tar 2>/dev/null
92>input_hard1
93chmod 741 input_hard1
94ln input_hard1 input_hard2
95mkdir input_dir
96ln input_hard1 input_dir
97ln input_hard2 input_dir
98chmod 550 input_dir
99# On some filesystems, input_dir/input_hard2 is returned by readdir
100# BEFORE input_dir/input_hard1! Thats why we cant just "tar cf ... input_*":
101tar cf test.tar input_dir/input_hard* input_hard*
102tar tvf test.tar | sed "s/.*[0-9] input/input/"
103chmod 770 input_dir
104rm -rf input_*
105tar xf test.tar 2>&1
106echo Ok: $?
107ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
108' "\
109input_dir/input_hard1
110input_dir/input_hard2 -> input_dir/input_hard1
111input_hard1 -> input_dir/input_hard1
112input_hard2 -> input_dir/input_hard1
113Ok: 0
114-rwxr----x input_dir/input_hard1
115-rwxr----x input_dir/input_hard2
116-rwxr----x input_hard1
117-rwxr----x input_hard2
118" \
119"" ""
120SKIP=
121
122optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
123testing "tar symlinks mode" '\
124rm -rf input_* test.tar 2>/dev/null
125>input_file
126chmod 741 input_file
127ln -s input_file input_soft
128mkdir input_dir
129ln input_file input_dir
130ln input_soft input_dir
131chmod 550 input_dir
132tar cf test.tar input_dir/* input_[fs]*
133tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
134chmod 770 input_dir
135rm -rf input_*
136tar xf test.tar 2>&1
137echo Ok: $?
138ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
139' "\
140input_dir/input_file
141input_dir/input_soft -> input_file
142input_file -> input_dir/input_file
143input_soft -> input_dir/input_soft
144Ok: 0
145-rwxr----x input_dir/input_file
146lrwxrwxrwx input_file
147-rwxr----x input_file
148lrwxrwxrwx input_file
149" \
150"" ""
151SKIP=
152
153optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
154testing "tar --overwrite" "\
155rm -rf input_* test.tar 2>/dev/null
156ln input input_hard
157tar cf test.tar input_hard
158echo WRONG >input
159# --overwrite opens 'input_hard' without unlinking,
160# thus 'input_hard' still linked to 'input' and we write 'Ok' into it
161tar xf test.tar --overwrite 2>&1 && cat input
162" "\
163Ok
164" \
165"Ok\n" ""
166SKIP=
167
168test x"$SKIP_KNOWN_BUGS" = x"" && {
169# Needs to be run under non-root for meaningful test
170optional FEATURE_TAR_CREATE
171testing "tar writing into read-only dir" '\
172rm -rf input_* test.tar 2>/dev/null
173mkdir input_dir
174>input_dir/input_file
175chmod 550 input_dir
176tar cf test.tar input_dir
177tar tvf test.tar | sed "s/.*[0-9] input/input/"
178chmod 770 input_dir
179rm -rf input_*
180tar xf test.tar 2>&1
181echo Ok: $?
182ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
183chmod 770 input_dir
184' "\
185input_dir/
186input_dir/input_file
187Ok: 0
188-rw-r--r-- input_dir/input_file
189dr-xr-x--- input_dir
190" \
191"" ""
192SKIP=
193}
194
195# Had a bug where on extract autodetect first "switched off" -z
196# and then failed to recognize .tgz extension
197optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ
198testing "tar extract tgz" "\
199dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
200tar -czf F0.tgz F0
201rm F0
202tar -xzvf F0.tgz && echo Ok
203rm F0 || echo BAD
204" "\
205F0
206Ok
207" \
208"" ""
209SKIP=
210
211# Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
212# (the uuencoded hello_world.txz contains one empty file named "hello_world")
213optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
214testing "tar extract txz" "\
215uudecode -o input && tar tf input && echo Ok
216" "\
217hello_world
218Ok
219" \
220"" "\
221begin-base64 644 hello_world.txz
222/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
223SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
224Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
225AAAEWVo=
226====
227"
228SKIP=
229
230# On extract, everything up to and including last ".." component is stripped
231optional FEATURE_TAR_CREATE
232testing "tar strips /../ on extract" "\
233rm -rf input_* test.tar 2>/dev/null
234mkdir input_dir
235echo Ok >input_dir/file
236tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
237rm -rf input_* 2>/dev/null
238tar -vxf test.tar 2>&1
239cat input_dir/file 2>&1
240" "\
241tar: removing leading './../tar.tempdir/input_dir/../' from member names
242input_dir/
243input_dir/file
244Ok
245" \
246"" ""
247SKIP=
248
249
250cd .. && rm -rf tar.tempdir || exit 1
251
252exit $FAILCOUNT
253