1170809Sdelphij#!/bin/sh
2170809Sdelphij#
3170809Sdelphij# $NetBSD: t_rmdir,v 1.6 2006/11/09 16:20:06 jmmv Exp $
4170809Sdelphij#
5170809Sdelphij# Copyright (c) 2005, 2006 The NetBSD Foundation, Inc.
6170809Sdelphij# All rights reserved.
7170809Sdelphij#
8170809Sdelphij# This code is derived from software contributed to The NetBSD Foundation
9170809Sdelphij# by Julio M. Merino Vidal, developed as part of Google's Summer of Code
10170809Sdelphij# 2005 program.
11170809Sdelphij#
12170809Sdelphij# Redistribution and use in source and binary forms, with or without
13170809Sdelphij# modification, are permitted provided that the following conditions
14170809Sdelphij# are met:
15170809Sdelphij# 1. Redistributions of source code must retain the above copyright
16170809Sdelphij#    notice, this list of conditions and the following disclaimer.
17170809Sdelphij# 2. Redistributions in binary form must reproduce the above copyright
18170809Sdelphij#    notice, this list of conditions and the following disclaimer in the
19170809Sdelphij#    documentation and/or other materials provided with the distribution.
20170809Sdelphij#
21170809Sdelphij# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22170809Sdelphij# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23170809Sdelphij# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24170809Sdelphij# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25170809Sdelphij# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26170809Sdelphij# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27170809Sdelphij# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28170809Sdelphij# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29170809Sdelphij# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30170809Sdelphij# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31170809Sdelphij# POSSIBILITY OF SUCH DAMAGE.
32170809Sdelphij#
33170809Sdelphij# $FreeBSD$
34170809Sdelphij#
35170809Sdelphij
36170809Sdelphij#
37170809Sdelphij# Verifies that rmdir works by creating and removing directories.  Also
38170809Sdelphij# checks multiple error conditions.
39170809Sdelphij#
40170809Sdelphij
41170809Sdelphijtest_run() {
42170809Sdelphij	test_mount
43170809Sdelphij
44170809Sdelphij	test_name "The mount point cannot be removed"
45170809Sdelphij	rmdir 2>/dev/null && die
46170809Sdelphij
47170809Sdelphij	test_name "Non-existing directories cannot be removed"
48170809Sdelphij	rmdir non-existent 2>/dev/null && die
49170809Sdelphij
50170809Sdelphij	test_name "Removal of a single directory works"
51170809Sdelphij	mkdir a || die
52170809Sdelphij	eval $(stat -s ${Work_Dir})
53170809Sdelphij	[ ${st_nlink} = 3 ] || die
54170809Sdelphij	rmdir a || die
55170809Sdelphij	eval $(stat -s ${Work_Dir})
56170809Sdelphij	[ ${st_nlink} = 2 ] || die
57170809Sdelphij
58170809Sdelphij	test_name "Removal of nested directories works"
59170809Sdelphij	mkdir -p a/b/c || die
60170809Sdelphij	rmdir a/b/c || die
61170809Sdelphij	rmdir a/b || die
62170809Sdelphij	rmdir a || die
63170809Sdelphij
64170809Sdelphij	test_name "'.' and '..' directories cannot be removed"
65170809Sdelphij	mkdir a || die
66170809Sdelphij	rmdir a/. 2>/dev/null && die
67170809Sdelphij	rmdir a/.. 2>/dev/null && die
68170809Sdelphij	rmdir a || die
69170809Sdelphij
70170809Sdelphij	test_name "Non-empty directories cannot be removed"
71170809Sdelphij	mkdir a || die
72170809Sdelphij	mkdir a/b || die
73170809Sdelphij	mkdir a/c || die
74170809Sdelphij	rmdir a 2>/dev/null && die
75170809Sdelphij	rmdir a/b || die
76170809Sdelphij	rmdir a/c || die
77170809Sdelphij	rmdir a || die
78170809Sdelphij
79170809Sdelphij	test_name "Root directory has two links after all removes"
80170809Sdelphij	eval $(stat -s ${Work_Dir})
81170809Sdelphij	[ ${st_nlink} = 2 ] || die
82170809Sdelphij
83170809Sdelphij	test_name "Removal of current directory"
84170809Sdelphij	mkdir a || die
85170809Sdelphij	# Catch a bug that would panic the system when accessing the
86170809Sdelphij	# current directory after being deleted: vop_open cannot assume
87170809Sdelphij	# that open files are still linked to a directory.
88170809Sdelphij	( cd a && rmdir ../a && ls >/dev/null 2>&1 ) && die
89170809Sdelphij	test -e a && die
90170809Sdelphij
91170809Sdelphij	mkdir dir || die
92170809Sdelphij	mkdir dir/a || die
93170809Sdelphij	echo 'rmdir dir/a' | kqueue_monitor 3 dir dir/a || die
94170809Sdelphij	test_name "Deleting a directory raises NOTE_DELETE on it"
95170809Sdelphij	kqueue_check dir/a NOTE_DELETE || die
96170809Sdelphij	test_name "Deleting a directory raises NOTE_LINK on the parent" \
97170809Sdelphij	    "directory"
98170809Sdelphij	kqueue_check dir NOTE_LINK || die
99170809Sdelphij	test_name "Deleting a directory raises NOTE_WRITE on the parent" \
100170809Sdelphij	    "directory"
101170809Sdelphij	kqueue_check dir NOTE_WRITE || die
102170809Sdelphij	rmdir dir || die
103170809Sdelphij
104170809Sdelphij	test_unmount
105170809Sdelphij}
106170809Sdelphij
107170809Sdelphij. ${SUBRDIR}/h_funcs.subr
108