playTests.sh 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745
  1. #!/bin/sh
  2. set -e
  3. die() {
  4. println "$@" 1>&2
  5. exit 1
  6. }
  7. datagen() {
  8. "$DATAGEN_BIN" "$@"
  9. }
  10. zstd() {
  11. if [ -z "$EXEC_PREFIX" ]; then
  12. "$ZSTD_BIN" "$@"
  13. else
  14. "$EXEC_PREFIX" "$ZSTD_BIN" "$@"
  15. fi
  16. }
  17. sudoZstd() {
  18. if [ -z "$EXEC_PREFIX" ]; then
  19. sudo "$ZSTD_BIN" "$@"
  20. else
  21. sudo "$EXEC_PREFIX" "$ZSTD_BIN" "$@"
  22. fi
  23. }
  24. roundTripTest() {
  25. if [ -n "$3" ]; then
  26. cLevel="$3"
  27. proba="$2"
  28. else
  29. cLevel="$2"
  30. proba=""
  31. fi
  32. if [ -n "$4" ]; then
  33. dLevel="$4"
  34. else
  35. dLevel="$cLevel"
  36. fi
  37. rm -f tmp1 tmp2
  38. println "roundTripTest: datagen $1 $proba | zstd -v$cLevel | zstd -d$dLevel"
  39. datagen $1 $proba | $MD5SUM > tmp1
  40. datagen $1 $proba | zstd --ultra -v$cLevel | zstd -d$dLevel | $MD5SUM > tmp2
  41. $DIFF -q tmp1 tmp2
  42. }
  43. fileRoundTripTest() {
  44. if [ -n "$3" ]; then
  45. local_c="$3"
  46. local_p="$2"
  47. else
  48. local_c="$2"
  49. local_p=""
  50. fi
  51. if [ -n "$4" ]; then
  52. local_d="$4"
  53. else
  54. local_d="$local_c"
  55. fi
  56. rm -f tmp.zst tmp.md5.1 tmp.md5.2
  57. println "fileRoundTripTest: datagen $1 $local_p > tmp && zstd -v$local_c -c tmp | zstd -d$local_d"
  58. datagen $1 $local_p > tmp
  59. < tmp $MD5SUM > tmp.md5.1
  60. zstd --ultra -v$local_c -c tmp | zstd -d$local_d | $MD5SUM > tmp.md5.2
  61. $DIFF -q tmp.md5.1 tmp.md5.2
  62. }
  63. truncateLastByte() {
  64. dd bs=1 count=$(($(wc -c < "$1") - 1)) if="$1"
  65. }
  66. println() {
  67. printf '%b\n' "${*}"
  68. }
  69. if [ -z "${size}" ]; then
  70. size=
  71. else
  72. size=${size}
  73. fi
  74. SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
  75. PRGDIR="$SCRIPT_DIR/../programs"
  76. TESTDIR="$SCRIPT_DIR/../tests"
  77. UNAME=$(uname)
  78. ZSTDGREP="$PRGDIR/zstdgrep"
  79. detectedTerminal=false
  80. if [ -t 0 ] && [ -t 1 ]
  81. then
  82. detectedTerminal=true
  83. fi
  84. isTerminal=${isTerminal:-$detectedTerminal}
  85. isWindows=false
  86. INTOVOID="/dev/null"
  87. case "$UNAME" in
  88. GNU) DEVDEVICE="/dev/random" ;;
  89. *) DEVDEVICE="/dev/zero" ;;
  90. esac
  91. case "$OS" in
  92. Windows*)
  93. isWindows=true
  94. INTOVOID="NUL"
  95. DEVDEVICE="NUL"
  96. ;;
  97. esac
  98. case "$UNAME" in
  99. Darwin) MD5SUM="md5 -r" ;;
  100. FreeBSD) MD5SUM="gmd5sum" ;;
  101. NetBSD) MD5SUM="md5 -n" ;;
  102. OpenBSD) MD5SUM="md5" ;;
  103. *) MD5SUM="md5sum" ;;
  104. esac
  105. MTIME="stat -c %Y"
  106. case "$UNAME" in
  107. Darwin | FreeBSD | OpenBSD | NetBSD) MTIME="stat -f %m" ;;
  108. esac
  109. assertSameMTime() {
  110. MT1=$($MTIME "$1")
  111. MT2=$($MTIME "$2")
  112. echo MTIME $MT1 $MT2
  113. [ "$MT1" = "$MT2" ] || die "mtime on $1 doesn't match mtime on $2 ($MT1 != $MT2)"
  114. }
  115. GET_PERMS="stat -c %a"
  116. case "$UNAME" in
  117. Darwin | FreeBSD | OpenBSD | NetBSD) GET_PERMS="stat -f %Lp" ;;
  118. esac
  119. assertFilePermissions() {
  120. STAT1=$($GET_PERMS "$1")
  121. STAT2=$2
  122. [ "$STAT1" = "$STAT2" ] || die "permissions on $1 don't match expected ($STAT1 != $STAT2)"
  123. }
  124. assertSamePermissions() {
  125. STAT1=$($GET_PERMS "$1")
  126. STAT2=$($GET_PERMS "$2")
  127. [ "$STAT1" = "$STAT2" ] || die "permissions on $1 don't match those on $2 ($STAT1 != $STAT2)"
  128. }
  129. DIFF="diff"
  130. case "$UNAME" in
  131. SunOS) DIFF="gdiff" ;;
  132. esac
  133. # check if ZSTD_BIN is defined. if not, use the default value
  134. if [ -z "${ZSTD_BIN}" ]; then
  135. println "\nZSTD_BIN is not set. Using the default value..."
  136. ZSTD_BIN="$PRGDIR/zstd"
  137. fi
  138. # check if DATAGEN_BIN is defined. if not, use the default value
  139. if [ -z "${DATAGEN_BIN}" ]; then
  140. println "\nDATAGEN_BIN is not set. Using the default value..."
  141. DATAGEN_BIN="$TESTDIR/datagen"
  142. fi
  143. # Why was this line here ? Generates a strange ZSTD_BIN when EXE_PREFIX is non empty
  144. # ZSTD_BIN="$EXE_PREFIX$ZSTD_BIN"
  145. # assertions
  146. [ -n "$ZSTD_BIN" ] || die "zstd not found at $ZSTD_BIN! \n Please define ZSTD_BIN pointing to the zstd binary. You might also consider rebuilding zstd following the instructions in README.md"
  147. [ -n "$DATAGEN_BIN" ] || die "datagen not found at $DATAGEN_BIN! \n Please define DATAGEN_BIN pointing to the datagen binary. You might also consider rebuilding zstd tests following the instructions in README.md. "
  148. println "\nStarting playTests.sh isWindows=$isWindows EXE_PREFIX='$EXE_PREFIX' ZSTD_BIN='$ZSTD_BIN' DATAGEN_BIN='$DATAGEN_BIN'"
  149. if echo hello | zstd -v -T2 2>&1 > $INTOVOID | grep -q 'multi-threading is disabled'
  150. then
  151. hasMT=""
  152. else
  153. hasMT="true"
  154. fi
  155. println "\n===> simple tests "
  156. datagen > tmp
  157. zstd -h
  158. zstd -H
  159. zstd -V
  160. println "test : basic compression "
  161. zstd -f tmp # trivial compression case, creates tmp.zst
  162. zstd -f -z tmp
  163. zstd -f -k tmp
  164. zstd -f -C tmp
  165. println "test : basic decompression"
  166. zstd -df tmp.zst # trivial decompression case (overwrites tmp)
  167. println "test : too large compression level => auto-fix"
  168. zstd -99 -f tmp # too large compression level, automatic sized down
  169. zstd -5000000000 -f tmp && die "too large numeric value : must fail"
  170. println "test : --fast aka negative compression levels"
  171. zstd --fast -f tmp # == -1
  172. zstd --fast=3 -f tmp # == -3
  173. zstd --fast=200000 -f tmp # too low compression level, automatic fixed
  174. zstd --fast=5000000000 -f tmp && die "too large numeric value : must fail"
  175. zstd -c --fast=0 tmp > $INTOVOID && die "--fast must not accept value 0"
  176. println "test : too large numeric argument"
  177. zstd --fast=9999999999 -f tmp && die "should have refused numeric value"
  178. println "test : set compression level with environment variable ZSTD_CLEVEL"
  179. ZSTD_CLEVEL=12 zstd -f tmp # positive compression level
  180. ZSTD_CLEVEL=-12 zstd -f tmp # negative compression level
  181. ZSTD_CLEVEL=+12 zstd -f tmp # valid: verbose '+' sign
  182. ZSTD_CLEVEL='' zstd -f tmp # empty env var, warn and revert to default setting
  183. ZSTD_CLEVEL=- zstd -f tmp # malformed env var, warn and revert to default setting
  184. ZSTD_CLEVEL=a zstd -f tmp # malformed env var, warn and revert to default setting
  185. ZSTD_CLEVEL=+a zstd -f tmp # malformed env var, warn and revert to default setting
  186. ZSTD_CLEVEL=3a7 zstd -f tmp # malformed env var, warn and revert to default setting
  187. ZSTD_CLEVEL=50000000000 zstd -f tmp # numeric value too large, warn and revert to default setting
  188. println "test : override ZSTD_CLEVEL with command line option"
  189. ZSTD_CLEVEL=12 zstd --fast=3 -f tmp # overridden by command line option
  190. println "test : compress to stdout"
  191. zstd tmp -c > tmpCompressed
  192. zstd tmp --stdout > tmpCompressed # long command format
  193. println "test : compress to named file"
  194. rm -f tmpCompressed
  195. zstd tmp -o tmpCompressed
  196. test -f tmpCompressed # file must be created
  197. println "test : force write, correct order"
  198. zstd tmp -fo tmpCompressed
  199. println "test : forgotten argument"
  200. cp tmp tmp2
  201. zstd tmp2 -fo && die "-o must be followed by filename "
  202. println "test : implied stdout when input is stdin"
  203. println bob | zstd | zstd -d
  204. if [ "$isTerminal" = true ]; then
  205. println "test : compressed data to terminal"
  206. println bob | zstd && die "should have refused : compressed data to terminal"
  207. println "test : compressed data from terminal (a hang here is a test fail, zstd is wrongly waiting on data from terminal)"
  208. zstd -d > $INTOVOID && die "should have refused : compressed data from terminal"
  209. fi
  210. println "test : null-length file roundtrip"
  211. println -n '' | zstd - --stdout | zstd -d --stdout
  212. println "test : ensure small file doesn't add 3-bytes null block"
  213. datagen -g1 > tmp1
  214. zstd tmp1 -c | wc -c | grep "14"
  215. zstd < tmp1 | wc -c | grep "14"
  216. println "test : decompress file with wrong suffix (must fail)"
  217. zstd -d tmpCompressed && die "wrong suffix error not detected!"
  218. zstd -df tmp && die "should have refused : wrong extension"
  219. println "test : decompress into stdout"
  220. zstd -d tmpCompressed -c > tmpResult # decompression using stdout
  221. zstd --decompress tmpCompressed -c > tmpResult
  222. zstd --decompress tmpCompressed --stdout > tmpResult
  223. println "test : decompress from stdin into stdout"
  224. zstd -dc < tmp.zst > $INTOVOID # combine decompression, stdin & stdout
  225. zstd -dc - < tmp.zst > $INTOVOID
  226. zstd -d < tmp.zst > $INTOVOID # implicit stdout when stdin is used
  227. zstd -d - < tmp.zst > $INTOVOID
  228. println "test : impose memory limitation (must fail)"
  229. zstd -d -f tmp.zst -M2K -c > $INTOVOID && die "decompression needs more memory than allowed"
  230. zstd -d -f tmp.zst --memlimit=2K -c > $INTOVOID && die "decompression needs more memory than allowed" # long command
  231. zstd -d -f tmp.zst --memory=2K -c > $INTOVOID && die "decompression needs more memory than allowed" # long command
  232. zstd -d -f tmp.zst --memlimit-decompress=2K -c > $INTOVOID && die "decompression needs more memory than allowed" # long command
  233. println "test : overwrite protection"
  234. zstd -q tmp && die "overwrite check failed!"
  235. println "test : force overwrite"
  236. zstd -q -f tmp
  237. zstd -q --force tmp
  238. println "test : overwrite readonly file"
  239. rm -f tmpro tmpro.zst
  240. println foo > tmpro.zst
  241. println foo > tmpro
  242. chmod 400 tmpro.zst
  243. zstd -q tmpro && die "should have refused to overwrite read-only file"
  244. zstd -q -f tmpro
  245. println "test: --no-progress flag"
  246. zstd tmpro -c --no-progress | zstd -d -f -o "$INTOVOID" --no-progress
  247. zstd tmpro -cv --no-progress | zstd -dv -f -o "$INTOVOID" --no-progress
  248. println "test: --progress flag"
  249. zstd tmpro -c | zstd -d -f -o "$INTOVOID" --progress 2>&1 | grep -E "[A-Za-z0-9._ ]+: [0-9]+ bytes"
  250. zstd tmpro -c | zstd -d -f -q -o "$INTOVOID" --progress 2>&1 | grep -E "[A-Za-z0-9._ ]+: [0-9]+ bytes"
  251. zstd tmpro -c | zstd -d -f -v -o "$INTOVOID" 2>&1 | grep -E "[A-Za-z0-9._ ]+: [0-9]+ bytes"
  252. rm -f tmpro tmpro.zst
  253. println "test: overwrite input file (must fail)"
  254. zstd tmp -fo tmp && die "zstd compression overwrote the input file"
  255. zstd tmp.zst -dfo tmp.zst && die "zstd decompression overwrote the input file"
  256. println "test: detect that input file does not exist"
  257. zstd nothere && die "zstd hasn't detected that input file does not exist"
  258. println "test: --[no-]compress-literals"
  259. zstd tmp -c --no-compress-literals -1 | zstd -t
  260. zstd tmp -c --no-compress-literals --fast=1 | zstd -t
  261. zstd tmp -c --no-compress-literals -19 | zstd -t
  262. zstd tmp -c --compress-literals -1 | zstd -t
  263. zstd tmp -c --compress-literals --fast=1 | zstd -t
  264. zstd tmp -c --compress-literals -19 | zstd -t
  265. zstd -b --fast=1 -i0e1 tmp --compress-literals
  266. zstd -b --fast=1 -i0e1 tmp --no-compress-literals
  267. println "test: --no-check for decompression"
  268. zstd -f tmp -o tmp_corrupt.zst --check
  269. zstd -f tmp -o tmp.zst --no-check
  270. printf '\xDE\xAD\xBE\xEF' | dd of=tmp_corrupt.zst bs=1 seek=$(($(wc -c < "tmp_corrupt.zst") - 4)) count=4 conv=notrunc # corrupt checksum in tmp
  271. zstd -d -f tmp_corrupt.zst --no-check
  272. zstd -d -f tmp_corrupt.zst --check --no-check # final flag overrides
  273. zstd -d -f tmp.zst --no-check
  274. if [ "$isWindows" = false ]; then
  275. if [ -n "$(which readelf)" ]; then
  276. println "test: check if binary has executable stack (#2963)"
  277. readelf -lW "$ZSTD_BIN" | grep 'GNU_STACK .* RW ' || die "zstd binary has executable stack!"
  278. fi
  279. fi
  280. println "\n===> zstdgrep tests"
  281. ln -sf "$ZSTD_BIN" zstdcat
  282. rm -f tmp_grep
  283. echo "1234" > tmp_grep
  284. zstd -f tmp_grep
  285. lines=$(ZCAT=./zstdcat "$ZSTDGREP" 2>&1 "1234" tmp_grep tmp_grep.zst | wc -l)
  286. test 2 -eq $lines
  287. ZCAT=./zstdcat "$ZSTDGREP" 2>&1 "1234" tmp_grep_bad.zst && die "Should have failed"
  288. ZCAT=./zstdcat "$ZSTDGREP" 2>&1 "1234" tmp_grep_bad.zst | grep "No such file or directory" || true
  289. rm -f tmp_grep*
  290. println "\n===> --exclude-compressed flag"
  291. rm -rf precompressedFilterTestDir
  292. mkdir -p precompressedFilterTestDir
  293. datagen $size > precompressedFilterTestDir/input.5
  294. datagen $size > precompressedFilterTestDir/input.6
  295. zstd --exclude-compressed --long --rm -r precompressedFilterTestDir
  296. datagen $size > precompressedFilterTestDir/input.7
  297. datagen $size > precompressedFilterTestDir/input.8
  298. zstd --exclude-compressed --long --rm -r precompressedFilterTestDir
  299. test ! -f precompressedFilterTestDir/input.5.zst.zst
  300. test ! -f precompressedFilterTestDir/input.6.zst.zst
  301. file1timestamp=`$MTIME precompressedFilterTestDir/input.5.zst`
  302. file2timestamp=`$MTIME precompressedFilterTestDir/input.7.zst`
  303. if [ $file2timestamp -ge $file1timestamp ]; then
  304. println "Test is successful. input.5.zst is precompressed and therefore not compressed/modified again."
  305. else
  306. println "Test is not successful"
  307. fi
  308. # File Extension check.
  309. datagen $size > precompressedFilterTestDir/input.zstbar
  310. zstd --exclude-compressed --long --rm -r precompressedFilterTestDir
  311. # zstd should compress input.zstbar
  312. test -f precompressedFilterTestDir/input.zstbar.zst
  313. # Check without the --exclude-compressed flag
  314. zstd --long --rm -r precompressedFilterTestDir
  315. # Files should get compressed again without the --exclude-compressed flag.
  316. test -f precompressedFilterTestDir/input.5.zst.zst
  317. test -f precompressedFilterTestDir/input.6.zst.zst
  318. rm -rf precompressedFilterTestDir
  319. println "Test completed"
  320. println "\n===> warning prompts should not occur if stdin is an input"
  321. println "y" > tmpPrompt
  322. println "hello world" >> tmpPrompt
  323. zstd tmpPrompt -f
  324. zstd < tmpPrompt -o tmpPrompt.zst && die "should have aborted immediately and failed to overwrite"
  325. zstd < tmpPrompt -o tmpPrompt.zst -f # should successfully overwrite with -f
  326. zstd -q -d -f tmpPrompt.zst -o tmpPromptRegenerated
  327. $DIFF tmpPromptRegenerated tmpPrompt # the first 'y' character should not be swallowed
  328. echo 'yes' | zstd tmpPrompt -v -o tmpPrompt.zst # accept piped "y" input to force overwrite when using files
  329. echo 'yes' | zstd < tmpPrompt -v -o tmpPrompt.zst && die "should have aborted immediately and failed to overwrite"
  330. zstd tmpPrompt - < tmpPrompt -o tmpPromp.zst --rm && die "should have aborted immediately and failed to remove"
  331. println "Test completed"
  332. println "\n===> recursive mode test "
  333. # combination of -r with empty list of input file
  334. zstd -c -r < tmp > tmp.zst
  335. println "\n===> file removal"
  336. zstd -f --rm tmp
  337. test ! -f tmp # tmp should no longer be present
  338. zstd -f -d --rm tmp.zst
  339. test ! -f tmp.zst # tmp.zst should no longer be present
  340. println "test : should quietly not remove non-regular file"
  341. println hello > tmp
  342. zstd tmp -f -o "$DEVDEVICE" 2>tmplog > "$INTOVOID"
  343. grep "Refusing to remove non-regular file" tmplog && die
  344. rm -f tmplog
  345. zstd tmp -f -o "$INTOVOID" 2>&1 | grep "Refusing to remove non-regular file" && die
  346. println "test : --rm on stdin"
  347. println a | zstd --rm > $INTOVOID # --rm should remain silent
  348. rm -f tmp
  349. zstd -f tmp && die "tmp not present : should have failed"
  350. test ! -f tmp.zst # tmp.zst should not be created
  351. println "test : -d -f do not delete destination when source is not present"
  352. touch tmp # create destination file
  353. zstd -d -f tmp.zst && die "attempt to decompress a non existing file"
  354. test -f tmp # destination file should still be present
  355. println "test : -f do not delete destination when source is not present"
  356. rm -f tmp # erase source file
  357. touch tmp.zst # create destination file
  358. zstd -f tmp && die "attempt to compress a non existing file"
  359. test -f tmp.zst # destination file should still be present
  360. rm -rf tmp* # may also erase tmp* directory from previous failed run
  361. println "\n===> decompression only tests "
  362. # the following test verifies that the decoder is compatible with RLE as first block
  363. # older versions of zstd cli are not able to decode such corner case.
  364. # As a consequence, the zstd cli do not generate them, to maintain compatibility with older versions.
  365. dd bs=1048576 count=1 if=/dev/zero of=tmp
  366. zstd -d -o tmp1 "$TESTDIR/golden-decompression/rle-first-block.zst"
  367. $DIFF -s tmp1 tmp
  368. rm -f tmp*
  369. println "\n===> compress multiple files"
  370. println hello > tmp1
  371. println world > tmp2
  372. zstd tmp1 tmp2 -o "$INTOVOID" -f
  373. zstd tmp1 tmp2 -c | zstd -t
  374. zstd tmp1 tmp2 -o tmp.zst
  375. test ! -f tmp1.zst
  376. test ! -f tmp2.zst
  377. zstd tmp1 tmp2
  378. zstd -t tmp1.zst tmp2.zst
  379. zstd -dc tmp1.zst tmp2.zst
  380. zstd tmp1.zst tmp2.zst -o "$INTOVOID" -f
  381. zstd -d tmp1.zst tmp2.zst -o tmp
  382. touch tmpexists
  383. zstd tmp1 tmp2 -f -o tmpexists
  384. zstd tmp1 tmp2 -q -o tmpexists && die "should have refused to overwrite"
  385. println gooder > tmp_rm1
  386. println boi > tmp_rm2
  387. println worldly > tmp_rm3
  388. echo 'y' | zstd tmp_rm1 tmp_rm2 -v -o tmp_rm3.zst --rm # tests the warning prompt for --rm with multiple inputs into once source
  389. test ! -f tmp_rm1
  390. test ! -f tmp_rm2
  391. cp tmp_rm3.zst tmp_rm4.zst
  392. echo 'Y' | zstd -d tmp_rm3.zst tmp_rm4.zst -v -o tmp_rm_out --rm
  393. test ! -f tmp_rm3.zst
  394. test ! -f tmp_rm4.zst
  395. echo 'yes' | zstd tmp_rm_out tmp_rm3 -c --rm && die "compressing multiple files to stdout with --rm should fail unless -f is specified"
  396. echo 'yes' | zstd tmp_rm_out tmp_rm3 -c --rm -v && die "compressing multiple files to stdout with --rm should fail unless -f is specified"
  397. println gooder > tmpexists1
  398. zstd tmpexists1 tmpexists -c --rm -f > $INTOVOID
  399. # Bug: PR #972
  400. if [ "$?" -eq 139 ]; then
  401. die "should not have segfaulted"
  402. fi
  403. println "\n===> multiple files and shell completion "
  404. datagen -s1 > tmp1 2> $INTOVOID
  405. datagen -s2 -g100K > tmp2 2> $INTOVOID
  406. datagen -s3 -g1M > tmp3 2> $INTOVOID
  407. println "compress tmp* : "
  408. zstd -f tmp*
  409. test -f tmp1.zst
  410. test -f tmp2.zst
  411. test -f tmp3.zst
  412. rm -f tmp1 tmp2 tmp3
  413. println "decompress tmp* : "
  414. zstd -df ./*.zst
  415. test -f tmp1
  416. test -f tmp2
  417. test -f tmp3
  418. println "compress tmp* into stdout > tmpall : "
  419. zstd -c tmp1 tmp2 tmp3 > tmpall
  420. test -f tmpall # should check size of tmpall (should be tmp1.zst + tmp2.zst + tmp3.zst)
  421. println "decompress tmpall* into stdout > tmpdec : "
  422. cp tmpall tmpall2
  423. zstd -dc tmpall* > tmpdec
  424. test -f tmpdec # should check size of tmpdec (should be 2*(tmp1 + tmp2 + tmp3))
  425. println "compress multiple files including a missing one (notHere) : "
  426. zstd -f tmp1 notHere tmp2 && die "missing file not detected!"
  427. rm -f tmp*
  428. if [ "$isWindows" = false ] ; then
  429. println "\n===> zstd fifo named pipe test "
  430. echo "Hello World!" > tmp_original
  431. mkfifo tmp_named_pipe
  432. # note : fifo test doesn't work in combination with `dd` or `cat`
  433. echo "Hello World!" > tmp_named_pipe &
  434. zstd tmp_named_pipe -o tmp_compressed
  435. zstd -d -o tmp_decompressed tmp_compressed
  436. $DIFF -s tmp_original tmp_decompressed
  437. rm -rf tmp*
  438. fi
  439. println "\n===> zstd created file permissions tests"
  440. if [ "$isWindows" = false ] ; then
  441. rm -f tmp1 tmp2 tmp1.zst tmp2.zst tmp1.out tmp2.out # todo: remove
  442. ORIGINAL_UMASK=$(umask)
  443. umask 0000
  444. datagen > tmp1
  445. datagen > tmp2
  446. assertFilePermissions tmp1 666
  447. assertFilePermissions tmp2 666
  448. println "test : copy 666 permissions in file -> file compression "
  449. zstd -f tmp1 -o tmp1.zst
  450. assertSamePermissions tmp1 tmp1.zst
  451. println "test : copy 666 permissions in file -> file decompression "
  452. zstd -f -d tmp1.zst -o tmp1.out
  453. assertSamePermissions tmp1.zst tmp1.out
  454. rm -f tmp1.zst tmp1.out
  455. println "test : copy 400 permissions in file -> file compression (write to a read-only file) "
  456. chmod 0400 tmp1
  457. assertFilePermissions tmp1 400
  458. zstd -f tmp1 -o tmp1.zst
  459. assertSamePermissions tmp1 tmp1.zst
  460. println "test : copy 400 permissions in file -> file decompression (write to a read-only file) "
  461. zstd -f -d tmp1.zst -o tmp1
  462. assertSamePermissions tmp1.zst tmp1
  463. rm -f tmp1.zst tmp1.out
  464. println "test : check created permissions from stdin input in compression "
  465. zstd -f -o tmp1.zst < tmp1
  466. assertFilePermissions tmp1.zst 666
  467. println "test : check created permissions from stdin input in decompression "
  468. zstd -f -d -o tmp1.out < tmp1.zst
  469. assertFilePermissions tmp1.out 666
  470. rm -f tmp1.zst tmp1.out
  471. println "test : check created permissions from multiple inputs in compression "
  472. zstd -f tmp1 tmp2 -o tmp1.zst
  473. assertFilePermissions tmp1.zst 666
  474. println "test : check created permissions from multiple inputs in decompression "
  475. cp tmp1.zst tmp2.zst
  476. zstd -f -d tmp1.zst tmp2.zst -o tmp1.out
  477. assertFilePermissions tmp1.out 666
  478. rm -f tmp1.zst tmp2.zst tmp1.out tmp2.out
  479. println "test : check permissions on pre-existing output file in compression "
  480. chmod 0600 tmp1
  481. touch tmp1.zst
  482. chmod 0400 tmp1.zst
  483. zstd -f tmp1 -o tmp1.zst
  484. assertFilePermissions tmp1.zst 600
  485. println "test : check permissions on pre-existing output file in decompression "
  486. chmod 0400 tmp1.zst
  487. touch tmp1.out
  488. chmod 0200 tmp1.out
  489. zstd -f -d tmp1.zst -o tmp1.out
  490. assertFilePermissions tmp1.out 400
  491. rm -f tmp1.zst tmp1.out
  492. umask 0666
  493. chmod 0666 tmp1 tmp2
  494. println "test : respect umask when copying permissions in file -> file compression "
  495. zstd -f tmp1 -o tmp1.zst
  496. assertFilePermissions tmp1.zst 0
  497. println "test : respect umask when copying permissions in file -> file decompression "
  498. chmod 0666 tmp1.zst
  499. zstd -f -d tmp1.zst -o tmp1.out
  500. assertFilePermissions tmp1.out 0
  501. rm -f tmp1.zst tmp1.out
  502. println "test : respect umask when compressing from stdin input "
  503. zstd -f -o tmp1.zst < tmp1
  504. assertFilePermissions tmp1.zst 0
  505. println "test : respect umask when decompressing from stdin input "
  506. chmod 0666 tmp1.zst
  507. zstd -f -d -o tmp1.out < tmp1.zst
  508. assertFilePermissions tmp1.out 0
  509. rm -f tmp1 tmp2 tmp1.zst tmp2.zst tmp1.out tmp2.out
  510. umask $ORIGINAL_UMASK
  511. fi
  512. if [ -n "$DEVNULLRIGHTS" ] ; then
  513. # these tests requires sudo rights, which is uncommon.
  514. # they are only triggered if DEVNULLRIGHTS macro is defined.
  515. println "\n===> checking /dev/null permissions are unaltered "
  516. datagen > tmp
  517. sudoZstd tmp -o $INTOVOID # sudo rights could modify /dev/null permissions
  518. sudoZstd tmp -c > $INTOVOID
  519. zstd tmp -f -o tmp.zst
  520. sudoZstd -d tmp.zst -c > $INTOVOID
  521. sudoZstd -d tmp.zst -o $INTOVOID
  522. ls -las $INTOVOID | grep "rw-rw-rw-"
  523. fi
  524. if [ -n "$READFROMBLOCKDEVICE" ] ; then
  525. # This creates a temporary block device, which is only possible on unix-y
  526. # systems, is somewhat invasive, and requires sudo. For these reasons, you
  527. # have to specifically ask for this test.
  528. println "\n===> checking that zstd can read from a block device"
  529. datagen -g65536 > tmp.img
  530. sudo losetup -fP tmp.img
  531. LOOP_DEV=$(losetup -a | grep 'tmp\.img' | cut -f1 -d:)
  532. [ -z "$LOOP_DEV" ] && die "failed to get loopback device"
  533. sudoZstd $LOOP_DEV -c > tmp.img.zst && die "should fail without -f"
  534. sudoZstd -f $LOOP_DEV -c > tmp.img.zst
  535. zstd -d tmp.img.zst -o tmp.img.copy
  536. sudo losetup -d $LOOP_DEV
  537. $DIFF -s tmp.img tmp.img.copy || die "round trip failed"
  538. rm -f tmp.img tmp.img.zst tmp.img.copy
  539. fi
  540. println "\n===> zstd created file timestamp tests"
  541. datagen > tmp
  542. touch -m -t 200001010000.00 tmp
  543. println "test : copy mtime in file -> file compression "
  544. zstd -f tmp -o tmp.zst
  545. assertSameMTime tmp tmp.zst
  546. println "test : copy mtime in file -> file decompression "
  547. zstd -f -d tmp.zst -o tmp.out
  548. assertSameMTime tmp.zst tmp.out
  549. rm -f tmp
  550. println "\n===> compress multiple files into an output directory, --output-dir-flat"
  551. println henlo > tmp1
  552. mkdir tmpInputTestDir
  553. mkdir tmpInputTestDir/we
  554. mkdir tmpInputTestDir/we/must
  555. mkdir tmpInputTestDir/we/must/go
  556. mkdir tmpInputTestDir/we/must/go/deeper
  557. println cool > tmpInputTestDir/we/must/go/deeper/tmp2
  558. mkdir tmpOutDir
  559. zstd tmp1 tmpInputTestDir/we/must/go/deeper/tmp2 --output-dir-flat tmpOutDir
  560. test -f tmpOutDir/tmp1.zst
  561. test -f tmpOutDir/tmp2.zst
  562. println "test : decompress multiple files into an output directory, --output-dir-flat"
  563. mkdir tmpOutDirDecomp
  564. zstd tmpOutDir -r -d --output-dir-flat tmpOutDirDecomp
  565. test -f tmpOutDirDecomp/tmp2
  566. test -f tmpOutDirDecomp/tmp1
  567. rm -f tmpOutDirDecomp/*
  568. zstd tmpOutDir -r -d --output-dir-flat=tmpOutDirDecomp
  569. test -f tmpOutDirDecomp/tmp2
  570. test -f tmpOutDirDecomp/tmp1
  571. rm -rf tmp*
  572. if [ "$isWindows" = false ] ; then
  573. println "\n===> compress multiple files into an output directory and mirror input folder, --output-dir-mirror"
  574. println "test --output-dir-mirror" > tmp1
  575. mkdir -p tmpInputTestDir/we/.../..must/go/deeper..
  576. println cool > tmpInputTestDir/we/.../..must/go/deeper../tmp2
  577. zstd tmp1 -r tmpInputTestDir --output-dir-mirror tmpOutDir
  578. test -f tmpOutDir/tmp1.zst
  579. test -f tmpOutDir/tmpInputTestDir/we/.../..must/go/deeper../tmp2.zst
  580. println "test: compress input dir will be ignored if it has '..'"
  581. zstd -r tmpInputTestDir/we/.../..must/../..mustgo/deeper.. --output-dir-mirror non-exist && die "input cannot contain '..'"
  582. zstd -r tmpInputTestDir/we/.../..must/deeper../.. --output-dir-mirror non-exist && die "input cannot contain '..'"
  583. zstd -r ../tests/tmpInputTestDir/we/.../..must/deeper.. --output-dir-mirror non-exist && die "input cannot contain '..'"
  584. test ! -d non-exist
  585. println "test: compress input dir should succeed with benign uses of '..'"
  586. zstd -r tmpInputTestDir/we/.../..must/go/deeper.. --output-dir-mirror tmpout
  587. test -d tmpout
  588. println "test : decompress multiple files into an output directory, --output-dir-mirror"
  589. zstd tmpOutDir -r -d --output-dir-mirror tmpOutDirDecomp
  590. test -f tmpOutDirDecomp/tmpOutDir/tmp1
  591. test -f tmpOutDirDecomp/tmpOutDir/tmpInputTestDir/we/.../..must/go/deeper../tmp2
  592. println "test: decompress input dir will be ignored if it has '..'"
  593. zstd -r tmpOutDir/tmpInputTestDir/we/.../..must/../..must --output-dir-mirror non-exist && die "input cannot contain '..'"
  594. test ! -d non-exist
  595. rm -rf tmp*
  596. fi
  597. println "test : compress multiple files reading them from a file, --filelist=FILE"
  598. println "Hello world!, file1" > tmp1
  599. println "Hello world!, file2" > tmp2
  600. println tmp1 > tmp_fileList
  601. println tmp2 >> tmp_fileList
  602. zstd -f --filelist=tmp_fileList
  603. test -f tmp2.zst
  604. test -f tmp1.zst
  605. println "test : alternate syntax: --filelist FILE"
  606. zstd -f --filelist tmp_fileList
  607. test -f tmp2.zst
  608. test -f tmp1.zst
  609. println "test : reading file list from a symlink, --filelist=FILE"
  610. rm -f *.zst
  611. ln -s tmp_fileList tmp_symLink
  612. zstd -f --filelist=tmp_symLink
  613. test -f tmp2.zst
  614. test -f tmp1.zst
  615. println "test : compress multiple files reading them from multiple files, --filelist=FILE"
  616. rm -f *.zst
  617. println "Hello world!, file3" > tmp3
  618. println "Hello world!, file4" > tmp4
  619. println tmp3 > tmp_fileList2
  620. println tmp4 >> tmp_fileList2
  621. zstd -f --filelist=tmp_fileList --filelist=tmp_fileList2
  622. test -f tmp1.zst
  623. test -f tmp2.zst
  624. test -f tmp3.zst
  625. test -f tmp4.zst
  626. println "test : decompress multiple files reading them from a file, --filelist=FILE"
  627. rm -f tmp1 tmp2
  628. println tmp1.zst > tmpZst
  629. println tmp2.zst >> tmpZst
  630. zstd -d -f --filelist=tmpZst
  631. test -f tmp1
  632. test -f tmp2
  633. println "test : decompress multiple files reading them from multiple files, --filelist=FILE"
  634. rm -f tmp1 tmp2 tmp3 tmp4
  635. println tmp3.zst > tmpZst2
  636. println tmp4.zst >> tmpZst2
  637. zstd -d -f --filelist=tmpZst --filelist=tmpZst2
  638. test -f tmp1
  639. test -f tmp2
  640. test -f tmp3
  641. test -f tmp4
  642. println "test : survive the list of files with too long filenames (--filelist=FILE)"
  643. datagen -g5M > tmp_badList
  644. zstd -f --filelist=tmp_badList && die "should have failed : file name length is too long"
  645. println "test : survive a list of files which is text garbage (--filelist=FILE)"
  646. datagen > tmp_badList
  647. zstd -f --filelist=tmp_badList && die "should have failed : list is text garbage"
  648. println "test : survive a list of files which is binary garbage (--filelist=FILE)"
  649. datagen -P0 -g1M > tmp_badList
  650. zstd -qq -f --filelist=tmp_badList && die "should have failed : list is binary garbage" # let's avoid printing binary garbage on console
  651. println "test : try to overflow internal list of files (--filelist=FILE)"
  652. touch tmp1 tmp2 tmp3 tmp4 tmp5 tmp6
  653. ls tmp* > tmpList
  654. zstd -f tmp1 --filelist=tmpList --filelist=tmpList tmp2 tmp3 # can trigger an overflow of internal file list
  655. rm -rf tmp*
  656. println "\n===> --[no-]content-size tests"
  657. datagen > tmp_contentsize
  658. zstd -f tmp_contentsize
  659. zstd -lv tmp_contentsize.zst | grep "Decompressed Size:"
  660. zstd -f --no-content-size tmp_contentsize
  661. zstd -lv tmp_contentsize.zst | grep "Decompressed Size:" && die
  662. zstd -f --content-size tmp_contentsize
  663. zstd -lv tmp_contentsize.zst | grep "Decompressed Size:"
  664. zstd -f --content-size --no-content-size tmp_contentsize
  665. zstd -lv tmp_contentsize.zst | grep "Decompressed Size:" && die
  666. rm -rf tmp*
  667. println "test : show-default-cparams regular"
  668. datagen > tmp
  669. zstd --show-default-cparams -f tmp
  670. zstd --show-default-cparams -d tmp.zst && die "error: can't use --show-default-cparams in decompression mode"
  671. rm -rf tmp*
  672. println "test : show-default-cparams recursive"
  673. mkdir tmp_files
  674. datagen -g15000 > tmp_files/tmp1
  675. datagen -g129000 > tmp_files/tmp2
  676. datagen -g257000 > tmp_files/tmp3
  677. zstd --show-default-cparams -f -r tmp_files
  678. rm -rf tmp*
  679. println "test : show compression parameters in verbose mode"
  680. datagen > tmp
  681. zstd -vv tmp 2>&1 | \
  682. grep -q -E -- "--zstd=wlog=[[:digit:]]+,clog=[[:digit:]]+,hlog=[[:digit:]]+,\
  683. slog=[[:digit:]]+,mml=[[:digit:]]+,tlen=[[:digit:]]+,strat=[[:digit:]]+"
  684. rm -rf tmp*
  685. println "\n===> Advanced compression parameters "
  686. println "Hello world!" | zstd --zstd=windowLog=21, - -o tmp.zst && die "wrong parameters not detected!"
  687. println "Hello world!" | zstd --zstd=windowLo=21 - -o tmp.zst && die "wrong parameters not detected!"
  688. println "Hello world!" | zstd --zstd=windowLog=21,slog - -o tmp.zst && die "wrong parameters not detected!"
  689. println "Hello world!" | zstd --zstd=strategy=10 - -o tmp.zst && die "parameter out of bound not detected!" # > btultra2 : does not exist
  690. test ! -f tmp.zst # tmp.zst should not be created
  691. roundTripTest -g512K
  692. roundTripTest -g512K " --zstd=mml=3,tlen=48,strat=6"
  693. roundTripTest -g512K " --zstd=strat=6,wlog=23,clog=23,hlog=22,slog=6"
  694. roundTripTest -g512K " --zstd=windowLog=23,chainLog=23,hashLog=22,searchLog=6,minMatch=3,targetLength=48,strategy=6"
  695. roundTripTest -g512K " --single-thread --long --zstd=ldmHashLog=20,ldmMinMatch=64,ldmBucketSizeLog=1,ldmHashRateLog=7"
  696. roundTripTest -g512K " --single-thread --long --zstd=lhlog=20,lmml=64,lblog=1,lhrlog=7"
  697. roundTripTest -g64K "19 --zstd=strat=9" # btultra2
  698. println "\n===> Pass-Through mode "
  699. println "Hello world 1!" | zstd -df
  700. println "Hello world 2!" | zstd -dcf
  701. println "Hello world 3!" > tmp1
  702. zstd -dcf tmp1
  703. println "\n===> frame concatenation "
  704. println "hello " > hello.tmp
  705. println "world!" > world.tmp
  706. cat hello.tmp world.tmp > helloworld.tmp
  707. zstd -c hello.tmp > hello.zst
  708. zstd -c world.tmp > world.zst
  709. zstd -c hello.tmp world.tmp > helloworld.zst
  710. zstd -dc helloworld.zst > result.tmp
  711. $DIFF helloworld.tmp result.tmp
  712. cat hello.zst world.zst > helloworld.zst
  713. zstd -dc helloworld.zst > result.tmp
  714. cat result.tmp
  715. $DIFF helloworld.tmp result.tmp
  716. println "frame concatenation without checksum"
  717. zstd -c hello.tmp > hello.zst --no-check
  718. zstd -c world.tmp > world.zst --no-check
  719. cat hello.zst world.zst > helloworld.zstd
  720. zstd -dc helloworld.zst > result.tmp
  721. $DIFF helloworld.tmp result.tmp
  722. println "testing zstdcat symlink"
  723. ln -sf "$ZSTD_BIN" zstdcat
  724. $EXE_PREFIX ./zstdcat helloworld.zst > result.tmp
  725. $DIFF helloworld.tmp result.tmp
  726. ln -s helloworld.zst helloworld.link.zst
  727. $EXE_PREFIX ./zstdcat helloworld.link.zst > result.tmp
  728. $DIFF helloworld.tmp result.tmp
  729. rm -f zstdcat
  730. rm -f result.tmp
  731. println "testing zcat symlink"
  732. ln -sf "$ZSTD_BIN" zcat
  733. $EXE_PREFIX ./zcat helloworld.zst > result.tmp
  734. $DIFF helloworld.tmp result.tmp
  735. $EXE_PREFIX ./zcat helloworld.link.zst > result.tmp
  736. $DIFF helloworld.tmp result.tmp
  737. rm -f zcat
  738. rm -f ./*.tmp ./*.zstd
  739. println "frame concatenation tests completed"
  740. if [ "$isWindows" = false ] && [ "$UNAME" != 'SunOS' ] && [ "$UNAME" != "OpenBSD" ] && [ "$UNAME" != "AIX" ]; then
  741. println "\n**** flush write error test **** "
  742. println "println foo | zstd > /dev/full"
  743. println foo | zstd > /dev/full && die "write error not detected!"
  744. println "println foo | zstd | zstd -d > /dev/full"
  745. println foo | zstd | zstd -d > /dev/full && die "write error not detected!"
  746. fi
  747. if [ "$isWindows" = false ] && [ "$UNAME" != 'SunOS' ] ; then
  748. println "\n===> symbolic link test "
  749. rm -f hello.tmp world.tmp world2.tmp hello.tmp.zst world.tmp.zst
  750. println "hello world" > hello.tmp
  751. ln -s hello.tmp world.tmp
  752. ln -s hello.tmp world2.tmp
  753. zstd world.tmp hello.tmp || true
  754. test -f hello.tmp.zst # regular file should have been compressed!
  755. test ! -f world.tmp.zst # symbolic link should not have been compressed!
  756. zstd world.tmp || true
  757. test ! -f world.tmp.zst # symbolic link should not have been compressed!
  758. zstd world.tmp world2.tmp || true
  759. test ! -f world.tmp.zst # symbolic link should not have been compressed!
  760. test ! -f world2.tmp.zst # symbolic link should not have been compressed!
  761. zstd world.tmp hello.tmp -f
  762. test -f world.tmp.zst # symbolic link should have been compressed with --force
  763. rm -f hello.tmp world.tmp world2.tmp hello.tmp.zst world.tmp.zst
  764. fi
  765. println "\n===> test sparse file support "
  766. datagen -g5M -P100 > tmpSparse
  767. zstd tmpSparse -c | zstd -dv -o tmpSparseRegen
  768. $DIFF -s tmpSparse tmpSparseRegen
  769. zstd tmpSparse -c | zstd -dv --sparse -c > tmpOutSparse
  770. $DIFF -s tmpSparse tmpOutSparse
  771. zstd tmpSparse -c | zstd -dv --no-sparse -c > tmpOutNoSparse
  772. $DIFF -s tmpSparse tmpOutNoSparse
  773. ls -ls tmpSparse* # look at file size and block size on disk
  774. datagen -s1 -g1200007 -P100 | zstd | zstd -dv --sparse -c > tmpSparseOdd # Odd size file (to not finish on an exact nb of blocks)
  775. datagen -s1 -g1200007 -P100 | $DIFF -s - tmpSparseOdd
  776. ls -ls tmpSparseOdd # look at file size and block size on disk
  777. println "\n Sparse Compatibility with Console :"
  778. println "Hello World 1 !" | zstd | zstd -d -c
  779. println "Hello World 2 !" | zstd | zstd -d | cat
  780. println "\n Sparse Compatibility with Append :"
  781. datagen -P100 -g1M > tmpSparse1M
  782. cat tmpSparse1M tmpSparse1M > tmpSparse2M
  783. zstd -v -f tmpSparse1M -o tmpSparseCompressed
  784. zstd -d -v -f tmpSparseCompressed -o tmpSparseRegenerated
  785. zstd -d -v -f tmpSparseCompressed -c >> tmpSparseRegenerated
  786. ls -ls tmpSparse* # look at file size and block size on disk
  787. $DIFF tmpSparse2M tmpSparseRegenerated
  788. rm -f tmpSparse*
  789. println "\n===> stream-size mode"
  790. datagen -g11000 > tmp
  791. println "test : basic file compression vs sized streaming compression"
  792. file_size=$(zstd -14 -f tmp -o tmp.zst && wc -c < tmp.zst)
  793. stream_size=$(cat tmp | zstd -14 --stream-size=11000 | wc -c)
  794. if [ "$stream_size" -gt "$file_size" ]; then
  795. die "hinted compression larger than expected"
  796. fi
  797. println "test : sized streaming compression and decompression"
  798. cat tmp | zstd -14 -f tmp -o tmp.zst --stream-size=11000
  799. zstd -df tmp.zst -o tmp_decompress
  800. cmp tmp tmp_decompress || die "difference between original and decompressed file"
  801. println "test : incorrect stream size"
  802. cat tmp | zstd -14 -f -o tmp.zst --stream-size=11001 && die "should fail with incorrect stream size"
  803. println "\n===> zstd zero weight dict test "
  804. rm -f tmp*
  805. cp "$TESTDIR/dict-files/zero-weight-dict" tmp_input
  806. zstd -D "$TESTDIR/dict-files/zero-weight-dict" tmp_input
  807. zstd -D "$TESTDIR/dict-files/zero-weight-dict" -d tmp_input.zst -o tmp_decomp
  808. $DIFF tmp_decomp tmp_input
  809. rm -rf tmp*
  810. println "\n===> zstd (valid) zero weight dict test "
  811. rm -f tmp*
  812. # 0 has a non-zero weight in the dictionary
  813. echo "0000000000000000000000000" > tmp_input
  814. zstd -D "$TESTDIR/dict-files/zero-weight-dict" tmp_input
  815. zstd -D "$TESTDIR/dict-files/zero-weight-dict" -d tmp_input.zst -o tmp_decomp
  816. $DIFF tmp_decomp tmp_input
  817. rm -rf tmp*
  818. println "\n===> size-hint mode"
  819. datagen -g11000 > tmp
  820. datagen -g11000 > tmp2
  821. datagen > tmpDict
  822. println "test : basic file compression vs hinted streaming compression"
  823. file_size=$(zstd -14 -f tmp -o tmp.zst && wc -c < tmp.zst)
  824. stream_size=$(cat tmp | zstd -14 --size-hint=11000 | wc -c)
  825. if [ "$stream_size" -ge "$file_size" ]; then
  826. die "hinted compression larger than expected"
  827. fi
  828. println "test : hinted streaming compression and decompression"
  829. cat tmp | zstd -14 -f -o tmp.zst --size-hint=11000
  830. zstd -df tmp.zst -o tmp_decompress
  831. cmp tmp tmp_decompress || die "difference between original and decompressed file"
  832. println "test : hinted streaming compression with dictionary"
  833. cat tmp | zstd -14 -f -D tmpDict --size-hint=11000 | zstd -t -D tmpDict
  834. println "test : multiple file compression with hints and dictionary"
  835. zstd -14 -f -D tmpDict --size-hint=11000 tmp tmp2
  836. zstd -14 -f -o tmp1_.zst -D tmpDict --size-hint=11000 tmp
  837. zstd -14 -f -o tmp2_.zst -D tmpDict --size-hint=11000 tmp2
  838. cmp tmp.zst tmp1_.zst || die "first file's output differs"
  839. cmp tmp2.zst tmp2_.zst || die "second file's output differs"
  840. println "test : incorrect hinted stream sizes"
  841. cat tmp | zstd -14 -f --size-hint=11050 | zstd -t # slightly too high
  842. cat tmp | zstd -14 -f --size-hint=10950 | zstd -t # slightly too low
  843. cat tmp | zstd -14 -f --size-hint=22000 | zstd -t # considerably too high
  844. cat tmp | zstd -14 -f --size-hint=5500 | zstd -t # considerably too low
  845. println "test : allows and interprets K,KB,KiB,M,MB and MiB suffix"
  846. cat tmp | zstd -14 -f --size-hint=11K | zstd -t
  847. cat tmp | zstd -14 -f --size-hint=11KB | zstd -t
  848. cat tmp | zstd -14 -f --size-hint=11KiB | zstd -t
  849. cat tmp | zstd -14 -f --size-hint=1M | zstd -t
  850. cat tmp | zstd -14 -f --size-hint=1MB | zstd -t
  851. cat tmp | zstd -14 -f --size-hint=1MiB | zstd -t
  852. println "\n===> dictionary tests "
  853. println "- Test high/low compressibility corpus training"
  854. datagen -g12M -P90 > tmpCorpusHighCompress
  855. datagen -g12M -P5 > tmpCorpusLowCompress
  856. zstd --train -B2K tmpCorpusHighCompress -o tmpDictHighCompress
  857. zstd --train -B2K tmpCorpusLowCompress -o tmpDictLowCompress
  858. rm -f tmpCorpusHighCompress tmpCorpusLowCompress tmpDictHighCompress tmpDictLowCompress
  859. println "- Test with raw dict (content only) "
  860. datagen > tmpDict
  861. datagen -g1M | $MD5SUM > tmp1
  862. datagen -g1M | zstd -D tmpDict | zstd -D tmpDict -dvq | $MD5SUM > tmp2
  863. $DIFF -q tmp1 tmp2
  864. println "- Create first dictionary "
  865. TESTFILE="$PRGDIR"/zstdcli.c
  866. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
  867. cp "$TESTFILE" tmp
  868. println "- Test dictionary compression with tmpDict as an input file and dictionary"
  869. zstd -f tmpDict -D tmpDict && die "compression error not detected!"
  870. println "- Dictionary compression roundtrip"
  871. zstd -f tmp -D tmpDict
  872. zstd -d tmp.zst -D tmpDict -fo result
  873. $DIFF "$TESTFILE" result
  874. println "- Dictionary compression with hlog < clog"
  875. zstd -6f tmp -D tmpDict --zstd=clog=25,hlog=23
  876. println "- Dictionary compression with btlazy2 strategy"
  877. zstd -f tmp -D tmpDict --zstd=strategy=6
  878. zstd -d tmp.zst -D tmpDict -fo result
  879. $DIFF "$TESTFILE" result
  880. if [ -e /proc/self/fd/0 ]; then
  881. println "- Test rejecting irregular dictionary file"
  882. cat tmpDict | zstd -f tmp -D /proc/self/fd/0 && die "Piped dictionary should fail!"
  883. cat tmpDict | zstd -d tmp.zst -D /proc/self/fd/0 -f && die "Piped dictionary should fail!"
  884. fi
  885. if [ -n "$hasMT" ]
  886. then
  887. println "- Test dictionary compression with multithreading "
  888. datagen -g5M | zstd -T2 -D tmpDict | zstd -t -D tmpDict # fails with v1.3.2
  889. fi
  890. println "- Create second (different) dictionary "
  891. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
  892. zstd -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
  893. println "- Create dictionary with short dictID"
  894. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
  895. cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
  896. println "- Create dictionary with wrong dictID parameter order (must fail)"
  897. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID -o 1 tmpDict1 && die "wrong order : --dictID must be followed by argument "
  898. println "- Create dictionary with size limit"
  899. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K -v
  900. println "- Create dictionary with small size limit"
  901. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict3 --maxdict=1K -v
  902. println "- Create dictionary with wrong parameter order (must fail)"
  903. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict3 --maxdict -v 4K && die "wrong order : --maxdict must be followed by argument "
  904. println "- Compress without dictID"
  905. zstd -f tmp -D tmpDict1 --no-dictID
  906. zstd -d tmp.zst -D tmpDict -fo result
  907. $DIFF "$TESTFILE" result
  908. println "- Compress multiple files with dictionary"
  909. rm -rf dirTestDict
  910. mkdir dirTestDict
  911. cp "$TESTDIR"/*.c dirTestDict
  912. cp "$PRGDIR"/*.c dirTestDict
  913. cp "$PRGDIR"/*.h dirTestDict
  914. $MD5SUM dirTestDict/* > tmph1
  915. zstd -f --rm dirTestDict/* -D tmpDictC
  916. zstd -d --rm dirTestDict/*.zst -D tmpDictC # note : use internal checksum by default
  917. case "$UNAME" in
  918. Darwin) println "md5sum -c not supported on OS-X : test skipped" ;; # not compatible with OS-X's md5
  919. *) $MD5SUM -c tmph1 ;;
  920. esac
  921. rm -rf dirTestDict
  922. println "- dictionary builder on bogus input"
  923. println "Hello World" > tmp
  924. zstd --train-legacy -q tmp && die "Dictionary training should fail : not enough input source"
  925. datagen -P0 -g10M > tmp
  926. zstd --train-legacy -q tmp && die "Dictionary training should fail : source is pure noise"
  927. println "- Test -o before --train"
  928. rm -f tmpDict dictionary
  929. zstd -o tmpDict --train "$TESTDIR"/*.c "$PRGDIR"/*.c
  930. test -f tmpDict
  931. zstd --train "$TESTDIR"/*.c "$PRGDIR"/*.c
  932. test -f dictionary
  933. if [ -n "$hasMT" ]
  934. then
  935. println "- Create dictionary with multithreading enabled"
  936. zstd --train -T0 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
  937. fi
  938. rm -f tmp* dictionary
  939. println "- Test --memory for dictionary compression"
  940. datagen -g12M -P90 > tmpCorpusHighCompress
  941. zstd --train -B2K tmpCorpusHighCompress -o tmpDictHighCompress --memory=10K && die "Dictionary training should fail : --memory too low (10K)"
  942. zstd --train -B2K tmpCorpusHighCompress -o tmpDictHighCompress --memory=5MB 2> zstTrainWithMemLimitStdErr
  943. cat zstTrainWithMemLimitStdErr | grep "setting manual memory limit for dictionary training data at 5 MB"
  944. cat zstTrainWithMemLimitStdErr | grep "Training samples set too large (12 MB); training on 5 MB only..."
  945. rm zstTrainWithMemLimitStdErr
  946. println "\n===> fastCover dictionary builder : advanced options "
  947. TESTFILE="$PRGDIR"/zstdcli.c
  948. datagen > tmpDict
  949. println "- Create first dictionary"
  950. zstd --train-fastcover=k=46,d=8,f=15,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
  951. cp "$TESTFILE" tmp
  952. zstd -f tmp -D tmpDict
  953. zstd -d tmp.zst -D tmpDict -fo result
  954. $DIFF "$TESTFILE" result
  955. println "- Create second (different) dictionary"
  956. zstd --train-fastcover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
  957. zstd -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
  958. zstd --train-fastcover=k=56,d=8 && die "Create dictionary without input file"
  959. println "- Create dictionary with short dictID"
  960. zstd --train-fastcover=k=46,d=8,f=15,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
  961. cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
  962. println "- Create dictionaries with shrink-dict flag enabled"
  963. zstd --train-fastcover=steps=1,shrink "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict
  964. zstd --train-fastcover=steps=1,shrink=1 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict1
  965. zstd --train-fastcover=steps=1,shrink=5 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict2
  966. zstd --train-fastcover=shrink=5,steps=1 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpShrinkDict3
  967. println "- Create dictionary with size limit"
  968. zstd --train-fastcover=steps=1 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K
  969. println "- Create dictionary using all samples for both training and testing"
  970. zstd --train-fastcover=k=56,d=8,split=100 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  971. println "- Create dictionary using f=16"
  972. zstd --train-fastcover=k=56,d=8,f=16 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  973. zstd --train-fastcover=k=56,d=8,accel=15 -r "$TESTDIR"/*.c "$PRGDIR"/*.c && die "Created dictionary using accel=15"
  974. println "- Create dictionary using accel=2"
  975. zstd --train-fastcover=k=56,d=8,accel=2 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  976. println "- Create dictionary using accel=10"
  977. zstd --train-fastcover=k=56,d=8,accel=10 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  978. println "- Create dictionary with multithreading"
  979. zstd --train-fastcover -T4 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  980. println "- Test -o before --train-fastcover"
  981. rm -f tmpDict dictionary
  982. zstd -o tmpDict --train-fastcover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c
  983. test -f tmpDict
  984. zstd --train-fastcover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c
  985. test -f dictionary
  986. rm -f tmp* dictionary
  987. println "\n===> legacy dictionary builder "
  988. TESTFILE="$PRGDIR"/zstdcli.c
  989. datagen > tmpDict
  990. println "- Create first dictionary"
  991. zstd --train-legacy=selectivity=8 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
  992. cp "$TESTFILE" tmp
  993. zstd -f tmp -D tmpDict
  994. zstd -d tmp.zst -D tmpDict -fo result
  995. $DIFF "$TESTFILE" result
  996. zstd --train-legacy=s=8 && die "Create dictionary without input files (should error)"
  997. println "- Create second (different) dictionary"
  998. zstd --train-legacy=s=5 "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
  999. zstd -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
  1000. println "- Create dictionary with short dictID"
  1001. zstd --train-legacy -s5 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
  1002. cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
  1003. println "- Create dictionary with size limit"
  1004. zstd --train-legacy -s9 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K
  1005. println "- Test -o before --train-legacy"
  1006. rm -f tmpDict dictionary
  1007. zstd -o tmpDict --train-legacy "$TESTDIR"/*.c "$PRGDIR"/*.c
  1008. test -f tmpDict
  1009. zstd --train-legacy "$TESTDIR"/*.c "$PRGDIR"/*.c
  1010. test -f dictionary
  1011. rm -f tmp* dictionary
  1012. println "\n===> integrity tests "
  1013. println "test one file (tmp1.zst) "
  1014. datagen > tmp1
  1015. zstd tmp1
  1016. zstd -t tmp1.zst
  1017. zstd --test tmp1.zst
  1018. println "test multiple files (*.zst) "
  1019. zstd -t ./*.zst
  1020. println "test bad files (*) "
  1021. zstd -t ./* && die "bad files not detected !"
  1022. zstd -t tmp1 && die "bad file not detected !"
  1023. cp tmp1 tmp2.zst
  1024. zstd -t tmp2.zst && die "bad file not detected !"
  1025. datagen -g0 > tmp3
  1026. zstd -t tmp3 && die "bad file not detected !" # detects 0-sized files as bad
  1027. println "test --rm and --test combined "
  1028. zstd -t --rm tmp1.zst
  1029. test -f tmp1.zst # check file is still present
  1030. split -b16384 tmp1.zst tmpSplit.
  1031. zstd -t tmpSplit.* && die "bad file not detected !"
  1032. datagen | zstd -c | zstd -t
  1033. println "\n===> golden files tests "
  1034. zstd -t -r "$TESTDIR/golden-decompression"
  1035. zstd -c -r "$TESTDIR/golden-compression" | zstd -t
  1036. zstd -D "$TESTDIR/golden-dictionaries/http-dict-missing-symbols" "$TESTDIR/golden-compression/http" -c | zstd -D "$TESTDIR/golden-dictionaries/http-dict-missing-symbols" -t
  1037. println "\n===> benchmark mode tests "
  1038. println "bench one file"
  1039. datagen > tmp1
  1040. zstd -bi0 tmp1
  1041. println "bench multiple levels"
  1042. zstd -i0b0e3 tmp1
  1043. println "bench negative level"
  1044. zstd -bi0 --fast tmp1
  1045. println "with recursive and quiet modes"
  1046. zstd -rqi0b1e2 tmp1
  1047. println "benchmark decompression only"
  1048. zstd -f tmp1
  1049. zstd -b -d -i0 tmp1.zst
  1050. println "\n===> zstd compatibility tests "
  1051. datagen > tmp
  1052. rm -f tmp.zst
  1053. zstd --format=zstd -f tmp
  1054. test -f tmp.zst
  1055. println "\n===> gzip compatibility tests "
  1056. GZIPMODE=1
  1057. zstd --format=gzip -V || GZIPMODE=0
  1058. if [ $GZIPMODE -eq 1 ]; then
  1059. println "gzip support detected"
  1060. GZIPEXE=1
  1061. gzip -V || GZIPEXE=0
  1062. if [ $GZIPEXE -eq 1 ]; then
  1063. datagen > tmp
  1064. zstd --format=gzip -f tmp
  1065. gzip -t -v tmp.gz
  1066. gzip -f tmp
  1067. zstd -d -f -v tmp.gz
  1068. rm -f tmp*
  1069. else
  1070. println "gzip binary not detected"
  1071. fi
  1072. else
  1073. println "gzip mode not supported"
  1074. fi
  1075. println "\n===> gzip frame tests "
  1076. if [ $GZIPMODE -eq 1 ]; then
  1077. datagen > tmp
  1078. zstd -f --format=gzip tmp
  1079. zstd -f tmp
  1080. cat tmp.gz tmp.zst tmp.gz tmp.zst | zstd -d -f -o tmp
  1081. truncateLastByte tmp.gz | zstd -t > $INTOVOID && die "incomplete frame not detected !"
  1082. rm -f tmp*
  1083. else
  1084. println "gzip mode not supported"
  1085. fi
  1086. if [ $GZIPMODE -eq 1 ]; then
  1087. datagen > tmp
  1088. rm -f tmp.zst
  1089. zstd --format=gzip --format=zstd -f tmp
  1090. test -f tmp.zst
  1091. fi
  1092. println "\n===> xz compatibility tests "
  1093. LZMAMODE=1
  1094. zstd --format=xz -V || LZMAMODE=0
  1095. if [ $LZMAMODE -eq 1 ]; then
  1096. println "xz support detected"
  1097. XZEXE=1
  1098. xz -Q -V && lzma -Q -V || XZEXE=0
  1099. if [ $XZEXE -eq 1 ]; then
  1100. println "Testing zstd xz and lzma support"
  1101. datagen > tmp
  1102. zstd --format=lzma -f tmp
  1103. zstd --format=xz -f tmp
  1104. xz -Q -t -v tmp.xz
  1105. xz -Q -t -v tmp.lzma
  1106. xz -Q -f -k tmp
  1107. lzma -Q -f -k --lzma1 tmp
  1108. zstd -d -f -v tmp.xz
  1109. zstd -d -f -v tmp.lzma
  1110. rm -f tmp*
  1111. println "Creating symlinks"
  1112. ln -s "$ZSTD_BIN" ./xz
  1113. ln -s "$ZSTD_BIN" ./unxz
  1114. ln -s "$ZSTD_BIN" ./lzma
  1115. ln -s "$ZSTD_BIN" ./unlzma
  1116. println "Testing xz and lzma symlinks"
  1117. datagen > tmp
  1118. ./xz tmp
  1119. xz -Q -d tmp.xz
  1120. ./lzma tmp
  1121. lzma -Q -d tmp.lzma
  1122. println "Testing unxz and unlzma symlinks"
  1123. xz -Q tmp
  1124. ./xz -d tmp.xz
  1125. lzma -Q tmp
  1126. ./lzma -d tmp.lzma
  1127. rm -f xz unxz lzma unlzma
  1128. rm -f tmp*
  1129. else
  1130. println "xz binary not detected"
  1131. fi
  1132. else
  1133. println "xz mode not supported"
  1134. fi
  1135. println "\n===> xz frame tests "
  1136. if [ $LZMAMODE -eq 1 ]; then
  1137. datagen > tmp
  1138. zstd -f --format=xz tmp
  1139. zstd -f --format=lzma tmp
  1140. zstd -f tmp
  1141. cat tmp.xz tmp.lzma tmp.zst tmp.lzma tmp.xz tmp.zst | zstd -d -f -o tmp
  1142. truncateLastByte tmp.xz | zstd -t > $INTOVOID && die "incomplete frame not detected !"
  1143. truncateLastByte tmp.lzma | zstd -t > $INTOVOID && die "incomplete frame not detected !"
  1144. rm -f tmp*
  1145. else
  1146. println "xz mode not supported"
  1147. fi
  1148. println "\n===> lz4 compatibility tests "
  1149. LZ4MODE=1
  1150. zstd --format=lz4 -V || LZ4MODE=0
  1151. if [ $LZ4MODE -eq 1 ]; then
  1152. println "lz4 support detected"
  1153. LZ4EXE=1
  1154. lz4 -V || LZ4EXE=0
  1155. if [ $LZ4EXE -eq 1 ]; then
  1156. datagen > tmp
  1157. zstd --format=lz4 -f tmp
  1158. lz4 -t -v tmp.lz4
  1159. lz4 -f -m tmp # ensure result is sent into tmp.lz4, not stdout
  1160. zstd -d -f -v tmp.lz4
  1161. rm -f tmp*
  1162. else
  1163. println "lz4 binary not detected"
  1164. fi
  1165. else
  1166. println "lz4 mode not supported"
  1167. fi
  1168. if [ $LZ4MODE -eq 1 ]; then
  1169. println "\n===> lz4 frame tests "
  1170. datagen > tmp
  1171. zstd -f --format=lz4 tmp
  1172. zstd -f tmp
  1173. cat tmp.lz4 tmp.zst tmp.lz4 tmp.zst | zstd -d -f -o tmp
  1174. truncateLastByte tmp.lz4 | zstd -t > $INTOVOID && die "incomplete frame not detected !"
  1175. rm -f tmp*
  1176. else
  1177. println "\nlz4 mode not supported"
  1178. fi
  1179. println "\n===> suffix list test"
  1180. ! zstd -d tmp.abc 2> tmplg
  1181. if [ $GZIPMODE -ne 1 ]; then
  1182. grep ".gz" tmplg > $INTOVOID && die "Unsupported suffix listed"
  1183. fi
  1184. if [ $LZMAMODE -ne 1 ]; then
  1185. grep ".lzma" tmplg > $INTOVOID && die "Unsupported suffix listed"
  1186. grep ".xz" tmplg > $INTOVOID && die "Unsupported suffix listed"
  1187. fi
  1188. if [ $LZ4MODE -ne 1 ]; then
  1189. grep ".lz4" tmplg > $INTOVOID && die "Unsupported suffix listed"
  1190. fi
  1191. touch tmp1
  1192. zstd tmp1 -o tmp1.zstd
  1193. zstd -d -f tmp1.zstd # support .zstd suffix even though it's not the default suffix
  1194. println "\n===> tar extension tests "
  1195. rm -f tmp tmp.tar tmp.tzst tmp.tgz tmp.txz tmp.tlz4 tmp1.zstd
  1196. datagen > tmp
  1197. tar -cf tmp.tar tmp
  1198. zstd tmp.tar -o tmp.tzst
  1199. rm -f tmp.tar
  1200. zstd -d tmp.tzst
  1201. [ -e tmp.tar ] || die ".tzst failed to decompress to .tar!"
  1202. rm -f tmp.tar tmp.tzst
  1203. if [ $GZIPMODE -eq 1 ]; then
  1204. tar -f - -c tmp | gzip > tmp.tgz
  1205. zstd -d tmp.tgz
  1206. [ -e tmp.tar ] || die ".tgz failed to decompress to .tar!"
  1207. rm -f tmp.tar tmp.tgz
  1208. fi
  1209. if [ $LZMAMODE -eq 1 ]; then
  1210. tar -f - -c tmp | zstd --format=xz > tmp.txz
  1211. zstd -d tmp.txz
  1212. [ -e tmp.tar ] || die ".txz failed to decompress to .tar!"
  1213. rm -f tmp.tar tmp.txz
  1214. fi
  1215. if [ $LZ4MODE -eq 1 ]; then
  1216. tar -f - -c tmp | zstd --format=lz4 > tmp.tlz4
  1217. zstd -d tmp.tlz4
  1218. [ -e tmp.tar ] || die ".tlz4 failed to decompress to .tar!"
  1219. rm -f tmp.tar tmp.tlz4
  1220. fi
  1221. touch tmp.t tmp.tz tmp.tzs
  1222. ! zstd -d tmp.t
  1223. ! zstd -d tmp.tz
  1224. ! zstd -d tmp.tzs
  1225. println "\n===> zstd round-trip tests "
  1226. roundTripTest
  1227. roundTripTest -g15K # TableID==3
  1228. roundTripTest -g127K # TableID==2
  1229. roundTripTest -g255K # TableID==1
  1230. roundTripTest -g522K # TableID==0
  1231. roundTripTest -g519K 6 # greedy, hash chain
  1232. roundTripTest -g517K 16 # btlazy2
  1233. roundTripTest -g516K 19 # btopt
  1234. fileRoundTripTest -g500K
  1235. println "\n===> zstd long distance matching round-trip tests "
  1236. roundTripTest -g0 "2 --single-thread --long"
  1237. roundTripTest -g1000K "1 --single-thread --long"
  1238. roundTripTest -g517K "6 --single-thread --long"
  1239. roundTripTest -g516K "16 --single-thread --long"
  1240. roundTripTest -g518K "19 --single-thread --long"
  1241. roundTripTest -g2M "22 --single-thread --ultra --long"
  1242. fileRoundTripTest -g5M "3 --single-thread --long"
  1243. roundTripTest -g96K "5 --single-thread"
  1244. if [ -n "$hasMT" ]
  1245. then
  1246. println "\n===> zstdmt round-trip tests "
  1247. roundTripTest -g4M "1 -T0"
  1248. roundTripTest -g4M "1 -T0 --auto-threads=physical"
  1249. roundTripTest -g4M "1 -T0 --auto-threads=logical"
  1250. roundTripTest -g8M "3 -T2"
  1251. roundTripTest -g8M "19 --long"
  1252. roundTripTest -g8000K "2 --threads=2"
  1253. fileRoundTripTest -g4M "19 -T2 -B1M"
  1254. println "\n===> zstdmt long distance matching round-trip tests "
  1255. roundTripTest -g8M "3 --long=24 -T2"
  1256. println "\n===> zstdmt environment variable tests "
  1257. echo "multifoo" >> mt_tmp
  1258. ZSTD_NBTHREADS=-3 zstd -f mt_tmp # negative value, warn and revert to default setting
  1259. ZSTD_NBTHREADS='' zstd -f mt_tmp # empty env var, warn and revert to default setting
  1260. ZSTD_NBTHREADS=- zstd -f mt_tmp # malformed env var, warn and revert to default setting
  1261. ZSTD_NBTHREADS=a zstd -f mt_tmp # malformed env var, warn and revert to default setting
  1262. ZSTD_NBTHREADS=+a zstd -f mt_tmp # malformed env var, warn and revert to default setting
  1263. ZSTD_NBTHREADS=3a7 zstd -f mt_tmp # malformed env var, warn and revert to default setting
  1264. ZSTD_NBTHREADS=50000000000 zstd -f mt_tmp # numeric value too large, warn and revert to default setting=
  1265. ZSTD_NBTHREADS=2 zstd -f mt_tmp # correct usage
  1266. ZSTD_NBTHREADS=1 zstd -f mt_tmp # correct usage: single thread
  1267. rm -f mt_tmp*
  1268. println "\n===> ovLog tests "
  1269. datagen -g2MB > tmp
  1270. refSize=$(zstd tmp -6 -c --zstd=wlog=18 | wc -c)
  1271. ov9Size=$(zstd tmp -6 -c --zstd=wlog=18,ovlog=9 | wc -c)
  1272. ov1Size=$(zstd tmp -6 -c --zstd=wlog=18,ovlog=1 | wc -c)
  1273. if [ "$refSize" -eq "$ov9Size" ]; then
  1274. echo ov9Size should be different from refSize
  1275. exit 1
  1276. fi
  1277. if [ "$refSize" -eq "$ov1Size" ]; then
  1278. echo ov1Size should be different from refSize
  1279. exit 1
  1280. fi
  1281. if [ "$ov9Size" -ge "$ov1Size" ]; then
  1282. echo ov9Size="$ov9Size" should be smaller than ov1Size="$ov1Size"
  1283. exit 1
  1284. fi
  1285. else
  1286. println "\n===> no multithreading, skipping zstdmt tests "
  1287. fi
  1288. rm -f tmp*
  1289. println "\n===> zstd --list/-l single frame tests "
  1290. datagen > tmp1
  1291. datagen > tmp2
  1292. datagen > tmp3
  1293. zstd tmp*
  1294. zstd -l ./*.zst
  1295. zstd -lv ./*.zst | grep "Decompressed Size:" # check that decompressed size is present in header
  1296. zstd --list ./*.zst
  1297. zstd --list -v ./*.zst
  1298. println "\n===> zstd --list/-l multiple frame tests "
  1299. cat tmp1.zst tmp2.zst > tmp12.zst
  1300. cat tmp12.zst tmp3.zst > tmp123.zst
  1301. zstd -l ./*.zst
  1302. zstd -lv ./*.zst
  1303. println "\n===> zstd --list/-l error detection tests "
  1304. zstd -l tmp1 tmp1.zst && die "-l must fail on non-zstd file"
  1305. zstd --list tmp* && die "-l must fail on non-zstd file"
  1306. zstd -lv tmp1* && die "-l must fail on non-zstd file"
  1307. zstd --list -v tmp2 tmp12.zst && die "-l must fail on non-zstd file"
  1308. println "test : detect truncated compressed file "
  1309. TEST_DATA_FILE=truncatable-input.txt
  1310. FULL_COMPRESSED_FILE=${TEST_DATA_FILE}.zst
  1311. TRUNCATED_COMPRESSED_FILE=truncated-input.txt.zst
  1312. datagen -g50000 > $TEST_DATA_FILE
  1313. zstd -f $TEST_DATA_FILE -o $FULL_COMPRESSED_FILE
  1314. dd bs=1 count=100 if=$FULL_COMPRESSED_FILE of=$TRUNCATED_COMPRESSED_FILE
  1315. zstd --list $TRUNCATED_COMPRESSED_FILE && die "-l must fail on truncated file"
  1316. rm -f $TEST_DATA_FILE
  1317. rm -f $FULL_COMPRESSED_FILE
  1318. rm -f $TRUNCATED_COMPRESSED_FILE
  1319. println "\n===> zstd --list/-l errors when presented with stdin / no files"
  1320. zstd -l && die "-l must fail on empty list of files"
  1321. zstd -l - && die "-l does not work on stdin"
  1322. zstd -l < tmp1.zst && die "-l does not work on stdin"
  1323. zstd -l - < tmp1.zst && die "-l does not work on stdin"
  1324. zstd -l - tmp1.zst && die "-l does not work on stdin"
  1325. zstd -l - tmp1.zst < tmp1.zst && die "-l does not work on stdin"
  1326. zstd -l tmp1.zst < tmp2.zst # this will check tmp1.zst, but not tmp2.zst, which is not an error : zstd simply doesn't read stdin in this case. It must not error just because stdin is not a tty
  1327. println "\n===> zstd --list/-l test with null files "
  1328. datagen -g0 > tmp5
  1329. zstd tmp5
  1330. zstd -l tmp5.zst
  1331. zstd -l tmp5* && die "-l must fail on non-zstd file"
  1332. zstd -lv tmp5.zst | grep "Decompressed Size: 0 B (0 B)" # check that 0 size is present in header
  1333. zstd -lv tmp5* && die "-l must fail on non-zstd file"
  1334. println "\n===> zstd --list/-l test with no content size field "
  1335. datagen -g513K | zstd > tmp6.zst
  1336. zstd -l tmp6.zst
  1337. zstd -lv tmp6.zst | grep "Decompressed Size:" && die "Field :Decompressed Size: should not be available in this compressed file"
  1338. println "\n===> zstd --list/-l test with no checksum "
  1339. zstd -f --no-check tmp1
  1340. zstd -l tmp1.zst
  1341. zstd -lv tmp1.zst
  1342. println "\n===> zstd trace tests "
  1343. zstd -f --trace tmp.trace tmp1
  1344. zstd -f --trace tmp.trace tmp1 tmp2 tmp3
  1345. zstd -f --trace tmp.trace tmp1 tmp2 tmp3 -o /dev/null
  1346. zstd -f --trace tmp.trace tmp1 tmp2 tmp3 --single-thread
  1347. zstd -f --trace tmp.trace -D tmp1 tmp2 tmp3 -o /dev/null
  1348. zstd -f --trace tmp.trace -D tmp1 tmp2 tmp3 -o /dev/null --single-thread
  1349. zstd --trace tmp.trace -t tmp1.zst
  1350. zstd --trace tmp.trace -t tmp1.zst tmp2.zst
  1351. zstd -f --trace tmp.trace -d tmp1.zst
  1352. zstd -f --trace tmp.trace -d tmp1.zst tmp2.zst tmp3.zst
  1353. zstd -D tmp1 tmp2 -c | zstd --trace tmp.trace -t -D tmp1
  1354. zstd -b1e10i0 --trace tmp.trace tmp1
  1355. zstd -b1e10i0 --trace tmp.trace tmp1 tmp2 tmp3
  1356. rm -f tmp*
  1357. println "\n===> zstd long distance matching tests "
  1358. roundTripTest -g0 " --single-thread --long"
  1359. roundTripTest -g9M "2 --single-thread --long"
  1360. # Test parameter parsing
  1361. roundTripTest -g1M -P50 "1 --single-thread --long=29" " --memory=512MB"
  1362. roundTripTest -g1M -P50 "1 --single-thread --long=29 --zstd=wlog=28" " --memory=256MB"
  1363. roundTripTest -g1M -P50 "1 --single-thread --long=29" " --long=28 --memory=512MB"
  1364. roundTripTest -g1M -P50 "1 --single-thread --long=29" " --zstd=wlog=28 --memory=512MB"
  1365. println "\n===> zstd long distance matching with optimal parser compressed size tests "
  1366. optCSize16=$(datagen -g511K | zstd -16 -c | wc -c)
  1367. longCSize16=$(datagen -g511K | zstd -16 --long -c | wc -c)
  1368. optCSize19=$(datagen -g2M | zstd -19 -c | wc -c)
  1369. longCSize19=$(datagen -g2M | zstd -19 --long -c | wc -c)
  1370. optCSize19wlog23=$(datagen -g2M | zstd -19 -c --zstd=wlog=23 | wc -c)
  1371. longCSize19wlog23=$(datagen -g2M | zstd -19 -c --long=23 | wc -c)
  1372. if [ "$longCSize16" -gt "$optCSize16" ]; then
  1373. echo using --long on compression level 16 should not cause compressed size regression
  1374. exit 1
  1375. elif [ "$longCSize19" -gt "$optCSize19" ]; then
  1376. echo using --long on compression level 19 should not cause compressed size regression
  1377. exit 1
  1378. elif [ "$longCSize19wlog23" -gt "$optCSize19wlog23" ]; then
  1379. echo using --long on compression level 19 with wLog=23 should not cause compressed size regression
  1380. exit 1
  1381. fi
  1382. if [ "$1" != "--test-large-data" ]; then
  1383. println "Skipping large data tests"
  1384. exit 0
  1385. fi
  1386. #############################################################################
  1387. if [ -n "$hasMT" ]
  1388. then
  1389. println "\n===> adaptive mode "
  1390. roundTripTest -g270000000 " --adapt"
  1391. roundTripTest -g27000000 " --adapt=min=1,max=4"
  1392. roundTripTest -g27000000 " --adapt=min=-2,max=-1"
  1393. println "===> test: --adapt must fail on incoherent bounds "
  1394. datagen > tmp
  1395. zstd --adapt= tmp && die "invalid compression parameter"
  1396. zstd -f -vv --adapt=min=10,max=9 tmp && die "--adapt must fail on incoherent bounds"
  1397. println "\n===> rsyncable mode "
  1398. roundTripTest -g10M " --rsyncable"
  1399. roundTripTest -g10M " --rsyncable -B100K"
  1400. println "===> test: --rsyncable must fail with --single-thread"
  1401. zstd -f -vv --rsyncable --single-thread tmp && die "--rsyncable must fail with --single-thread"
  1402. fi
  1403. println "\n===> patch-from=origin tests"
  1404. datagen -g1000 -P50 > tmp_dict
  1405. datagen -g1000 -P10 > tmp_patch
  1406. zstd --patch-from=tmp_dict tmp_patch -o tmp_patch_diff
  1407. zstd -d --patch-from=tmp_dict tmp_patch_diff -o tmp_patch_recon
  1408. $DIFF -s tmp_patch_recon tmp_patch
  1409. println "\n===> alternate syntax: patch-from origin"
  1410. zstd -f --patch-from tmp_dict tmp_patch -o tmp_patch_diff
  1411. zstd -df --patch-from tmp_dict tmp_patch_diff -o tmp_patch_recon
  1412. $DIFF -s tmp_patch_recon tmp_patch
  1413. rm -rf tmp_*
  1414. println "\n===> patch-from recursive tests"
  1415. mkdir tmp_dir
  1416. datagen > tmp_dir/tmp1
  1417. datagen > tmp_dir/tmp2
  1418. datagen > tmp_dict
  1419. zstd --patch-from=tmp_dict -r tmp_dir && die
  1420. rm -rf tmp*
  1421. println "\n===> patch-from long mode trigger larger file test"
  1422. datagen -g5000000 > tmp_dict
  1423. datagen -g5000000 > tmp_patch
  1424. zstd -15 --patch-from=tmp_dict tmp_patch 2>&1 | grep "long mode automatically triggered"
  1425. rm -rf tmp*
  1426. println "\n===> patch-from very large dictionary and file test"
  1427. datagen -g550000000 -P0 > tmp_dict
  1428. datagen -g100000000 -P1 > tmp_patch
  1429. zstd --long=30 -1f --patch-from tmp_dict tmp_patch
  1430. zstd --long=30 -df --patch-from tmp_dict tmp_patch.zst -o tmp_patch_recon
  1431. $DIFF -s tmp_patch_recon tmp_patch
  1432. rm -rf tmp*
  1433. println "\n===> patch-from --stream-size test"
  1434. datagen -g1000 -P50 > tmp_dict
  1435. datagen -g1000 -P10 > tmp_patch
  1436. cat tmp_patch | zstd -f --patch-from=tmp_dict -c -o tmp_patch_diff && die
  1437. cat tmp_patch | zstd -f --patch-from=tmp_dict --stream-size=1000 -c -o tmp_patch_diff
  1438. rm -rf tmp*
  1439. println "\n===> large files tests "
  1440. roundTripTest -g270000000 1
  1441. roundTripTest -g250000000 2
  1442. roundTripTest -g230000000 3
  1443. roundTripTest -g140000000 -P60 4
  1444. roundTripTest -g130000000 -P62 5
  1445. roundTripTest -g120000000 -P65 6
  1446. roundTripTest -g70000000 -P70 7
  1447. roundTripTest -g60000000 -P71 8
  1448. roundTripTest -g50000000 -P73 9
  1449. roundTripTest -g35000000 -P75 10
  1450. roundTripTest -g30000000 -P76 11
  1451. roundTripTest -g25000000 -P78 12
  1452. roundTripTest -g18000013 -P80 13
  1453. roundTripTest -g18000014 -P80 14
  1454. roundTripTest -g18000015 -P81 15
  1455. roundTripTest -g18000016 -P84 16
  1456. roundTripTest -g18000017 -P88 17
  1457. roundTripTest -g18000018 -P94 18
  1458. roundTripTest -g18000019 -P96 19
  1459. roundTripTest -g5000000000 -P99 "1 --zstd=wlog=25"
  1460. roundTripTest -g3700000000 -P0 "1 --zstd=strategy=6,wlog=25" # ensure btlazy2 can survive an overflow rescale
  1461. fileRoundTripTest -g4193M -P99 1
  1462. println "\n===> zstd long, long distance matching round-trip tests "
  1463. roundTripTest -g270000000 "1 --single-thread --long"
  1464. roundTripTest -g130000000 -P60 "5 --single-thread --long"
  1465. roundTripTest -g35000000 -P70 "8 --single-thread --long"
  1466. roundTripTest -g18000001 -P80 "18 --single-thread --long"
  1467. # Test large window logs
  1468. roundTripTest -g700M -P50 "1 --single-thread --long=29"
  1469. roundTripTest -g600M -P50 "1 --single-thread --long --zstd=wlog=29,clog=28"
  1470. if [ -n "$hasMT" ]
  1471. then
  1472. println "\n===> zstdmt long round-trip tests "
  1473. roundTripTest -g80000000 -P99 "19 -T2" " "
  1474. roundTripTest -g5000000000 -P99 "1 -T2" " "
  1475. roundTripTest -g500000000 -P97 "1 -T999" " "
  1476. fileRoundTripTest -g4103M -P98 " -T0" " "
  1477. roundTripTest -g400000000 -P97 "1 --long=24 -T2" " "
  1478. # Exposes the bug in https://github.com/facebook/zstd/pull/1678
  1479. # This test fails on 4 different travis builds at the time of writing
  1480. # because it needs to allocate 8 GB of memory.
  1481. # roundTripTest -g10G -P99 "1 -T1 --long=31 --zstd=clog=27 --fast=1000"
  1482. else
  1483. println "\n**** no multithreading, skipping zstdmt tests **** "
  1484. fi
  1485. println "\n===> cover dictionary builder : advanced options "
  1486. TESTFILE="$PRGDIR"/zstdcli.c
  1487. datagen > tmpDict
  1488. println "- Create first dictionary"
  1489. zstd --train-cover=k=46,d=8,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict
  1490. cp "$TESTFILE" tmp
  1491. zstd -f tmp -D tmpDict
  1492. zstd -f tmp -D tmpDict --patch-from=tmpDict && die "error: can't use -D and --patch-from=#at the same time"
  1493. zstd -d tmp.zst -D tmpDict -fo result
  1494. $DIFF "$TESTFILE" result
  1495. zstd --train-cover=k=56,d=8 && die "Create dictionary without input file (should error)"
  1496. println "- Create second (different) dictionary"
  1497. zstd --train-cover=k=56,d=8 "$TESTDIR"/*.c "$PRGDIR"/*.c "$PRGDIR"/*.h -o tmpDictC
  1498. zstd -d tmp.zst -D tmpDictC -fo result && die "wrong dictionary not detected!"
  1499. println "- Create dictionary using shrink-dict flag"
  1500. zstd --train-cover=steps=256,shrink "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict
  1501. zstd --train-cover=steps=256,shrink=1 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict1
  1502. zstd --train-cover=steps=256,shrink=5 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict2
  1503. zstd --train-cover=shrink=5,steps=256 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpShrinkDict3
  1504. println "- Create dictionary with short dictID"
  1505. zstd --train-cover=k=46,d=8,split=80 "$TESTDIR"/*.c "$PRGDIR"/*.c --dictID=1 -o tmpDict1
  1506. cmp tmpDict tmpDict1 && die "dictionaries should have different ID !"
  1507. println "- Create dictionary with size limit"
  1508. zstd --train-cover=steps=8 "$TESTDIR"/*.c "$PRGDIR"/*.c -o tmpDict2 --maxdict=4K
  1509. println "- Compare size of dictionary from 90% training samples with 80% training samples"
  1510. zstd --train-cover=split=90 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  1511. zstd --train-cover=split=80 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  1512. println "- Create dictionary using all samples for both training and testing"
  1513. zstd --train-cover=split=100 -r "$TESTDIR"/*.c "$PRGDIR"/*.c
  1514. println "- Test -o before --train-cover"
  1515. rm -f tmpDict dictionary
  1516. zstd -o tmpDict --train-cover "$TESTDIR"/*.c "$PRGDIR"/*.c
  1517. test -f tmpDict
  1518. zstd --train-cover "$TESTDIR"/*.c "$PRGDIR"/*.c
  1519. test -f dictionary
  1520. rm -f tmp* dictionary
  1521. rm -f tmp*