blob: 890a73dd57a894fa237b17da7b92021da2cd3e3c [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 Vlasenko6c563e32015-10-22 01:07:13 +020056# "tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input":
57# GNU tar 1.26 records as hardlinks:
58# input_hard2 -> input_hard1
59# input_hard1 -> input_hard1 (!!!)
60# input_dir/file -> input_dir/file
61# input -> input
62# As of 1.24.0, we don't record last two: for them, nlink==1
63# and we check for "hardlink"ness only files with nlink!=1
64# We also don't use "hrw-r--r--" notation for hardlinks in "tar tv" listing.
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020065optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenko02365a62010-04-09 10:52:52 +020066testing "tar hardlinks and repeated files" '\
Denys Vlasenko425ad9c2009-12-16 22:46:01 +010067rm -rf input_* test.tar 2>/dev/null
68>input_hard1
Denys Vlasenkobf224752009-11-29 19:09:29 +010069ln input_hard1 input_hard2
70mkdir input_dir
71>input_dir/file
Denys Vlasenko02365a62010-04-09 10:52:52 +020072chmod -R 644 *
73chmod 755 input_dir
Denys Vlasenkobf224752009-11-29 19:09:29 +010074tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
Denys Vlasenko02365a62010-04-09 10:52:52 +020075tar tvf test.tar | sed "s/.*[0-9] input/input/"
Denys Vlasenko6c563e32015-10-22 01:07:13 +020076rm -rf input_dir
Denys Vlasenko02365a62010-04-09 10:52:52 +020077tar xf test.tar 2>&1
78echo Ok: $?
79ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
80' "\
Denys Vlasenko425ad9c2009-12-16 22:46:01 +010081input
Denys Vlasenkobf224752009-11-29 19:09:29 +010082input_dir/
83input_dir/file
84input_hard1
85input_hard2 -> input_hard1
86input_hard1 -> input_hard1
87input_dir/
88input_dir/file
89input
Denys Vlasenko02365a62010-04-09 10:52:52 +020090Ok: 0
91-rw-r--r-- input_dir/file
92drwxr-xr-x input_dir
93-rw-r--r-- input_hard1
94-rw-r--r-- input_hard2
95" \
96"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +020097SKIP=
Denys Vlasenko02365a62010-04-09 10:52:52 +020098
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +020099optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenko02365a62010-04-09 10:52:52 +0200100testing "tar hardlinks mode" '\
101rm -rf input_* test.tar 2>/dev/null
102>input_hard1
103chmod 741 input_hard1
104ln input_hard1 input_hard2
105mkdir input_dir
Denys Vlasenko02365a62010-04-09 10:52:52 +0200106ln input_hard1 input_dir
107ln input_hard2 input_dir
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700108chmod 550 input_dir
Denys Vlasenko0d7cb4c2010-09-03 17:22:56 +0200109# On some filesystems, input_dir/input_hard2 is returned by readdir
110# BEFORE input_dir/input_hard1! Thats why we cant just "tar cf ... input_*":
111tar cf test.tar input_dir/input_hard* input_hard*
Denys Vlasenko02365a62010-04-09 10:52:52 +0200112tar tvf test.tar | sed "s/.*[0-9] input/input/"
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700113chmod 770 input_dir
114rm -rf input_*
Denys Vlasenko02365a62010-04-09 10:52:52 +0200115tar xf test.tar 2>&1
116echo Ok: $?
Denys Vlasenko0d7cb4c2010-09-03 17:22:56 +0200117ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
Denys Vlasenko02365a62010-04-09 10:52:52 +0200118' "\
Denys Vlasenko02365a62010-04-09 10:52:52 +0200119input_dir/input_hard1
120input_dir/input_hard2 -> input_dir/input_hard1
121input_hard1 -> input_dir/input_hard1
122input_hard2 -> input_dir/input_hard1
123Ok: 0
124-rwxr----x input_dir/input_hard1
125-rwxr----x input_dir/input_hard2
Denys Vlasenko02365a62010-04-09 10:52:52 +0200126-rwxr----x input_hard1
127-rwxr----x input_hard2
Denys Vlasenkobf224752009-11-29 19:09:29 +0100128" \
129"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200130SKIP=
Denys Vlasenkobf224752009-11-29 19:09:29 +0100131
Denys Vlasenkobfa1b2e2010-05-11 03:53:57 +0200132optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200133testing "tar symlinks mode" '\
134rm -rf input_* test.tar 2>/dev/null
135>input_file
136chmod 741 input_file
137ln -s input_file input_soft
138mkdir input_dir
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200139ln input_file input_dir
140ln input_soft input_dir
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700141chmod 550 input_dir
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700142tar cf test.tar input_dir/* input_[fs]*
Denys Vlasenkoe82cf332010-05-12 15:59:32 +0200143tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
Dan Fandricheb2bf5b2010-09-02 18:38:00 -0700144chmod 770 input_dir
145rm -rf input_*
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200146tar xf test.tar 2>&1
147echo Ok: $?
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700148ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200149' "\
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200150input_dir/input_file
151input_dir/input_soft -> input_file
152input_file -> input_dir/input_file
153input_soft -> input_dir/input_soft
154Ok: 0
155-rwxr----x input_dir/input_file
156lrwxrwxrwx input_file
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200157-rwxr----x input_file
158lrwxrwxrwx input_file
159" \
160"" ""
Denys Vlasenkoe3d90a92010-05-10 05:53:16 +0200161SKIP=
Denys Vlasenkoe69ad872010-04-09 14:11:45 +0200162
Denys Vlasenkoda138242010-05-11 12:02:48 +0200163optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
Denys Vlasenko8a936cf2009-12-16 23:18:59 +0100164testing "tar --overwrite" "\
165rm -rf input_* test.tar 2>/dev/null
166ln input input_hard
167tar cf test.tar input_hard
168echo WRONG >input
169# --overwrite opens 'input_hard' without unlinking,
170# thus 'input_hard' still linked to 'input' and we write 'Ok' into it
171tar xf test.tar --overwrite 2>&1 && cat input
172" "\
173Ok
174" \
175"Ok\n" ""
Chris Metcalf208d35d2010-04-02 09:57:27 +0200176SKIP=
Denys Vlasenko8a936cf2009-12-16 23:18:59 +0100177
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700178test x"$SKIP_KNOWN_BUGS" = x"" && {
Dan Fandrich8d789e42010-09-05 16:16:46 +0200179# Needs to be run under non-root for meaningful test
180optional FEATURE_TAR_CREATE
181testing "tar writing into read-only dir" '\
182rm -rf input_* test.tar 2>/dev/null
183mkdir input_dir
184>input_dir/input_file
185chmod 550 input_dir
186tar cf test.tar input_dir
187tar tvf test.tar | sed "s/.*[0-9] input/input/"
188chmod 770 input_dir
189rm -rf input_*
190tar xf test.tar 2>&1
191echo Ok: $?
192ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
193chmod 770 input_dir
194' "\
195input_dir/
196input_dir/input_file
197Ok: 0
198-rw-r--r-- input_dir/input_file
199dr-xr-x--- input_dir
200" \
201"" ""
202SKIP=
Dan Fandrich80d80ba2010-09-11 00:28:50 -0700203}
204
Denys Vlasenkob8ab4b02011-02-06 20:02:15 +0100205# Had a bug where on extract autodetect first "switched off" -z
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100206# and then failed to recognize .tgz extension
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200207optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100208testing "tar extract tgz" "\
209dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
210tar -czf F0.tgz F0
211rm F0
212tar -xzvf F0.tgz && echo Ok
213rm F0 || echo BAD
214" "\
215F0
216Ok
217" \
218"" ""
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200219SKIP=
Denys Vlasenkoaef441c2011-02-06 20:01:11 +0100220
Denys Vlasenkobb8d7db2012-03-06 16:57:01 +0100221# Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
222# (the uuencoded hello_world.txz contains one empty file named "hello_world")
223optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
224testing "tar extract txz" "\
225uudecode -o input && tar tf input && echo Ok
226" "\
227hello_world
228Ok
229" \
230"" "\
231begin-base64 644 hello_world.txz
232/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
233SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
234Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
235AAAEWVo=
236====
237"
238SKIP=
239
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100240# On extract, everything up to and including last ".." component is stripped
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200241optional FEATURE_TAR_CREATE
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100242testing "tar strips /../ on extract" "\
243rm -rf input_* test.tar 2>/dev/null
244mkdir input_dir
245echo Ok >input_dir/file
246tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
247rm -rf input_* 2>/dev/null
248tar -vxf test.tar 2>&1
249cat input_dir/file 2>&1
250" "\
Denys Vlasenkob80acf52011-03-02 01:21:02 +0100251tar: removing leading './../tar.tempdir/input_dir/../' from member names
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100252input_dir/
253input_dir/file
254Ok
255" \
256"" ""
Denys Vlasenkob47b3ce2011-08-10 00:51:29 +0200257SKIP=
Denys Vlasenko5e29e262011-03-01 17:21:07 +0100258
Denys Vlasenkoa9607482015-10-22 16:37:01 +0200259# attack.tar.bz2 has symlink pointing to a system file
260# followed by a regular file with the same name
261# containing "root::0:0::/root:/bin/sh":
262# lrwxrwxrwx root/root passwd -> /tmp/passwd
263# -rw-r--r-- root/root passwd
264# naive tar implementation may end up creating the symlink
265# and then writing into it.
266# The correct implementation unlinks target before
267# creating the second file.
268# We test that /tmp/passwd remains empty:
269optional UUDECODE FEATURE_SEAMLESS_BZ2
270testing "tar does not extract into symlinks" "\
271>>/tmp/passwd && uudecode -o input && tar xf input 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
272" "\
2730
274" \
275"" "\
276begin-base64 644 attack.tar.bz2
277QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
278po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
279DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
280l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
281====
282"
283SKIP=
284# And same with -k
285optional UUDECODE FEATURE_SEAMLESS_BZ2
286testing "tar -k does not extract into symlinks" "\
287>>/tmp/passwd && uudecode -o input && tar xf input -k 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
288" "\
289tar: can't open 'passwd': File exists
2900
291" \
292"" "\
293begin-base64 644 attack.tar.bz2
294QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
295po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
296DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
297l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
298====
299"
300SKIP=
301
Dan Fandrich8d789e42010-09-05 16:16:46 +0200302
Denys Vlasenko02365a62010-04-09 10:52:52 +0200303cd .. && rm -rf tar.tempdir || exit 1
Denys Vlasenkobf224752009-11-29 19:09:29 +0100304
305exit $FAILCOUNT