blob: 4929f4e499a851583db0af0f3aaf032e37270981 [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 Vlasenko198b02f2013-12-31 23:22:36 +010028# 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.
Denys Vlasenkodd1d8ab2013-11-19 16:56:26 +010031testing "Empty file is not a tarball.tar.gz" '\
Denys Vlasenko198b02f2013-12-31 23:22:36 +010032{ tar xvzf - 2>&1; echo $?; } | grep -Fv "invalid magic"
Denys Vlasenko1cbc6422013-11-19 14:52:02 +010033' "\
Denys Vlasenko1cbc6422013-11-19 14:52:02 +010034tar: short read
351
36" \
37"" ""
38SKIP=
39
Denys Vlasenko0545e3b2013-11-19 17:17:48 +010040testing "Two zeroed blocks is a ('truncated') empty tarball" '\
Denys Vlasenko76258112013-12-31 23:25:46 +010041dd if=/dev/zero bs=512 count=2 2>/dev/null | tar xvf - 2>&1; echo $?
Denys Vlasenko0545e3b2013-11-19 17:17:48 +010042' "\
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
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020056optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenko02365a62010-04-09 10:52:52 +020057testing "tar hardlinks and repeated files" '\
Denys Vlasenko425ad9c2009-12-16 22:46:01 +010058rm -rf input_* test.tar 2>/dev/null
59>input_hard1
Denys Vlasenkobf224752009-11-29 19:09:29 +010060ln input_hard1 input_hard2
61mkdir input_dir
62>input_dir/file
Denys Vlasenko02365a62010-04-09 10:52:52 +020063chmod -R 644 *
64chmod 755 input_dir
Denys Vlasenkobf224752009-11-29 19:09:29 +010065tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
Denys Vlasenko02365a62010-04-09 10:52:52 +020066tar 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' "\
Denys Vlasenko425ad9c2009-12-16 22:46:01 +010071input
Denys Vlasenkobf224752009-11-29 19:09:29 +010072input_dir/
73input_dir/file
74input_hard1
75input_hard2 -> input_hard1
76input_hard1 -> input_hard1
77input_dir/
78input_dir/file
79input
Denys Vlasenko02365a62010-04-09 10:52:52 +020080Ok: 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"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020087SKIP=
Denys Vlasenko02365a62010-04-09 10:52:52 +020088
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020089optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenko02365a62010-04-09 10:52:52 +020090testing "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
Denys Vlasenko02365a62010-04-09 10:52:52 +020096ln input_hard1 input_dir
97ln input_hard2 input_dir
Dan Fandricheb2bf5b2010-09-02 18:38:00 -070098chmod 550 input_dir
Denys Vlasenko0d7cb4c2010-09-03 17:22:56 +020099# 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*
Denys Vlasenko02365a62010-04-09 10:52:52 +0200102tar tvf test.tar | sed "s/.*[0-9] input/input/"
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700103chmod 770 input_dir
104rm -rf input_*
Denys Vlasenko02365a62010-04-09 10:52:52 +0200105tar xf test.tar 2>&1
106echo Ok: $?
Denys Vlasenko0d7cb4c2010-09-03 17:22:56 +0200107ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
Denys Vlasenko02365a62010-04-09 10:52:52 +0200108' "\
Denys Vlasenko02365a62010-04-09 10:52:52 +0200109input_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
Denys Vlasenko02365a62010-04-09 10:52:52 +0200116-rwxr----x input_hard1
117-rwxr----x input_hard2
Denys Vlasenkobf224752009-11-29 19:09:29 +0100118" \
119"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200120SKIP=
Denys Vlasenkobf224752009-11-29 19:09:29 +0100121
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +0200122optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200123testing "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
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200129ln input_file input_dir
130ln input_soft input_dir
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700131chmod 550 input_dir
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700132tar cf test.tar input_dir/* input_[fs]*
Denys Vlasenkoe82cf332010-05-12 15:59:32 +0200133tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700134chmod 770 input_dir
135rm -rf input_*
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200136tar xf test.tar 2>&1
137echo Ok: $?
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700138ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200139' "\
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200140input_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
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200147-rwxr----x input_file
148lrwxrwxrwx input_file
149" \
150"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200151SKIP=
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200152
Denys Vlasenkoda138242010-05-11 12:02:48 +0200153optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
Denys Vlasenko8a936cf2009-12-16 23:18:59 +0100154testing "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" ""
Chris Metcalf208d35d2010-04-02 09:57:27 +0200166SKIP=
Denys Vlasenko8a936cf2009-12-16 23:18:59 +0100167
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700168test x"$SKIP_KNOWN_BUGS" = x"" && {
Dan Fandrich8d789e42010-09-05 16:16:46 +0200169# 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=
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700193}
194
Denys Vlasenkob8ab4b02011-02-06 20:02:15 +0100195# Had a bug where on extract autodetect first "switched off" -z
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100196# and then failed to recognize .tgz extension
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200197optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100198testing "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"" ""
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200209SKIP=
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100210
Denys Vlasenkobb8d7db2012-03-06 16:57:01 +0100211# 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
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100230# On extract, everything up to and including last ".." component is stripped
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200231optional FEATURE_TAR_CREATE
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100232testing "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" "\
Denys Vlasenkob80acf52011-03-02 01:21:02 +0100241tar: removing leading './../tar.tempdir/input_dir/../' from member names
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100242input_dir/
243input_dir/file
244Ok
245" \
246"" ""
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200247SKIP=
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100248
Dan Fandrich8d789e42010-09-05 16:16:46 +0200249
Denys Vlasenko02365a62010-04-09 10:52:52 +0200250cd .. && rm -rf tar.tempdir || exit 1
Denys Vlasenkobf224752009-11-29 19:09:29 +0100251
252exit $FAILCOUNT