Home | History | Annotate | Line # | Download | only in tests
      1 #!/bin/sh
      2 # SPDX-License-Identifier: 0BSD
      3 
      4 ###############################################################################
      5 #
      6 # Author: Jia Tan
      7 #
      8 ###############################################################################
      9 
     10 # Optional argument:
     11 # $1 = directory of the xz executable
     12 
     13 # If xz was not built, skip this test. Autotools and CMake put
     14 # the xz executable in a different location.
     15 XZ=${1:-../src/xz}/xz
     16 if test ! -x "$XZ"; then
     17 	echo "xz was not built, skipping this test."
     18 	exit 77
     19 fi
     20 
     21 # If compression or decompression support is missing, this test is skipped.
     22 # This isn't perfect because it does not specifically check for LZMA1/2
     23 # filters. Many of the other tests also assume LZMA1/2 support if encoders
     24 # or decoders are enabled.
     25 if test ! -f ../config.h ; then
     26 	:
     27 elif grep 'define HAVE_ENCODERS' ../config.h > /dev/null \
     28 		&& grep 'define HAVE_DECODERS' ../config.h > /dev/null ; then
     29 	:
     30 else
     31 	echo "Compression or decompression support is disabled, skipping this test."
     32 	exit 77
     33 fi
     34 
     35 # Create temporary input file. The file contents are not important.
     36 SUFFIX_INPUT="suffix_temp"
     37 SUFFIX_INPUT_FILES="$SUFFIX_INPUT"_files
     38 SUFFIX_INPUT_FILES0="$SUFFIX_INPUT"_files0
     39 
     40 # Remove possible leftover temporary files
     41 rm -f \
     42 	"$SUFFIX_INPUT" \
     43 	"$SUFFIX_INPUT.foo" \
     44 	"$SUFFIX_INPUT_FILES" \
     45 	"$SUFFIX_INPUT_FILES"
     46 
     47 echo "foobar" > "$SUFFIX_INPUT"
     48 
     49 # Test basic suffix when compressing with raw format.
     50 if "$XZ" -zfk --suffix=".foo" -Fraw --lzma1=preset=0 "$SUFFIX_INPUT" ; then
     51 	:
     52 else
     53 	echo "Failed to compress a file with a suffix set in raw format"
     54 	exit 1
     55 fi
     56 
     57 # Test the output file is named properly.
     58 if test -f "$SUFFIX_INPUT.foo" ; then
     59 	:
     60 else
     61 	echo "Raw format compressed output file not named properly"
     62 	exit 1
     63 fi
     64 
     65 # Expect an error when compressing with raw format without a suffix
     66 if "$XZ" -zfk -Fraw --lzma1=preset=0 "$SUFFIX_INPUT" 2> /dev/null; then
     67 	echo "Error not reported when compressing in raw format without a suffix"
     68 	exit 1
     69 fi
     70 
     71 # Expect an error when decompressing with raw format without a suffix
     72 if "$XZ" -df -Fraw --lzma1=preset=0 "$SUFFIX_INPUT.foo" 2> /dev/null; then
     73 	echo "Error not reported when decompressing in raw format without a suffix"
     74 	exit 1
     75 fi
     76 
     77 # Test basic decompression with raw format and a suffix. This will also
     78 # delete $SUFFIX_INPUT.foo
     79 if "$XZ" -df --suffix=".foo" -Fraw --lzma1=preset=0 "$SUFFIX_INPUT.foo"; then
     80 	:
     81 else
     82 	echo "Failed to decompress a file with a suffix set in raw format"
     83 	exit 1
     84 fi
     85 
     86 # Test basic compression with .xz format and a suffix
     87 if "$XZ" -zfk --suffix=".foo" --lzma2=preset=0 "$SUFFIX_INPUT" ; then
     88 	:
     89 else
     90 	echo "Failed to compress a file with a suffix set in .xz format"
     91 	exit 1
     92 fi
     93 
     94 # Test the output file is named properly.
     95 if test -f "$SUFFIX_INPUT.foo" ; then
     96 	:
     97 else
     98 	echo ".xz format compressed output file named properly"
     99 	exit 1
    100 fi
    101 
    102 # This will delete $SUFFIX_INPUT.foo
    103 if "$XZ" -df --suffix=".foo" "$SUFFIX_INPUT.foo"; then
    104 	:
    105 else
    106 	echo "Failed to decompress a file with a suffix set in .xz format"
    107 	exit 1
    108 fi
    109 
    110 # Test reading from stdin in raw mode. This was broken in
    111 # cc5aa9ab138beeecaee5a1e81197591893ee9ca0 and fixed in
    112 # 837ea40b1c9d4998cac4500b55171bf33e0c31a6
    113 if echo foo | "$XZ" -Fraw --lzma1=preset=0 > /dev/null ; then
    114 	:
    115 else
    116 	echo "Implicit write to stdout not detected"
    117 	exit 1
    118 fi
    119 
    120 # Create two temporary files to be used with --files and --files0.
    121 printf '%s\n' "$SUFFIX_INPUT" > "$SUFFIX_INPUT_FILES"
    122 printf '%s\0' "$SUFFIX_INPUT" > "$SUFFIX_INPUT_FILES0"
    123 
    124 # Test proper handling of --files/--files0 when no suffix is set. This
    125 # must result in an error because xz does not know how to rename the output
    126 # file from the input files. This caused a segmentation fault due to a
    127 # mistake in f481523baac946fa3bc13d79186ffaf0c0b818a7, which was fixed by
    128 # 0a601ddc89fd7e1370807c6b58964f361dfcd34a.
    129 if "$XZ" -Fraw --lzma1=preset=0 --files="$SUFFIX_INPUT_FILES" 2> /dev/null ; then
    130 	echo "Failed to report error when compressing a file specified by --files in raw mode without a suffix"
    131 	exit 1
    132 fi
    133 
    134 if "$XZ" -Fraw --lzma1=preset=0 --files0="$SUFFIX_INPUT_FILES0" 2> /dev/null ; then
    135 	echo "Failed to report error when compressing a file specified by --files0 in raw mode without a suffix"
    136 	exit 1
    137 fi
    138 
    139 # Test proper suffix usage in raw mode with --files and --files0.
    140 if "$XZ" -zfk -Fraw --lzma1=preset=0 --suffix=.foo --files="$SUFFIX_INPUT_FILES" ; then
    141 	:
    142 else
    143 	echo "Error compressing a file specified by --files in raw mode with a suffix set"
    144 	exit 1
    145 fi
    146 
    147 if test -f "$SUFFIX_INPUT.foo" ; then
    148 	:
    149 else
    150 	echo "Entry processed by --files not named properly"
    151 	exit 1
    152 fi
    153 
    154 # Remove the artifact so we can be sure the next test executes properly.
    155 rm "$SUFFIX_INPUT.foo"
    156 
    157 if "$XZ" -zfk -Fraw --lzma1=preset=0 --suffix=.foo --files0="$SUFFIX_INPUT_FILES0" ; then
    158 	:
    159 else
    160 	echo "Error compressing a file specified by --files0 in raw mode with a suffix set"
    161 	exit 1
    162 fi
    163 
    164 if test -f "$SUFFIX_INPUT.foo" ; then
    165 	:
    166 else
    167 	echo "Entry processed by --files0 not named properly"
    168 	exit 1
    169 fi
    170 
    171 # When the file type cannot be determined by xz, it will copy the contents
    172 # of the file only if -c,--stdout is used. This was broken by
    173 # 837ea40b1c9d4998cac4500b55171bf33e0c31a6 and fixed by
    174 # f481523baac946fa3bc13d79186ffaf0c0b818a7.
    175 if echo foo | "$XZ" -df > /dev/null 2>&1; then
    176 	echo "Failed to report error when decompressing unknown file type without -c,--stdout"
    177 	exit 1
    178 fi
    179 
    180 if echo foo | "$XZ" -dfc > /dev/null; then
    181 	:
    182 else
    183 	echo "Failed to copy input to standard out when decompressing unknown file type with -c,--stdout"
    184 	exit 1
    185 fi
    186 
    187 # Remove remaining test artifacts
    188 rm -f \
    189 	"$SUFFIX_INPUT" \
    190 	"$SUFFIX_INPUT.foo" \
    191 	"$SUFFIX_INPUT_FILES" \
    192 	"$SUFFIX_INPUT_FILES0"
    193