xref: /freebsd/bin/chmod/tests/chmod_test.sh (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1#
2# Copyright (c) 2017 Dell EMC
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28get_filesystem()
29{
30	local mountpoint=$1
31
32	df -T $mountpoint | tail -n 1 | cut -wf 2
33}
34
35atf_test_case RH_flag
36RH_flag_head()
37{
38	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
39			"affect symlinks specified via the arguments when -H " \
40			"is specified"
41}
42RH_flag_body()
43{
44	atf_check mkdir -m 0777 -p A/B
45	atf_check ln -s B A/C
46	atf_check chmod -h 0777 A/C
47	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
48	atf_check chmod -RH 0700 A
49	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
50	atf_check chmod -RH 0600 A/C
51	atf_check -o inline:'40700\n40600\n120700\n' stat -f '%p' A A/B A/C
52}
53
54atf_test_case RL_flag
55RL_flag_head()
56{
57	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
58			"affect symlinks specified via the arguments when -L " \
59			"is specified"
60}
61RL_flag_body()
62{
63	atf_check mkdir -m 0777 -p A/B
64	atf_check ln -s B A/C
65	atf_check chmod -h 0777 A/C
66	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
67	atf_check chmod -RL 0700 A
68	atf_check -o inline:'40700\n40700\n120777\n' stat -f '%p' A A/B A/C
69	atf_check chmod -RL 0600 A/C
70	atf_check -o inline:'40700\n40600\n120777\n' stat -f '%p' A A/B A/C
71}
72
73atf_test_case RP_flag
74RP_flag_head()
75{
76	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
77			"affect symlinks specified via the arguments when -P " \
78			"is specified"
79}
80RP_flag_body()
81{
82	atf_check mkdir -m 0777 -p A/B
83	atf_check ln -s B A/C
84	atf_check chmod -h 0777 A/C
85	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
86	atf_check chmod -RP 0700 A
87	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
88	atf_check chmod -RP 0600 A/C
89	atf_check -o inline:'40700\n40700\n120600\n' stat -f '%p' A A/B A/C
90}
91
92atf_test_case f_flag cleanup
93f_flag_head()
94{
95	atf_set	"descr" "Verify that setting a mode for a file with -f " \
96			"doesn't emit an error message/exit with a non-zero " \
97			"code"
98}
99
100f_flag_body()
101{
102	atf_check truncate -s 0 foo bar
103	atf_check chmod 0750 foo bar
104	case "$(get_filesystem .)" in
105	zfs)
106		atf_expect_fail "ZFS doesn't support UF_IMMUTABLE; returns EPERM - bug 221189"
107		;;
108	esac
109	atf_check chflags uchg foo
110	atf_check -e not-empty -s not-exit:0 chmod 0700 foo bar
111	atf_check -o inline:'100750\n100700\n' stat -f '%p' foo bar
112	atf_check -s exit:0 chmod -f 0600 foo bar
113	atf_check -o inline:'100750\n100600\n' stat -f '%p' foo bar
114}
115
116f_flag_cleanup()
117{
118	chflags 0 foo || :
119}
120
121atf_test_case h_flag
122h_flag_head()
123{
124	atf_set	"descr" "Verify that setting a mode for a file with -f " \
125			"doesn't emit an error message/exit with a non-zero " \
126			"code"
127}
128
129h_flag_body()
130{
131	atf_check truncate -s 0 foo
132	atf_check chmod 0600 foo
133	atf_check -o inline:'100600\n' stat -f '%p' foo
134	umask 0077
135	atf_check ln -s foo bar
136	atf_check -o inline:'100600\n120700\n' stat -f '%p' foo bar
137	atf_check chmod -h 0500 bar
138	atf_check -o inline:'100600\n120500\n' stat -f '%p' foo bar
139	atf_check chmod 0660 bar
140	atf_check -o inline:'100660\n120500\n' stat -f '%p' foo bar
141}
142
143atf_test_case v_flag
144v_flag_head()
145{
146	atf_set	"descr" "Verify that setting a mode with -v emits the file when " \
147			"doesn't emit an error message/exit with a non-zero " \
148			"code"
149}
150v_flag_body()
151{
152	atf_check truncate -s 0 foo bar
153	atf_check chmod 0600 foo
154	atf_check chmod 0750 bar
155	case "$(get_filesystem .)" in
156	zfs)
157		atf_expect_fail "ZFS updates mode for foo unnecessarily - bug 221188"
158		;;
159	esac
160	atf_check -o 'inline:bar\n' chmod -v 0600 foo bar
161	atf_check chmod -v 0600 foo bar
162	for f in foo bar; do
163		echo "$f: 0100600 [-rw------- ] -> 0100700 [-rwx------ ]";
164	done > output.txt
165	atf_check -o file:output.txt chmod -vv 0700 foo bar
166	atf_check chmod -vv 0700 foo bar
167}
168
169atf_init_test_cases()
170{
171	atf_add_test_case RH_flag
172	atf_add_test_case RL_flag
173	atf_add_test_case RP_flag
174	atf_add_test_case f_flag
175	atf_add_test_case h_flag
176	atf_add_test_case v_flag
177}
178