Compress-Stream-Zstd

 view release on metacpan or  search on metacpan

ext/zstd/tests/playTests.sh  view on Meta::CPAN


# temporary envvar changes in the above tests would actually persist in macos /bin/sh
unset ZSTD_CLEVEL


println "test : compress to stdout"
zstd tmp -c > tmpCompressed
zstd tmp --stdout > tmpCompressed       # long command format
println "test : compress to named file"
rm -f tmpCompressed
zstd tmp -o tmpCompressed
test -f tmpCompressed   # file must be created
println "test : force write, correct order"
zstd tmp -fo tmpCompressed
println "test : forgotten argument"
cp tmp tmp2
zstd tmp2 -fo && die "-o must be followed by filename "
println "test : implied stdout when input is stdin"
println bob | zstd | zstd -d
if [ "$isTerminal" = true ]; then
println "test : compressed data to terminal"
println bob | zstd && die "should have refused : compressed data to terminal"
println "test : compressed data from terminal (a hang here is a test fail, zstd is wrongly waiting on data from terminal)"
zstd -d > $INTOVOID && die "should have refused : compressed data from terminal"
fi
println "test : null-length file roundtrip"
println -n '' | zstd - --stdout | zstd -d --stdout
println "test : ensure small file doesn't add 3-bytes null block"
datagen -g1 > tmp1
zstd tmp1 -c | wc -c | grep "14"
zstd < tmp1  | wc -c | grep "14"
println "test : decompress file with wrong suffix (must fail)"
zstd -d tmpCompressed && die "wrong suffix error not detected!"
zstd -df tmp && die "should have refused : wrong extension"
println "test : decompress into stdout"
zstd -d tmpCompressed -c > tmpResult    # decompression using stdout
zstd --decompress tmpCompressed -c > tmpResult
zstd --decompress tmpCompressed --stdout > tmpResult
println "test : decompress from stdin into stdout"
zstd -dc   < tmp.zst > $INTOVOID   # combine decompression, stdin & stdout
zstd -dc - < tmp.zst > $INTOVOID
zstd -d    < tmp.zst > $INTOVOID   # implicit stdout when stdin is used
zstd -d  - < tmp.zst > $INTOVOID
println "test : impose memory limitation (must fail)"
datagen -g500K > tmplimit
zstd -f tmplimit
zstd -d -f tmplimit.zst -M2K -c > $INTOVOID && die "decompression needs more memory than allowed"
zstd -d -f tmplimit.zst --memlimit=2K -c > $INTOVOID && die "decompression needs more memory than allowed"  # long command
zstd -d -f tmplimit.zst --memory=2K -c > $INTOVOID && die "decompression needs more memory than allowed"  # long command
zstd -d -f tmplimit.zst --memlimit-decompress=2K -c > $INTOVOID && die "decompression needs more memory than allowed"  # long command
rm -f tmplimit tmplimit.zst
println "test : overwrite protection"
zstd -q tmp && die "overwrite check failed!"
println "test : force overwrite"
zstd -q -f tmp
zstd -q --force tmp
println "test : overwrite readonly file"
rm -f tmpro tmpro.zst
println foo > tmpro.zst
println foo > tmpro
chmod 400 tmpro.zst
zstd -q tmpro && die "should have refused to overwrite read-only file"
zstd -q -f tmpro
println "test: --no-progress flag"
zstd tmpro -c --no-progress | zstd -d -f -o "$INTOVOID" --no-progress
zstd tmpro -cv --no-progress | zstd -dv -f -o "$INTOVOID" --no-progress
println "test: --progress flag"
zstd tmpro -c | zstd -d -f -o "$INTOVOID" --progress 2>&1 | grep -E "[A-Za-z0-9._ ]+: [0-9]+ bytes"
zstd tmpro -c | zstd -d -f -q -o "$INTOVOID" --progress 2>&1 | grep -E "[A-Za-z0-9._ ]+: [0-9]+ bytes"
zstd tmpro -c | zstd -d -f -v -o "$INTOVOID" 2>&1 | grep -E "[A-Za-z0-9._ ]+: [0-9]+ bytes"
rm -f tmpro tmpro.zst
println "test: overwrite input file (must fail)"
zstd tmp -fo tmp && die "zstd compression overwrote the input file"
zstd tmp.zst -dfo tmp.zst && die "zstd decompression overwrote the input file"
println "test: detect that input file does not exist"
zstd nothere && die "zstd hasn't detected that input file does not exist"
println "test: --[no-]compress-literals"
zstd tmp -c --no-compress-literals -1       | zstd -t
zstd tmp -c --no-compress-literals --fast=1 | zstd -t
zstd tmp -c --no-compress-literals -19      | zstd -t
zstd tmp -c --compress-literals    -1       | zstd -t
zstd tmp -c --compress-literals    --fast=1 | zstd -t
zstd tmp -c --compress-literals    -19      | zstd -t
zstd -b --fast=1 -i0e1 tmp --compress-literals
zstd -b --fast=1 -i0e1 tmp --no-compress-literals
println "test: --no-check for decompression"
zstd -f tmp -o tmp_corrupt.zst --check
zstd -f tmp -o tmp.zst --no-check
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
zstd -d -f tmp_corrupt.zst --no-check
zstd -d -f tmp_corrupt.zst --check --no-check # final flag overrides
zstd -d -f tmp.zst --no-check

