1 # $NetBSD: t_cp.sh,v 1.2 2008/04/30 13:11:00 martin Exp $
3 # Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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.
28 FILES
="file file2 file3 link dir dir2 dirlink target"
35 echo "Ensuring that $2 and $3 are identical"
36 cmp -s $2 $3 || atf_fail
"$2 and $3 are different"
41 echo "I'm a file" > file
42 echo "I'm a file, 2" > file2
43 echo "I'm a file, 3" > file3
49 atf_test_case file_to_file
51 atf_set
"descr" "Checks the copy of a file to a file"
61 file_to_file_simple
() {
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"
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"
79 file_to_file_preserve
() {
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"
91 file_to_file_noflags
() {
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"
103 atf_test_case file_to_link
104 file_to_link_head
() {
105 atf_set
"descr" "Checks the copy of a file to a symbolic link"
107 file_to_link_body
() {
109 atf_check
-s eq
:0 -o empty
-e empty
cp file2 link
110 cp_compare file_to_link
file file2
113 atf_test_case link_to_file
114 link_to_file_head
() {
115 atf_set
"descr" "Checks the copy of a symbolic link to a file"
117 link_to_file_body
() {
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
125 atf_test_case file_over_link
126 file_over_link_head
() {
127 atf_set
"descr" "Checks the copy of a file to a symbolic link" \
128 "without following it"
130 file_over_link_body
() {
132 atf_check
-s eq
:0 -o empty
-e empty
cp -P file link
133 cp_compare file_over_link
file link
136 atf_test_case link_over_file
137 link_over_file_head
() {
138 atf_set
"descr" "Checks the copy of a symbolic link to a file" \
139 "without following the former"
141 link_over_file_body
() {
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"
149 atf_test_case files_to_dir
150 files_to_dir_head
() {
151 atf_set
"descr" "Checks the copy of multiple files into a directory"
153 files_to_dir_body
() {
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"
161 atf_test_case dir_to_file
163 atf_set
"descr" "Checks the copy of a directory onto a file, which" \
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
173 atf_test_case file_to_linkdir
174 file_to_linkdir_head
() {
175 atf_set
"descr" "Checks the copy of a file to a symbolic link that" \
176 "points to a directory"
178 file_to_linkdir_body
() {
180 atf_check
-s eq
:0 -o empty
-e empty
cp file dirlink
181 cp_compare file_to_linkdir
file "dir/file"
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
189 atf_test_case linkdir_to_file
190 linkdir_to_file_head
() {
191 atf_set
"descr" "Checks the copy of a symbolic link that points to" \
192 "a directory onto a file"
194 linkdir_to_file_body
() {
196 # cannot copy a dir onto a file
197 atf_check
-s eq
:1 -o empty
-e ignore
cp dirlink
file
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"
207 atf_check
-s eq
:1 -o empty
-e ignore
cp dir dir2
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 dir
2/link
>/dev
/null
214 if [ $?
-gt 0 ]; then
215 atf_fail
"-R didn't copy a link as a link"
221 atf_check
-s eq
:0 -o empty
-e empty
cp -R dir dir2
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 dir
2/dirlink
/link
>/dev
/null
229 if [ $?
-gt 0 ]; then
230 atf_fail
"didn't copy a link as a link"
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"
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
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 dir
2/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 dir
2/dirlink
/dirlink
/link
252 dir_to_dir_subdir_exists
() {
253 # recursively copy a dir into another dir, with some subdirs already
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"
266 cp -P file file2 file3 link dir
269 atf_test_case dir_to_dir
271 atf_set
"descr" "Checks the copy of a directory onto another directory"
279 dir_to_dir_subdir_exists
282 atf_init_test_cases
()
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