1319642Sngie#
2319642Sngie# Copyright (c) 2017 Dell EMC
3319642Sngie# All rights reserved.
4319642Sngie#
5319642Sngie# Redistribution and use in source and binary forms, with or without
6319642Sngie# modification, are permitted provided that the following conditions
7319642Sngie# are met:
8319642Sngie# 1. Redistributions of source code must retain the above copyright
9319642Sngie#    notice, this list of conditions and the following disclaimer.
10319642Sngie# 2. Redistributions in binary form must reproduce the above copyright
11319642Sngie#    notice, this list of conditions and the following disclaimer in the
12319642Sngie#    documentation and/or other materials provided with the distribution.
13319642Sngie#
14319642Sngie# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15319642Sngie# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16319642Sngie# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17319642Sngie# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18319642Sngie# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19319642Sngie# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20319642Sngie# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21319642Sngie# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22319642Sngie# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23319642Sngie# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24319642Sngie# SUCH DAMAGE.
25319642Sngie#
26319642Sngie# $FreeBSD: stable/10/usr.sbin/chown/tests/chown_test.sh 322835 2017-08-24 13:39:24Z gjb $
27319642Sngie
28322835Sgjbget_filesystem()
29322835Sgjb{
30322835Sgjb	local mountpoint=$1
31322835Sgjb
32322835Sgjb	df -T $mountpoint | tail -n 1 | cut -wf 2
33322835Sgjb}
34322835Sgjb
35319642Sngieatf_test_case RH_flag
36319642SngieRH_flag_head()
37319642Sngie{
38319830Sngie	atf_set	"descr" "Verify that setting ownership recursively via -R doesn't " \
39319642Sngie			"affect symlinks specified via the arguments when -H " \
40319642Sngie			"is specified"
41319830Sngie	atf_set "require.user" "root"
42319642Sngie}
43319642SngieRH_flag_body()
44319642Sngie{
45319830Sngie	atf_check mkdir -p A/B
46319642Sngie	atf_check ln -s B A/C
47319830Sngie	atf_check chown -h 42:42 A/C
48319830Sngie	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
49319830Sngie	atf_check chown -RH 84:84 A
50319830Sngie	atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C
51319830Sngie	atf_check chown -RH 126:126 A/C
52319830Sngie	atf_check -o inline:'84:84\n126:126\n84:84\n' stat -f '%u:%g' A A/B A/C
53319642Sngie}
54319642Sngie
55319642Sngieatf_test_case RL_flag
56319642SngieRL_flag_head()
57319642Sngie{
58319830Sngie	atf_set	"descr" "Verify that setting ownership recursively via -R doesn't " \
59319642Sngie			"affect symlinks specified via the arguments when -L " \
60319642Sngie			"is specified"
61319830Sngie	atf_set "require.user" "root"
62319642Sngie}
63319642SngieRL_flag_body()
64319642Sngie{
65319830Sngie	atf_check mkdir -p A/B
66319642Sngie	atf_check ln -s B A/C
67319830Sngie	atf_check chown -h 42:42 A/C
68319830Sngie	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
69319830Sngie	atf_check chown -RL 84:84 A
70319830Sngie	atf_check -o inline:'84:84\n84:84\n42:42\n' stat -f '%u:%g' A A/B A/C
71319830Sngie	atf_check chown -RL 126:126 A/C
72319830Sngie	atf_check -o inline:'84:84\n126:126\n42:42\n' stat -f '%u:%g' A A/B A/C
73319642Sngie}
74319642Sngie
75319642Sngieatf_test_case RP_flag
76319642SngieRP_flag_head()
77319642Sngie{
78319830Sngie	atf_set	"descr" "Verify that setting ownership recursively via -R " \
79319830Sngie			"doesn't affect symlinks specified via the arguments " \
80319830Sngie			"when -P is specified"
81319830Sngie	atf_set "require.user" "root"
82319642Sngie}
83319642SngieRP_flag_body()
84319642Sngie{
85319830Sngie	atf_check mkdir -p A/B
86319642Sngie	atf_check ln -s B A/C
87319830Sngie	atf_check chown -h 42:42 A/C
88319830Sngie	atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C
89319830Sngie	atf_check chown -RP 84:84 A
90319830Sngie	atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C
91319830Sngie	atf_check chown -RP 126:126 A/C
92319830Sngie	atf_check -o inline:'84:84\n84:84\n126:126\n' stat -f '%u:%g' A A/B A/C
93319642Sngie}
94319642Sngie
95319642Sngieatf_test_case f_flag cleanup
96319642Sngief_flag_head()
97319642Sngie{
98319642Sngie	atf_set	"descr" "Verify that setting a mode for a file with -f " \
99319642Sngie			"doesn't emit an error message/exit with a non-zero " \
100319642Sngie			"code"
101319830Sngie	atf_set "require.user" "root"
102319642Sngie}
103319642Sngie
104319642Sngief_flag_body()
105319642Sngie{
106319642Sngie	atf_check truncate -s 0 foo bar
107319830Sngie	atf_check chown 0:0 foo bar
108322835Sgjb	case "$(get_filesystem .)" in
109322835Sgjb	zfs)
110322835Sgjb		atf_expect_fail "ZFS does not support UF_IMMUTABLE; returns EPERM"
111322835Sgjb		;;
112322835Sgjb	esac
113319642Sngie	atf_check chflags uchg foo
114319830Sngie	atf_check -e not-empty -s not-exit:0 chown 42:42 foo bar
115319830Sngie	atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar
116319830Sngie	atf_check -s exit:0 chown -f 84:84 foo bar
117319830Sngie	atf_check -o inline:'0:0\n84:84\n' stat -f '%u:%g' foo bar
118319642Sngie}
119319642Sngie
120319642Sngief_flag_cleanup()
121319642Sngie{
122322835Sgjb	chflags 0 foo || :
123319642Sngie}
124319642Sngie
125319642Sngieatf_test_case h_flag
126319642Sngieh_flag_head()
127319642Sngie{
128319642Sngie	atf_set	"descr" "Verify that setting a mode for a file with -f " \
129319642Sngie			"doesn't emit an error message/exit with a non-zero " \
130319642Sngie			"code"
131319830Sngie	atf_set "require.user" "root"
132319642Sngie}
133319642Sngie
134319642Sngieh_flag_body()
135319642Sngie{
136319642Sngie	atf_check truncate -s 0 foo
137319830Sngie	atf_check -o inline:'0:0\n' stat -f '%u:%g' foo
138319642Sngie	atf_check ln -s foo bar
139319830Sngie	atf_check -o inline:'0:0\n0:0\n' stat -f '%u:%g' foo bar
140319830Sngie	atf_check chown -h 42:42 bar
141319830Sngie	atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar
142319830Sngie	atf_check chown 84:84 bar
143319830Sngie	atf_check -o inline:'84:84\n42:42\n' stat -f '%u:%g' foo bar
144319642Sngie}
145319642Sngie
146319642Sngieatf_test_case v_flag
147319642Sngiev_flag_head()
148319642Sngie{
149319830Sngie	atf_set	"descr" "Verify that setting ownership with -v emits the " \
150319830Sngie			"file doesn't emit an error message/exit with a " \
151319830Sngie			"non-zero code"
152319830Sngie	atf_set "require.user" "root"
153319642Sngie}
154319642Sngiev_flag_body()
155319642Sngie{
156319642Sngie	atf_check truncate -s 0 foo bar
157319830Sngie	atf_check chown 0:0 foo
158319830Sngie	atf_check chown 42:42 bar
159319830Sngie	atf_check -o 'inline:bar\n' chown -v 0:0 foo bar
160319830Sngie	atf_check chown -v 0:0 foo bar
161319642Sngie	for f in foo bar; do
162319830Sngie		echo "$f: 0:0 -> 84:84";
163319642Sngie	done > output.txt
164319830Sngie	atf_check -o file:output.txt chown -vv 84:84 foo bar
165319830Sngie	atf_check chown -vv 84:84 foo bar
166319642Sngie}
167319642Sngie
168319830Sngiemd_file="md.out"
169319830Sngieatf_test_case x_flag cleanup
170319830Sngiex_flag_head()
171319830Sngie{
172319830Sngie	atf_set	"descr" "Verify that setting a mode with -x doesn't set " \
173319830Sngie			"ownership across mountpoints"
174319830Sngie	atf_set "require.user" "root"
175319830Sngie}
176319830Sngiex_flag_body()
177319830Sngie{
178319830Sngie	atf_check -o save:$md_file mdconfig -a -t malloc -s 20m
179319830Sngie	if ! md_device=$(cat $md_file); then
180319830Sngie		atf_fail "cat $md_file failed"
181319830Sngie	fi
182319830Sngie	atf_check -o not-empty newfs /dev/$md_device
183319830Sngie	atf_check mkdir mnt
184319830Sngie	atf_check mount /dev/$md_device mnt
185319830Sngie	atf_check truncate -s 0 foo bar mnt/bazbaz
186319830Sngie	atf_check ln -s bar mnt/barbaz
187319830Sngie	atf_check ln -s ../foo mnt/foobaz
188319830Sngie	cd mnt
189319830Sngie	test_files="../foo ../bar barbaz bazbaz foobaz"
190319830Sngie	atf_check -o inline:'0:0\n0:0\n0:0\n0:0\n0:0\n' \
191319830Sngie	    stat -f '%u:%g' $test_files
192319830Sngie	atf_check chown -Rx 42:42 .
193319830Sngie	atf_check -o inline:'0:0\n0:0\n42:42\n42:42\n42:42\n' \
194319830Sngie	    stat -f '%u:%g' $test_files
195319830Sngie	atf_check chown -R 84:84 .
196319830Sngie	atf_check -o inline:'0:0\n0:0\n84:84\n84:84\n84:84\n' \
197319830Sngie	    stat -f '%u:%g' $test_files
198319830Sngie}
199319830Sngiex_flag_cleanup()
200319830Sngie{
201319830Sngie	if ! md_device=$(cat $md_file) || [ -z "$md_device" ]; then
202319830Sngie		echo "Couldn't get device from $md_file"
203319830Sngie		exit 0
204319830Sngie	fi
205319830Sngie	umount mnt
206319830Sngie	mdconfig -d -u $md_device
207319830Sngie}
208319830Sngie
209319642Sngieatf_init_test_cases()
210319642Sngie{
211319642Sngie	atf_add_test_case RH_flag
212319642Sngie	atf_add_test_case RL_flag
213319642Sngie	atf_add_test_case RP_flag
214319642Sngie	atf_add_test_case f_flag
215319642Sngie	atf_add_test_case h_flag
216319642Sngie	atf_add_test_case v_flag
217319830Sngie	atf_add_test_case x_flag
218319642Sngie}
219