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