xref: /netbsd-src/tests/usr.bin/compress/t_pr_19722.sh (revision 33a964ec6703eeed0ed90c0556f742faa7d11418)
1#	$NetBSD: t_pr_19722.sh,v 1.4 2022/05/22 21:39:44 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
28atf_test_case 'compress_small_file'
29compress_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
43atf_test_case 'compress_small_file_force'
44compress_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
60atf_test_case 'roundtrip'
61roundtrip_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
75atf_test_case 'uncompress_basename'
76uncompress_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
91atf_test_case 'uncompress_no_source_no_target'
92uncompress_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
109atf_test_case 'uncompress_no_source_existing_target'
110uncompress_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
129atf_test_case 'uncompress_broken_source_no_target'
130uncompress_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
147atf_test_case 'uncompress_broken_source_existing_target'
148uncompress_broken_source_existing_target_body()
149{
150	# PR 19722: uncompressing a broken source removes existing target
151	#
152	# Before compress.c 1.29 from 2022-05-22, uncompress removed an
153	# existing target before checking that the source has the correct
154	# format.
155
156	echo 'broken' > file.Z
157	echo 'before' > file
158
159	atf_check \
160	    -s 'not-exit:0' \
161	    -e 'inline:uncompress: file.Z: Inappropriate file type or format\n' \
162	    uncompress -f file.Z
163
164	atf_check -o 'inline:broken\n' cat file.Z
165	atf_check -o 'inline:before\n' cat file
166}
167
168
169atf_init_test_cases()
170{
171
172	atf_add_test_case compress_small_file
173	atf_add_test_case compress_small_file_force
174	atf_add_test_case roundtrip
175	atf_add_test_case uncompress_basename
176	atf_add_test_case uncompress_no_source_no_target
177	atf_add_test_case uncompress_no_source_existing_target
178	atf_add_test_case uncompress_broken_source_no_target
179	atf_add_test_case uncompress_broken_source_existing_target
180}
181