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
29copy_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
37atf_test_case copy_to_nonexistent
38copy_to_nonexistent_body() {
39	copy_to_nonexistent_with_opts
40}
41
42atf_test_case copy_to_nonexistent_safe
43copy_to_nonexistent_safe_body() {
44	copy_to_nonexistent_with_opts -S
45}
46
47atf_test_case copy_to_nonexistent_comparing
48copy_to_nonexistent_comparing_body() {
49	copy_to_nonexistent_with_opts -C
50}
51
52atf_test_case copy_to_nonexistent_safe_comparing
53copy_to_nonexistent_safe_comparing_body() {
54	copy_to_nonexistent_with_opts -S -C
55}
56
57atf_test_case copy_to_nonexistent_backup
58copy_to_nonexistent_backup_body() {
59	copy_to_nonexistent_with_opts -b -B.bak
60}
61
62atf_test_case copy_to_nonexistent_backup_safe
63copy_to_nonexistent_backup_safe_body() {
64	copy_to_nonexistent_with_opts -b -B.bak -S
65}
66
67atf_test_case copy_to_nonexistent_preserving
68copy_to_nonexistent_preserving_body() {
69	copy_to_nonexistent_with_opts -p
70	[ ! testf -ot copyf ] || atf_fail "bad timestamp 2"
71}
72
73copy_self_with_opts() {
74	printf 'test\n123\r456\r\n789\0z' >testf
75	printf 'test\n123\r456\r\n789\0z' >testf2
76	atf_check -s not-exit:0 -o empty -e match:. install "$@" testf testf
77	cmp testf testf2 || atf_fail "file changed after self-copy attempt"
78}
79
80atf_test_case copy_self
81copy_self_body() {
82	copy_self_with_opts
83}
84
85atf_test_case copy_self_safe
86copy_self_safe_body() {
87	copy_self_with_opts -S
88}
89
90atf_test_case copy_self_comparing
91copy_self_comparing_body() {
92	copy_self_with_opts -C
93}
94
95atf_test_case copy_self_safe_comparing
96copy_self_safe_comparing_body() {
97	copy_self_with_opts -S -C
98}
99
100overwrite_with_opts() {
101	printf 'test\n123\r456\r\n789\0z' >testf
102	printf 'test\n123\r456\r\n789\0w' >otherf
103	atf_check install "$@" testf otherf
104	cmp testf otherf || atf_fail "bad overwrite"
105	[ ! testf -nt otherf ] || atf_fail "bad timestamp"
106}
107
108atf_test_case overwrite
109overwrite_body() {
110	overwrite_with_opts
111}
112
113atf_test_case overwrite_safe
114overwrite_safe_body() {
115	overwrite_with_opts -S
116}
117
118atf_test_case overwrite_comparing
119overwrite_comparing_body() {
120	overwrite_with_opts -C
121}
122
123atf_test_case overwrite_safe_comparing
124overwrite_safe_comparing_body() {
125	overwrite_with_opts -S -C
126}
127
128overwrite_eq_with_opts() {
129	printf 'test\n123\r456\r\n789\0z' >testf
130	printf 'test\n123\r456\r\n789\0z' >otherf
131	atf_check install "$@" testf otherf
132	cmp testf otherf || atf_fail "bad overwrite"
133	[ ! testf -nt otherf ] || atf_fail "bad timestamp"
134}
135
136atf_test_case overwrite_eq
137overwrite_eq_body() {
138	overwrite_eq_with_opts
139}
140
141atf_test_case overwrite_eq_safe
142overwrite_eq_safe_body() {
143	overwrite_eq_with_opts -S
144}
145
146atf_test_case overwrite_eq_comparing
147overwrite_eq_comparing_body() {
148	overwrite_eq_with_opts -C
149}
150
151atf_test_case overwrite_eq_safe_comparing
152overwrite_eq_safe_comparing_body() {
153	overwrite_eq_with_opts -S -C
154}
155
156overwrite_backup_with_opts() {
157	printf 'test\n123\r456\r\n789\0z' >testf
158	printf 'test\n123\r456\r\n789\0w' >otherf
159	printf 'test\n123\r456\r\n789\0w' >otherf2
160	atf_check install -b -B.bak "$@" testf otherf
161	cmp testf otherf || atf_fail "bad overwrite"
162	[ ! testf -nt otherf ] || atf_fail "bad timestamp"
163	cmp otherf.bak otherf2 || atf_fail "bad backup"
164}
165
166atf_test_case overwrite_backup
167overwrite_backup_body() {
168	overwrite_backup_with_opts
169}
170
171atf_test_case overwrite_backup_safe
172overwrite_backup_safe_body() {
173	overwrite_backup_with_opts -S
174}
175
176atf_test_case overwrite_backup_comparing
177overwrite_backup_comparing_body() {
178	overwrite_backup_with_opts -C
179}
180
181atf_test_case overwrite_backup_safe_comparing
182overwrite_backup_safe_comparing_body() {
183	overwrite_backup_with_opts -S -C
184}
185
186setup_stripbin() {
187	cat <<\STRIPBIN >stripbin
188#!/bin/sh
189tr z @ <"$1" >"$1.new" && mv -- "$1.new" "$1"
190STRIPBIN
191	chmod 755 stripbin
192	export STRIPBIN="$PWD/stripbin"
193}
194
195strip_changing_with_opts() {
196	setup_stripbin
197	printf 'test\n123\r456\r\n789\0z' >testf
198	atf_check install -s "$@" testf copyf
199	[ ! testf -nt copyf ] || atf_fail "bad timestamp"
200	printf 'test\n123\r456\r\n789\0@' >otherf
201	cmp otherf copyf || atf_fail "bad stripped copy"
202}
203
204atf_test_case strip_changing
205strip_changing_body() {
206	strip_changing_with_opts
207}
208
209atf_test_case strip_changing_comparing
210strip_changing_comparing_body() {
211	strip_changing_with_opts -C
212}
213
214strip_changing_overwrite_with_opts() {
215	setup_stripbin
216	printf 'test\n123\r456\r\n789\0z' >testf
217	printf 'test\n123\r456\r\n789\0w' >copyf
218	atf_check install -s "$@" testf copyf
219	[ ! testf -nt copyf ] || atf_fail "bad timestamp"
220	printf 'test\n123\r456\r\n789\0@' >otherf
221	cmp otherf copyf || atf_fail "bad stripped copy"
222}
223
224atf_test_case strip_changing_overwrite
225strip_changing_overwrite_body() {
226	strip_changing_overwrite_with_opts
227}
228
229atf_test_case strip_changing_overwrite_comparing
230strip_changing_overwrite_comparing_body() {
231	strip_changing_overwrite_with_opts -C
232}
233
234strip_changing_overwrite_eq_with_opts() {
235	setup_stripbin
236	printf 'test\n123\r456\r\n789\0z' >testf
237	printf 'test\n123\r456\r\n789\0@' >copyf
238	atf_check install -s "$@" testf copyf
239	[ ! testf -nt copyf ] || atf_fail "bad timestamp"
240	printf 'test\n123\r456\r\n789\0@' >otherf
241	cmp otherf copyf || atf_fail "bad stripped copy"
242}
243
244atf_test_case strip_changing_overwrite_eq
245strip_changing_overwrite_eq_body() {
246	strip_changing_overwrite_eq_with_opts
247}
248
249atf_test_case strip_changing_overwrite_eq_comparing
250strip_changing_overwrite_eq_comparing_body() {
251	strip_changing_overwrite_eq_with_opts -C
252}
253
254atf_test_case strip_noop
255strip_noop_body() {
256	export STRIPBIN=true
257	printf 'test\n123\r456\r\n789\0z' >testf
258	atf_check install -s testf copyf
259	[ ! testf -nt copyf ] || atf_fail "bad timestamp"
260	printf 'test\n123\r456\r\n789\0z' >otherf
261	cmp otherf copyf || atf_fail "bad stripped copy"
262}
263
264atf_test_case hard_link
265hard_link_body() {
266	printf 'test\n123\r456\r\n789\0z' >testf
267	atf_check install -l h testf copyf
268	[ testf -ef copyf ] || atf_fail "not same file"
269	[ ! -L copyf ] || atf_fail "copy is symlink"
270}
271
272atf_test_case symbolic_link
273symbolic_link_body() {
274	printf 'test\n123\r456\r\n789\0z' >testf
275	atf_check install -l s testf copyf
276	[ testf -ef copyf ] || atf_fail "not same file"
277	[ -L copyf ] || atf_fail "copy is not symlink"
278}
279
280atf_test_case symbolic_link_absolute
281symbolic_link_absolute_body() {
282	printf 'test\n123\r456\r\n789\0z' >testf
283	atf_check install -l sa testf copyf
284	[ testf -ef copyf ] || atf_fail "not same file"
285	[ -L copyf ] || atf_fail "copy is not symlink"
286	[ "$(readlink copyf)" = "$(pwd -P)/testf" ] || atf_fail "unexpected symlink contents"
287}
288
289atf_test_case symbolic_link_relative
290symbolic_link_relative_body() {
291	printf 'test\n123\r456\r\n789\0z' >testf
292	atf_check install -l sr testf copyf
293	[ testf -ef copyf ] || atf_fail "not same file"
294	[ -L copyf ] || atf_fail "copy is not symlink"
295	[ "$(readlink copyf)" = "testf" ] || atf_fail "unexpected symlink contents"
296}
297
298atf_test_case mkdir_simple
299mkdir_simple_body() {
300	atf_check install -d dir1/dir2
301	[ -d dir1 ] || atf_fail "dir1 missing"
302	[ -d dir1/dir2 ] || atf_fail "dir2 missing"
303	atf_check install -d dir1/dir2/dir3
304	[ -d dir1/dir2/dir3 ] || atf_fail "dir3 missing"
305	atf_check install -d dir1
306	atf_check install -d dir1/dir2/dir3
307}
308
309atf_init_test_cases() {
310	atf_add_test_case copy_to_nonexistent
311	atf_add_test_case copy_to_nonexistent_safe
312	atf_add_test_case copy_to_nonexistent_comparing
313	atf_add_test_case copy_to_nonexistent_safe_comparing
314	atf_add_test_case copy_to_nonexistent_backup
315	atf_add_test_case copy_to_nonexistent_backup_safe
316	atf_add_test_case copy_to_nonexistent_preserving
317	atf_add_test_case copy_self
318	atf_add_test_case copy_self_safe
319	atf_add_test_case copy_self_comparing
320	atf_add_test_case copy_self_safe_comparing
321	atf_add_test_case overwrite
322	atf_add_test_case overwrite_safe
323	atf_add_test_case overwrite_comparing
324	atf_add_test_case overwrite_safe_comparing
325	atf_add_test_case overwrite_eq
326	atf_add_test_case overwrite_eq_safe
327	atf_add_test_case overwrite_eq_comparing
328	atf_add_test_case overwrite_eq_safe_comparing
329	atf_add_test_case overwrite_backup
330	atf_add_test_case overwrite_backup_safe
331	atf_add_test_case overwrite_backup_comparing
332	atf_add_test_case overwrite_backup_safe_comparing
333	atf_add_test_case strip_changing
334	atf_add_test_case strip_changing_comparing
335	atf_add_test_case strip_changing_overwrite
336	atf_add_test_case strip_changing_overwrite_comparing
337	atf_add_test_case strip_changing_overwrite_eq
338	atf_add_test_case strip_changing_overwrite_eq_comparing
339	atf_add_test_case strip_noop
340	atf_add_test_case hard_link
341	atf_add_test_case symbolic_link
342	atf_add_test_case symbolic_link_absolute
343	atf_add_test_case symbolic_link_relative
344	atf_add_test_case mkdir_simple
345}
346