xref: /freebsd/contrib/netbsd-tests/bin/cp/t_cp.sh (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1# $NetBSD: t_cp.sh,v 1.1 2012/03/17 16:33:10 jruoho Exp $
2#
3# Copyright (c) 2007, 2008 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
28FILES="file file2 file3 link dir dir2 dirlink target"
29
30cleanup() {
31        rm -fr ${FILES}
32}
33
34cp_compare() {
35	echo "Ensuring that $2 and $3 are identical"
36	cmp -s $2 $3 || atf_fail "$2 and $3 are different"
37}
38
39reset() {
40	cleanup
41	echo "I'm a file" > file
42	echo "I'm a file, 2" > file2
43	echo "I'm a file, 3" > file3
44	ln -s file link
45	mkdir dir
46	ln -s dir dirlink
47}
48
49atf_test_case file_to_file
50file_to_file_head() {
51	atf_set "descr" "Checks the copy of a file to a file"
52}
53file_to_file_body() {
54	reset
55
56	file_to_file_simple
57	file_to_file_preserve
58	file_to_file_noflags
59}
60
61file_to_file_simple() {
62	rm -f file2
63	umask 022
64	chmod 777 file
65	atf_check -s eq:0 -o empty -e empty cp file file2
66	cp_compare file_to_file_simple file file2
67	if [ `stat -f "%Lp" file2` != "755" ]; then
68		atf_fail "new file not created with umask"
69	fi
70
71	chmod 644 file
72	chmod 777 file2
73	cp_compare file_to_file_simple file file2
74	if [ `stat -f "%Lp" file2` != "777" ]; then
75		atf_fail "existing files permissions not retained"
76	fi
77}
78
79file_to_file_preserve() {
80	rm file3
81	chmod 644 file
82	chflags nodump file
83	atf_check -s eq:0 -o empty -e empty cp -p file file3
84	finfo=`stat -f "%p%u%g%m%z%f" file`
85	f3info=`stat -f "%p%u%g%m%z%f" file3`
86	if [ $finfo != $f3info ]; then
87		atf_fail "attributes not preserved"
88	fi
89}
90
91file_to_file_noflags() {
92	rm file3
93	chmod 644 file
94	chflags nodump file
95	atf_check -s eq:0 -o empty -e empty cp -p -N file file3
96	finfo=`stat -f "%f" file`
97	f3info=`stat -f "%f" file3`
98	if [ $finfo = $f3info ]; then
99		atf_fail "-p -N preserved file flags"
100	fi
101}
102
103atf_test_case file_to_link
104file_to_link_head() {
105	atf_set "descr" "Checks the copy of a file to a symbolic link"
106}
107file_to_link_body() {
108	reset
109	atf_check -s eq:0 -o empty -e empty cp file2 link
110	cp_compare file_to_link file file2
111}
112
113atf_test_case link_to_file
114link_to_file_head() {
115	atf_set "descr" "Checks the copy of a symbolic link to a file"
116}
117link_to_file_body() {
118	reset
119	# file and link are identical (not copied).
120	atf_check -s eq:1 -o empty -e ignore cp link file
121	atf_check -s eq:0 -o empty -e empty cp link file2
122	cp_compare link_to_file file file2
123}
124
125atf_test_case file_over_link
126file_over_link_head() {
127	atf_set "descr" "Checks the copy of a file to a symbolic link" \
128	                "without following it"
129}
130file_over_link_body() {
131	reset
132	atf_check -s eq:0 -o empty -e empty cp -P file link
133	cp_compare file_over_link file link
134}
135
136atf_test_case link_over_file
137link_over_file_head() {
138	atf_set "descr" "Checks the copy of a symbolic link to a file" \
139	                "without following the former"
140}
141link_over_file_body() {
142	reset
143	atf_check -s eq:0 -o empty -e empty cp -P link file
144	if [ `readlink link` != `readlink file` ]; then
145		atf_fail "readlink link != readlink file"
146	fi
147}
148
149atf_test_case files_to_dir
150files_to_dir_head() {
151	atf_set "descr" "Checks the copy of multiple files into a directory"
152}
153files_to_dir_body() {
154	reset
155	# can't copy multiple files to a file
156	atf_check -s eq:1 -o empty -e ignore cp file file2 file3
157	atf_check -s eq:0 -o empty -e empty cp file file2 link dir
158	cp_compare files_to_dir file "dir/file"
159}
160
161atf_test_case dir_to_file
162dir_to_file_head() {
163	atf_set "descr" "Checks the copy of a directory onto a file, which" \
164	                "should not work"
165}
166dir_to_file_body() {
167	reset
168	# can't copy a dir onto a file
169	atf_check -s eq:1 -o empty -e ignore cp dir file
170	atf_check -s eq:1 -o empty -e ignore cp -R dir file
171}
172
173atf_test_case file_to_linkdir
174file_to_linkdir_head() {
175	atf_set "descr" "Checks the copy of a file to a symbolic link that" \
176	                "points to a directory"
177}
178file_to_linkdir_body() {
179	reset
180	atf_check -s eq:0 -o empty -e empty cp file dirlink
181	cp_compare file_to_linkdir file "dir/file"
182
183	# overwrite the link
184	atf_check -s eq:0 -o empty -e empty cp -P file dirlink
185	atf_check -s eq:1 -o empty -e empty readlink dirlink
186	cp_compare file_to_linkdir file dirlink
187}
188
189atf_test_case linkdir_to_file
190linkdir_to_file_head() {
191	atf_set "descr" "Checks the copy of a symbolic link that points to" \
192	                "a directory onto a file"
193}
194linkdir_to_file_body() {
195	reset
196	# cannot copy a dir onto a file
197	atf_check -s eq:1 -o empty -e ignore cp dirlink file
198
199	# overwrite the link
200	atf_check -s eq:0 -o empty -e empty cp -P dirlink file
201	if [ `readlink file` != `readlink dirlink` ]; then
202		atf_fail "readlink link != readlink file"
203	fi
204}
205
206dir_to_dne_no_R() {
207	atf_check -s eq:1 -o empty -e ignore cp dir dir2
208}
209
210dir_to_dne() {
211	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
212	cp_compare dir_to_dne "dir/file" "dir2/file"
213	readlink dir2/link >/dev/null
214	if [ $? -gt 0 ]; then
215		atf_fail "-R didn't copy a link as a link"
216	fi
217}
218
219dir_to_dir_H() {
220	dir_to_dir_setup
221	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
222
223	chmod 777 dir
224
225	# copy a dir into a dir, only command-line links are followed
226	atf_check -s eq:0 -o empty -e empty cp -R -H dirlink dir2
227	cp_compare dir_to_dir_H "dir/file" "dir2/dirlink/file"
228	readlink dir2/dirlink/link >/dev/null
229	if [ $? -gt 0 ]; then
230		atf_fail "didn't copy a link as a link"
231	fi
232
233	# Created directories have the same mode as the corresponding
234        # source directory, unmodified by the process's umask.
235	if [ `stat -f "%Lp" dir2/dirlink` != "777" ]; then
236		atf_fail "-R modified dir perms with umask"
237	fi
238}
239
240dir_to_dir_L() {
241	dir_to_dir_setup
242	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
243	atf_check -s eq:0 -o empty -e empty cp -R -H dirlink dir2
244
245	# copy a dir into a dir, following all links
246	atf_check -s eq:0 -o empty -e empty cp -R -H -L dirlink dir2/dirlink
247	cp_compare dir_to_dir_L "dir/file" "dir2/dirlink/dirlink/file"
248	# fail if -R -L copied a link as a link
249	atf_check -s eq:1 -o ignore -e empty readlink dir2/dirlink/dirlink/link
250}
251
252dir_to_dir_subdir_exists() {
253	# recursively copy a dir into another dir, with some subdirs already
254	# existing
255	cleanup
256
257	mkdir -p dir/1 dir/2 dir/3 target/2
258	echo "file" > dir/2/file
259	atf_check -s eq:0 -o empty -e empty cp -R dir/* target
260	cp_compare dir_to_dir_subdir_exists "dir/2/file" "target/2/file"
261}
262
263dir_to_dir_setup() {
264	reset
265	umask 077
266	cp -P file file2 file3 link dir
267}
268
269atf_test_case dir_to_dir
270dir_to_dir_head() {
271	atf_set "descr" "Checks the copy of a directory onto another directory"
272}
273dir_to_dir_body() {
274	dir_to_dir_setup
275	dir_to_dne_no_R
276	dir_to_dne
277	dir_to_dir_H
278	dir_to_dir_L
279	dir_to_dir_subdir_exists
280}
281
282atf_init_test_cases()
283{
284	atf_add_test_case file_to_file
285	atf_add_test_case file_to_link
286	atf_add_test_case link_to_file
287	atf_add_test_case file_over_link
288	atf_add_test_case link_over_file
289	atf_add_test_case files_to_dir
290	atf_add_test_case file_to_linkdir
291	atf_add_test_case linkdir_to_file
292	atf_add_test_case dir_to_file
293	atf_add_test_case dir_to_dir
294}
295