Home | History | Annotate | Line # | Download | only in compress
t_pr_19722.sh revision 1.2
      1 #	$NetBSD: t_pr_19722.sh,v 1.2 2022/05/22 20:49:12 rillig Exp $
      2 #
      3 # Copyright (c) 2022 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions
      8 # are met:
      9 # 1. Redistributions of source code must retain the above copyright
     10 #    notice, this list of conditions and the following disclaimer.
     11 # 2. Redistributions in binary form must reproduce the above copyright
     12 #    notice, this list of conditions and the following disclaimer in the
     13 #    documentation and/or other materials provided with the distribution.
     14 #
     15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25 # POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 atf_test_case 'compress_small_file'
     29 compress_small_file_body()
     30 {
     31 	# If the compressed version of a file would be larger than the
     32 	# original file, the original file is kept.
     33 
     34 	echo 'hello' > file
     35 
     36 	atf_check compress file
     37 
     38 	atf_check -o 'inline:hello\n' cat file
     39 	atf_check test ! -f file.Z
     40 }
     41 
     42 
     43 atf_test_case 'compress_small_file_force'
     44 compress_small_file_force_body()
     45 {
     46 	# The option '-f' forces compression to happen, even if the resulting
     47 	# file becomes larger than the original.
     48 
     49 	echo 'hello' > file
     50 
     51 	atf_check compress -f file
     52 
     53 	atf_check test ! -f file
     54 	atf_check \
     55 	    -o 'inline:0000000   1f  9d  90  68  ca  b0  61  f3  46  01                        \n000000a\n' \
     56 	    od -Ax -tx1 file.Z
     57 }
     58 
     59 
     60 atf_test_case 'roundtrip'
     61 roundtrip_body()
     62 {
     63 	# Compressing and decompressing a file must preserve every byte.
     64 
     65 	atf_check -e 'ignore' dd if=/dev/urandom of=file bs=4k count=10
     66 	atf_check cp file original
     67 
     68 	atf_check compress -f file
     69 	atf_check uncompress file.Z
     70 
     71 	atf_check cmp file original
     72 }
     73 
     74 
     75 atf_test_case 'uncompress_basename'
     76 uncompress_basename_body()
     77 {
     78 	# To uncompress a file, it suffices to specify the basename of the
     79 	# file, the filename extension '.Z' is optional.
     80 
     81 	atf_check sh -c "echo 'hello' > file"
     82 	atf_check compress -f file
     83 
     84 	atf_check uncompress file
     85 
     86 	atf_check -o 'inline:hello\n' cat file
     87 	atf_check test ! -f file.Z
     88 }
     89 
     90 
     91 atf_test_case 'uncompress_no_source_no_target'
     92 uncompress_no_source_no_target_body()
     93 {
     94 	# PR 19722: uncompressing a missing source creates empty target
     95 
     96 	atf_check \
     97 	    -s 'not-exit:0' \
     98 	    -e 'inline:uncompress: file.Z: No such file or directory\n' \
     99 	    uncompress -f file
    100 
    101 	# FIXME: The target file must not be created.
    102 	atf_check cat file
    103 	atf_check test ! -f nonexistent.Z
    104 }
    105 
    106 
    107 atf_test_case 'uncompress_no_source_existing_target'
    108 uncompress_no_source_existing_target_body()
    109 {
    110 	# PR 19722: uncompressing a missing source truncates target
    111 
    112 	atf_check sh -c "echo 'hello' > file"
    113 
    114 	atf_check \
    115 	    -s 'not-exit:0' \
    116 	    -e 'inline:uncompress: file.Z: No such file or directory\n' \
    117 	    uncompress -f file
    118 
    119 	# FIXME: The file must not be truncated.
    120 	atf_check cat file
    121 	atf_check test ! -f file.Z
    122 }
    123 
    124 
    125 atf_test_case 'uncompress_broken_source_no_target'
    126 uncompress_broken_source_no_target_body()
    127 {
    128 	# When trying to uncompress a broken source, the target is created
    129 	# temporarily but deleted again, as part of the cleanup.
    130 
    131 	echo 'broken' > file.Z
    132 
    133 	atf_check \
    134 	    -s 'not-exit:0' \
    135 	    -e 'inline:uncompress: file.Z: Inappropriate file type or format\n' \
    136 	    uncompress -f file
    137 
    138 	atf_check test ! -f file
    139 	atf_check test -f file.Z
    140 }
    141 
    142 
    143 atf_test_case 'uncompress_broken_source_existing_target'
    144 uncompress_broken_source_existing_target_body()
    145 {
    146 	# PR 19722: uncompressing a broken source removes existing target
    147 
    148 	echo 'broken' > file.Z
    149 	echo 'before' > file
    150 
    151 	atf_check \
    152 	    -s 'not-exit:0' \
    153 	    -e 'inline:uncompress: file.Z: Inappropriate file type or format\n' \
    154 	    uncompress -f file.Z
    155 
    156 	atf_check -o 'inline:broken\n' cat file.Z
    157 	# FIXME: Must not be modified.
    158 	atf_check test ! -f file
    159 }
    160 
    161 
    162 atf_init_test_cases()
    163 {
    164 
    165 	atf_add_test_case compress_small_file
    166 	atf_add_test_case compress_small_file_force
    167 	atf_add_test_case roundtrip
    168 	atf_add_test_case uncompress_basename
    169 	atf_add_test_case uncompress_no_source_no_target
    170 	atf_add_test_case uncompress_no_source_existing_target
    171 	atf_add_test_case uncompress_broken_source_no_target
    172 	atf_add_test_case uncompress_broken_source_existing_target
    173 }
    174