1#!/bin/sh
2#
3# $NetBSD: t_rename,v 1.7 2007/07/23 15:05:43 jmmv Exp $
4#
5# Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6# All rights reserved.
7#
8# This code is derived from software contributed to The NetBSD Foundation
9# by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10# 2005 program.
11#
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions
14# are met:
15# 1. Redistributions of source code must retain the above copyright
16#    notice, this list of conditions and the following disclaimer.
17# 2. Redistributions in binary form must reproduce the above copyright
18#    notice, this list of conditions and the following disclaimer in the
19#    documentation and/or other materials provided with the distribution.
20#
21# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31# POSSIBILITY OF SUCH DAMAGE.
32#
33# $FreeBSD$
34#
35
36#
37# Verifies that the rename operation works (either by renaming entries or
38# by moving them).
39#
40
41test_run() {
42	test_mount
43
44	test_name "'.' and '..' entries cannot be renamed"
45	mkdir a || die
46	mv a/. c 2>/dev/null && die
47	mv a/.. c 2>/dev/null && die
48	rmdir a || die
49
50	test_name "Cross device renames do not work"
51	mkdir a || die
52	${Src_Dir}/h_tools rename a /var/tmp/a >/dev/null 2>&1 && die
53	rmdir a || die
54
55	test_name "Directories can be renamed"
56	mkdir a || die
57	mv a c || die
58	test -d a && die
59	test -d c || die
60	rmdir c || die
61
62	test_name "The '..' entry is updated after moves"
63	mkdir a || die
64	mkdir b || die
65	mv b a || die
66	test -d a/b/../b || die
67	test -d a/b/../../a || die
68	eval $(stat -s a/b)
69	[ ${st_nlink} = 2 ] || die
70	eval $(stat -s a)
71	[ ${st_nlink} = 3 ] || die
72	rmdir a/b || die
73	rmdir a || die
74
75	test_name "The '..' entry is correct after renames"
76	mkdir a || die
77	mkdir b || die
78	mv b a || die
79	mv a c || die
80	test -d c/b/../b || die
81	test -d c/b/../../c || die
82	rmdir c/b || die
83	rmdir c || die
84
85	test_name "The '..' entry is correct after multiple moves"
86	mkdir a || die
87	mkdir b || die
88	mv b a || die
89	mv a c || die
90	mv c/b d || die
91	test -d d/../c || die
92	rmdir d || die
93	rmdir c || die
94
95	test_name "Rename works if the target file exists"
96	touch a || die
97	touch b || die
98	mv a b || die
99	test -f a && die
100	test -f b || die
101	rm b
102
103	test_name "Rename a directory to a override an empty directory works"
104	mkdir a || die
105	touch a/c || die
106	mkdir b || die
107	${Src_Dir}/h_tools rename a b >/dev/null 2>&1 || die
108	test -e a && die
109	test -d b || die
110	test -f b/c || die
111	rm b/c
112	rmdir b
113
114	test_name "Rename a directory to a override a non-empty directory fails"
115	mkdir a || die
116	touch a/c || die
117	mkdir b || die
118	touch b/d || die
119	err=$(${Src_Dir}/h_tools rename a b 2>&1) && die
120	echo ${err} | grep 'Directory not empty' >/dev/null || die
121	test -d a || die
122	test -f a/c || die
123	test -d b || die
124	test -f b/d || die
125	rm a/c
126	rm b/d
127	rmdir a
128	rmdir b
129
130	test_name "Rename a directory to a override a file fails"
131	mkdir a || die
132	touch b || die
133	err=$(${Src_Dir}/h_tools rename a b 2>&1) && die
134	echo ${err} | grep 'Not a directory' >/dev/null || die
135	test -d a || die
136	test -f b || die
137	rmdir a
138	rm b
139
140	test_name "Rename a file to a override a directory fails"
141	touch a || die
142	mkdir b || die
143	err=$(${Src_Dir}/h_tools rename a b 2>&1) && die
144	echo ${err} | grep 'Is a directory' >/dev/null || die
145	test -f a || die
146	test -d b || die
147	rm a
148	rmdir b
149
150	mkdir dir || die
151	touch dir/a
152	echo 'mv dir/a dir/b' | kqueue_monitor 2 dir dir/a || die
153	test_name "Renaming a file raises NOTE_RENAME on the old file"
154	kqueue_check dir/a NOTE_RENAME || die
155	test_name "Renaming a file raises NOTE_WRITE on the parent directory"
156	kqueue_check dir NOTE_WRITE || die
157	rm dir/b || die
158	rmdir dir || die
159
160	mkdir dir || die
161	touch dir/a
162	touch dir/b
163	echo 'mv dir/a dir/b' | kqueue_monitor 3 dir dir/a dir/b || die
164	test_name "Renaming a file raises NOTE_RENAME on the old file"
165	kqueue_check dir/a NOTE_RENAME || die
166	test_name "Renaming a file raises NOTE_WRITE on the parent directory"
167	kqueue_check dir NOTE_WRITE || die
168	test_name "Renaming a file raises NOTE_DELETE on the target file"
169	kqueue_check dir/b NOTE_DELETE || die
170	rm dir/b || die
171	rmdir dir || die
172
173	mkdir dir1 || die
174	mkdir dir2 || die
175	touch dir1/a
176	echo 'mv dir1/a dir2/a' | kqueue_monitor 3 dir1 dir1/a dir2 || die
177	test_name "Moving a file raises NOTE_RENAME on the old file"
178	kqueue_check dir1/a NOTE_RENAME || die
179	test_name "Moving a file raises NOTE_WRITE on the source directory"
180	kqueue_check dir1 NOTE_WRITE || die
181	test_name "Moving a file raises NOTE_WRITE on the target directory"
182	kqueue_check dir2 NOTE_WRITE || die
183	rm dir2/a || die
184	rmdir dir1 || die
185	rmdir dir2 || die
186
187	test_unmount
188}
189
190. ${SUBRDIR}/h_funcs.subr
191