1#!/bin/bash 2 3[ -f testing.sh ] && . testing.sh 4 5#testing "name" "command" "result" "infile" "stdin" 6 7# For reproducibility: UTC and umask 0002 8 9OLDTZ="$TZ" 10export TZ=utc 11OLDUMASK=$(umask) 12umask 0002 13 14# 255 bytes, longest VFS name 15LONG=0123456789abcdef0123456789abcdef 16LONG=$LONG$LONG$LONG$LONG$LONG$LONG$LONG$LONG 17LONG=${LONG:1:255} 18 19# Reproducible tarballs: override ownership and timestamp. 20TAR='tar c --owner root --group root --mtime @1234567890' 21 22# Different tars add variable trailing NUL padding (1024 bytes is just minimum) 23# so look at first N 512-byte frames when analyzing header content. 24function SUM() 25{ 26 tee save.dat | head -c $(($1*512)) | sha1sum | sed "s/ .*//" 27} 28 29function LST() 30{ 31 tar tv "$@" | sed 's/[ \t][ \t]*/ /g' 32} 33 34touch file 35testing "create file" "$TAR file | SUM 3" \ 36 "fecaecba936e604bb115627a6ef4db7c7a3a8f81\n" "" "" 37 38testing "pass file" "$TAR file | LST" \ 39 "-rw-rw-r-- root/root 0 2009-02-13 23:31 file\n" "" "" 40 41# The kernel has two hardwired meaningful UIDs: 0 (root) and 65534 (nobody). 42# (Technically changeable via /proc/sys/*/overflowuid but nobody ever does) 43skipnot id nobody >/dev/null 44testing "pass user" "tar -c --owner nobody:65534 --group root --mtime @0 file | LST" \ 45 "-rw-rw-r-- nobody/root 0 1970-01-01 00:00 file\n" "" "" 46# (We assume that if we have the nobody user, we also have the group, in the 47# absence of a good portable way to test for the existence of a named group.) 48skipnot id nobody >/dev/null 49testing "pass group" "tar c --owner root --group nobody:65534 --mtime @0 file | LST" \ 50 "-rw-rw-r-- root/nobody 0 1970-01-01 00:00 file\n" "" "" 51 52# Historically we output a "base 256" format that _we_ could decode but that 53# GNU tar choked on, so check the exact bytes with SUM, not a LST round trip. 54testing "huge values" "tar c --owner 9999999 --group 8888888 --mtime @0 file | SUM 3" \ 55 "396b07fd2f80eeb312462e3bfb7dc1325dc6bcfb\n" "" "" 56 57touch -t 198701231234.56 file 58testing "pass mtime" \ 59 "tar c --owner root --group root file | LST --full-time" \ 60 "-rw-rw-r-- root/root 0 1987-01-23 12:34:56 file\n" "" "" 61 62testing "adjust mode" \ 63 "tar c --owner root --group root --mode a+x file | LST --full-time" \ 64 "-rwxrwxr-x root/root 0 1987-01-23 12:34:56 file\n" "" "" 65 66mkdir dir 67testing "create dir" "$TAR dir | SUM 3" \ 68 "05739c423d7d4a7f12b3dbb7c94149acb2bb4f8d\n" "" "" 69 70testing "pass dir" "$TAR dir | LST" \ 71 "drwxrwxr-x root/root 0 2009-02-13 23:31 dir/\n" "" "" 72 73# note: does _not_ include dir entry in archive, just file 74touch dir/file 75testing "create file in dir" "$TAR dir/file | SUM 3" \ 76 "2d7b96c7025987215f5a41f10eaa84311160afdb\n" "" "" 77 78# Tests recursion without worrying about content order 79testing "create dir and dir/file" "$TAR dir | SUM 3" \ 80 "0bcc8005a3e07eb63c9b735267aecc5b774795d7\n" "" "" 81 82testing "pass dir/file" "$TAR dir | LST" \ 83 "drwxrwxr-x root/root 0 2009-02-13 23:31 dir/\n-rw-rw-r-- root/root 0 2009-02-13 23:31 dir/file\n" "" "" 84 85echo boing > dir/that 86testing "tar C" "$TAR -C dir that | SUM 3" \ 87 "f0deff71bf4858eb0c5f49d99d052f12f1831feb\n" "" "" 88 89# / and .. only stripped from name, not symlink target. 90ln -s ../name.././.. dir/link 91testing "create symlink" "$TAR dir/link | SUM 3" \ 92 "7324cafbd9aeec5036b6efc54d741f11528aeb10\n" "" "" 93 94# Also two explicit targets 95ln dir/file dir/hardlink 96testing "create hardlink" "$TAR dir/file dir/hardlink | SUM 3" \ 97 "c5383651f8c03ec0fe15e8a9e28a4e8e5273990d\n" "" "" 98 99ln dir/link dir/hlink 100testing "create hardlink to symlink" "$TAR dir/link dir/hlink | SUM 3" \ 101 "3bc16f8fb6fc8b05f691da8caf989a70ee99284a\n" "" "" 102 103skipnot mkfifo dir/fifo 2>/dev/null 104testing "create dir/fifo" "$TAR dir/fifo | SUM 3" \ 105 "bd1365db6e8ead4c813333f9666994c1899924d9\n" "" "" 106 107# test L and K records 108 109# 4+96=100 (biggest short name), 4+97=101 (shortest long name) 110touch dir/${LONG:1:96} dir/${LONG:1:97} 111testing "create long fname" "$TAR dir/${LONG:1:97} dir/${LONG:1:96} | SUM 3" \ 112 "99348686fe9c9bf80f5740f1fc0c6f32f2021e3d\n" "" "" 113 114ln -s dir/${LONG:1:96} dir/lshort 115ln -s dir/${LONG:1:97} dir/llong 116testing "create long symlnk" "$TAR dir/lshort dir/llong | SUM 3" \ 117 "8a5d652dc85f252a2e3b3f47d1ecd699e98a5f4b\n" "" "" 118 119ln -s $LONG dir/${LONG:5} 120testing "create long->long" "$TAR dir/${LONG:5} | SUM 7" \ 121 "543116b8e690a116a559ab5b673f9b6d6601c925\n" "" "" 122# absolute and relative link names, broken and not 123 124ln -s file dir/linkok 125testing "create symlink" "$TAR dir/linkok | SUM 3" \ 126 "55652846506cf0a9d43b3ef03ccf9e98123befaf\n" "" "" 127 128ln -s /dev/null dir/linknull 129testing "pass absolute symlink" "$TAR dir/linknull | LST" \ 130 "lrwxrwxrwx root/root 0 2009-02-13 23:31 dir/linknull -> /dev/null\n" "" "" 131 132ln -s rel/broken dir/relbrok 133testing "pass broken symlink" "$TAR dir/relbrok | LST" \ 134 "lrwxrwxrwx root/root 0 2009-02-13 23:31 dir/relbrok -> rel/broken\n" "" "" 135 136ln -s /does/not/exist dir/linkabsbrok 137testing "pass broken absolute symlink" "$TAR dir/linkabsbrok | LST" \ 138 "lrwxrwxrwx root/root 0 2009-02-13 23:31 dir/linkabsbrok -> /does/not/exist\n" \ 139 "" "" 140 141# this expects devtmpfs values 142 143testing "pass /dev/null" \ 144 "tar c --mtime @0 /dev/null 2>/dev/null | LST" \ 145 "crw-rw-rw- root/root 1,3 1970-01-01 00:00 dev/null\n" "" "" 146testing "--absolute-names" \ 147 "tar c --mtime @0 --absolute-names /dev/null 2>/dev/null | LST" \ 148 "crw-rw-rw- root/root 1,3 1970-01-01 00:00 /dev/null\n" "" "" 149 150# compression types 151testing "autodetect gzip" 'LST -f "$FILES"/tar/tar.tgz' \ 152 "drwxr-x--- enh/eng 0 2017-05-13 01:05 dir/\n-rw-r----- enh/eng 12 2017-05-13 01:05 dir/file\n" \ 153 "" "" 154 155testing "manually specify bz2" 'LST -jf "$FILES"/tar/tar.tbz2' \ 156 "drwxr-x--- enh/eng 0 2017-05-13 01:05 dir/\n-rw-r----- enh/eng 12 2017-05-13 01:05 dir/file\n" \ 157 "" "" 158 159# -I 160testing "-I gzip c" "$TAR -Igzip file | file - | grep -o 'gzip compressed'" \ 161 "gzip compressed\n" "" "" 162testing "-I gzip t" 'LST -Igzip -f "$FILES"/tar/tar.tgz' \ 163 "drwxr-x--- enh/eng 0 2017-05-13 01:05 dir/\n-rw-r----- enh/eng 12 2017-05-13 01:05 dir/file\n" \ 164 "" "" 165 166skipnot mknod dir/char c 12 34 2>/dev/null 167testing "character special" "tar --mtime @0 -cf test.tar dir/char && rm -f dir/char && tar xf test.tar && ls -l dir/char" \ 168 "crw-rw---- 1 root root 12, 34 1970-01-01 00:00 dir/char\n" "" "" 169 170skipnot mknod dir/block b 23 45 2>/dev/null 171testing "block special" "tar --mtime @0 -cf test.tar dir/block && rm -f dir/block && tar xf test.tar && ls -l dir/block" \ 172 "brw-rw---- 1 root root 23, 45 1970-01-01 00:00 dir/block\n" "" "" 173 174skipnot chown nobody dir/file 2>/dev/null 175testing "ownership" "$TAR dir/file | SUM 3" \ 176 "2d7b96c7025987215f5a41f10eaa84311160afdb\n" "" "" 177 178mkdir -p dd/sub/blah && 179tar cf test.tar dd/sub/blah && 180rm -rf dd/sub && 181ln -s ../.. dd/sub || SKIPNEXT=1 182toyonly testing "symlink out of cwd" \ 183 "tar xf test.tar 2> /dev/null || echo yes ; [ ! -e dd/sub/blah ] && echo yes" \ 184 "yes\nyes\n" "" "" 185 186# If not root can't preserve ownership, so don't try yet. 187 188testing "extract dir/file from tar" \ 189 "tar xvCf dd $FILES/tar/tar.tar && stat -c '%A %Y %n' dd/dir dd/dir/file" \ 190 "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ 191 "" "" 192 193testing "extract dir/file from tgz (autodetect)" \ 194 "tar xvCf dd $FILES/tar/tar.tgz && stat -c '%A %Y %n' dd/dir dd/dir/file" \ 195 "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ 196 "" "" 197 198toyonly testing "cat tgz | extract dir/file (autodetect)" \ 199 "cat $FILES/tar/tar.tgz | tar xvC dd && stat -c '%A %Y %n' dd/dir dd/dir/file" \ 200 "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ 201 "" "" 202 203testing "extract dir/file from tbz2 (autodetect)" \ 204 "tar xvCf dd $FILES/tar/tar.tbz2 && stat -c '%A %Y %n' dd/dir dd/dir/file" \ 205 "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ 206 "" "" 207 208toyonly testing "cat tbz | extract dir/file (autodetect)" \ 209 "cat $FILES/tar/tar.tbz2 | tar xvC dd && stat -c '%A %Y %n' dd/dir dd/dir/file" \ 210 "dir/\ndir/file\ndrwxr-x--- 1494637555 dd/dir\n-rw-r----- 1494637555 dd/dir/file\n" \ 211 "" "" 212 213yes | head -n $((1<<18)) > bang 214{ dd bs=$((1<<16)) count=1 status=none; dd bs=8192 seek=14 count=1 status=none; dd bs=4096 seek=64 count=5 status=none; } < bang > fweep 215testing "sparse without overflow" "$TAR --sparse fweep | SUM 3" \ 216 "e1560110293247934493626d564c8f03c357cec5\n" "" "" 217rm bang fweep 218 219for i in 1 3 5 7 9 14 27 36 128 256 300 304 220do 221 dd if=/dev/zero of=fweep bs=65536 seek=$i count=1 2>/dev/null 222done 223 224testing "sparse single overflow" "$TAR --sparse fweep | SUM 6" \ 225 "063fc6519ea2607763bc591cc90dd15ac2b43eb8\n" "" "" 226 227rm fweep 228for i in $(seq 8 3 200) 229do 230 dd if=/dev/zero of=fweep bs=65536 seek=$i count=1 2>/dev/null 231 dd if=/dev/zero of=fweep2 bs=65536 seek=$i count=1 2>/dev/null 232done 233truncate -s 20m fweep2 234 235testing "sparse double overflow" "$TAR --sparse fweep | SUM 7" \ 236 "f1fe57f8313a9d682ec9013a80f3798910b6ff51\n" "" "" 237 238tar c --sparse fweep > fweep.tar 239rm fweep 240testing "sparse extract" "tar xf fweep.tar && $TAR --sparse fweep | SUM 4" \ 241 "38dc57b8b95632a287db843c214b5c96d1cfe415\n" "" "" 242testing "sparse tvf" "tar tvf fweep.tar | grep -wq 13172736 && echo right size"\ 243 "right size\n" "" "" 244rm fweep fweep.tar 245 246tar c --sparse fweep2 > fweep2.tar 247rm fweep2 248testing "sparse extract hole at end" \ 249 "tar xf fweep2.tar && $TAR --sparse fweep2 | SUM 4" \ 250 "791060574c569e5c059e2b90c1961a3575898f97\n" "" "" 251rm fweep2 fweep2.tar 252 253testcmd "longname" "tf $FILES/tar/long_path.tar" \ 254 "$(printf 'long file name%86cTRAILING' ' ' | tr ' ' _)\n" "" "" 255 256mkdir -p links 257touch links/orig 258ln links/{orig,link1} 259ln links/{orig,link2} 260testcmd 'links' '-cf test.tar links' '' '' '' 261rm -rf links 262 263mkdir links 264for i in {0..12}; do touch links/orig$i; ln links/{orig,link}$i; done 265testcmd 'links2' '-cf test.tar links' '' '' '' 266rm -rf links 267 268install -m 000 -d folder/skip/oof && 269testcmd 'exclude' '--exclude skip -cvf tar.tar folder && echo yes' \ 270 'folder/\nyes\n' '' '' 271rm -rf folder tar.tar 272 273mkdir -p one/two; echo hello > one/two/three; tar czf test.tar one/two/three 274rm one/two/three; mkdir one/two/three 275testcmd 'replace dir with file' '-xf test.tar && cat one/two/three' \ 276 'hello\n' '' '' 277rm -rf one test.tar 278 279mkdir ..dotsdir 280testing "create ..dotsdir" "$TAR ..dotsdir | SUM 3" \ 281 "de99091a91c74ef6b90093e9165b413670730572\n" "" "" 282 283testing "pass ..dotsdir" "$TAR ..dotsdir | LST" \ 284 "drwxrwxr-x root/root 0 2009-02-13 23:31 ..dotsdir/\n" "" "" 285rmdir ..dotsdir 286 287if false 288then 289 290chmod 700 dir 291tar cpf tar.tgz dir/file 292#chmod 700 dir 293#tar xpf file 294#ls -ld dir/file 295 296# restore ownership of file, dir, and symlink 297 298# merge add_to_tar and write_longname, 299# filter, incl or excl and anchored/wildcards 300 301# extract file not under cwd 302# exclusion defaults to --no-anchored and --wildcards-match-slash 303# both incl and excl 304 305# catch symlink overwrite 306# add dir with no trailing slash 307# don't allow hardlink target outside cwd 308# extract dir/file without dir in tarball 309# create with and without each flag 310# --owner --group --numeric-owner 311# extract with and without each flag 312# --owner 0 --group 0 313# set symlink owner 314# >256 hardlink inodes 315# // remove leading / and any .. entries from saved name 316# // exclusion defaults to --no-anchored and --wildcards-match-slash 317# //bork blah../thing blah/../thing blah/../and/../that blah/.. ../blah 318# tar tv --owner --group --mtime 319# extract file within dir date correct 320# name ending in /.. or just ".." as a name 321 322 323fi 324 325TZ="$OLDTZ" 326umask $OLDUMASK 327unset LONG TAR SUM OLDUMASK OLDTZ 328unset -f LST 329rm save.dat 330