xref: /minix3/tests/bin/cp/t_cp.sh (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc# $NetBSD: t_cp.sh,v 1.1 2012/03/17 16:33:10 jruoho Exp $
2*11be35a1SLionel Sambuc#
3*11be35a1SLionel Sambuc# Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc# All rights reserved.
5*11be35a1SLionel Sambuc#
6*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc# are met:
9*11be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc#    documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc#
15*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*11be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*11be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*11be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*11be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*11be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*11be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*11be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*11be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*11be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*11be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
26*11be35a1SLionel Sambuc#
27*11be35a1SLionel Sambuc
28*11be35a1SLionel SambucFILES="file file2 file3 link dir dir2 dirlink target"
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuccleanup() {
31*11be35a1SLionel Sambuc        rm -fr ${FILES}
32*11be35a1SLionel Sambuc}
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuccp_compare() {
35*11be35a1SLionel Sambuc	echo "Ensuring that $2 and $3 are identical"
36*11be35a1SLionel Sambuc	cmp -s $2 $3 || atf_fail "$2 and $3 are different"
37*11be35a1SLionel Sambuc}
38*11be35a1SLionel Sambuc
39*11be35a1SLionel Sambucreset() {
40*11be35a1SLionel Sambuc	cleanup
41*11be35a1SLionel Sambuc	echo "I'm a file" > file
42*11be35a1SLionel Sambuc	echo "I'm a file, 2" > file2
43*11be35a1SLionel Sambuc	echo "I'm a file, 3" > file3
44*11be35a1SLionel Sambuc	ln -s file link
45*11be35a1SLionel Sambuc	mkdir dir
46*11be35a1SLionel Sambuc	ln -s dir dirlink
47*11be35a1SLionel Sambuc}
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambucatf_test_case file_to_file
50*11be35a1SLionel Sambucfile_to_file_head() {
51*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a file to a file"
52*11be35a1SLionel Sambuc}
53*11be35a1SLionel Sambucfile_to_file_body() {
54*11be35a1SLionel Sambuc	reset
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc	file_to_file_simple
57*11be35a1SLionel Sambuc	file_to_file_preserve
58*11be35a1SLionel Sambuc	file_to_file_noflags
59*11be35a1SLionel Sambuc}
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambucfile_to_file_simple() {
62*11be35a1SLionel Sambuc	rm -f file2
63*11be35a1SLionel Sambuc	umask 022
64*11be35a1SLionel Sambuc	chmod 777 file
65*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp file file2
66*11be35a1SLionel Sambuc	cp_compare file_to_file_simple file file2
67*11be35a1SLionel Sambuc	if [ `stat -f "%Lp" file2` != "755" ]; then
68*11be35a1SLionel Sambuc		atf_fail "new file not created with umask"
69*11be35a1SLionel Sambuc	fi
70*11be35a1SLionel Sambuc
71*11be35a1SLionel Sambuc	chmod 644 file
72*11be35a1SLionel Sambuc	chmod 777 file2
73*11be35a1SLionel Sambuc	cp_compare file_to_file_simple file file2
74*11be35a1SLionel Sambuc	if [ `stat -f "%Lp" file2` != "777" ]; then
75*11be35a1SLionel Sambuc		atf_fail "existing files permissions not retained"
76*11be35a1SLionel Sambuc	fi
77*11be35a1SLionel Sambuc}
78*11be35a1SLionel Sambuc
79*11be35a1SLionel Sambucfile_to_file_preserve() {
80*11be35a1SLionel Sambuc	rm file3
81*11be35a1SLionel Sambuc	chmod 644 file
82*11be35a1SLionel Sambuc	chflags nodump file
83*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -p file file3
84*11be35a1SLionel Sambuc	finfo=`stat -f "%p%u%g%m%z%f" file`
85*11be35a1SLionel Sambuc	f3info=`stat -f "%p%u%g%m%z%f" file3`
86*11be35a1SLionel Sambuc	if [ $finfo != $f3info ]; then
87*11be35a1SLionel Sambuc		atf_fail "attributes not preserved"
88*11be35a1SLionel Sambuc	fi
89*11be35a1SLionel Sambuc}
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambucfile_to_file_noflags() {
92*11be35a1SLionel Sambuc	rm file3
93*11be35a1SLionel Sambuc	chmod 644 file
94*11be35a1SLionel Sambuc	chflags nodump file
95*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -p -N file file3
96*11be35a1SLionel Sambuc	finfo=`stat -f "%f" file`
97*11be35a1SLionel Sambuc	f3info=`stat -f "%f" file3`
98*11be35a1SLionel Sambuc	if [ $finfo = $f3info ]; then
99*11be35a1SLionel Sambuc		atf_fail "-p -N preserved file flags"
100*11be35a1SLionel Sambuc	fi
101*11be35a1SLionel Sambuc}
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambucatf_test_case file_to_link
104*11be35a1SLionel Sambucfile_to_link_head() {
105*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a file to a symbolic link"
106*11be35a1SLionel Sambuc}
107*11be35a1SLionel Sambucfile_to_link_body() {
108*11be35a1SLionel Sambuc	reset
109*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp file2 link
110*11be35a1SLionel Sambuc	cp_compare file_to_link file file2
111*11be35a1SLionel Sambuc}
112*11be35a1SLionel Sambuc
113*11be35a1SLionel Sambucatf_test_case link_to_file
114*11be35a1SLionel Sambuclink_to_file_head() {
115*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a symbolic link to a file"
116*11be35a1SLionel Sambuc}
117*11be35a1SLionel Sambuclink_to_file_body() {
118*11be35a1SLionel Sambuc	reset
119*11be35a1SLionel Sambuc	# file and link are identical (not copied).
120*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e ignore cp link file
121*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp link file2
122*11be35a1SLionel Sambuc	cp_compare link_to_file file file2
123*11be35a1SLionel Sambuc}
124*11be35a1SLionel Sambuc
125*11be35a1SLionel Sambucatf_test_case file_over_link
126*11be35a1SLionel Sambucfile_over_link_head() {
127*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a file to a symbolic link" \
128*11be35a1SLionel Sambuc	                "without following it"
129*11be35a1SLionel Sambuc}
130*11be35a1SLionel Sambucfile_over_link_body() {
131*11be35a1SLionel Sambuc	reset
132*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -P file link
133*11be35a1SLionel Sambuc	cp_compare file_over_link file link
134*11be35a1SLionel Sambuc}
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambucatf_test_case link_over_file
137*11be35a1SLionel Sambuclink_over_file_head() {
138*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a symbolic link to a file" \
139*11be35a1SLionel Sambuc	                "without following the former"
140*11be35a1SLionel Sambuc}
141*11be35a1SLionel Sambuclink_over_file_body() {
142*11be35a1SLionel Sambuc	reset
143*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -P link file
144*11be35a1SLionel Sambuc	if [ `readlink link` != `readlink file` ]; then
145*11be35a1SLionel Sambuc		atf_fail "readlink link != readlink file"
146*11be35a1SLionel Sambuc	fi
147*11be35a1SLionel Sambuc}
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambucatf_test_case files_to_dir
150*11be35a1SLionel Sambucfiles_to_dir_head() {
151*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of multiple files into a directory"
152*11be35a1SLionel Sambuc}
153*11be35a1SLionel Sambucfiles_to_dir_body() {
154*11be35a1SLionel Sambuc	reset
155*11be35a1SLionel Sambuc	# can't copy multiple files to a file
156*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e ignore cp file file2 file3
157*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp file file2 link dir
158*11be35a1SLionel Sambuc	cp_compare files_to_dir file "dir/file"
159*11be35a1SLionel Sambuc}
160*11be35a1SLionel Sambuc
161*11be35a1SLionel Sambucatf_test_case dir_to_file
162*11be35a1SLionel Sambucdir_to_file_head() {
163*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a directory onto a file, which" \
164*11be35a1SLionel Sambuc	                "should not work"
165*11be35a1SLionel Sambuc}
166*11be35a1SLionel Sambucdir_to_file_body() {
167*11be35a1SLionel Sambuc	reset
168*11be35a1SLionel Sambuc	# can't copy a dir onto a file
169*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e ignore cp dir file
170*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e ignore cp -R dir file
171*11be35a1SLionel Sambuc}
172*11be35a1SLionel Sambuc
173*11be35a1SLionel Sambucatf_test_case file_to_linkdir
174*11be35a1SLionel Sambucfile_to_linkdir_head() {
175*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a file to a symbolic link that" \
176*11be35a1SLionel Sambuc	                "points to a directory"
177*11be35a1SLionel Sambuc}
178*11be35a1SLionel Sambucfile_to_linkdir_body() {
179*11be35a1SLionel Sambuc	reset
180*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp file dirlink
181*11be35a1SLionel Sambuc	cp_compare file_to_linkdir file "dir/file"
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc	# overwrite the link
184*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -P file dirlink
185*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e empty readlink dirlink
186*11be35a1SLionel Sambuc	cp_compare file_to_linkdir file dirlink
187*11be35a1SLionel Sambuc}
188*11be35a1SLionel Sambuc
189*11be35a1SLionel Sambucatf_test_case linkdir_to_file
190*11be35a1SLionel Sambuclinkdir_to_file_head() {
191*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a symbolic link that points to" \
192*11be35a1SLionel Sambuc	                "a directory onto a file"
193*11be35a1SLionel Sambuc}
194*11be35a1SLionel Sambuclinkdir_to_file_body() {
195*11be35a1SLionel Sambuc	reset
196*11be35a1SLionel Sambuc	# cannot copy a dir onto a file
197*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e ignore cp dirlink file
198*11be35a1SLionel Sambuc
199*11be35a1SLionel Sambuc	# overwrite the link
200*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -P dirlink file
201*11be35a1SLionel Sambuc	if [ `readlink file` != `readlink dirlink` ]; then
202*11be35a1SLionel Sambuc		atf_fail "readlink link != readlink file"
203*11be35a1SLionel Sambuc	fi
204*11be35a1SLionel Sambuc}
205*11be35a1SLionel Sambuc
206*11be35a1SLionel Sambucdir_to_dne_no_R() {
207*11be35a1SLionel Sambuc	atf_check -s eq:1 -o empty -e ignore cp dir dir2
208*11be35a1SLionel Sambuc}
209*11be35a1SLionel Sambuc
210*11be35a1SLionel Sambucdir_to_dne() {
211*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
212*11be35a1SLionel Sambuc	cp_compare dir_to_dne "dir/file" "dir2/file"
213*11be35a1SLionel Sambuc	readlink dir2/link >/dev/null
214*11be35a1SLionel Sambuc	if [ $? -gt 0 ]; then
215*11be35a1SLionel Sambuc		atf_fail "-R didn't copy a link as a link"
216*11be35a1SLionel Sambuc	fi
217*11be35a1SLionel Sambuc}
218*11be35a1SLionel Sambuc
219*11be35a1SLionel Sambucdir_to_dir_H() {
220*11be35a1SLionel Sambuc	dir_to_dir_setup
221*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
222*11be35a1SLionel Sambuc
223*11be35a1SLionel Sambuc	chmod 777 dir
224*11be35a1SLionel Sambuc
225*11be35a1SLionel Sambuc	# copy a dir into a dir, only command-line links are followed
226*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R -H dirlink dir2
227*11be35a1SLionel Sambuc	cp_compare dir_to_dir_H "dir/file" "dir2/dirlink/file"
228*11be35a1SLionel Sambuc	readlink dir2/dirlink/link >/dev/null
229*11be35a1SLionel Sambuc	if [ $? -gt 0 ]; then
230*11be35a1SLionel Sambuc		atf_fail "didn't copy a link as a link"
231*11be35a1SLionel Sambuc	fi
232*11be35a1SLionel Sambuc
233*11be35a1SLionel Sambuc	# Created directories have the same mode as the corresponding
234*11be35a1SLionel Sambuc        # source directory, unmodified by the process's umask.
235*11be35a1SLionel Sambuc	if [ `stat -f "%Lp" dir2/dirlink` != "777" ]; then
236*11be35a1SLionel Sambuc		atf_fail "-R modified dir perms with umask"
237*11be35a1SLionel Sambuc	fi
238*11be35a1SLionel Sambuc}
239*11be35a1SLionel Sambuc
240*11be35a1SLionel Sambucdir_to_dir_L() {
241*11be35a1SLionel Sambuc	dir_to_dir_setup
242*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R dir dir2
243*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R -H dirlink dir2
244*11be35a1SLionel Sambuc
245*11be35a1SLionel Sambuc	# copy a dir into a dir, following all links
246*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R -H -L dirlink dir2/dirlink
247*11be35a1SLionel Sambuc	cp_compare dir_to_dir_L "dir/file" "dir2/dirlink/dirlink/file"
248*11be35a1SLionel Sambuc	# fail if -R -L copied a link as a link
249*11be35a1SLionel Sambuc	atf_check -s eq:1 -o ignore -e empty readlink dir2/dirlink/dirlink/link
250*11be35a1SLionel Sambuc}
251*11be35a1SLionel Sambuc
252*11be35a1SLionel Sambucdir_to_dir_subdir_exists() {
253*11be35a1SLionel Sambuc	# recursively copy a dir into another dir, with some subdirs already
254*11be35a1SLionel Sambuc	# existing
255*11be35a1SLionel Sambuc	cleanup
256*11be35a1SLionel Sambuc
257*11be35a1SLionel Sambuc	mkdir -p dir/1 dir/2 dir/3 target/2
258*11be35a1SLionel Sambuc	echo "file" > dir/2/file
259*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp -R dir/* target
260*11be35a1SLionel Sambuc	cp_compare dir_to_dir_subdir_exists "dir/2/file" "target/2/file"
261*11be35a1SLionel Sambuc}
262*11be35a1SLionel Sambuc
263*11be35a1SLionel Sambucdir_to_dir_setup() {
264*11be35a1SLionel Sambuc	reset
265*11be35a1SLionel Sambuc	umask 077
266*11be35a1SLionel Sambuc	cp -P file file2 file3 link dir
267*11be35a1SLionel Sambuc}
268*11be35a1SLionel Sambuc
269*11be35a1SLionel Sambucatf_test_case dir_to_dir
270*11be35a1SLionel Sambucdir_to_dir_head() {
271*11be35a1SLionel Sambuc	atf_set "descr" "Checks the copy of a directory onto another directory"
272*11be35a1SLionel Sambuc}
273*11be35a1SLionel Sambucdir_to_dir_body() {
274*11be35a1SLionel Sambuc	dir_to_dir_setup
275*11be35a1SLionel Sambuc	dir_to_dne_no_R
276*11be35a1SLionel Sambuc	dir_to_dne
277*11be35a1SLionel Sambuc	dir_to_dir_H
278*11be35a1SLionel Sambuc	dir_to_dir_L
279*11be35a1SLionel Sambuc	dir_to_dir_subdir_exists
280*11be35a1SLionel Sambuc}
281*11be35a1SLionel Sambuc
282*11be35a1SLionel Sambucatf_init_test_cases()
283*11be35a1SLionel Sambuc{
284*11be35a1SLionel Sambuc	atf_add_test_case file_to_file
285*11be35a1SLionel Sambuc	atf_add_test_case file_to_link
286*11be35a1SLionel Sambuc	atf_add_test_case link_to_file
287*11be35a1SLionel Sambuc	atf_add_test_case file_over_link
288*11be35a1SLionel Sambuc	atf_add_test_case link_over_file
289*11be35a1SLionel Sambuc	atf_add_test_case files_to_dir
290*11be35a1SLionel Sambuc	atf_add_test_case file_to_linkdir
291*11be35a1SLionel Sambuc	atf_add_test_case linkdir_to_file
292*11be35a1SLionel Sambuc	atf_add_test_case dir_to_file
293*11be35a1SLionel Sambuc	atf_add_test_case dir_to_dir
294*11be35a1SLionel Sambuc}
295