xref: /freebsd/bin/chmod/tests/chmod_test.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1245e210cSEnji Cooper#
2245e210cSEnji Cooper# Copyright (c) 2017 Dell EMC
3245e210cSEnji Cooper# All rights reserved.
4245e210cSEnji Cooper#
5245e210cSEnji Cooper# Redistribution and use in source and binary forms, with or without
6245e210cSEnji Cooper# modification, are permitted provided that the following conditions
7245e210cSEnji Cooper# are met:
8245e210cSEnji Cooper# 1. Redistributions of source code must retain the above copyright
9245e210cSEnji Cooper#    notice, this list of conditions and the following disclaimer.
10245e210cSEnji Cooper# 2. Redistributions in binary form must reproduce the above copyright
11245e210cSEnji Cooper#    notice, this list of conditions and the following disclaimer in the
12245e210cSEnji Cooper#    documentation and/or other materials provided with the distribution.
13245e210cSEnji Cooper#
14245e210cSEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15245e210cSEnji Cooper# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16245e210cSEnji Cooper# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17245e210cSEnji Cooper# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18245e210cSEnji Cooper# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19245e210cSEnji Cooper# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20245e210cSEnji Cooper# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21245e210cSEnji Cooper# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22245e210cSEnji Cooper# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23245e210cSEnji Cooper# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24245e210cSEnji Cooper# SUCH DAMAGE.
25245e210cSEnji Cooper#
26245e210cSEnji Cooper
272d3e9c25SEnji Cooperget_filesystem()
282d3e9c25SEnji Cooper{
293e46b70fSEnji Cooper	local mountpoint=$1
303e46b70fSEnji Cooper
313e46b70fSEnji Cooper	df -T $mountpoint | tail -n 1 | cut -wf 2
322d3e9c25SEnji Cooper}
332d3e9c25SEnji Cooper
34245e210cSEnji Cooperatf_test_case RH_flag
35245e210cSEnji CooperRH_flag_head()
36245e210cSEnji Cooper{
37245e210cSEnji Cooper	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
38245e210cSEnji Cooper			"affect symlinks specified via the arguments when -H " \
39245e210cSEnji Cooper			"is specified"
40245e210cSEnji Cooper}
41245e210cSEnji CooperRH_flag_body()
42245e210cSEnji Cooper{
43245e210cSEnji Cooper	atf_check mkdir -m 0777 -p A/B
44245e210cSEnji Cooper	atf_check ln -s B A/C
45245e210cSEnji Cooper	atf_check chmod -h 0777 A/C
46245e210cSEnji Cooper	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
47245e210cSEnji Cooper	atf_check chmod -RH 0700 A
48245e210cSEnji Cooper	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
49245e210cSEnji Cooper	atf_check chmod -RH 0600 A/C
50245e210cSEnji Cooper	atf_check -o inline:'40700\n40600\n120700\n' stat -f '%p' A A/B A/C
51245e210cSEnji Cooper}
52245e210cSEnji Cooper
53245e210cSEnji Cooperatf_test_case RL_flag
54245e210cSEnji CooperRL_flag_head()
55245e210cSEnji Cooper{
56245e210cSEnji Cooper	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
57245e210cSEnji Cooper			"affect symlinks specified via the arguments when -L " \
58245e210cSEnji Cooper			"is specified"
59245e210cSEnji Cooper}
60245e210cSEnji CooperRL_flag_body()
61245e210cSEnji Cooper{
62245e210cSEnji Cooper	atf_check mkdir -m 0777 -p A/B
63245e210cSEnji Cooper	atf_check ln -s B A/C
64245e210cSEnji Cooper	atf_check chmod -h 0777 A/C
65245e210cSEnji Cooper	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
66245e210cSEnji Cooper	atf_check chmod -RL 0700 A
67245e210cSEnji Cooper	atf_check -o inline:'40700\n40700\n120777\n' stat -f '%p' A A/B A/C
68245e210cSEnji Cooper	atf_check chmod -RL 0600 A/C
69245e210cSEnji Cooper	atf_check -o inline:'40700\n40600\n120777\n' stat -f '%p' A A/B A/C
70245e210cSEnji Cooper}
71245e210cSEnji Cooper
72245e210cSEnji Cooperatf_test_case RP_flag
73245e210cSEnji CooperRP_flag_head()
74245e210cSEnji Cooper{
75245e210cSEnji Cooper	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
76245e210cSEnji Cooper			"affect symlinks specified via the arguments when -P " \
77245e210cSEnji Cooper			"is specified"
78245e210cSEnji Cooper}
79245e210cSEnji CooperRP_flag_body()
80245e210cSEnji Cooper{
81245e210cSEnji Cooper	atf_check mkdir -m 0777 -p A/B
82245e210cSEnji Cooper	atf_check ln -s B A/C
83245e210cSEnji Cooper	atf_check chmod -h 0777 A/C
84245e210cSEnji Cooper	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
85245e210cSEnji Cooper	atf_check chmod -RP 0700 A
86245e210cSEnji Cooper	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
87245e210cSEnji Cooper	atf_check chmod -RP 0600 A/C
88245e210cSEnji Cooper	atf_check -o inline:'40700\n40700\n120600\n' stat -f '%p' A A/B A/C
89245e210cSEnji Cooper}
90245e210cSEnji Cooper
91245e210cSEnji Cooperatf_test_case f_flag cleanup
92245e210cSEnji Cooperf_flag_head()
93245e210cSEnji Cooper{
94245e210cSEnji Cooper	atf_set	"descr" "Verify that setting a mode for a file with -f " \
95245e210cSEnji Cooper			"doesn't emit an error message/exit with a non-zero " \
96245e210cSEnji Cooper			"code"
97245e210cSEnji Cooper}
98245e210cSEnji Cooper
99245e210cSEnji Cooperf_flag_body()
100245e210cSEnji Cooper{
101245e210cSEnji Cooper	atf_check truncate -s 0 foo bar
102245e210cSEnji Cooper	atf_check chmod 0750 foo bar
1032d3e9c25SEnji Cooper	case "$(get_filesystem .)" in
1042d3e9c25SEnji Cooper	zfs)
1052d3e9c25SEnji Cooper		atf_expect_fail "ZFS doesn't support UF_IMMUTABLE; returns EPERM - bug 221189"
1062d3e9c25SEnji Cooper		;;
1072d3e9c25SEnji Cooper	esac
108245e210cSEnji Cooper	atf_check chflags uchg foo
109245e210cSEnji Cooper	atf_check -e not-empty -s not-exit:0 chmod 0700 foo bar
110245e210cSEnji Cooper	atf_check -o inline:'100750\n100700\n' stat -f '%p' foo bar
111245e210cSEnji Cooper	atf_check -s exit:0 chmod -f 0600 foo bar
112245e210cSEnji Cooper	atf_check -o inline:'100750\n100600\n' stat -f '%p' foo bar
113245e210cSEnji Cooper}
114245e210cSEnji Cooper
115245e210cSEnji Cooperf_flag_cleanup()
116245e210cSEnji Cooper{
117*3783c851SEnji Cooper	chflags 0 foo || :
118245e210cSEnji Cooper}
119245e210cSEnji Cooper
120245e210cSEnji Cooperatf_test_case h_flag
121245e210cSEnji Cooperh_flag_head()
122245e210cSEnji Cooper{
123245e210cSEnji Cooper	atf_set	"descr" "Verify that setting a mode for a file with -f " \
124245e210cSEnji Cooper			"doesn't emit an error message/exit with a non-zero " \
125245e210cSEnji Cooper			"code"
126245e210cSEnji Cooper}
127245e210cSEnji Cooper
128245e210cSEnji Cooperh_flag_body()
129245e210cSEnji Cooper{
130245e210cSEnji Cooper	atf_check truncate -s 0 foo
131245e210cSEnji Cooper	atf_check chmod 0600 foo
132245e210cSEnji Cooper	atf_check -o inline:'100600\n' stat -f '%p' foo
133245e210cSEnji Cooper	umask 0077
134245e210cSEnji Cooper	atf_check ln -s foo bar
135245e210cSEnji Cooper	atf_check -o inline:'100600\n120700\n' stat -f '%p' foo bar
136245e210cSEnji Cooper	atf_check chmod -h 0500 bar
137245e210cSEnji Cooper	atf_check -o inline:'100600\n120500\n' stat -f '%p' foo bar
138245e210cSEnji Cooper	atf_check chmod 0660 bar
139245e210cSEnji Cooper	atf_check -o inline:'100660\n120500\n' stat -f '%p' foo bar
140245e210cSEnji Cooper}
141245e210cSEnji Cooper
142245e210cSEnji Cooperatf_test_case v_flag
143245e210cSEnji Cooperv_flag_head()
144245e210cSEnji Cooper{
145245e210cSEnji Cooper	atf_set	"descr" "Verify that setting a mode with -v emits the file when " \
146245e210cSEnji Cooper			"doesn't emit an error message/exit with a non-zero " \
147245e210cSEnji Cooper			"code"
148245e210cSEnji Cooper}
149245e210cSEnji Cooperv_flag_body()
150245e210cSEnji Cooper{
151245e210cSEnji Cooper	atf_check truncate -s 0 foo bar
152245e210cSEnji Cooper	atf_check chmod 0600 foo
153245e210cSEnji Cooper	atf_check chmod 0750 bar
1542d3e9c25SEnji Cooper	case "$(get_filesystem .)" in
1552d3e9c25SEnji Cooper	zfs)
1562d3e9c25SEnji Cooper		atf_expect_fail "ZFS updates mode for foo unnecessarily - bug 221188"
1572d3e9c25SEnji Cooper		;;
1582d3e9c25SEnji Cooper	esac
159245e210cSEnji Cooper	atf_check -o 'inline:bar\n' chmod -v 0600 foo bar
160245e210cSEnji Cooper	atf_check chmod -v 0600 foo bar
161245e210cSEnji Cooper	for f in foo bar; do
162245e210cSEnji Cooper		echo "$f: 0100600 [-rw------- ] -> 0100700 [-rwx------ ]";
163245e210cSEnji Cooper	done > output.txt
164245e210cSEnji Cooper	atf_check -o file:output.txt chmod -vv 0700 foo bar
165245e210cSEnji Cooper	atf_check chmod -vv 0700 foo bar
166245e210cSEnji Cooper}
167245e210cSEnji Cooper
168245e210cSEnji Cooperatf_init_test_cases()
169245e210cSEnji Cooper{
170245e210cSEnji Cooper	atf_add_test_case RH_flag
171245e210cSEnji Cooper	atf_add_test_case RL_flag
172245e210cSEnji Cooper	atf_add_test_case RP_flag
173245e210cSEnji Cooper	atf_add_test_case f_flag
174245e210cSEnji Cooper	atf_add_test_case h_flag
175245e210cSEnji Cooper	atf_add_test_case v_flag
176245e210cSEnji Cooper}
177