tar.tests 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
  48. testing "tar hardlinks and repeated files" '\
  49. rm -rf input_* test.tar 2>/dev/null
  50. >input_hard1
  51. ln input_hard1 input_hard2
  52. mkdir input_dir
  53. >input_dir/file
  54. chmod -R 644 *
  55. chmod 755 input_dir
  56. tar cf test.tar input input_dir/ input_hard1 input_hard2 input_hard1 input_dir/ input
  57. tar tvf test.tar | sed "s/.*[0-9] input/input/"
  58. tar xf test.tar 2>&1
  59. echo Ok: $?
  60. ls -l . input_dir/* | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  61. ' "\
  62. input
  63. input_dir/
  64. input_dir/file
  65. input_hard1
  66. input_hard2 -> input_hard1
  67. input_hard1 -> input_hard1
  68. input_dir/
  69. input_dir/file
  70. input
  71. Ok: 0
  72. -rw-r--r-- input_dir/file
  73. drwxr-xr-x input_dir
  74. -rw-r--r-- input_hard1
  75. -rw-r--r-- input_hard2
  76. " \
  77. "" ""
  78. SKIP=
  79. optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
  80. testing "tar hardlinks mode" '\
  81. rm -rf input_* test.tar 2>/dev/null
  82. >input_hard1
  83. chmod 741 input_hard1
  84. ln input_hard1 input_hard2
  85. mkdir input_dir
  86. ln input_hard1 input_dir
  87. ln input_hard2 input_dir
  88. chmod 550 input_dir
  89. # On some filesystems, input_dir/input_hard2 is returned by readdir
  90. # BEFORE input_dir/input_hard1! Thats why we cant just "tar cf ... input_*":
  91. tar cf test.tar input_dir/input_hard* input_hard*
  92. tar tvf test.tar | sed "s/.*[0-9] input/input/"
  93. chmod 770 input_dir
  94. rm -rf input_*
  95. tar xf test.tar 2>&1
  96. echo Ok: $?
  97. ls -l . input_dir/* | grep "input.*hard" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  98. ' "\
  99. input_dir/input_hard1
  100. input_dir/input_hard2 -> input_dir/input_hard1
  101. input_hard1 -> input_dir/input_hard1
  102. input_hard2 -> input_dir/input_hard1
  103. Ok: 0
  104. -rwxr----x input_dir/input_hard1
  105. -rwxr----x input_dir/input_hard2
  106. -rwxr----x input_hard1
  107. -rwxr----x input_hard2
  108. " \
  109. "" ""
  110. SKIP=
  111. optional FEATURE_TAR_CREATE FEATURE_LS_SORTFILES
  112. testing "tar symlinks mode" '\
  113. rm -rf input_* test.tar 2>/dev/null
  114. >input_file
  115. chmod 741 input_file
  116. ln -s input_file input_soft
  117. mkdir input_dir
  118. ln input_file input_dir
  119. ln input_soft input_dir
  120. chmod 550 input_dir
  121. tar cf test.tar input_dir/* input_[fs]*
  122. tar tvf test.tar | sed "s/.*[0-9] input/input/" | sort
  123. chmod 770 input_dir
  124. rm -rf input_*
  125. tar xf test.tar 2>&1
  126. echo Ok: $?
  127. ls -l . input_dir/* | grep "input_[fs]" | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  128. ' "\
  129. input_dir/input_file
  130. input_dir/input_soft -> input_file
  131. input_file -> input_dir/input_file
  132. input_soft -> input_dir/input_soft
  133. Ok: 0
  134. -rwxr----x input_dir/input_file
  135. lrwxrwxrwx input_file
  136. -rwxr----x input_file
  137. lrwxrwxrwx input_file
  138. " \
  139. "" ""
  140. SKIP=
  141. optional FEATURE_TAR_CREATE FEATURE_TAR_LONG_OPTIONS
  142. testing "tar --overwrite" "\
  143. rm -rf input_* test.tar 2>/dev/null
  144. ln input input_hard
  145. tar cf test.tar input_hard
  146. echo WRONG >input
  147. # --overwrite opens 'input_hard' without unlinking,
  148. # thus 'input_hard' still linked to 'input' and we write 'Ok' into it
  149. tar xf test.tar --overwrite 2>&1 && cat input
  150. " "\
  151. Ok
  152. " \
  153. "Ok\n" ""
  154. SKIP=
  155. test x"$SKIP_KNOWN_BUGS" = x"" && {
  156. # Needs to be run under non-root for meaningful test
  157. optional FEATURE_TAR_CREATE
  158. testing "tar writing into read-only dir" '\
  159. rm -rf input_* test.tar 2>/dev/null
  160. mkdir input_dir
  161. >input_dir/input_file
  162. chmod 550 input_dir
  163. tar cf test.tar input_dir
  164. tar tvf test.tar | sed "s/.*[0-9] input/input/"
  165. chmod 770 input_dir
  166. rm -rf input_*
  167. tar xf test.tar 2>&1
  168. echo Ok: $?
  169. ls -l input_dir/* . | grep input_ | sed "s/\\(^[^ ]*\\) .* input/\\1 input/"
  170. chmod 770 input_dir
  171. ' "\
  172. input_dir/
  173. input_dir/input_file
  174. Ok: 0
  175. -rw-r--r-- input_dir/input_file
  176. dr-xr-x--- input_dir
  177. " \
  178. "" ""
  179. SKIP=
  180. }
  181. # Had a bug where on extract autodetect first "switched off" -z
  182. # and then failed to recognize .tgz extension
  183. optional FEATURE_TAR_CREATE FEATURE_SEAMLESS_GZ
  184. testing "tar extract tgz" "\
  185. dd count=1 bs=1M if=/dev/zero of=F0 2>/dev/null
  186. tar -czf F0.tgz F0
  187. rm F0
  188. tar -xzvf F0.tgz && echo Ok
  189. rm F0 || echo BAD
  190. " "\
  191. F0
  192. Ok
  193. " \
  194. "" ""
  195. SKIP=
  196. # Do we detect XZ-compressed data (even w/o .tar.xz or txz extension)?
  197. # (the uuencoded hello_world.txz contains one empty file named "hello_world")
  198. optional UUDECODE FEATURE_TAR_AUTODETECT FEATURE_SEAMLESS_XZ
  199. testing "tar extract txz" "\
  200. uudecode -o input && tar tf input && echo Ok
  201. " "\
  202. hello_world
  203. Ok
  204. " \
  205. "" "\
  206. begin-base64 644 hello_world.txz
  207. /Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4AX/AEldADQZSe6ODIZQ3rSQ8kAJ
  208. SnMPTX+XWGKW3Yu/Rwqg4Ik5wqgQKgVH97J8yA8IvZ4ahaCQogUNHRkXibr2
  209. Q615wcb2G7fJU49AhWAAAAAAUA8gu9DyXfAAAWWADAAAAB5FXGCxxGf7AgAA
  210. AAAEWVo=
  211. ====
  212. "
  213. SKIP=
  214. # On extract, everything up to and including last ".." component is stripped
  215. optional FEATURE_TAR_CREATE
  216. testing "tar strips /../ on extract" "\
  217. rm -rf input_* test.tar 2>/dev/null
  218. mkdir input_dir
  219. echo Ok >input_dir/file
  220. tar cf test.tar ./../tar.tempdir/input_dir/../input_dir 2>&1
  221. rm -rf input_* 2>/dev/null
  222. tar -vxf test.tar 2>&1
  223. cat input_dir/file 2>&1
  224. " "\
  225. tar: removing leading './../tar.tempdir/input_dir/../' from member names
  226. input_dir/
  227. input_dir/file
  228. Ok
  229. " \
  230. "" ""
  231. SKIP=
  232. cd .. && rm -rf tar.tempdir || exit 1
  233. exit $FAILCOUNT