tar.tests 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #!/bin/sh
  2. # Copyright 2009 by Denys Vlasenko
  3. # Licensed under GPLv2, see file LICENSE in this source tree.
  4. . ./testing.sh
  5. unset LANG
  6. unset LANGUAGE
  7. unset LC_COLLATE
  8. unset LC_ALL
  9. umask 022
  10. rm -rf tar.tempdir 2>/dev/null
  11. mkdir tar.tempdir && cd tar.tempdir || exit 1
  12. # testing "test name" "script" "expected result" "file input" "stdin"
  13. testing "Empty file is not a tarball" '\
  14. tar xvf - 2>&1; echo $?
  15. ' "\
  16. tar: short read
  17. 1
  18. " \
  19. "" ""
  20. SKIP=
  21. optional FEATURE_SEAMLESS_GZ
  22. # In NOMMU case, "invalid magic" message comes from gunzip child process.
  23. # Otherwise, it comes from tar.
  24. # Need to fix output up to avoid false positive.
  25. testing "Empty file is not a tarball.tar.gz" '\
  26. { tar xvzf - 2>&1; echo $?; } | grep -Fv "invalid magic"
  27. ' "\
  28. tar: short read
  29. 1
  30. " \
  31. "" ""
  32. SKIP=
  33. testing "Two zeroed blocks is a ('truncated') empty tarball" '\
  34. dd if=/dev/zero bs=512 count=2 2>/dev/null | tar xvf - 2>&1; echo $?
  35. ' "\
  36. 0
  37. " \
  38. "" ""
  39. SKIP=
  40. testing "Twenty zeroed blocks is an empty tarball" '\
  41. dd if=/dev/zero bs=512 count=20 2>/dev/null | tar xvf - 2>&1; echo $?
  42. ' "\
  43. 0
  44. " \
  45. "" ""
  46. SKIP=
  47. # "tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input":
  48. # GNU tar 1.26 records as hardlinks:
  49. # input_hard2 -> input_hard1
  50. # input_hard1 -> input_hard1 (!!!)
  51. # input_dir/file -> input_dir/file
  52. # input -> input
  53. # As of 1.24.0, we don't record last two: for them, nlink==1
  54. # and we check for "hardlink"ness only files with nlink!=1
  55. # We also don't use "hrw-r--r--" notation for hardlinks in "tar tv" listing.
  56. optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
  57. testing "tar hardlinks and repeated files" '\
  58. rm -rf input_* test.tar 2>/dev/null
  59. >input_hard1
  60. ln input_hard1 input_hard2
  61. mkdir input_dir
  62. >input_dir/file
  63. chmod -R 644 *
  64. chmod 755 input_dir
  65. tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
  66. tar tvf test.tar | sed "s/.*[0-9] input/input/"
  67. rm -rf input_dir
  68. tar xf test.tar 2>&1
  69. echo Ok: $?
  70. ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  71. ' "\
  72. input
  73. input_dir/
  74. input_dir/file
  75. input_hard1
  76. input_hard2 -> input_hard1
  77. input_hard1 -> input_hard1
  78. input_dir/
  79. input_dir/file
  80. input
  81. Ok: 0
  82. -rw-r--r-- input_dir/file
  83. drwxr-xr-x input_dir
  84. -rw-r--r-- input_hard1
  85. -rw-r--r-- input_hard2
  86. " \
  87. "" ""
  88. SKIP=
  89. optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
  90. testing "tar hardlinks mode" '\
  91. rm -rf input_* test.tar 2>/dev/null
  92. >input_hard1
  93. chmod 741 input_hard1
  94. ln input_hard1 input_hard2
  95. mkdir input_dir
  96. ln input_hard1 input_dir
  97. ln input_hard2 input_dir
  98. chmod 550 input_dir
  99. # 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_*":
  101. tar cf test.tar input_dir/input_hard* input_hard*
  102. tar tvf test.tar | sed "s/.*[0-9] input/input/"
  103. chmod 770 input_dir
  104. rm -rf input_*
  105. tar xf test.tar 2>&1
  106. echo Ok: $?
  107. ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  108. ' "\
  109. input_dir/input_hard1
  110. input_dir/input_hard2 -> input_dir/input_hard1
  111. input_hard1 -> input_dir/input_hard1
  112. input_hard2 -> input_dir/input_hard1
  113. Ok: 0
  114. -rwxr----x input_dir/input_hard1
  115. -rwxr----x input_dir/input_hard2
  116. -rwxr----x input_hard1
  117. -rwxr----x input_hard2
  118. " \
  119. "" ""
  120. SKIP=
  121. optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
  122. testing "tar symlinks mode" '\
  123. rm -rf input_* test.tar 2>/dev/null
  124. >input_file
  125. chmod 741 input_file
  126. ln -s input_file input_soft
  127. mkdir input_dir
  128. ln input_file input_dir
  129. ln input_soft input_dir
  130. chmod 550 input_dir
  131. tar cf test.tar input_dir/* input_[fs]*
  132. tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
  133. chmod 770 input_dir
  134. rm -rf input_*
  135. tar xf test.tar 2>&1
  136. echo Ok: $?
  137. ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  138. ' "\
  139. input_dir/input_file
  140. input_dir/input_soft -> input_file
  141. input_file -> input_dir/input_file
  142. input_soft -> input_dir/input_soft
  143. Ok: 0
  144. -rwxr----x input_dir/input_file
  145. lrwxrwxrwx input_file
  146. -rwxr----x input_file
  147. lrwxrwxrwx input_file
  148. " \
  149. "" ""
  150. SKIP=
  151. optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
  152. testing "tar --overwrite" "\
  153. rm -rf input_* test.tar 2>/dev/null
  154. ln input input_hard
  155. tar cf test.tar input_hard
  156. echo WRONG >input
  157. # --overwrite opens 'input_hard' without unlinking,
  158. # thus 'input_hard' still linked to 'input' and we write 'Ok' into it
  159. tar xf test.tar --overwrite 2>&1 && cat input
  160. " "\
  161. Ok
  162. " \
  163. "Ok\n" ""
  164. SKIP=
  165. test x"$SKIP_KNOWN_BUGS" = x"" && {
  166. # Needs to be run under non-root for meaningful test
  167. optional FEATURE_TAR_CREATE
  168. testing "tar writing into read-only dir" '\
  169. rm -rf input_* test.tar 2>/dev/null
  170. mkdir input_dir
  171. >input_dir/input_file
  172. chmod 550 input_dir
  173. tar cf test.tar input_dir
  174. tar tvf test.tar | sed "s/.*[0-9] input/input/"
  175. chmod 770 input_dir
  176. rm -rf input_*
  177. tar xf test.tar 2>&1
  178. echo Ok: $?
  179. ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  180. chmod 770 input_dir
  181. ' "\
  182. input_dir/
  183. input_dir/input_file
  184. Ok: 0
  185. -rw-r--r-- input_dir/input_file
  186. dr-xr-x--- input_dir
  187. " \
  188. "" ""
  189. SKIP=
  190. }
  191. # Had a bug where on extract autodetect first "switched off" -z
  192. # and then failed to recognize .tgz extension
  193. optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ
  194. testing "tar extract tgz" "\
  195. dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
  196. tar -czf F0.tgz F0
  197. rm F0
  198. tar -xzvf F0.tgz && echo Ok
  199. rm F0 || echo BAD
  200. " "\
  201. F0
  202. Ok
  203. " \
  204. "" ""
  205. SKIP=
  206. # Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
  207. # (the uuencoded hello_world.txz contains one empty file named "hello_world")
  208. optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
  209. testing "tar extract txz" "\
  210. uudecode -o input && tar tf input && echo Ok
  211. " "\
  212. hello_world
  213. Ok
  214. " \
  215. "" "\
  216. begin-base64 644 hello_world.txz
  217. /Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
  218. SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
  219. Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
  220. AAAEWVo=
  221. ====
  222. "
  223. SKIP=
  224. # On extract, everything up to and including last ".." component is stripped
  225. optional FEATURE_TAR_CREATE
  226. testing "tar strips /../ on extract" "\
  227. rm -rf input_* test.tar 2>/dev/null
  228. mkdir input_dir
  229. echo Ok >input_dir/file
  230. tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
  231. rm -rf input_* 2>/dev/null
  232. tar -vxf test.tar 2>&1
  233. cat input_dir/file 2>&1
  234. " "\
  235. tar: removing leading './../tar.tempdir/input_dir/../' from member names
  236. input_dir/
  237. input_dir/file
  238. Ok
  239. " \
  240. "" ""
  241. SKIP=
  242. # attack.tar.bz2 has symlink pointing to a system file
  243. # followed by a regular file with the same name
  244. # containing "root::0:0::/root:/bin/sh":
  245. # lrwxrwxrwx root/root passwd -> /tmp/passwd
  246. # -rw-r--r-- root/root passwd
  247. # naive tar implementation may end up creating the symlink
  248. # and then writing into it.
  249. # The correct implementation unlinks target before
  250. # creating the second file.
  251. # We test that /tmp/passwd remains empty:
  252. optional UUDECODE FEATURE_SEAMLESS_BZ2
  253. testing "tar does not extract into symlinks" "\
  254. >>/tmp/passwd && uudecode -o input && tar xf input 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
  255. " "\
  256. 0
  257. " \
  258. "" "\
  259. begin-base64 644 attack.tar.bz2
  260. QlpoOTFBWSZTWRVn/bIAAKt7hMqwAEBAAP2QAhB0Y96AAACACCAAlISgpqe0
  261. po0DIaDynqAkpDRP1ANAhiYNSPR8VchKhAz0AK59+DA6FcMKBggOARIJdVHL
  262. DGllrjs20ATUgR1HmccBX3EhoMnpMJaNyggmxgLDMz54lBnBTJO/1L1lbMS4
  263. l4/V8LDoe90yiWJhOJvIypgEfxdyRThQkBVn/bI=
  264. ====
  265. "
  266. SKIP=
  267. # And same with -k
  268. optional UUDECODE FEATURE_SEAMLESS_BZ2
  269. testing "tar -k does not extract into symlinks" "\
  270. >>/tmp/passwd && uudecode -o input && tar xf input -k 2>&1 && rm passwd; cat /tmp/passwd; echo \$?
  271. " "\
  272. tar: can't open 'passwd': File exists
  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. cd .. && rm -rf tar.tempdir || exit 1
  285. exit $FAILCOUNT