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