blob: 93ba5f0043479edded144b336a48fd129c362e26 [file] [log] [blame]
Denys Vlasenkobf224752009-11-29 19:09:29 +01001#!/bin/sh
2# Copyright 2009 by Denys Vlasenko
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003# Licensed under GPLv2, see file LICENSE in this source tree.
Denys Vlasenkobf224752009-11-29 19:09:29 +01004
5. ./testing.sh
6
Dan Fandrich775965d2010-08-10 23:33:57 -07007unset LANG
8unset LANGUAGE
9unset LC_COLLATE
10unset LC_ALL
11umask 022
12
Denys Vlasenko02365a62010-04-09 10:52:52 +020013rm -rf tar.tempdir 2>/dev/null
14mkdir tar.tempdir && cd tar.tempdir || exit 1
Denys Vlasenkobf224752009-11-29 19:09:29 +010015
16# testing "test name" "script" "expected result" "file input" "stdin"
17
Denys Vlasenko0545e3b2013-11-19 17:17:48 +010018testing "Empty file is not a tarball" '\
19tar xvf - 2>&1; echo $?
20' "\
21tar: short read
221
23" \
24"" ""
25SKIP=
26
Denys Vlasenko1cbc6422013-11-19 14:52:02 +010027optional FEATURE_SEAMLESS_GZ
Denys Vlasenkodd1d8ab2013-11-19 16:56:26 +010028testing "Empty file is not a tarball.tar.gz" '\
Denys Vlasenko1cbc6422013-11-19 14:52:02 +010029tar xvzf - 2>&1; echo $?
30' "\
31tar: invalid magic
32tar: short read
331
34" \
35"" ""
36SKIP=
37
Denys Vlasenko0545e3b2013-11-19 17:17:48 +010038testing "Two zeroed blocks is a ('truncated') empty tarball" '\
39dd if=/dev/zero bs=512 count=2 2>/dev/null | strace -o /tmp/zzz -tt -s99 tar xvf - 2>&1; echo $?
40' "\
410
42" \
43"" ""
44SKIP=
45
46testing "Twenty zeroed blocks is an empty tarball" '\
47dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $?
48' "\
490
50" \
51"" ""
52SKIP=
53
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020054optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenko02365a62010-04-09 10:52:52 +020055testing "tar hardlinks and repeated files" '\
Denys Vlasenko425ad9c2009-12-16 22:46:01 +010056rm -rf input_* test.tar 2>/dev/null
57>input_hard1
Denys Vlasenkobf224752009-11-29 19:09:29 +010058ln input_hard1 input_hard2
59mkdir input_dir
60>input_dir/file
Denys Vlasenko02365a62010-04-09 10:52:52 +020061chmod -R 644 *
62chmod 755 input_dir
Denys Vlasenkobf224752009-11-29 19:09:29 +010063tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
Denys Vlasenko02365a62010-04-09 10:52:52 +020064tar tvf test.tar | sed "s/.*[0-9] input/input/"
65tar xf test.tar 2>&1
66echo Ok: $?
67ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
68' "\
Denys Vlasenko425ad9c2009-12-16 22:46:01 +010069input
Denys Vlasenkobf224752009-11-29 19:09:29 +010070input_dir/
71input_dir/file
72input_hard1
73input_hard2 -> input_hard1
74input_hard1 -> input_hard1
75input_dir/
76input_dir/file
77input
Denys Vlasenko02365a62010-04-09 10:52:52 +020078Ok: 0
79-rw-r--r-- input_dir/file
80drwxr-xr-x input_dir
81-rw-r--r-- input_hard1
82-rw-r--r-- input_hard2
83" \
84"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020085SKIP=
Denys Vlasenko02365a62010-04-09 10:52:52 +020086
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020087optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenko02365a62010-04-09 10:52:52 +020088testing "tar hardlinks mode" '\
89rm -rf input_* test.tar 2>/dev/null
90>input_hard1
91chmod 741 input_hard1
92ln input_hard1 input_hard2
93mkdir input_dir
Denys Vlasenko02365a62010-04-09 10:52:52 +020094ln input_hard1 input_dir
95ln input_hard2 input_dir
Dan Fandricheb2bf5b2010-09-02 18:38:00 -070096chmod 550 input_dir
Denys Vlasenko0d7cb4c2010-09-03 17:22:56 +020097# On some filesystems, input_dir/input_hard2 is returned by readdir
98# BEFORE input_dir/input_hard1! Thats why we cant just "tar cf ... input_*":
99tar cf test.tar input_dir/input_hard* input_hard*
Denys Vlasenko02365a62010-04-09 10:52:52 +0200100tar tvf test.tar | sed "s/.*[0-9] input/input/"
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700101chmod 770 input_dir
102rm -rf input_*
Denys Vlasenko02365a62010-04-09 10:52:52 +0200103tar xf test.tar 2>&1
104echo Ok: $?
Denys Vlasenko0d7cb4c2010-09-03 17:22:56 +0200105ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
Denys Vlasenko02365a62010-04-09 10:52:52 +0200106' "\
Denys Vlasenko02365a62010-04-09 10:52:52 +0200107input_dir/input_hard1
108input_dir/input_hard2 -> input_dir/input_hard1
109input_hard1 -> input_dir/input_hard1
110input_hard2 -> input_dir/input_hard1
111Ok: 0
112-rwxr----x input_dir/input_hard1
113-rwxr----x input_dir/input_hard2
Denys Vlasenko02365a62010-04-09 10:52:52 +0200114-rwxr----x input_hard1
115-rwxr----x input_hard2
Denys Vlasenkobf224752009-11-29 19:09:29 +0100116" \
117"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200118SKIP=
Denys Vlasenkobf224752009-11-29 19:09:29 +0100119
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +0200120optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200121testing "tar symlinks mode" '\
122rm -rf input_* test.tar 2>/dev/null
123>input_file
124chmod 741 input_file
125ln -s input_file input_soft
126mkdir input_dir
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200127ln input_file input_dir
128ln input_soft input_dir
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700129chmod 550 input_dir
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700130tar cf test.tar input_dir/* input_[fs]*
Denys Vlasenkoe82cf332010-05-12 15:59:32 +0200131tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700132chmod 770 input_dir
133rm -rf input_*
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200134tar xf test.tar 2>&1
135echo Ok: $?
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700136ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200137' "\
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200138input_dir/input_file
139input_dir/input_soft -> input_file
140input_file -> input_dir/input_file
141input_soft -> input_dir/input_soft
142Ok: 0
143-rwxr----x input_dir/input_file
144lrwxrwxrwx input_file
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200145-rwxr----x input_file
146lrwxrwxrwx input_file
147" \
148"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200149SKIP=
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200150
Denys Vlasenkoda138242010-05-11 12:02:48 +0200151optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
Denys Vlasenko8a936cf2009-12-16 23:18:59 +0100152testing "tar --overwrite" "\
153rm -rf input_* test.tar 2>/dev/null
154ln input input_hard
155tar cf test.tar input_hard
156echo WRONG >input
157# --overwrite opens 'input_hard' without unlinking,
158# thus 'input_hard' still linked to 'input' and we write 'Ok' into it
159tar xf test.tar --overwrite 2>&1 && cat input
160" "\
161Ok
162" \
163"Ok\n" ""
Chris Metcalf208d35d2010-04-02 09:57:27 +0200164SKIP=
Denys Vlasenko8a936cf2009-12-16 23:18:59 +0100165
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700166test x"$SKIP_KNOWN_BUGS" = x"" && {
Dan Fandrich8d789e42010-09-05 16:16:46 +0200167# Needs to be run under non-root for meaningful test
168optional FEATURE_TAR_CREATE
169testing "tar writing into read-only dir" '\
170rm -rf input_* test.tar 2>/dev/null
171mkdir input_dir
172>input_dir/input_file
173chmod 550 input_dir
174tar cf test.tar input_dir
175tar tvf test.tar | sed "s/.*[0-9] input/input/"
176chmod 770 input_dir
177rm -rf input_*
178tar xf test.tar 2>&1
179echo Ok: $?
180ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
181chmod 770 input_dir
182' "\
183input_dir/
184input_dir/input_file
185Ok: 0
186-rw-r--r-- input_dir/input_file
187dr-xr-x--- input_dir
188" \
189"" ""
190SKIP=
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700191}
192
Denys Vlasenkob8ab4b02011-02-06 20:02:15 +0100193# Had a bug where on extract autodetect first "switched off" -z
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100194# and then failed to recognize .tgz extension
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200195optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100196testing "tar extract tgz" "\
197dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
198tar -czf F0.tgz F0
199rm F0
200tar -xzvf F0.tgz && echo Ok
201rm F0 || echo BAD
202" "\
203F0
204Ok
205" \
206"" ""
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200207SKIP=
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100208
Denys Vlasenkobb8d7db2012-03-06 16:57:01 +0100209# Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
210# (the uuencoded hello_world.txz contains one empty file named "hello_world")
211optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
212testing "tar extract txz" "\
213uudecode -o input && tar tf input && echo Ok
214" "\
215hello_world
216Ok
217" \
218"" "\
219begin-base64 644 hello_world.txz
220/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
221SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
222Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
223AAAEWVo=
224====
225"
226SKIP=
227
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100228# On extract, everything up to and including last ".." component is stripped
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200229optional FEATURE_TAR_CREATE
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100230testing "tar strips /../ on extract" "\
231rm -rf input_* test.tar 2>/dev/null
232mkdir input_dir
233echo Ok >input_dir/file
234tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
235rm -rf input_* 2>/dev/null
236tar -vxf test.tar 2>&1
237cat input_dir/file 2>&1
238" "\
Denys Vlasenkob80acf52011-03-02 01:21:02 +0100239tar: removing leading './../tar.tempdir/input_dir/../' from member names
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100240input_dir/
241input_dir/file
242Ok
243" \
244"" ""
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200245SKIP=
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100246
Dan Fandrich8d789e42010-09-05 16:16:46 +0200247
Denys Vlasenko02365a62010-04-09 10:52:52 +0200248cd .. && rm -rf tar.tempdir || exit 1
Denys Vlasenkobf224752009-11-29 19:09:29 +0100249
250exit $FAILCOUNT