chown_test.sh revision 322835
1212525Simp#
2212525Simp# Copyright (c) 2017 Dell EMC
3212525Simp# All rights reserved.
4212525Simp#
5212525Simp# Redistribution and use in source and binary forms, with or without
6212525Simp# modification, are permitted provided that the following conditions
7212525Simp# are met:
8212525Simp# 1. Redistributions of source code must retain the above copyright
9212525Simp#    notice, this list of conditions and the following disclaimer.
10212525Simp# 2. Redistributions in binary form must reproduce the above copyright
11212525Simp#    notice, this list of conditions and the following disclaimer in the
12212525Simp#    documentation and/or other materials provided with the distribution.
13245652Sneel#
14245652Sneel# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15245652Sneel# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16212525Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17212525Simp# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18212525Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19212525Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20212525Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21212525Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22212525Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23212525Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24212525Simp# SUCH DAMAGE.
25212525Simp#
26212525Simp# $FreeBSD: stable/10/usr.sbin/chown/tests/chown_test.sh 322835 2017-08-24 13:39:24Z gjb $
27212525Simp
28212525Simpget_filesystem()
29212525Simp{
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 ownership recursively via -R doesn't " \
39			"affect symlinks specified via the arguments when -H " \
40			"is specified"
41	atf_set "require.user" "root"
42}
43RH_flag_body()
44{
45	atf_check mkdir -p A/B
46	atf_check ln -s B A/C
47	atf_check chown -h 42:42 A/C
48	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
49	atf_check chown -RH 84:84 A
50	atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C
51	atf_check chown -RH 126:126 A/C
52	atf_check -o inline:'84:84\n126:126\n84:84\n' stat -f '%u:%g' A A/B A/C
53}
54
55atf_test_case RL_flag
56RL_flag_head()
57{
58	atf_set	"descr" "Verify that setting ownership recursively via -R doesn't " \
59			"affect symlinks specified via the arguments when -L " \
60			"is specified"
61	atf_set "require.user" "root"
62}
63RL_flag_body()
64{
65	atf_check mkdir -p A/B
66	atf_check ln -s B A/C
67	atf_check chown -h 42:42 A/C
68	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
69	atf_check chown -RL 84:84 A
70	atf_check -o inline:'84:84\n84:84\n42:42\n' stat -f '%u:%g' A A/B A/C
71	atf_check chown -RL 126:126 A/C
72	atf_check -o inline:'84:84\n126:126\n42:42\n' stat -f '%u:%g' A A/B A/C
73}
74
75atf_test_case RP_flag
76RP_flag_head()
77{
78	atf_set	"descr" "Verify that setting ownership recursively via -R " \
79			"doesn't affect symlinks specified via the arguments " \
80			"when -P is specified"
81	atf_set "require.user" "root"
82}
83RP_flag_body()
84{
85	atf_check mkdir -p A/B
86	atf_check ln -s B A/C
87	atf_check chown -h 42:42 A/C
88	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
89	atf_check chown -RP 84:84 A
90	atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C
91	atf_check chown -RP 126:126 A/C
92	atf_check -o inline:'84:84\n84:84\n126:126\n' stat -f '%u:%g' A A/B A/C
93}
94
95atf_test_case f_flag cleanup
96f_flag_head()
97{
98	atf_set	"descr" "Verify that setting a mode for a file with -f " \
99			"doesn't emit an error message/exit with a non-zero " \
100			"code"
101	atf_set "require.user" "root"
102}
103
104f_flag_body()
105{
106	atf_check truncate -s 0 foo bar
107	atf_check chown 0:0 foo bar
108	case "$(get_filesystem .)" in
109	zfs)
110		atf_expect_fail "ZFS does not support UF_IMMUTABLE; returns EPERM"
111		;;
112	esac
113	atf_check chflags uchg foo
114	atf_check -e not-empty -s not-exit:0 chown 42:42 foo bar
115	atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar
116	atf_check -s exit:0 chown -f 84:84 foo bar
117	atf_check -o inline:'0:0\n84:84\n' stat -f '%u:%g' foo bar
118}
119
120f_flag_cleanup()
121{
122	chflags 0 foo || :
123}
124
125atf_test_case h_flag
126h_flag_head()
127{
128	atf_set	"descr" "Verify that setting a mode for a file with -f " \
129			"doesn't emit an error message/exit with a non-zero " \
130			"code"
131	atf_set "require.user" "root"
132}
133
134h_flag_body()
135{
136	atf_check truncate -s 0 foo
137	atf_check -o inline:'0:0\n' stat -f '%u:%g' foo
138	atf_check ln -s foo bar
139	atf_check -o inline:'0:0\n0:0\n' stat -f '%u:%g' foo bar
140	atf_check chown -h 42:42 bar
141	atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar
142	atf_check chown 84:84 bar
143	atf_check -o inline:'84:84\n42:42\n' stat -f '%u:%g' foo bar
144}
145
146atf_test_case v_flag
147v_flag_head()
148{
149	atf_set	"descr" "Verify that setting ownership with -v emits the " \
150			"file doesn't emit an error message/exit with a " \
151			"non-zero code"
152	atf_set "require.user" "root"
153}
154v_flag_body()
155{
156	atf_check truncate -s 0 foo bar
157	atf_check chown 0:0 foo
158	atf_check chown 42:42 bar
159	atf_check -o 'inline:bar\n' chown -v 0:0 foo bar
160	atf_check chown -v 0:0 foo bar
161	for f in foo bar; do
162		echo "$f: 0:0 -> 84:84";
163	done > output.txt
164	atf_check -o file:output.txt chown -vv 84:84 foo bar
165	atf_check chown -vv 84:84 foo bar
166}
167
168md_file="md.out"
169atf_test_case x_flag cleanup
170x_flag_head()
171{
172	atf_set	"descr" "Verify that setting a mode with -x doesn't set " \
173			"ownership across mountpoints"
174	atf_set "require.user" "root"
175}
176x_flag_body()
177{
178	atf_check -o save:$md_file mdconfig -a -t malloc -s 20m
179	if ! md_device=$(cat $md_file); then
180		atf_fail "cat $md_file failed"
181	fi
182	atf_check -o not-empty newfs /dev/$md_device
183	atf_check mkdir mnt
184	atf_check mount /dev/$md_device mnt
185	atf_check truncate -s 0 foo bar mnt/bazbaz
186	atf_check ln -s bar mnt/barbaz
187	atf_check ln -s ../foo mnt/foobaz
188	cd mnt
189	test_files="../foo ../bar barbaz bazbaz foobaz"
190	atf_check -o inline:'0:0\n0:0\n0:0\n0:0\n0:0\n' \
191	    stat -f '%u:%g' $test_files
192	atf_check chown -Rx 42:42 .
193	atf_check -o inline:'0:0\n0:0\n42:42\n42:42\n42:42\n' \
194	    stat -f '%u:%g' $test_files
195	atf_check chown -R 84:84 .
196	atf_check -o inline:'0:0\n0:0\n84:84\n84:84\n84:84\n' \
197	    stat -f '%u:%g' $test_files
198}
199x_flag_cleanup()
200{
201	if ! md_device=$(cat $md_file) || [ -z "$md_device" ]; then
202		echo "Couldn't get device from $md_file"
203		exit 0
204	fi
205	umount mnt
206	mdconfig -d -u $md_device
207}
208
209atf_init_test_cases()
210{
211	atf_add_test_case RH_flag
212	atf_add_test_case RL_flag
213	atf_add_test_case RP_flag
214	atf_add_test_case f_flag
215	atf_add_test_case h_flag
216	atf_add_test_case v_flag
217	atf_add_test_case x_flag
218}
219