t_pr_19722.sh revision 1.3 1 # $NetBSD: t_pr_19722.sh,v 1.3 2022/05/22 21:16:50 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 # Before compress.c 1.28 from 2022-05-22, uncompress created an empty
97 # target file and didn't clean it up.
98
99 atf_check \
100 -s 'not-exit:0' \
101 -e 'inline:uncompress: file.Z: No such file or directory\n' \
102 uncompress -f file
103
104 atf_check test ! -f file
105 atf_check test ! -f file.Z
106 }
107
108
109 atf_test_case 'uncompress_no_source_existing_target'
110 uncompress_no_source_existing_target_body()
111 {
112 # PR 19722: uncompressing a missing source truncates target
113 #
114 # Before compress.c 1.28 from 2022-05-22, uncompress truncated the
115 # target.
116
117 atf_check sh -c "echo 'hello' > file"
118
119 atf_check \
120 -s 'not-exit:0' \
121 -e 'inline:uncompress: file.Z: No such file or directory\n' \
122 uncompress -f file
123
124 atf_check -o 'inline:hello\n' cat file
125 atf_check test ! -f file.Z
126 }
127
128
129 atf_test_case 'uncompress_broken_source_no_target'
130 uncompress_broken_source_no_target_body()
131 {
132 # When trying to uncompress a broken source, the target is created
133 # temporarily but deleted again, as part of the cleanup.
134
135 echo 'broken' > file.Z
136
137 atf_check \
138 -s 'not-exit:0' \
139 -e 'inline:uncompress: file.Z: Inappropriate file type or format\n' \
140 uncompress -f file
141
142 atf_check test ! -f file
143 atf_check test -f file.Z
144 }
145
146
147 atf_test_case 'uncompress_broken_source_existing_target'
148 uncompress_broken_source_existing_target_body()
149 {
150 # PR 19722: uncompressing a broken source removes existing target
151
152 echo 'broken' > file.Z
153 echo 'before' > file
154
155 atf_check \
156 -s 'not-exit:0' \
157 -e 'inline:uncompress: file.Z: Inappropriate file type or format\n' \
158 uncompress -f file.Z
159
160 atf_check -o 'inline:broken\n' cat file.Z
161 # FIXME: Must not be modified.
162 atf_check test ! -f file
163 }
164
165
166 atf_init_test_cases()
167 {
168
169 atf_add_test_case compress_small_file
170 atf_add_test_case compress_small_file_force
171 atf_add_test_case roundtrip
172 atf_add_test_case uncompress_basename
173 atf_add_test_case uncompress_no_source_no_target
174 atf_add_test_case uncompress_no_source_existing_target
175 atf_add_test_case uncompress_broken_source_no_target
176 atf_add_test_case uncompress_broken_source_existing_target
177 }
178