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