if [ "$isWindows" = false ] && [ "$UNAME" != "AIX" ]; then
  if [ -n "$(which readelf)" ]; then
    println "test: check if binary has executable stack (#2963)"
    readelf -lW "$ZSTD_BIN" | grep 'GNU_STACK .* RW ' || die "zstd binary has executable stack!"
  fi
fi

println "\n===>  --exclude-compressed flag"
rm -rf precompressedFilterTestDir
mkdir -p precompressedFilterTestDir
datagen $size > precompressedFilterTestDir/input.5
datagen $size > precompressedFilterTestDir/input.6
zstd --exclude-compressed --long --rm -r precompressedFilterTestDir
datagen $size > precompressedFilterTestDir/input.7
datagen $size > precompressedFilterTestDir/input.8
zstd --exclude-compressed --long --rm -r precompressedFilterTestDir
test ! -f precompressedFilterTestDir/input.5.zst.zst
test ! -f precompressedFilterTestDir/input.6.zst.zst
file1timestamp=`$MTIME precompressedFilterTestDir/input.5.zst`
file2timestamp=`$MTIME precompressedFilterTestDir/input.7.zst`
if [ $file2timestamp -ge $file1timestamp ]; then
  println "Test is successful. input.5.zst is precompressed and therefore not compressed/modified again."
else
  println "Test is not successful"
fi
# File Extension check.
datagen $size > precompressedFilterTestDir/input.zstbar
zstd --exclude-compressed --long --rm -r precompressedFilterTestDir

ext/zstd/tests/playTests.sh  view on Meta::CPAN

