1272343Sngie# $NetBSD: t_cp.sh,v 1.1 2012/03/17 16:33:10 jruoho Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343SngieFILES="file file2 file3 link dir dir2 dirlink target"
29272343Sngie
30272343Sngiecleanup() {
31272343Sngie        rm -fr ${FILES}
32272343Sngie}
33272343Sngie
34272343Sngiecp_compare() {
35272343Sngie	echo "Ensuring that $2 and $3 are identical"
36272343Sngie	cmp -s $2 $3 || atf_fail "$2 and $3 are different"
37272343Sngie}
38272343Sngie
39272343Sngiereset() {
40272343Sngie	cleanup
41272343Sngie	echo "I'm a file" > file
42272343Sngie	echo "I'm a file, 2" > file2
43272343Sngie	echo "I'm a file, 3" > file3
44272343Sngie	ln -s file link
45272343Sngie	mkdir dir
46272343Sngie	ln -s dir dirlink
47272343Sngie}
48272343Sngie
49272343Sngieatf_test_case file_to_file
50272343Sngiefile_to_file_head() {
51272343Sngie	atf_set "descr" "Checks the copy of a file to a file"
52272343Sngie}
53272343Sngiefile_to_file_body() {
54272343Sngie	reset
55272343Sngie
56272343Sngie	file_to_file_simple
57272343Sngie	file_to_file_preserve
58272343Sngie	file_to_file_noflags
59272343Sngie}
60272343Sngie
61272343Sngiefile_to_file_simple() {
62272343Sngie	rm -f file2
63272343Sngie	umask 022
64272343Sngie	chmod 777 file
65272343Sngie	atf_check -s eq:0 -o empty -e empty cp file file2
66272343Sngie	cp_compare file_to_file_simple file file2
67272343Sngie	if [ `stat -f "%Lp" file2` != "755" ]; then
68272343Sngie		atf_fail "new file not created with umask"
69272343Sngie	fi
70272343Sngie
71272343Sngie	chmod 644 file
72272343Sngie	chmod 777 file2
73272343Sngie	cp_compare file_to_file_simple file file2
74272343Sngie	if [ `stat -f "%Lp" file2` != "777" ]; then
75272343Sngie		atf_fail "existing files permissions not retained"
76272343Sngie	fi
77272343Sngie}
78272343Sngie
79272343Sngiefile_to_file_preserve() {
80272343Sngie	rm file3
81272343Sngie	chmod 644 file
82272343Sngie	chflags nodump file
83272343Sngie	atf_check -s eq:0 -o empty -e empty cp -p file file3
84272343Sngie	finfo=`stat -f "%p%u%g%m%z%f" file`
85272343Sngie	f3info=`stat -f "%p%u%g%m%z%f" file3`
86272343Sngie	if [ $finfo != $f3info ]; then
87272343Sngie		atf_fail "attributes not preserved"
88272343Sngie	fi
89272343Sngie}
90272343Sngie
91272343Sngiefile_to_file_noflags() {
92272343Sngie	rm file3
93272343Sngie	chmod 644 file
94272343Sngie	chflags nodump file
95272343Sngie	atf_check -s eq:0 -o empty -e empty cp -p -N file file3
96272343Sngie	finfo=`stat -f "%f" file`
97272343Sngie	f3info=`stat -f "%f" file3`
98272343Sngie	if [ $finfo = $f3info ]; then
99272343Sngie		atf_fail "-p -N preserved file flags"
100272343Sngie	fi
101272343Sngie}
102272343Sngie
103272343Sngieatf_test_case file_to_link
104272343Sngiefile_to_link_head() {
105272343Sngie	atf_set "descr" "Checks the copy of a file to a symbolic link"
106272343Sngie}
107272343Sngiefile_to_link_body() {
108272343Sngie	reset
109272343Sngie	atf_check -s eq:0 -o empty -e empty cp file2 link
110272343Sngie	cp_compare file_to_link file file2
111272343Sngie}
112272343Sngie
113272343Sngieatf_test_case link_to_file
114272343Sngielink_to_file_head() {
115272343Sngie	atf_set "descr" "Checks the copy of a symbolic link to a file"
116272343Sngie}
117272343Sngielink_to_file_body() {
118272343Sngie	reset
119272343Sngie	# file and link are identical (not copied).
120272343Sngie	atf_check -s eq:1 -o empty -e ignore cp link file
121272343Sngie	atf_check -s eq:0 -o empty -e empty cp link file2
122272343Sngie	cp_compare link_to_file file file2
123272343Sngie}
124272343Sngie
125272343Sngieatf_test_case file_over_link
126272343Sngiefile_over_link_head() {
127272343Sngie	atf_set "descr" "Checks the copy of a file to a symbolic link" \
128272343Sngie	                "without following it"
129272343Sngie}
130272343Sngiefile_over_link_body() {
131272343Sngie	reset
132272343Sngie	atf_check -s eq:0 -o empty -e empty cp -P file link
133272343Sngie	cp_compare file_over_link file link
134272343Sngie}
135272343Sngie
136272343Sngieatf_test_case link_over_file
137272343Sngielink_over_file_head() {
138272343Sngie	atf_set "descr" "Checks the copy of a symbolic link to a file" \
139272343Sngie	                "without following the former"
140272343Sngie}
141272343Sngielink_over_file_body() {
142272343Sngie	reset
143272343Sngie	atf_check -s eq:0 -o empty -e empty cp -P link file
144272343Sngie	if [ `readlink link` != `readlink file` ]; then
145272343Sngie		atf_fail "readlink link != readlink file"
146272343Sngie	fi
147272343Sngie}
148272343Sngie
149272343Sngieatf_test_case files_to_dir
150272343Sngiefiles_to_dir_head() {
151272343Sngie	atf_set "descr" "Checks the copy of multiple files into a directory"
152272343Sngie}
153272343Sngiefiles_to_dir_body() {
154272343Sngie	reset
155272343Sngie	# can't copy multiple files to a file
156272343Sngie	atf_check -s eq:1 -o empty -e ignore cp file file2 file3
157272343Sngie	atf_check -s eq:0 -o empty -e empty cp file file2 link dir
158272343Sngie	cp_compare files_to_dir file "dir/file"
159272343Sngie}
160272343Sngie
161272343Sngieatf_test_case dir_to_file
162272343Sngiedir_to_file_head() {
163272343Sngie	atf_set "descr" "Checks the copy of a directory onto a file, which" \
164272343Sngie	                "should not work"
165272343Sngie}
166272343Sngiedir_to_file_body() {
167272343Sngie	reset
168272343Sngie	# can't copy a dir onto a file
169272343Sngie	atf_check -s eq:1 -o empty -e ignore cp dir file
170272343Sngie	atf_check -s eq:1 -o empty -e ignore cp -R dir file
171272343Sngie}
172272343Sngie
173272343Sngieatf_test_case file_to_linkdir
174272343Sngiefile_to_linkdir_head() {
175272343Sngie	atf_set "descr" "Checks the copy of a file to a symbolic link that" \
176272343Sngie	                "points to a directory"
177272343Sngie}
178272343Sngiefile_to_linkdir_body() {
179272343Sngie	reset
180272343Sngie	atf_check -s eq:0 -o empty -e empty cp file dirlink
181272343Sngie	cp_compare file_to_linkdir file "dir/file"
182272343Sngie
183272343Sngie	# overwrite the link
184272343Sngie	atf_check -s eq:0 -o empty -e empty cp -P file dirlink
185272343Sngie	atf_check -s eq:1 -o empty -e empty readlink dirlink
186272343Sngie	cp_compare file_to_linkdir file dirlink
187272343Sngie}
188272343Sngie
189272343Sngieatf_test_case linkdir_to_file
190272343Sngielinkdir_to_file_head() {
191272343Sngie	atf_set "descr" "Checks the copy of a symbolic link that points to" \
192272343Sngie	                "a directory onto a file"
193272343Sngie}
194272343Sngielinkdir_to_file_body() {
195272343Sngie	reset
196272343Sngie	# cannot copy a dir onto a file
197272343Sngie	atf_check -s eq:1 -o empty -e ignore cp dirlink file
198272343Sngie
199272343Sngie	# overwrite the link
200272343Sngie	atf_check -s eq:0 -o empty -e empty cp -P dirlink file
201272343Sngie	if [ `readlink file` != `readlink dirlink` ]; then
202272343Sngie		atf_fail "readlink link != readlink file"
203272343Sngie	fi
204272343Sngie}
205272343Sngie
206272343Sngiedir_to_dne_no_R() {
207272343Sngie	atf_check -s eq:1 -o empty -e ignore cp dir dir2
208272343Sngie}
209272343Sngie
210272343Sngiedir_to_dne() {
211272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
212272343Sngie	cp_compare dir_to_dne "dir/file" "dir2/file"
213272343Sngie	readlink dir2/link >/dev/null
214272343Sngie	if [ $? -gt 0 ]; then
215272343Sngie		atf_fail "-R didn't copy a link as a link"
216272343Sngie	fi
217272343Sngie}
218272343Sngie
219272343Sngiedir_to_dir_H() {
220272343Sngie	dir_to_dir_setup
221272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
222272343Sngie
223272343Sngie	chmod 777 dir
224272343Sngie
225272343Sngie	# copy a dir into a dir, only command-line links are followed
226272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R -H dirlink dir2
227272343Sngie	cp_compare dir_to_dir_H "dir/file" "dir2/dirlink/file"
228272343Sngie	readlink dir2/dirlink/link >/dev/null
229272343Sngie	if [ $? -gt 0 ]; then
230272343Sngie		atf_fail "didn't copy a link as a link"
231272343Sngie	fi
232272343Sngie
233272343Sngie	# Created directories have the same mode as the corresponding
234272343Sngie        # source directory, unmodified by the process's umask.
235272343Sngie	if [ `stat -f "%Lp" dir2/dirlink` != "777" ]; then
236272343Sngie		atf_fail "-R modified dir perms with umask"
237272343Sngie	fi
238272343Sngie}
239272343Sngie
240272343Sngiedir_to_dir_L() {
241272343Sngie	dir_to_dir_setup
242272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
243272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R -H dirlink dir2
244272343Sngie
245272343Sngie	# copy a dir into a dir, following all links
246272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R -H -L dirlink dir2/dirlink
247272343Sngie	cp_compare dir_to_dir_L "dir/file" "dir2/dirlink/dirlink/file"
248272343Sngie	# fail if -R -L copied a link as a link
249272343Sngie	atf_check -s eq:1 -o ignore -e empty readlink dir2/dirlink/dirlink/link
250272343Sngie}
251272343Sngie
252272343Sngiedir_to_dir_subdir_exists() {
253272343Sngie	# recursively copy a dir into another dir, with some subdirs already
254272343Sngie	# existing
255272343Sngie	cleanup
256272343Sngie
257272343Sngie	mkdir -p dir/1 dir/2 dir/3 target/2
258272343Sngie	echo "file" > dir/2/file
259272343Sngie	atf_check -s eq:0 -o empty -e empty cp -R dir/* target
260272343Sngie	cp_compare dir_to_dir_subdir_exists "dir/2/file" "target/2/file"
261272343Sngie}
262272343Sngie
263272343Sngiedir_to_dir_setup() {
264272343Sngie	reset
265272343Sngie	umask 077
266272343Sngie	cp -P file file2 file3 link dir
267272343Sngie}
268272343Sngie
269272343Sngieatf_test_case dir_to_dir
270272343Sngiedir_to_dir_head() {
271272343Sngie	atf_set "descr" "Checks the copy of a directory onto another directory"
272272343Sngie}
273272343Sngiedir_to_dir_body() {
274272343Sngie	dir_to_dir_setup
275272343Sngie	dir_to_dne_no_R
276272343Sngie	dir_to_dne
277272343Sngie	dir_to_dir_H
278272343Sngie	dir_to_dir_L
279272343Sngie	dir_to_dir_subdir_exists
280272343Sngie}
281272343Sngie
282272343Sngieatf_init_test_cases()
283272343Sngie{
284272343Sngie	atf_add_test_case file_to_file
285272343Sngie	atf_add_test_case file_to_link
286272343Sngie	atf_add_test_case link_to_file
287272343Sngie	atf_add_test_case file_over_link
288272343Sngie	atf_add_test_case link_over_file
289272343Sngie	atf_add_test_case files_to_dir
290272343Sngie	atf_add_test_case file_to_linkdir
291272343Sngie	atf_add_test_case linkdir_to_file
292272343Sngie	atf_add_test_case dir_to_file
293272343Sngie	atf_add_test_case dir_to_dir
294272343Sngie}
295