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