Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright 2009 by Denys Vlasenko |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | # Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 4 | |
| 5 | . ./testing.sh |
| 6 | |
Dan Fandrich | 775965d | 2010-08-10 23:33:57 -0700 | [diff] [blame] | 7 | unset LANG |
| 8 | unset LANGUAGE |
| 9 | unset LC_COLLATE |
| 10 | unset LC_ALL |
| 11 | umask 022 |
| 12 | |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 13 | rm -rf tar.tempdir 2>/dev/null |
| 14 | mkdir tar.tempdir && cd tar.tempdir || exit 1 |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 15 | |
| 16 | # testing "test name" "script" "expected result" "file input" "stdin" |
| 17 | |
Denys Vlasenko | 0545e3b | 2013-11-19 17:17:48 +0100 | [diff] [blame] | 18 | testing "Empty file is not a tarball" '\ |
| 19 | tar xvf - 2>&1; echo $? |
| 20 | ' "\ |
| 21 | tar: short read |
| 22 | 1 |
| 23 | " \ |
| 24 | "" "" |
| 25 | SKIP= |
| 26 | |
Denys Vlasenko | 1cbc642 | 2013-11-19 14:52:02 +0100 | [diff] [blame] | 27 | optional FEATURE_SEAMLESS_GZ |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 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. |
Denys Vlasenko | dd1d8ab | 2013-11-19 16:56:26 +0100 | [diff] [blame] | 31 | testing "Empty file is not a tarball.tar.gz" '\ |
Denys Vlasenko | 198b02f | 2013-12-31 23:22:36 +0100 | [diff] [blame] | 32 | { tar xvzf - 2>&1; echo $?; } | grep -Fv "invalid magic" |
Denys Vlasenko | 1cbc642 | 2013-11-19 14:52:02 +0100 | [diff] [blame] | 33 | ' "\ |
Denys Vlasenko | 1cbc642 | 2013-11-19 14:52:02 +0100 | [diff] [blame] | 34 | tar: short read |
| 35 | 1 |
| 36 | " \ |
| 37 | "" "" |
| 38 | SKIP= |
| 39 | |
Denys Vlasenko | 0545e3b | 2013-11-19 17:17:48 +0100 | [diff] [blame] | 40 | testing "Two zeroed blocks is a ('truncated') empty tarball" '\ |
Denys Vlasenko | 7625811 | 2013-12-31 23:25:46 +0100 | [diff] [blame] | 41 | dd if=/dev/zero bs=512 count=2 2>/dev/null | tar xvf - 2>&1; echo $? |
Denys Vlasenko | 0545e3b | 2013-11-19 17:17:48 +0100 | [diff] [blame] | 42 | ' "\ |
| 43 | 0 |
| 44 | " \ |
| 45 | "" "" |
| 46 | SKIP= |
| 47 | |
| 48 | testing "Twenty zeroed blocks is an empty tarball" '\ |
| 49 | dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $? |
| 50 | ' "\ |
| 51 | 0 |
| 52 | " \ |
| 53 | "" "" |
| 54 | SKIP= |
| 55 | |
Denys Vlasenko | 6c563e3 | 2015-10-22 01:07:13 +0200 | [diff] [blame] | 56 | # "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 Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 65 | optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 66 | testing "tar hardlinks and repeated files" '\ |
Denys Vlasenko | 425ad9c | 2009-12-16 22:46:01 +0100 | [diff] [blame] | 67 | rm -rf input_* test.tar 2>/dev/null |
| 68 | >input_hard1 |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 69 | ln input_hard1 input_hard2 |
| 70 | mkdir input_dir |
| 71 | >input_dir/file |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 72 | chmod -R 644 * |
| 73 | chmod 755 input_dir |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 74 | tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 75 | tar tvf test.tar | sed "s/.*[0-9] input/input/" |
Denys Vlasenko | 6c563e3 | 2015-10-22 01:07:13 +0200 | [diff] [blame] | 76 | rm -rf input_dir |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 77 | tar xf test.tar 2>&1 |
| 78 | echo Ok: $? |
| 79 | ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/" |
| 80 | ' "\ |
Denys Vlasenko | 425ad9c | 2009-12-16 22:46:01 +0100 | [diff] [blame] | 81 | input |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 82 | input_dir/ |
| 83 | input_dir/file |
| 84 | input_hard1 |
| 85 | input_hard2 -> input_hard1 |
| 86 | input_hard1 -> input_hard1 |
| 87 | input_dir/ |
| 88 | input_dir/file |
| 89 | input |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 90 | Ok: 0 |
| 91 | -rw-r--r-- input_dir/file |
| 92 | drwxr-xr-x input_dir |
| 93 | -rw-r--r-- input_hard1 |
| 94 | -rw-r--r-- input_hard2 |
| 95 | " \ |
| 96 | "" "" |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 97 | SKIP= |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 98 | |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 99 | optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 100 | testing "tar hardlinks mode" '\ |
| 101 | rm -rf input_* test.tar 2>/dev/null |
| 102 | >input_hard1 |
| 103 | chmod 741 input_hard1 |
| 104 | ln input_hard1 input_hard2 |
| 105 | mkdir input_dir |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 106 | ln input_hard1 input_dir |
| 107 | ln input_hard2 input_dir |
Dan Fandrich | eb2bf5b | 2010-09-02 18:38:00 -0700 | [diff] [blame] | 108 | chmod 550 input_dir |
Denys Vlasenko | 0d7cb4c | 2010-09-03 17:22:56 +0200 | [diff] [blame] | 109 | # 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_*": |
| 111 | tar cf test.tar input_dir/input_hard* input_hard* |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 112 | tar tvf test.tar | sed "s/.*[0-9] input/input/" |
Dan Fandrich | eb2bf5b | 2010-09-02 18:38:00 -0700 | [diff] [blame] | 113 | chmod 770 input_dir |
| 114 | rm -rf input_* |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 115 | tar xf test.tar 2>&1 |
| 116 | echo Ok: $? |
Denys Vlasenko | 0d7cb4c | 2010-09-03 17:22:56 +0200 | [diff] [blame] | 117 | ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/" |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 118 | ' "\ |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 119 | input_dir/input_hard1 |
| 120 | input_dir/input_hard2 -> input_dir/input_hard1 |
| 121 | input_hard1 -> input_dir/input_hard1 |
| 122 | input_hard2 -> input_dir/input_hard1 |
| 123 | Ok: 0 |
| 124 | -rwxr----x input_dir/input_hard1 |
| 125 | -rwxr----x input_dir/input_hard2 |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 126 | -rwxr----x input_hard1 |
| 127 | -rwxr----x input_hard2 |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 128 | " \ |
| 129 | "" "" |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 130 | SKIP= |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 131 | |
Denys Vlasenko | bfa1b2e | 2010-05-11 03:53:57 +0200 | [diff] [blame] | 132 | optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 133 | testing "tar symlinks mode" '\ |
| 134 | rm -rf input_* test.tar 2>/dev/null |
| 135 | >input_file |
| 136 | chmod 741 input_file |
| 137 | ln -s input_file input_soft |
| 138 | mkdir input_dir |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 139 | ln input_file input_dir |
| 140 | ln input_soft input_dir |
Dan Fandrich | eb2bf5b | 2010-09-02 18:38:00 -0700 | [diff] [blame] | 141 | chmod 550 input_dir |
Dan Fandrich | 80d80ba | 2010-09-11 00:28:50 -0700 | [diff] [blame] | 142 | tar cf test.tar input_dir/* input_[fs]* |
Denys Vlasenko | e82cf33 | 2010-05-12 15:59:32 +0200 | [diff] [blame] | 143 | tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort |
Dan Fandrich | eb2bf5b | 2010-09-02 18:38:00 -0700 | [diff] [blame] | 144 | chmod 770 input_dir |
| 145 | rm -rf input_* |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 146 | tar xf test.tar 2>&1 |
| 147 | echo Ok: $? |
Dan Fandrich | 80d80ba | 2010-09-11 00:28:50 -0700 | [diff] [blame] | 148 | ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/" |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 149 | ' "\ |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 150 | input_dir/input_file |
| 151 | input_dir/input_soft -> input_file |
| 152 | input_file -> input_dir/input_file |
| 153 | input_soft -> input_dir/input_soft |
| 154 | Ok: 0 |
| 155 | -rwxr----x input_dir/input_file |
| 156 | lrwxrwxrwx input_file |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 157 | -rwxr----x input_file |
| 158 | lrwxrwxrwx input_file |
| 159 | " \ |
| 160 | "" "" |
Denys Vlasenko | e3d90a9 | 2010-05-10 05:53:16 +0200 | [diff] [blame] | 161 | SKIP= |
Denys Vlasenko | e69ad87 | 2010-04-09 14:11:45 +0200 | [diff] [blame] | 162 | |
Denys Vlasenko | da13824 | 2010-05-11 12:02:48 +0200 | [diff] [blame] | 163 | optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS |
Denys Vlasenko | 8a936cf | 2009-12-16 23:18:59 +0100 | [diff] [blame] | 164 | testing "tar --overwrite" "\ |
| 165 | rm -rf input_* test.tar 2>/dev/null |
| 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" "" |
Chris Metcalf | 208d35d | 2010-04-02 09:57:27 +0200 | [diff] [blame] | 176 | SKIP= |
Denys Vlasenko | 8a936cf | 2009-12-16 23:18:59 +0100 | [diff] [blame] | 177 | |
Dan Fandrich | 80d80ba | 2010-09-11 00:28:50 -0700 | [diff] [blame] | 178 | test x"$SKIP_KNOWN_BUGS" = x"" && { |
Dan Fandrich | 8d789e4 | 2010-09-05 16:16:46 +0200 | [diff] [blame] | 179 | # Needs to be run under non-root for meaningful test |
| 180 | optional FEATURE_TAR_CREATE |
| 181 | testing "tar writing into read-only dir" '\ |
| 182 | rm -rf input_* test.tar 2>/dev/null |
| 183 | mkdir input_dir |
| 184 | >input_dir/input_file |
| 185 | chmod 550 input_dir |
| 186 | tar cf test.tar input_dir |
| 187 | tar tvf test.tar | sed "s/.*[0-9] input/input/" |
| 188 | chmod 770 input_dir |
| 189 | rm -rf input_* |
| 190 | tar xf test.tar 2>&1 |
| 191 | echo Ok: $? |
| 192 | ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/" |
| 193 | chmod 770 input_dir |
| 194 | ' "\ |
| 195 | input_dir/ |
| 196 | input_dir/input_file |
| 197 | Ok: 0 |
| 198 | -rw-r--r-- input_dir/input_file |
| 199 | dr-xr-x--- input_dir |
| 200 | " \ |
| 201 | "" "" |
| 202 | SKIP= |
Dan Fandrich | 80d80ba | 2010-09-11 00:28:50 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Denys Vlasenko | b8ab4b0 | 2011-02-06 20:02:15 +0100 | [diff] [blame] | 205 | # Had a bug where on extract autodetect first "switched off" -z |
Denys Vlasenko | aef441c | 2011-02-06 20:01:11 +0100 | [diff] [blame] | 206 | # and then failed to recognize .tgz extension |
Denys Vlasenko | b47b3ce | 2011-08-10 00:51:29 +0200 | [diff] [blame] | 207 | optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ |
Denys Vlasenko | aef441c | 2011-02-06 20:01:11 +0100 | [diff] [blame] | 208 | testing "tar extract tgz" "\ |
| 209 | dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null |
| 210 | tar -czf F0.tgz F0 |
| 211 | rm F0 |
| 212 | tar -xzvf F0.tgz && echo Ok |
| 213 | rm F0 || echo BAD |
| 214 | " "\ |
| 215 | F0 |
| 216 | Ok |
| 217 | " \ |
| 218 | "" "" |
Denys Vlasenko | b47b3ce | 2011-08-10 00:51:29 +0200 | [diff] [blame] | 219 | SKIP= |
Denys Vlasenko | aef441c | 2011-02-06 20:01:11 +0100 | [diff] [blame] | 220 | |
Denys Vlasenko | bb8d7db | 2012-03-06 16:57:01 +0100 | [diff] [blame] | 221 | # 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") |
| 223 | optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ |
| 224 | testing "tar extract txz" "\ |
| 225 | uudecode -o input && tar tf input && echo Ok |
| 226 | " "\ |
| 227 | hello_world |
| 228 | Ok |
| 229 | " \ |
| 230 | "" "\ |
| 231 | begin-base64 644 hello_world.txz |
| 232 | /Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ |
| 233 | SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2 |
| 234 | Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA |
| 235 | AAAEWVo= |
| 236 | ==== |
| 237 | " |
| 238 | SKIP= |
| 239 | |
Denys Vlasenko | 5e29e26 | 2011-03-01 17:21:07 +0100 | [diff] [blame] | 240 | # On extract, everything up to and including last ".." component is stripped |
Denys Vlasenko | b47b3ce | 2011-08-10 00:51:29 +0200 | [diff] [blame] | 241 | optional FEATURE_TAR_CREATE |
Denys Vlasenko | 5e29e26 | 2011-03-01 17:21:07 +0100 | [diff] [blame] | 242 | testing "tar strips /../ on extract" "\ |
| 243 | rm -rf input_* test.tar 2>/dev/null |
| 244 | mkdir input_dir |
| 245 | echo Ok >input_dir/file |
| 246 | tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1 |
| 247 | rm -rf input_* 2>/dev/null |
| 248 | tar -vxf test.tar 2>&1 |
| 249 | cat input_dir/file 2>&1 |
| 250 | " "\ |
Denys Vlasenko | b80acf5 | 2011-03-02 01:21:02 +0100 | [diff] [blame] | 251 | tar: removing leading './../tar.tempdir/input_dir/../' from member names |
Denys Vlasenko | 5e29e26 | 2011-03-01 17:21:07 +0100 | [diff] [blame] | 252 | input_dir/ |
| 253 | input_dir/file |
| 254 | Ok |
| 255 | " \ |
| 256 | "" "" |
Denys Vlasenko | b47b3ce | 2011-08-10 00:51:29 +0200 | [diff] [blame] | 257 | SKIP= |
Denys Vlasenko | 5e29e26 | 2011-03-01 17:21:07 +0100 | [diff] [blame] | 258 | |
Denys Vlasenko | a960748 | 2015-10-22 16:37:01 +0200 | [diff] [blame] | 259 | # 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: |
| 269 | optional UUDECODE FEATURE_SEAMLESS_BZ2 |
| 270 | testing "tar does not extract into symlinks" "\ |
| 271 | >>/tmp/passwd && uudecode -o input && tar xf input 2>&1 && rm passwd; cat /tmp/passwd; echo \$? |
| 272 | " "\ |
| 273 | 0 |
| 274 | " \ |
| 275 | "" "\ |
| 276 | begin-base64 644 attack.tar.bz2 |
| 277 | QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0 |
| 278 | po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL |
| 279 | DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4 |
| 280 | l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI= |
| 281 | ==== |
| 282 | " |
| 283 | SKIP= |
| 284 | # And same with -k |
| 285 | optional UUDECODE FEATURE_SEAMLESS_BZ2 |
| 286 | testing "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 | " "\ |
| 289 | tar: can't open 'passwd': File exists |
| 290 | 0 |
| 291 | " \ |
| 292 | "" "\ |
| 293 | begin-base64 644 attack.tar.bz2 |
| 294 | QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0 |
| 295 | po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL |
| 296 | DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4 |
| 297 | l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI= |
| 298 | ==== |
| 299 | " |
| 300 | SKIP= |
| 301 | |
Dan Fandrich | 8d789e4 | 2010-09-05 16:16:46 +0200 | [diff] [blame] | 302 | |
Denys Vlasenko | 02365a6 | 2010-04-09 10:52:52 +0200 | [diff] [blame] | 303 | cd .. && rm -rf tar.tempdir || exit 1 |
Denys Vlasenko | bf22475 | 2009-11-29 19:09:29 +0100 | [diff] [blame] | 304 | |
| 305 | exit $FAILCOUNT |