unexpand: correct behavior for --first-only --tabs=4
[oweals/busybox.git] / testsuite / tar.tests
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
7 unset LANG
8 unset LANGUAGE
9 unset LC_COLLATE
10 unset LC_ALL
11 umask 022
12
13 # testing "test name" "script" "expected result" "file input" "stdin"
14
15 testing "Empty file is not a tarball" '\
16 tar xvf - 2>&1; echo $?
17 ' "\
18 tar: short read
19 1
20 " \
21 "" ""
22 SKIP=
23
24 optional FEATURE_SEAMLESS_GZ GUNZIP
25 # In NOMMU case, "invalid magic" message comes from gunzip child process.
26 # Otherwise, it comes from tar.
27 # Need to fix output up to avoid false positive.
28 testing "Empty file is not a tarball.tar.gz" '\
29 { tar xvzf - 2>&1; echo $?; } | grep -Fv "invalid magic"
30 ' "\
31 tar: short read
32 1
33 " \
34 "" ""
35 SKIP=
36
37 testing "Two zeroed blocks is a ('truncated') empty tarball" '\
38 dd if=/dev/zero bs=512 count=2 2>/dev/null | tar xvf - 2>&1; echo $?
39 ' "\
40 0
41 " \
42 "" ""
43 SKIP=
44
45 testing "Twenty zeroed blocks is an empty tarball" '\
46 dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $?
47 ' "\
48 0
49 " \
50 "" ""
51 SKIP=
52
53 mkdir tar.tempdir && cd tar.tempdir || exit 1
54 # "tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input":
55 # GNU tar 1.26 records as hardlinks:
56 #  input_hard2 -> input_hard1
57 #  input_hard1 -> input_hard1 (!!!)
58 #  input_dir/file -> input_dir/file
59 #  input -> input
60 # As of 1.24.0, we don't record last two: for them, nlink==1
61 # and we check for "hardlink"ness only files with nlink!=1
62 # We also don't use "hrw-r--r--" notation for hardlinks in "tar tv" listing.
63 optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
64 testing "tar hardlinks and repeated files" '\
65 >input_hard1
66 ln input_hard1 input_hard2
67 mkdir input_dir
68 >input_dir/file
69 chmod -R 644 *
70 chmod    755 input_dir
71 tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
72 tar tvf test.tar | sed "s/.*[0-9] input/input/"
73 rm -rf input_dir
74 tar xf test.tar 2>&1
75 echo Ok: $?
76 ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
77 ' "\
78 input
79 input_dir/
80 input_dir/file
81 input_hard1
82 input_hard2 -> input_hard1
83 input_hard1 -> input_hard1
84 input_dir/
85 input_dir/file
86 input
87 Ok: 0
88 -rw-r--r-- input_dir/file
89 drwxr-xr-x input_dir
90 -rw-r--r-- input_hard1
91 -rw-r--r-- input_hard2
92 " \
93 "" ""
94 SKIP=
95 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
96
97 mkdir tar.tempdir && cd tar.tempdir || exit 1
98 optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
99 testing "tar hardlinks mode" '\
100 >input_hard1
101 chmod 741 input_hard1
102 ln input_hard1 input_hard2
103 mkdir input_dir
104 ln input_hard1 input_dir
105 ln input_hard2 input_dir
106 chmod 550 input_dir
107 # On some filesystems, input_dir/input_hard2 is returned by readdir
108 # BEFORE input_dir/input_hard1! Thats why we cant just "tar cf ... input_*":
109 tar cf test.tar input_dir/input_hard* input_hard*
110 tar tvf test.tar | sed "s/.*[0-9] input/input/"
111 chmod 770 input_dir
112 rm -rf input_*
113 tar xf test.tar 2>&1
114 echo Ok: $?
115 ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
116 ' "\
117 input_dir/input_hard1
118 input_dir/input_hard2 -> input_dir/input_hard1
119 input_hard1 -> input_dir/input_hard1
120 input_hard2 -> input_dir/input_hard1
121 Ok: 0
122 -rwxr----x input_dir/input_hard1
123 -rwxr----x input_dir/input_hard2
124 -rwxr----x input_hard1
125 -rwxr----x input_hard2
126 " \
127 "" ""
128 SKIP=
129 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
130
131 mkdir tar.tempdir && cd tar.tempdir || exit 1
132 optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
133 testing "tar symlinks mode" '\
134 >input_file
135 chmod 741 input_file
136 ln -s input_file input_soft
137 mkdir input_dir
138 ln input_file input_dir
139 ln input_soft input_dir
140 chmod 550 input_dir
141 tar cf test.tar input_dir/* input_[fs]*
142 tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
143 chmod 770 input_dir
144 rm -rf input_*
145 tar xf test.tar 2>&1
146 echo Ok: $?
147 ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
148 ' "\
149 input_dir/input_file
150 input_dir/input_soft -> input_file
151 input_file -> input_dir/input_file
152 input_soft -> input_dir/input_soft
153 Ok: 0
154 -rwxr----x input_dir/input_file
155 lrwxrwxrwx input_file
156 -rwxr----x input_file
157 lrwxrwxrwx input_file
158 " \
159 "" ""
160 SKIP=
161 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
162
163 mkdir tar.tempdir && cd tar.tempdir || exit 1
164 optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
165 testing "tar --overwrite" "\
166 ln input input_hard
167 tar cf test.tar input_hard
168 echo WRONG >input
169 # --overwrite opens 'input_hard' without unlinking,
170 # thus 'input_hard' still linked to 'input' and we write 'Ok' into it
171 tar xf test.tar --overwrite 2>&1 && cat input
172 " "\
173 Ok
174 " \
175 "Ok\n" ""
176 SKIP=
177 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
178
179 mkdir tar.tempdir && cd tar.tempdir || exit 1
180 test x"$SKIP_KNOWN_BUGS" = x"" && {
181 # Needs to be run under non-root for meaningful test
182 optional FEATURE_TAR_CREATE
183 testing "tar writing into read-only dir" '\
184 mkdir input_dir
185 >input_dir/input_file
186 chmod 550 input_dir
187 tar cf test.tar input_dir
188 tar tvf test.tar | sed "s/.*[0-9] input/input/"
189 chmod 770 input_dir
190 rm -rf input_*
191 tar xf test.tar 2>&1
192 echo Ok: $?
193 ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
194 chmod 770 input_dir
195 ' "\
196 input_dir/
197 input_dir/input_file
198 Ok: 0
199 -rw-r--r-- input_dir/input_file
200 dr-xr-x--- input_dir
201 " \
202 "" ""
203 SKIP=
204 }
205 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
206
207 mkdir tar.tempdir && cd tar.tempdir || exit 1
208 # Had a bug where on extract autodetect first "switched off" -z
209 # and then failed to recognize .tgz extension
210 optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ GUNZIP
211 testing "tar extract tgz" "\
212 dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
213 tar -czf F0.tgz F0
214 rm F0
215 tar -xzvf F0.tgz && echo Ok
216 rm F0 || echo BAD
217 " "\
218 F0
219 Ok
220 " \
221 "" ""
222 SKIP=
223 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
224
225 mkdir tar.tempdir && cd tar.tempdir || exit 1
226 # Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
227 # (the uuencoded hello_world.txz contains one empty file named "hello_world")
228 optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
229 testing "tar extract txz" "\
230 uudecode -o input && tar tf input && echo Ok
231 " "\
232 hello_world
233 Ok
234 " \
235 "" "\
236 begin-base64 644 hello_world.txz
237 /Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
238 SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
239 Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
240 AAAEWVo=
241 ====
242 "
243 SKIP=
244 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
245
246 mkdir tar.tempdir && cd tar.tempdir || exit 1
247 # On extract, everything up to and including last ".." component is stripped
248 optional FEATURE_TAR_CREATE
249 testing "tar strips /../ on extract" "\
250 rm -rf input_* test.tar 2>/dev/null
251 mkdir input_dir
252 echo Ok >input_dir/file
253 tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
254 rm -rf input_* 2>/dev/null
255 tar -vxf test.tar 2>&1
256 cat input_dir/file 2>&1
257 " "\
258 tar: removing leading './../tar.tempdir/input_dir/../' from member names
259 input_dir/
260 input_dir/file
261 Ok
262 " \
263 "" ""
264 SKIP=
265 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
266
267 mkdir tar.tempdir && cd tar.tempdir || exit 1
268 # attack.tar.bz2 has symlink pointing to a system file
269 # followed by a regular file with the same name
270 # containing "root::0:0::/root:/bin/sh":
271 #  lrwxrwxrwx root/root passwd -> /tmp/passwd
272 #  -rw-r--r-- root/root passwd
273 # naive tar implementation may end up creating the symlink
274 # and then writing into it.
275 # The correct implementation unlinks target before
276 # creating the second file.
277 # We test that /tmp/passwd remains empty:
278 optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_BZ2
279 testing "tar does not extract into symlinks" "\
280 >>/tmp/passwd && uudecode -o input && tar xf input 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
281 " "\
282 tar: can't create symlink 'passwd' to '/tmp/passwd'
283 0
284 " \
285 "" "\
286 begin-base64 644 attack.tar.bz2
287 QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
288 po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
289 DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
290 l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
291 ====
292 "
293 SKIP=
294 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
295
296 mkdir tar.tempdir && cd tar.tempdir || exit 1
297 # And same with -k
298 optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_BZ2
299 testing "tar -k does not extract into symlinks" "\
300 >>/tmp/passwd && uudecode -o input && tar xf input -k 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
301 " "\
302 tar: can't create symlink 'passwd' to '/tmp/passwd'
303 0
304 " \
305 "" "\
306 begin-base64 644 attack.tar.bz2
307 QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
308 po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
309 DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
310 l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
311 ====
312 "
313 SKIP=
314 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
315
316 mkdir tar.tempdir && cd tar.tempdir || exit 1
317 optional UNICODE_SUPPORT FEATURE_TAR_GNU_EXTENSIONS FEATURE_SEAMLESS_BZ2 FEATURE_TAR_AUTODETECT
318 testing "Pax-encoded UTF8 names and symlinks" '\
319 tar xvf ../tar.utf8.tar.bz2 2>&1; echo $?
320 export LANG=en_US.UTF-8
321 ls -l etc/ssl/certs/* | sed "s:.*etc/:etc/:" | sort
322 unset LANG
323 rm -rf etc usr
324 ' "\
325 etc/ssl/certs/3b2716e5.0
326 etc/ssl/certs/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem
327 etc/ssl/certs/f80cc7f6.0
328 usr/share/ca-certificates/mozilla/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.crt
329 0
330 etc/ssl/certs/3b2716e5.0 -> EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem
331 etc/ssl/certs/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem -> /usr/share/ca-certificates/mozilla/EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.crt
332 etc/ssl/certs/f80cc7f6.0 -> EBG_Elektronik_Sertifika_Hizmet_Sağlayıcısı.pem
333 " \
334 "" ""
335 SKIP=
336 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
337
338 mkdir tar.tempdir && cd tar.tempdir || exit 1
339 optional FEATURE_SEAMLESS_BZ2 FEATURE_TAR_AUTODETECT LS
340 testing "Symlink attack: create symlink and then write through it" '\
341 exec 2>&1
342 uudecode -o input && tar xvf input; echo $?
343 ls /tmp/bb_test_evilfile
344 ls bb_test_evilfile
345 ls symlink/bb_test_evilfile
346 ' "\
347 anything.txt
348 symlink
349 symlink/bb_test_evilfile
350 tar: can't create symlink 'symlink' to '/tmp'
351 1
352 ls: /tmp/bb_test_evilfile: No such file or directory
353 ls: bb_test_evilfile: No such file or directory
354 symlink/bb_test_evilfile
355 " \
356 "" "\
357 begin-base64 644 tar_symlink_attack.tar.bz2
358 QlpoOTFBWSZTWZgs7bQAALT/hMmQAFBAAf+AEMAGJPPv32AAAIAIMAC5thlR
359 omAjAmCMADQT1BqNE0AEwAAjAEwElTKeo9NTR6h6gaeoA0DQNLVdwZZ5iNTk
360 AQwCAV6S00QFJYhrlfFkVCEDEGtgNVqYrI0uK3ggnt30gqk4e1TTQm5QIAKa
361 SJqzRGSFLMmOloHSAcvLiFxxRiQtQZF+qPxbo173ZDISOAoNoPN4PQPhBhKS
362 n8fYaKlioCTzL2oXYczyUUIP4u5IpwoSEwWdtoA=
363 ====
364 "
365 SKIP=
366 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
367
368 mkdir tar.tempdir && cd tar.tempdir || exit 1
369 optional FEATURE_TAR_CREATE
370 testing "Symlinks and hardlinks coexist" '\
371 mkdir dir
372 >dir/a
373 ln -s ../dir/a dir/b
374 ln dir/b dir/c
375 mkdir new
376 tar cf - dir/* | tar -C new -xvf - 2>&1
377 ' "\
378 dir/a
379 dir/b
380 dir/c
381 " \
382 "" ""
383 SKIP=
384 cd .. || exit 1; rm -rf tar.tempdir 2>/dev/null
385
386 exit $FAILCOUNT