xref: /freebsd/bin/chmod/tests/chmod_test.sh (revision 069ac184)
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
27get_filesystem()
28{
29	local mountpoint=$1
30
31	df -T $mountpoint | tail -n 1 | cut -wf 2
32}
33
34atf_test_case RH_flag
35RH_flag_head()
36{
37	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
38			"affect symlinks specified via the arguments when -H " \
39			"is specified"
40}
41RH_flag_body()
42{
43	atf_check mkdir -m 0777 -p A/B
44	atf_check ln -s B A/C
45	atf_check chmod -h 0777 A/C
46	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
47	atf_check chmod -RH 0700 A
48	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
49	atf_check chmod -RH 0600 A/C
50	atf_check -o inline:'40700\n40600\n120700\n' stat -f '%p' A A/B A/C
51}
52
53atf_test_case RL_flag
54RL_flag_head()
55{
56	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
57			"affect symlinks specified via the arguments when -L " \
58			"is specified"
59}
60RL_flag_body()
61{
62	atf_check mkdir -m 0777 -p A/B
63	atf_check ln -s B A/C
64	atf_check chmod -h 0777 A/C
65	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
66	atf_check chmod -RL 0700 A
67	atf_check -o inline:'40700\n40700\n120777\n' stat -f '%p' A A/B A/C
68	atf_check chmod -RL 0600 A/C
69	atf_check -o inline:'40700\n40600\n120777\n' stat -f '%p' A A/B A/C
70}
71
72atf_test_case RP_flag
73RP_flag_head()
74{
75	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
76			"affect symlinks specified via the arguments when -P " \
77			"is specified"
78}
79RP_flag_body()
80{
81	atf_check mkdir -m 0777 -p A/B
82	atf_check ln -s B A/C
83	atf_check chmod -h 0777 A/C
84	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
85	atf_check chmod -RP 0700 A
86	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
87	atf_check chmod -RP 0600 A/C
88	atf_check -o inline:'40700\n40700\n120600\n' stat -f '%p' A A/B A/C
89}
90
91atf_test_case f_flag cleanup
92f_flag_head()
93{
94	atf_set	"descr" "Verify that setting a mode for a file with -f " \
95			"doesn't emit an error message/exit with a non-zero " \
96			"code"
97}
98
99f_flag_body()
100{
101	atf_check truncate -s 0 foo bar
102	atf_check chmod 0750 foo bar
103	case "$(get_filesystem .)" in
104	zfs)
105		atf_expect_fail "ZFS doesn't support UF_IMMUTABLE; returns EPERM - bug 221189"
106		;;
107	esac
108	atf_check chflags uchg foo
109	atf_check -e not-empty -s not-exit:0 chmod 0700 foo bar
110	atf_check -o inline:'100750\n100700\n' stat -f '%p' foo bar
111	atf_check -s exit:0 chmod -f 0600 foo bar
112	atf_check -o inline:'100750\n100600\n' stat -f '%p' foo bar
113}
114
115f_flag_cleanup()
116{
117	chflags 0 foo || :
118}
119
120atf_test_case h_flag
121h_flag_head()
122{
123	atf_set	"descr" "Verify that setting a mode for a file with -f " \
124			"doesn't emit an error message/exit with a non-zero " \
125			"code"
126}
127
128h_flag_body()
129{
130	atf_check truncate -s 0 foo
131	atf_check chmod 0600 foo
132	atf_check -o inline:'100600\n' stat -f '%p' foo
133	umask 0077
134	atf_check ln -s foo bar
135	atf_check -o inline:'100600\n120700\n' stat -f '%p' foo bar
136	atf_check chmod -h 0500 bar
137	atf_check -o inline:'100600\n120500\n' stat -f '%p' foo bar
138	atf_check chmod 0660 bar
139	atf_check -o inline:'100660\n120500\n' stat -f '%p' foo bar
140}
141
142atf_test_case v_flag
143v_flag_head()
144{
145	atf_set	"descr" "Verify that setting a mode with -v emits the file when " \
146			"doesn't emit an error message/exit with a non-zero " \
147			"code"
148}
149v_flag_body()
150{
151	atf_check truncate -s 0 foo bar
152	atf_check chmod 0600 foo
153	atf_check chmod 0750 bar
154	case "$(get_filesystem .)" in
155	zfs)
156		atf_expect_fail "ZFS updates mode for foo unnecessarily - bug 221188"
157		;;
158	esac
159	atf_check -o 'inline:bar\n' chmod -v 0600 foo bar
160	atf_check chmod -v 0600 foo bar
161	for f in foo bar; do
162		echo "$f: 0100600 [-rw------- ] -> 0100700 [-rwx------ ]";
163	done > output.txt
164	atf_check -o file:output.txt chmod -vv 0700 foo bar
165	atf_check chmod -vv 0700 foo bar
166}
167
168atf_init_test_cases()
169{
170	atf_add_test_case RH_flag
171	atf_add_test_case RL_flag
172	atf_add_test_case RP_flag
173	atf_add_test_case f_flag
174	atf_add_test_case h_flag
175	atf_add_test_case v_flag
176}
177