chown_test.sh revision 321130
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: stable/10/usr.sbin/chown/tests/chown_test.sh 321130 2017-07-18 16:27:10Z ngie $
27
28atf_test_case RH_flag
29RH_flag_head()
30{
31	atf_set	"descr" "Verify that setting ownership recursively via -R doesn't " \
32			"affect symlinks specified via the arguments when -H " \
33			"is specified"
34	atf_set "require.user" "root"
35}
36RH_flag_body()
37{
38	atf_check mkdir -p A/B
39	atf_check ln -s B A/C
40	atf_check chown -h 42:42 A/C
41	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
42	atf_check chown -RH 84:84 A
43	atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C
44	atf_check chown -RH 126:126 A/C
45	atf_check -o inline:'84:84\n126:126\n84:84\n' stat -f '%u:%g' A A/B A/C
46}
47
48atf_test_case RL_flag
49RL_flag_head()
50{
51	atf_set	"descr" "Verify that setting ownership recursively via -R doesn't " \
52			"affect symlinks specified via the arguments when -L " \
53			"is specified"
54	atf_set "require.user" "root"
55}
56RL_flag_body()
57{
58	atf_check mkdir -p A/B
59	atf_check ln -s B A/C
60	atf_check chown -h 42:42 A/C
61	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
62	atf_check chown -RL 84:84 A
63	atf_check -o inline:'84:84\n84:84\n42:42\n' stat -f '%u:%g' A A/B A/C
64	atf_check chown -RL 126:126 A/C
65	atf_check -o inline:'84:84\n126:126\n42:42\n' stat -f '%u:%g' A A/B A/C
66}
67
68atf_test_case RP_flag
69RP_flag_head()
70{
71	atf_set	"descr" "Verify that setting ownership recursively via -R " \
72			"doesn't affect symlinks specified via the arguments " \
73			"when -P is specified"
74	atf_set "require.user" "root"
75}
76RP_flag_body()
77{
78	atf_check mkdir -p A/B
79	atf_check ln -s B A/C
80	atf_check chown -h 42:42 A/C
81	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
82	atf_check chown -RP 84:84 A
83	atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C
84	atf_check chown -RP 126:126 A/C
85	atf_check -o inline:'84:84\n84:84\n126:126\n' stat -f '%u:%g' A A/B A/C
86}
87
88atf_test_case f_flag cleanup
89f_flag_head()
90{
91	atf_set	"descr" "Verify that setting a mode for a file with -f " \
92			"doesn't emit an error message/exit with a non-zero " \
93			"code"
94	atf_set "require.user" "root"
95}
96
97f_flag_body()
98{
99	atf_check truncate -s 0 foo bar
100	atf_check chown 0:0 foo bar
101	atf_check chflags uchg foo
102	atf_check -e not-empty -s not-exit:0 chown 42:42 foo bar
103	atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar
104	atf_check -s exit:0 chown -f 84:84 foo bar
105	atf_check -o inline:'0:0\n84:84\n' stat -f '%u:%g' foo bar
106}
107
108f_flag_cleanup()
109{
110	atf_check chflags 0 foo
111}
112
113atf_test_case h_flag
114h_flag_head()
115{
116	atf_set	"descr" "Verify that setting a mode for a file with -f " \
117			"doesn't emit an error message/exit with a non-zero " \
118			"code"
119	atf_set "require.user" "root"
120}
121
122h_flag_body()
123{
124	atf_check truncate -s 0 foo
125	atf_check -o inline:'0:0\n' stat -f '%u:%g' foo
126	atf_check ln -s foo bar
127	atf_check -o inline:'0:0\n0:0\n' stat -f '%u:%g' foo bar
128	atf_check chown -h 42:42 bar
129	atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar
130	atf_check chown 84:84 bar
131	atf_check -o inline:'84:84\n42:42\n' stat -f '%u:%g' foo bar
132}
133
134atf_test_case v_flag
135v_flag_head()
136{
137	atf_set	"descr" "Verify that setting ownership with -v emits the " \
138			"file doesn't emit an error message/exit with a " \
139			"non-zero code"
140	atf_set "require.user" "root"
141}
142v_flag_body()
143{
144	atf_check truncate -s 0 foo bar
145	atf_check chown 0:0 foo
146	atf_check chown 42:42 bar
147	atf_check -o 'inline:bar\n' chown -v 0:0 foo bar
148	atf_check chown -v 0:0 foo bar
149	for f in foo bar; do
150		echo "$f: 0:0 -> 84:84";
151	done > output.txt
152	atf_check -o file:output.txt chown -vv 84:84 foo bar
153	atf_check chown -vv 84:84 foo bar
154}
155
156md_file="md.out"
157atf_test_case x_flag cleanup
158x_flag_head()
159{
160	atf_set	"descr" "Verify that setting a mode with -x doesn't set " \
161			"ownership across mountpoints"
162	atf_set "require.user" "root"
163}
164x_flag_body()
165{
166	atf_check -o save:$md_file mdconfig -a -t malloc -s 20m
167	if ! md_device=$(cat $md_file); then
168		atf_fail "cat $md_file failed"
169	fi
170	atf_check -o not-empty newfs /dev/$md_device
171	atf_check mkdir mnt
172	atf_check mount /dev/$md_device mnt
173	atf_check truncate -s 0 foo bar mnt/bazbaz
174	atf_check ln -s bar mnt/barbaz
175	atf_check ln -s ../foo mnt/foobaz
176	cd mnt
177	test_files="../foo ../bar barbaz bazbaz foobaz"
178	atf_check -o inline:'0:0\n0:0\n0:0\n0:0\n0:0\n' \
179	    stat -f '%u:%g' $test_files
180	atf_check chown -Rx 42:42 .
181	atf_check -o inline:'0:0\n0:0\n42:42\n42:42\n42:42\n' \
182	    stat -f '%u:%g' $test_files
183	atf_check chown -R 84:84 .
184	atf_check -o inline:'0:0\n0:0\n84:84\n84:84\n84:84\n' \
185	    stat -f '%u:%g' $test_files
186}
187x_flag_cleanup()
188{
189	if ! md_device=$(cat $md_file) || [ -z "$md_device" ]; then
190		echo "Couldn't get device from $md_file"
191		exit 0
192	fi
193	umount mnt
194	mdconfig -d -u $md_device
195}
196
197atf_init_test_cases()
198{
199	atf_add_test_case RH_flag
200	atf_add_test_case RL_flag
201	atf_add_test_case RP_flag
202	atf_add_test_case f_flag
203	atf_add_test_case h_flag
204	atf_add_test_case v_flag
205	atf_add_test_case x_flag
206}
207