datagen -s1        > tmp1 2> $INTOVOID
datagen -s2 -g100K > tmp2 2> $INTOVOID
datagen -s3 -g1M   > tmp3 2> $INTOVOID
println "compress tmp* : "
zstd -f tmp*
test -f tmp1.zst
test -f tmp2.zst
test -f tmp3.zst
rm -f tmp1 tmp2 tmp3
println "decompress tmp* : "
zstd -df ./*.zst
test -f tmp1
test -f tmp2
test -f tmp3
println "compress tmp* into stdout > tmpall : "
zstd -c tmp1 tmp2 tmp3 > tmpall
test -f tmpall  # should check size of tmpall (should be tmp1.zst + tmp2.zst + tmp3.zst)
println "decompress tmpall* into stdout > tmpdec : "
cp tmpall tmpall2
zstd -dc tmpall* > tmpdec
test -f tmpdec  # should check size of tmpdec (should be 2*(tmp1 + tmp2 + tmp3))
println "compress multiple files including a missing one (notHere) : "
zstd -f tmp1 notHere tmp2 && die "missing file not detected!"
rm -f tmp*


if [ "$isWindows" = false ] ; then
    println "\n===>  zstd fifo named pipe test "
    echo "Hello World!" > tmp_original
    mkfifo tmp_named_pipe
    # note : fifo test doesn't work in combination with `dd` or `cat`
    echo "Hello World!" > tmp_named_pipe &
    zstd tmp_named_pipe -o tmp_compressed
    zstd -d -o tmp_decompressed tmp_compressed
    $DIFF -s tmp_original tmp_decompressed
    rm -rf tmp*
fi

println "\n===>  zstd created file permissions tests"
if [ "$isWindows" = false ] ; then
    rm -f tmp1 tmp2 tmp1.zst tmp2.zst tmp1.out tmp2.out # todo: remove

    ORIGINAL_UMASK=$(umask)
    umask 0000

    datagen > tmp1
    datagen > tmp2
    assertFilePermissions tmp1 666
    assertFilePermissions tmp2 666

    println "test : copy 666 permissions in file -> file compression "
    zstd -f tmp1 -o tmp1.zst
    assertSamePermissions tmp1 tmp1.zst
    println "test : copy 666 permissions in file -> file decompression "
    zstd -f -d tmp1.zst -o tmp1.out
    assertSamePermissions tmp1.zst tmp1.out

    rm -f tmp1.zst tmp1.out

    println "test : copy 400 permissions in file -> file compression (write to a read-only file) "
    chmod 0400 tmp1
    assertFilePermissions tmp1 400
    zstd -f tmp1 -o tmp1.zst
    assertSamePermissions tmp1 tmp1.zst
    println "test : copy 400 permissions in file -> file decompression (write to a read-only file) "
    zstd -f -d tmp1.zst -o tmp1
    assertSamePermissions tmp1.zst tmp1

    rm -f tmp1.zst tmp1.out

    println "test : check created permissions from stdin input in compression "
    zstd -f -o tmp1.zst < tmp1
    assertFilePermissions tmp1.zst 666
    println "test : check created permissions from stdin input in decompression "
    zstd -f -d -o tmp1.out < tmp1.zst
    assertFilePermissions tmp1.out 666

    rm -f tmp1.zst tmp1.out

    println "test : check created permissions from multiple inputs in compression "
    zstd -f tmp1 tmp2 -o tmp1.zst
    assertFilePermissions tmp1.zst 666
    println "test : check created permissions from multiple inputs in decompression "
    cp tmp1.zst tmp2.zst
    zstd -f -d tmp1.zst tmp2.zst -o tmp1.out
    assertFilePermissions tmp1.out 666

    rm -f tmp1.zst tmp2.zst tmp1.out tmp2.out

    println "test : check permissions on pre-existing output file in compression "
    chmod 0600 tmp1
    touch tmp1.zst
    chmod 0400 tmp1.zst
    zstd -f tmp1 -o tmp1.zst
    assertFilePermissions tmp1.zst 600
    println "test : check permissions on pre-existing output file in decompression "
    chmod 0400 tmp1.zst
    touch tmp1.out
    chmod 0200 tmp1.out
    zstd -f -d tmp1.zst -o tmp1.out
    assertFilePermissions tmp1.out 400

    umask 0666
    chmod 0666 tmp1 tmp2

    rm -f tmp1.zst tmp1.out

    println "test : respect umask when compressing from stdin input "
    zstd -f -o tmp1.zst < tmp1
    assertFilePermissions tmp1.zst 0
    println "test : respect umask when decompressing from stdin input "
    chmod 0666 tmp1.zst
    zstd -f -d -o tmp1.out < tmp1.zst
    assertFilePermissions tmp1.out 0

    rm -f tmp1 tmp2 tmp1.zst tmp2.zst tmp1.out tmp2.out
    umask $ORIGINAL_UMASK
fi

if [ -n "$DEVNULLRIGHTS" ] ; then
    # these tests requires sudo rights, which is uncommon.
    # they are only triggered if DEVNULLRIGHTS macro is defined.
    println "\n===> checking /dev/null permissions are unaltered "
    datagen > tmp
    sudoZstd tmp -o $INTOVOID   # sudo rights could modify /dev/null permissions
    sudoZstd tmp -c > $INTOVOID
    zstd tmp -f -o tmp.zst
    sudoZstd -d tmp.zst -c > $INTOVOID
    sudoZstd -d tmp.zst -o $INTOVOID
    ls -las $INTOVOID | grep "rw-rw-rw-"
fi

if [ -n "$READFROMBLOCKDEVICE" ] ; then
    # This creates a temporary block device, which is only possible on unix-y
    # systems, is somewhat invasive, and requires sudo. For these reasons, you
    # have to specifically ask for this test.
    println "\n===> checking that zstd can read from a block device"
    datagen -g65536 > tmp.img
    sudo losetup -fP tmp.img
    LOOP_DEV=$(losetup -a | grep 'tmp\.img' | cut -f1 -d:)
    [ -z "$LOOP_DEV" ] && die "failed to get loopback device"
    sudoZstd $LOOP_DEV -c > tmp.img.zst && die "should fail without -f"
    sudoZstd -f $LOOP_DEV -c > tmp.img.zst
    zstd -d tmp.img.zst -o tmp.img.copy
    sudo losetup -d $LOOP_DEV
    $DIFF -s tmp.img tmp.img.copy || die "round trip failed"
    rm -f tmp.img tmp.img.zst tmp.img.copy
fi

println "\n===>  zstd created file timestamp tests"
datagen > tmp
touch -m -t 200001010000.00 tmp
println "test : copy mtime in file -> file compression "
zstd -f tmp -o tmp.zst
assertSameMTime tmp tmp.zst
println "test : copy mtime in file -> file decompression "
zstd -f -d tmp.zst -o tmp.out
assertSameMTime tmp.zst tmp.out
rm -f tmp

println "\n===>  compress multiple files into an output directory, --output-dir-flat"
println henlo > tmp1
mkdir tmpInputTestDir
mkdir tmpInputTestDir/we
mkdir tmpInputTestDir/we/must
mkdir tmpInputTestDir/we/must/go
mkdir tmpInputTestDir/we/must/go/deeper
println cool > tmpInputTestDir/we/must/go/deeper/tmp2
mkdir tmpOutDir
zstd tmp1 tmpInputTestDir/we/must/go/deeper/tmp2 --output-dir-flat tmpOutDir
test -f tmpOutDir/tmp1.zst
test -f tmpOutDir/tmp2.zst



( run in 1.184 second using v1.01-cache-2.11-cpan-39bf76dae61 )