xref: /netbsd-src/tests/fs/tmpfs/t_rmdir.sh (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1# $NetBSD: t_rmdir.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 rmdir works by creating and removing directories.  Also
30# checks multiple error conditions.
31#
32
33atf_test_case mntpt
34mntpt_head() {
35	atf_set "descr" "Checks that the mount point cannot be removed"
36	atf_set "require.user" "root"
37	atf_set "use.fs" "true"
38}
39mntpt_body() {
40	test_mount
41
42	atf_check -s eq:1 -o empty -e ignore rmdir ${Mount_Point}
43
44	test_unmount
45}
46
47atf_test_case non_existent
48non_existent_head() {
49	atf_set "descr" "Checks that non-existent directories cannot" \
50	                "be removed"
51	atf_set "require.user" "root"
52	atf_set "use.fs" "true"
53}
54non_existent_body() {
55	test_mount
56
57	atf_check -s eq:1 -o empty -e ignore rmdir non-existent
58
59	test_unmount
60}
61
62atf_test_case single
63single_head() {
64	atf_set "descr" "Checks that removing a single directory works"
65	atf_set "require.user" "root"
66	atf_set "use.fs" "true"
67}
68single_body() {
69	test_mount
70
71	atf_check -s eq:0 -o empty -e empty mkdir a
72	eval $(stat -s ${Mount_Point})
73	[ ${st_nlink} = 3 ] || \
74	    atf_fail "Incorrect number of links after creation"
75	atf_check -s eq:0 -o empty -e empty rmdir a
76	eval $(stat -s ${Mount_Point})
77	[ ${st_nlink} = 2 ] || \
78	    atf_fail "Incorrect number of links after removal"
79
80	test_unmount
81}
82
83atf_test_case nested
84nested_head() {
85	atf_set "descr" "Checks that removing nested directories works"
86	atf_set "require.user" "root"
87	atf_set "use.fs" "true"
88}
89nested_body() {
90	test_mount
91
92	atf_check -s eq:0 -o empty -e empty mkdir -p a/b/c
93	atf_check -s eq:0 -o empty -e empty rmdir a/b/c
94	atf_check -s eq:0 -o empty -e empty rmdir a/b
95	atf_check -s eq:0 -o empty -e empty rmdir a
96
97	test_unmount
98}
99
100atf_test_case dots
101dots_head() {
102	atf_set "descr" "Checks that '.' and '..' cannot be removed"
103	atf_set "require.user" "root"
104	atf_set "use.fs" "true"
105}
106dots_body() {
107	test_mount
108
109	atf_check -s eq:0 -o empty -e empty mkdir a
110	atf_check -s eq:1 -o empty -e ignore rmdir a/.
111	atf_check -s eq:1 -o empty -e ignore rmdir a/..
112	atf_check -s eq:0 -o empty -e empty rmdir a
113
114	test_unmount
115}
116
117atf_test_case non_empty
118non_empty_head() {
119	atf_set "descr" "Checks that non-empty directories cannot be removed"
120	atf_set "require.user" "root"
121	atf_set "use.fs" "true"
122}
123non_empty_body() {
124	test_mount
125
126	atf_check -s eq:0 -o empty -e empty mkdir a
127	atf_check -s eq:0 -o empty -e empty mkdir a/b
128	atf_check -s eq:0 -o empty -e empty mkdir a/c
129	atf_check -s eq:1 -o empty -e ignore rmdir a
130	atf_check -s eq:0 -o empty -e empty rmdir a/b
131	atf_check -s eq:0 -o empty -e empty rmdir a/c
132	atf_check -s eq:0 -o empty -e empty rmdir a
133
134	test_unmount
135}
136
137atf_test_case links
138links_head() {
139	atf_set "descr" "Checks the root directory's links after removals"
140	atf_set "require.user" "root"
141	atf_set "use.fs" "true"
142}
143links_body() {
144	test_mount
145
146	atf_check -s eq:0 -o empty -e empty mkdir a
147	atf_check -s eq:0 -o empty -e empty mkdir a/b
148	atf_check -s eq:0 -o empty -e empty mkdir c
149
150	atf_check -s eq:0 -o empty -e empty rmdir c
151	atf_check -s eq:0 -o empty -e empty rmdir a/b
152	atf_check -s eq:0 -o empty -e empty rmdir a
153
154	eval $(stat -s ${Mount_Point})
155	[ ${st_nlink} = 2 ] || atf_fail "Incorrect number of links"
156
157	test_unmount
158}
159
160atf_test_case curdir
161curdir_head() {
162	atf_set "descr" "Checks that the current directory cannot be removed"
163	atf_set "require.user" "root"
164	atf_set "use.fs" "true"
165}
166curdir_body() {
167	test_mount
168
169	atf_check -s eq:0 -o empty -e empty mkdir a
170	# Catch a bug that would panic the system when accessing the
171	# current directory after being deleted: vop_open cannot assume
172	# that open files are still linked to a directory.
173	atf_check -s eq:1 -o empty -e ignore -x '( cd a && rmdir ../a && ls )'
174	atf_check -s eq:1 -o empty -e empty test -e a
175
176	test_unmount
177}
178
179atf_test_case kqueue
180kqueue_head() {
181	atf_set "descr" "Checks that removing a directory raises the" \
182	                "correct kqueue events"
183	atf_set "require.user" "root"
184	atf_set "use.fs" "true"
185}
186kqueue_body() {
187	test_mount
188
189	atf_check -s eq:0 -o empty -e empty mkdir dir
190	atf_check -s eq:0 -o empty -e empty mkdir dir/a
191	echo 'rmdir dir/a' | kqueue_monitor 3 dir dir/a
192	kqueue_check dir/a NOTE_DELETE
193	kqueue_check dir NOTE_LINK
194	kqueue_check dir NOTE_WRITE
195	atf_check -s eq:0 -o empty -e empty rmdir dir
196
197	test_unmount
198}
199
200atf_init_test_cases() {
201	. $(atf_get_srcdir)/../h_funcs.subr
202	. $(atf_get_srcdir)/h_funcs.subr
203
204	atf_add_test_case mntpt
205	atf_add_test_case non_existent
206	atf_add_test_case single
207	atf_add_test_case nested
208	atf_add_test_case dots
209	atf_add_test_case non_empty
210	atf_add_test_case links
211	atf_add_test_case curdir
212	atf_add_test_case kqueue
213}
214