xref: /netbsd-src/tests/fs/tmpfs/t_mkdir.sh (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1# $NetBSD: t_mkdir.sh,v 1.5 2010/06/07 03:39:41 riz 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 mkdir works by creating some nested directories.  It also
30# checks, in part, the lookup operation.
31#
32
33atf_test_case single
34single_head() {
35	atf_set "descr" "Creates a single directory and checks the" \
36	                "mount point's hard link count"
37	atf_set "require.user" "root"
38	atf_set "use.fs" "true"
39}
40single_body() {
41	test_mount
42
43	atf_check -s eq:1 -o empty -e empty test -d a
44	atf_check -s eq:0 -o empty -e empty mkdir a
45	atf_check -s eq:0 -o empty -e empty test -d a
46	test -d a
47	eval $(stat -s ${Mount_Point})
48	[ ${st_nlink} = 3 ] || atf_fail "Link count is not 3"
49
50	test_unmount
51}
52
53atf_test_case many
54many_head() {
55	atf_set "descr" "Creates multiple directories and checks the" \
56	                "mount point's hard link count"
57	atf_set "require.user" "root"
58	atf_set "use.fs" "true"
59}
60many_body() {
61	test_mount
62
63	for d in $(jot 100); do
64		atf_check -s eq:1 -o empty -e empty test -d ${d}
65		atf_check -s eq:0 -o empty -e empty mkdir ${d}
66		atf_check -s eq:0 -o empty -e empty test -d ${d}
67	done
68	eval $(stat -s ${Mount_Point})
69	[ ${st_nlink} = 102 ] || atf_fail "Link count is not 102"
70
71	test_unmount
72}
73
74atf_test_case nested
75nested_head() {
76	atf_set "descr" "Checks if nested directories can be created"
77	atf_set "require.user" "root"
78	atf_set "use.fs" "true"
79}
80nested_body() {
81	test_mount
82
83	atf_check -s eq:1 -o empty -e empty test -d a/b/c/d/e
84	atf_check -s eq:0 -o empty -e empty mkdir -p a/b/c/d/e
85	atf_check -s eq:0 -o empty -e empty test -d a/b/c/d/e
86
87	test_unmount
88}
89
90atf_test_case attrs
91attrs_head() {
92	atf_set "descr" "Checks that new directories get the proper" \
93	                "attributes (owner and group)"
94	atf_set "require.config" "unprivileged-user"
95	atf_set "require.user" "root"
96	atf_set "use.fs" "true"
97}
98attrs_body() {
99	# Allow the unprivileged user to access the work directory.
100	chmod 711 .
101
102	test_mount
103
104	user=$(atf_config_get unprivileged-user)
105
106	atf_check -s eq:0 -o empty -e empty mkdir b c
107
108	atf_check -s eq:0 -o empty -e empty chown ${user}:0 b
109	eval $(stat -s b)
110	[ ${st_uid} -eq $(id -u ${user}) ] || atf_fail "Incorrect owner"
111	[ ${st_gid} -eq 0 ] || atf_fail "Incorrect group"
112
113	atf_check -s eq:0 -o empty -e empty chown ${user}:100 c
114	eval $(stat -s c)
115	[ ${st_uid} -eq $(id -u ${user}) ] || atf_fail "Incorrect owner"
116	[ ${st_gid} -eq 100 ] || atf_fail "Incorrect group"
117
118	atf_check -s eq:0 -o empty -e empty su ${user} -c 'mkdir b/a'
119	eval $(stat -s b/a)
120	[ ${st_uid} -eq $(id -u ${user}) ] || atf_fail "Incorrect owner"
121	[ ${st_gid} -eq 0 ] || atf_fail "Incorrect group"
122
123	atf_check -s eq:0 -o empty -e empty su ${user} -c 'mkdir c/a'
124	eval $(stat -s c/a)
125	[ ${st_uid} -eq $(id -u ${user}) ] || atf_fail "Incorrect owner"
126	[ ${st_gid} -eq 100 ] || atf_fail "Incorrect group"
127
128	test_unmount
129}
130
131atf_test_case kqueue
132kqueue_head() {
133	atf_set "descr" "Creates a directory and checks the kqueue events" \
134	                "raised"
135	atf_set "require.user" "root"
136	atf_set "use.fs" "true"
137}
138kqueue_body() {
139	test_mount
140
141	atf_check -s eq:0 -o empty -e empty mkdir dir
142	echo 'mkdir dir/a' | kqueue_monitor 2 dir
143
144	# Creating a directory raises NOTE_LINK on the parent directory
145	kqueue_check dir NOTE_LINK
146
147	# Creating a directory raises NOTE_WRITE on the parent directory
148	kqueue_check dir NOTE_WRITE
149
150	atf_check -s eq:0 -o empty -e empty rmdir dir/a
151	atf_check -s eq:0 -o empty -e empty rmdir dir
152
153	test_unmount
154}
155
156atf_init_test_cases() {
157	. $(atf_get_srcdir)/../h_funcs.subr
158	. $(atf_get_srcdir)/h_funcs.subr
159
160	atf_add_test_case single
161	atf_add_test_case many
162	atf_add_test_case nested
163	atf_add_test_case attrs
164	atf_add_test_case kqueue
165}
166