]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/xinstall/tests/install_test.sh
install: Add some tests.
[FreeBSD/FreeBSD.git] / usr.bin / xinstall / tests / install_test.sh
1 #
2 # Copyright (c) 2016 Jilles Tjoelker
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27 #
28
29 copy_to_nonexistent_with_opts() {
30         printf 'test\n123\r456\r\n789\0z' >testf
31         atf_check install "$@" testf copyf
32         cmp testf copyf || atf_fail "bad copy"
33         [ ! testf -nt copyf ] || atf_fail "bad timestamp"
34         [ ! -e copyf.bak ] || atf_fail "no backup expected"
35 }
36
37 atf_test_case copy_to_nonexistent
38 copy_to_nonexistent_body() {
39         copy_to_nonexistent_with_opts
40 }
41
42 atf_test_case copy_to_nonexistent_safe
43 copy_to_nonexistent_safe_body() {
44         copy_to_nonexistent_with_opts -S
45 }
46
47 atf_test_case copy_to_nonexistent_comparing
48 copy_to_nonexistent_comparing_body() {
49         copy_to_nonexistent_with_opts -C
50 }
51
52 atf_test_case copy_to_nonexistent_safe_comparing
53 copy_to_nonexistent_safe_comparing_body() {
54         copy_to_nonexistent_with_opts -S -C
55 }
56
57 atf_test_case copy_to_nonexistent_backup
58 copy_to_nonexistent_backup_body() {
59         copy_to_nonexistent_with_opts -b -B.bak
60 }
61
62 atf_test_case copy_to_nonexistent_backup_safe
63 copy_to_nonexistent_backup_safe_body() {
64         copy_to_nonexistent_with_opts -b -B.bak -S
65 }
66
67 copy_self_with_opts() {
68         printf 'test\n123\r456\r\n789\0z' >testf
69         printf 'test\n123\r456\r\n789\0z' >testf2
70         atf_check -s not-exit:0 -o empty -e match:. install "$@" testf testf
71         cmp testf testf2 || atf_fail "file changed after self-copy attempt"
72 }
73
74 atf_test_case copy_self
75 copy_self_body() {
76         copy_self_with_opts
77 }
78
79 atf_test_case copy_self_safe
80 copy_self_safe_body() {
81         copy_self_with_opts -S
82 }
83
84 atf_test_case copy_self_comparing
85 copy_self_comparing_body() {
86         copy_self_with_opts -C
87 }
88
89 atf_test_case copy_self_safe_comparing
90 copy_self_safe_comparing_body() {
91         copy_self_with_opts -S -C
92 }
93
94 overwrite_with_opts() {
95         printf 'test\n123\r456\r\n789\0z' >testf
96         printf 'test\n123\r456\r\n789\0w' >otherf
97         atf_check install "$@" testf otherf
98         cmp testf otherf || atf_fail "bad overwrite"
99         [ ! testf -nt otherf ] || atf_fail "bad timestamp"
100 }
101
102 atf_test_case overwrite
103 overwrite_body() {
104         overwrite_with_opts
105 }
106
107 atf_test_case overwrite_safe
108 overwrite_safe_body() {
109         overwrite_with_opts -S
110 }
111
112 atf_test_case overwrite_comparing
113 overwrite_comparing_body() {
114         overwrite_with_opts -C
115 }
116
117 atf_test_case overwrite_safe_comparing
118 overwrite_safe_comparing_body() {
119         overwrite_with_opts -S -C
120 }
121
122 overwrite_eq_with_opts() {
123         printf 'test\n123\r456\r\n789\0z' >testf
124         printf 'test\n123\r456\r\n789\0z' >otherf
125         atf_check install "$@" testf otherf
126         cmp testf otherf || atf_fail "bad overwrite"
127         [ ! testf -nt otherf ] || atf_fail "bad timestamp"
128 }
129
130 atf_test_case overwrite_eq
131 overwrite_eq_body() {
132         overwrite_eq_with_opts
133 }
134
135 atf_test_case overwrite_eq_safe
136 overwrite_eq_safe_body() {
137         overwrite_eq_with_opts -S
138 }
139
140 atf_test_case overwrite_eq_comparing
141 overwrite_eq_comparing_body() {
142         overwrite_eq_with_opts -C
143 }
144
145 atf_test_case overwrite_eq_safe_comparing
146 overwrite_eq_safe_comparing_body() {
147         overwrite_eq_with_opts -S -C
148 }
149
150 overwrite_backup_with_opts() {
151         printf 'test\n123\r456\r\n789\0z' >testf
152         printf 'test\n123\r456\r\n789\0w' >otherf
153         printf 'test\n123\r456\r\n789\0w' >otherf2
154         atf_check install -b -B.bak "$@" testf otherf
155         cmp testf otherf || atf_fail "bad overwrite"
156         [ ! testf -nt otherf ] || atf_fail "bad timestamp"
157         cmp otherf.bak otherf2 || atf_fail "bad backup"
158 }
159
160 atf_test_case overwrite_backup
161 overwrite_backup_body() {
162         overwrite_backup_with_opts
163 }
164
165 atf_test_case overwrite_backup_safe
166 overwrite_backup_safe_body() {
167         overwrite_backup_with_opts -S
168 }
169
170 atf_test_case overwrite_backup_comparing
171 overwrite_backup_comparing_body() {
172         overwrite_backup_with_opts -C
173 }
174
175 atf_test_case overwrite_backup_safe_comparing
176 overwrite_backup_safe_comparing_body() {
177         overwrite_backup_with_opts -S -C
178 }
179
180 setup_stripbin() {
181         cat <<\STRIPBIN >stripbin
182 #!/bin/sh
183 tr z @ <"$1" >"$1.new" && mv -- "$1.new" "$1"
184 STRIPBIN
185         chmod 755 stripbin
186         export STRIPBIN="$PWD/stripbin"
187 }
188
189 strip_changing_with_opts() {
190         setup_stripbin
191         printf 'test\n123\r456\r\n789\0z' >testf
192         atf_check install -s "$@" testf copyf
193         [ ! testf -nt copyf ] || atf_fail "bad timestamp"
194         printf 'test\n123\r456\r\n789\0@' >otherf
195         cmp otherf copyf || atf_fail "bad stripped copy"
196 }
197
198 atf_test_case strip_changing
199 strip_changing_body() {
200         strip_changing_with_opts
201 }
202
203 atf_test_case strip_changing_comparing
204 strip_changing_comparing_body() {
205         strip_changing_with_opts -C
206 }
207
208 strip_changing_overwrite_with_opts() {
209         setup_stripbin
210         printf 'test\n123\r456\r\n789\0z' >testf
211         printf 'test\n123\r456\r\n789\0w' >copyf
212         atf_check install -s "$@" testf copyf
213         [ ! testf -nt copyf ] || atf_fail "bad timestamp"
214         printf 'test\n123\r456\r\n789\0@' >otherf
215         cmp otherf copyf || atf_fail "bad stripped copy"
216 }
217
218 atf_test_case strip_changing_overwrite
219 strip_changing_overwrite_body() {
220         strip_changing_overwrite_with_opts
221 }
222
223 atf_test_case strip_changing_overwrite_comparing
224 strip_changing_overwrite_comparing_body() {
225         strip_changing_overwrite_with_opts -C
226 }
227
228 strip_changing_overwrite_eq_with_opts() {
229         setup_stripbin
230         printf 'test\n123\r456\r\n789\0z' >testf
231         printf 'test\n123\r456\r\n789\0@' >copyf
232         atf_check install -s "$@" testf copyf
233         [ ! testf -nt copyf ] || atf_fail "bad timestamp"
234         printf 'test\n123\r456\r\n789\0@' >otherf
235         cmp otherf copyf || atf_fail "bad stripped copy"
236 }
237
238 atf_test_case strip_changing_overwrite_eq
239 strip_changing_overwrite_eq_body() {
240         strip_changing_overwrite_eq_with_opts
241 }
242
243 atf_test_case strip_changing_overwrite_eq_comparing
244 strip_changing_overwrite_eq_comparing_body() {
245         strip_changing_overwrite_eq_with_opts -C
246 }
247
248 atf_test_case strip_noop
249 strip_noop_body() {
250         export STRIPBIN=true
251         printf 'test\n123\r456\r\n789\0z' >testf
252         atf_check install -s testf copyf
253         [ ! testf -nt copyf ] || atf_fail "bad timestamp"
254         printf 'test\n123\r456\r\n789\0z' >otherf
255         cmp otherf copyf || atf_fail "bad stripped copy"
256 }
257
258 atf_test_case hard_link
259 hard_link_body() {
260         printf 'test\n123\r456\r\n789\0z' >testf
261         atf_check install -l h testf copyf
262         [ testf -ef copyf ] || atf_fail "not same file"
263         [ ! -L copyf ] || atf_fail "copy is symlink"
264 }
265
266 atf_test_case symbolic_link
267 symbolic_link_body() {
268         printf 'test\n123\r456\r\n789\0z' >testf
269         atf_check install -l s testf copyf
270         [ testf -ef copyf ] || atf_fail "not same file"
271         [ -L copyf ] || atf_fail "copy is not symlink"
272 }
273
274 atf_test_case symbolic_link_absolute
275 symbolic_link_absolute_body() {
276         printf 'test\n123\r456\r\n789\0z' >testf
277         atf_check install -l sa testf copyf
278         [ testf -ef copyf ] || atf_fail "not same file"
279         [ -L copyf ] || atf_fail "copy is not symlink"
280         [ "$(readlink copyf)" = "$(pwd -P)/testf" ] || atf_fail "unexpected symlink contents"
281 }
282
283 atf_test_case symbolic_link_relative
284 symbolic_link_relative_body() {
285         printf 'test\n123\r456\r\n789\0z' >testf
286         atf_check install -l sr testf copyf
287         [ testf -ef copyf ] || atf_fail "not same file"
288         [ -L copyf ] || atf_fail "copy is not symlink"
289         [ "$(readlink copyf)" = "testf" ] || atf_fail "unexpected symlink contents"
290 }
291
292 atf_test_case mkdir_simple
293 mkdir_simple_body() {
294         atf_check install -d dir1/dir2
295         [ -d dir1 ] || atf_fail "dir1 missing"
296         [ -d dir1/dir2 ] || atf_fail "dir2 missing"
297         atf_check install -d dir1/dir2/dir3
298         [ -d dir1/dir2/dir3 ] || atf_fail "dir3 missing"
299         atf_check install -d dir1
300         atf_check install -d dir1/dir2/dir3
301 }
302
303 atf_init_test_cases() {
304         atf_add_test_case copy_to_nonexistent
305         atf_add_test_case copy_to_nonexistent_safe
306         atf_add_test_case copy_to_nonexistent_comparing
307         atf_add_test_case copy_to_nonexistent_safe_comparing
308         atf_add_test_case copy_to_nonexistent_backup
309         atf_add_test_case copy_to_nonexistent_backup_safe
310         atf_add_test_case copy_self
311         atf_add_test_case copy_self_safe
312         atf_add_test_case copy_self_comparing
313         atf_add_test_case copy_self_safe_comparing
314         atf_add_test_case overwrite
315         atf_add_test_case overwrite_safe
316         atf_add_test_case overwrite_comparing
317         atf_add_test_case overwrite_safe_comparing
318         atf_add_test_case overwrite_eq
319         atf_add_test_case overwrite_eq_safe
320         atf_add_test_case overwrite_eq_comparing
321         atf_add_test_case overwrite_eq_safe_comparing
322         atf_add_test_case overwrite_backup
323         atf_add_test_case overwrite_backup_safe
324         atf_add_test_case overwrite_backup_comparing
325         atf_add_test_case overwrite_backup_safe_comparing
326         atf_add_test_case strip_changing
327         atf_add_test_case strip_changing_comparing
328         atf_add_test_case strip_changing_overwrite
329         atf_add_test_case strip_changing_overwrite_comparing
330         atf_add_test_case strip_changing_overwrite_eq
331         atf_add_test_case strip_changing_overwrite_eq_comparing
332         atf_add_test_case strip_noop
333         atf_add_test_case hard_link
334         atf_add_test_case symbolic_link
335         atf_add_test_case symbolic_link_absolute
336         atf_add_test_case symbolic_link_relative
337         atf_add_test_case mkdir_simple
338 }