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