Add SPDX license identifiers to files under tests/ossfuzz
[xz/debian.git] / tests / test_compress.sh
bloba10343a75a8e59573f8ae7ac745c4aa0dc57a602
1 #!/bin/sh
2 # SPDX-License-Identifier: 0BSD
4 ###############################################################################
6 # Author: Lasse Collin
8 ###############################################################################
10 # Mandatory argument:
11 # $1 = test filename: compress_generated_<foo> or compress_prepared_<foo>
13 # Optional argument:
14 # $2 = directory of the xz and xzdec executables
16 XZ=${2:-../src/xz}/xz
17 XZDEC=${2:-../src/xzdec}/xzdec
19 # If xz wasn't built, this test is skipped.
20 if test ! -x "$XZ"; then
21 echo "xz was not built, skipping this test."
22 exit 77
25 # xzdec isn't mandatory for this script.
26 test -x "$XZDEC" || XZDEC=
28 # If compression or decompression support is missing, this test is skipped.
29 # This isn't perfect as if only some compressors or decompressors are disabled
30 # then this script can still fail because for now this doesn't check the
31 # availability of each filter.
32 if test ! -f ../config.h ; then
34 elif grep 'define HAVE_ENCODERS' ../config.h > /dev/null \
35 && grep 'define HAVE_DECODERS' ../config.h > /dev/null ; then
37 else
38 echo "Compression or decompression support is disabled, skipping this test."
39 exit 77
42 # Find out if our shell supports functions.
43 eval 'unset foo ; foo() { return 42; } ; foo'
44 if test $? != 42 ; then
45 echo "/bin/sh doesn't support functions, skipping this test."
46 exit 77
49 test_xz() {
50 if $XZ -c "$@" "$FILE" > "$TMP_COMP"; then
52 else
53 echo "Compressing failed: $* $FILE"
54 exit 1
57 if $XZ -cd "$TMP_COMP" > "$TMP_UNCOMP" ; then
59 else
60 echo "Decompressing failed: $* $FILE"
61 exit 1
64 if cmp "$TMP_UNCOMP" "$FILE" ; then
66 else
67 echo "Decompressed file does not match" \
68 "the original: $* $FILE"
69 exit 1
72 if test -n "$XZDEC" ; then
73 if $XZDEC "$TMP_COMP" > "$TMP_UNCOMP" ; then
75 else
76 echo "Decompressing failed: $* $FILE"
77 exit 1
80 if cmp "$TMP_UNCOMP" "$FILE" ; then
82 else
83 echo "Decompressed file does not match" \
84 "the original: $* $FILE"
85 exit 1
90 # Set memory usage limit for xz. xzdec has no memory usage limiter.
91 # Force single-threaded mode as the test files are small
92 # (so more than one thread wouldn't be used anyway) and
93 # the tests are usually run in parallel.
94 XZ="$XZ --memlimit-compress=48MiB --memlimit-decompress=5MiB \
95 --no-adjust --threads=1"
97 # Create the required input file if needed.
99 # Derive temporary filenames for compressed and uncompressed outputs
100 # from the input filename. This is needed when multiple tests are
101 # run in parallel.
102 FILE=$1
103 TMP_COMP="tmp_comp_$FILE"
104 TMP_UNCOMP="tmp_uncomp_$FILE"
106 case $FILE in
107 # compress_generated files will be created in the build directory
108 # in the /tests/ sub-directory.
109 compress_generated_*)
110 if ./create_compress_files "$FILE" ; then
112 else
113 rm -f "$FILE"
114 echo "Failed to create the file '$FILE'."
115 exit 1
118 # compress_prepared files exist in the source directory since they
119 # do not need to be copied or regenerated.
120 compress_prepared_*)
121 FILE="$srcdir/$FILE"
124 echo "No test file was specified."
125 exit 1
127 esac
129 # Remove temporary now (in case they are something weird), and on exit.
130 rm -f "$TMP_COMP" "$TMP_UNCOMP"
131 trap 'rm -f "$TMP_COMP" "$TMP_UNCOMP"' 0
133 # Compress and decompress the file with various filter configurations.
135 # Don't test with empty arguments; it breaks some ancient
136 # proprietary /bin/sh versions due to $@ used in test_xz().
137 test_xz -1
138 test_xz -2
139 test_xz -3
140 test_xz -4
142 test_filter()
144 if test -f ../config.h ; then
145 grep "define HAVE_ENCODER_$1 1" ../config.h > /dev/null \
146 || return
147 grep "define HAVE_DECODER_$1 1" ../config.h > /dev/null \
148 || return
150 shift
151 test_xz --filters="$* lzma2:dict=64KiB,nice=32,mode=fast"
154 test_filter DELTA delta:dist=1
155 test_filter DELTA delta:dist=4
156 test_filter DELTA delta:dist=256
157 test_filter X86 x86
158 test_filter POWERPC powerpc
159 test_filter IA64 ia64
160 test_filter ARM arm
161 test_filter ARMTHUMB armthumb
162 test_filter ARM64 arm64
163 test_filter SPARC sparc
164 test_filter RISCV riscv
166 exit 0