1# $NetBSD: t_rename.sh,v 1.5 2010/11/07 17:51:18 jmmv Exp $
2#
3# Copyright (c) 2005, 2006, 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
28#
29# Verifies that the rename operation works (either by renaming entries or
30# by moving them).
31#
32
33atf_test_case dots
34dots_head() {
35	atf_set "descr" "Tests that '.' and '..' cannot be renamed"
36	atf_set "require.user" "root"
37}
38dots_body() {
39	test_mount
40
41	atf_check -s eq:0 -o empty -e empty mkdir a
42	atf_check -s eq:1 -o empty -e ignore mv a/. c
43	atf_check -s eq:1 -o empty -e ignore mv a/.. c
44	atf_check -s eq:0 -o empty -e empty rmdir a
45
46	test_unmount
47}
48
49atf_test_case crossdev
50crossdev_head() {
51	atf_set "descr" "Tests that cross-device renames do not work"
52	atf_set "require.user" "root"
53}
54crossdev_body() {
55	test_mount
56
57	atf_check -s eq:0 -o empty -e empty mkdir a
58	atf_check -s eq:1 -o empty -e save:stderr \
59	    $(atf_get_srcdir)/h_tools rename a /var/tmp/a
60	atf_check -s eq:0 -o ignore -e empty grep "Cross-device link" stderr
61	atf_check -s eq:0 -o empty -e empty test -d a
62	atf_check -s eq:0 -o empty -e empty rmdir a
63
64	test_unmount
65}
66
67atf_test_case basic
68basic_head() {
69	atf_set "descr" "Tests that basic renames work"
70	atf_set "require.user" "root"
71}
72basic_body() {
73	test_mount
74
75	atf_check -s eq:0 -o empty -e empty mkdir a
76	atf_check -s eq:0 -o empty -e empty mv a c
77	atf_check -s eq:1 -o empty -e empty test -d a
78	atf_check -s eq:0 -o empty -e empty test -d c
79	atf_check -s eq:0 -o empty -e empty rmdir c
80
81	test_unmount
82}
83
84atf_test_case dotdot
85dotdot_head() {
86	atf_set "descr" "Tests that the '..' entry is properly updated" \
87	                "during moves"
88	atf_set "require.user" "root"
89}
90dotdot_body() {
91	test_mount
92
93	echo "Checking if the '..' entry is updated after moves"
94	atf_check -s eq:0 -o empty -e empty mkdir a
95	atf_check -s eq:0 -o empty -e empty mkdir b
96	atf_check -s eq:0 -o empty -e empty mv b a
97	atf_check -s eq:0 -o empty -e empty test -d a/b/../b
98	atf_check -s eq:0 -o empty -e empty test -d a/b/../../a
99	eval $(stat -s a/b)
100	[ ${st_nlink} = 2 ] || atf_fail "Incorrect number of links"
101	eval $(stat -s a)
102	[ ${st_nlink} = 3 ] || atf_fail "Incorrect number of links"
103	atf_check -s eq:0 -o empty -e empty rmdir a/b
104	atf_check -s eq:0 -o empty -e empty rmdir a
105
106	echo "Checking if the '..' entry is correct after renames"
107	atf_check -s eq:0 -o empty -e empty mkdir a
108	atf_check -s eq:0 -o empty -e empty mkdir b
109	atf_check -s eq:0 -o empty -e empty mv b a
110	atf_check -s eq:0 -o empty -e empty mv a c
111	atf_check -s eq:0 -o empty -e empty test -d c/b/../b
112	atf_check -s eq:0 -o empty -e empty test -d c/b/../../c
113	atf_check -s eq:0 -o empty -e empty rmdir c/b
114	atf_check -s eq:0 -o empty -e empty rmdir c
115
116	echo "Checking if the '..' entry is correct after multiple moves"
117	atf_check -s eq:0 -o empty -e empty mkdir a
118	atf_check -s eq:0 -o empty -e empty mkdir b
119	atf_check -s eq:0 -o empty -e empty mv b a
120	atf_check -s eq:0 -o empty -e empty mv a c
121	atf_check -s eq:0 -o empty -e empty mv c/b d
122	atf_check -s eq:0 -o empty -e empty test -d d/../c
123	atf_check -s eq:0 -o empty -e empty rmdir d
124	atf_check -s eq:0 -o empty -e empty rmdir c
125
126	test_unmount
127}
128
129atf_test_case dir_to_emptydir
130dir_to_emptydir_head() {
131	atf_set "descr" "Tests that renaming a directory to override an" \
132	                "empty directory works"
133	atf_set "require.user" "root"
134}
135dir_to_emptydir_body() {
136	test_mount
137
138	atf_check -s eq:0 -o empty -e empty mkdir a
139	atf_check -s eq:0 -o empty -e empty touch a/c
140	atf_check -s eq:0 -o empty -e empty mkdir b
141	atf_check -s eq:0 -o empty -e empty \
142	    $(atf_get_srcdir)/h_tools rename a b
143	atf_check -s eq:1 -o empty -e empty test -e a
144	atf_check -s eq:0 -o empty -e empty test -d b
145	atf_check -s eq:0 -o empty -e empty test -f b/c
146	rm b/c
147	rmdir b
148
149	test_unmount
150}
151
152atf_test_case dir_to_fulldir
153dir_to_fulldir_head() {
154	atf_set "descr" "Tests that renaming a directory to override a" \
155	                "non-empty directory fails"
156	atf_set "require.user" "root"
157}
158dir_to_fulldir_body() {
159	test_mount
160
161	atf_check -s eq:0 -o empty -e empty mkdir a
162	atf_check -s eq:0 -o empty -e empty touch a/c
163	atf_check -s eq:0 -o empty -e empty mkdir b
164	atf_check -s eq:0 -o empty -e empty touch b/d
165	atf_check -s eq:1 -o empty -e save:stderr \
166	    $(atf_get_srcdir)/h_tools rename a b
167	atf_check -s eq:0 -o ignore -e empty grep "Directory not empty" stderr
168	atf_check -s eq:0 -o empty -e empty test -d a
169	atf_check -s eq:0 -o empty -e empty test -f a/c
170	atf_check -s eq:0 -o empty -e empty test -d b
171	atf_check -s eq:0 -o empty -e empty test -f b/d
172	rm a/c
173	rm b/d
174	rmdir a
175	rmdir b
176
177	test_unmount
178}
179
180atf_test_case dir_to_file
181dir_to_file_head() {
182	atf_set "descr" "Tests that renaming a directory to override a" \
183	                "file fails"
184	atf_set "require.user" "root"
185}
186dir_to_file_body() {
187	test_mount
188
189	atf_check -s eq:0 -o empty -e empty mkdir a
190	atf_check -s eq:0 -o empty -e empty touch b
191	atf_check -s eq:1 -o empty -e save:stderr \
192	    $(atf_get_srcdir)/h_tools rename a b
193	atf_check -s eq:0 -o ignore -e empty grep "Not a directory" stderr
194	atf_check -s eq:0 -o empty -e empty test -d a
195	atf_check -s eq:0 -o empty -e empty test -f b
196	rmdir a
197	rm b
198
199	test_unmount
200}
201
202atf_test_case file_to_dir
203file_to_dir_head() {
204	atf_set "descr" "Tests that renaming a file to override a" \
205	                "directory fails"
206	atf_set "require.user" "root"
207}
208file_to_dir_body() {
209	test_mount
210
211	atf_check -s eq:0 -o empty -e empty touch a
212	atf_check -s eq:0 -o empty -e empty mkdir b
213	atf_check -s eq:1 -o empty -e save:stderr \
214	    $(atf_get_srcdir)/h_tools rename a b
215	atf_check -s eq:0 -o ignore -e empty grep "Is a directory" stderr
216	atf_check -s eq:0 -o empty -e empty test -f a
217	atf_check -s eq:0 -o empty -e empty test -d b
218	rm a
219	rmdir b
220
221	test_unmount
222}
223
224atf_test_case kqueue
225kqueue_head() {
226	atf_set "descr" "Tests that moving and renaming files raise the" \
227	                "correct kqueue events"
228	atf_set "require.user" "root"
229}
230kqueue_body() {
231	test_mount
232
233	atf_check -s eq:0 -o empty -e empty mkdir dir
234	atf_check -s eq:0 -o empty -e empty touch dir/a
235	echo 'mv dir/a dir/b' | kqueue_monitor 2 dir dir/a
236	kqueue_check dir/a NOTE_RENAME
237	kqueue_check dir NOTE_WRITE
238	atf_check -s eq:0 -o empty -e empty rm dir/b
239	atf_check -s eq:0 -o empty -e empty rmdir dir
240
241	atf_check -s eq:0 -o empty -e empty mkdir dir
242	atf_check -s eq:0 -o empty -e empty touch dir/a
243	atf_check -s eq:0 -o empty -e empty touch dir/b
244	echo 'mv dir/a dir/b' | kqueue_monitor 3 dir dir/a dir/b
245	kqueue_check dir/a NOTE_RENAME
246	kqueue_check dir NOTE_WRITE
247	kqueue_check dir/b NOTE_DELETE
248	atf_check -s eq:0 -o empty -e empty rm dir/b
249	atf_check -s eq:0 -o empty -e empty rmdir dir
250
251	atf_check -s eq:0 -o empty -e empty mkdir dir1
252	atf_check -s eq:0 -o empty -e empty mkdir dir2
253	atf_check -s eq:0 -o empty -e empty touch dir1/a
254	echo 'mv dir1/a dir2/a' | kqueue_monitor 3 dir1 dir1/a dir2
255	kqueue_check dir1/a NOTE_RENAME
256	kqueue_check dir1 NOTE_WRITE
257	kqueue_check dir2 NOTE_WRITE
258	atf_check -s eq:0 -o empty -e empty rm dir2/a
259	atf_check -s eq:0 -o empty -e empty rmdir dir1
260	atf_check -s eq:0 -o empty -e empty rmdir dir2
261
262	test_unmount
263}
264
265atf_init_test_cases() {
266	. $(atf_get_srcdir)/../h_funcs.subr
267	. $(atf_get_srcdir)/h_funcs.subr
268
269	atf_add_test_case dots
270	atf_add_test_case crossdev
271	atf_add_test_case basic
272	atf_add_test_case dotdot
273	atf_add_test_case dir_to_emptydir
274	atf_add_test_case dir_to_fulldir
275	atf_add_test_case dir_to_file
276	atf_add_test_case file_to_dir
277	atf_add_test_case kqueue
278}
279