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/bin/chmod/tests/chmod_test.sh 322759 2017-08-21 17:20:31Z gjb $
27319642Sngie
28322759Sgjbget_filesystem()
29322759Sgjb{
30322759Sgjb	local mountpoint=$1
31322759Sgjb
32322759Sgjb	df -T $mountpoint | tail -n 1 | cut -wf 2
33322759Sgjb}
34322759Sgjb
35319642Sngieatf_test_case RH_flag
36319642SngieRH_flag_head()
37319642Sngie{
38319642Sngie	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
39319642Sngie			"affect symlinks specified via the arguments when -H " \
40319642Sngie			"is specified"
41319642Sngie}
42319642SngieRH_flag_body()
43319642Sngie{
44319642Sngie	atf_check mkdir -m 0777 -p A/B
45319642Sngie	atf_check ln -s B A/C
46319642Sngie	atf_check chmod -h 0777 A/C
47319642Sngie	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
48319642Sngie	atf_check chmod -RH 0700 A
49319642Sngie	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
50319642Sngie	atf_check chmod -RH 0600 A/C
51319642Sngie	atf_check -o inline:'40700\n40600\n120700\n' stat -f '%p' A A/B A/C
52319642Sngie}
53319642Sngie
54319642Sngieatf_test_case RL_flag
55319642SngieRL_flag_head()
56319642Sngie{
57319642Sngie	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
58319642Sngie			"affect symlinks specified via the arguments when -L " \
59319642Sngie			"is specified"
60319642Sngie}
61319642SngieRL_flag_body()
62319642Sngie{
63319642Sngie	atf_check mkdir -m 0777 -p A/B
64319642Sngie	atf_check ln -s B A/C
65319642Sngie	atf_check chmod -h 0777 A/C
66319642Sngie	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
67319642Sngie	atf_check chmod -RL 0700 A
68319642Sngie	atf_check -o inline:'40700\n40700\n120777\n' stat -f '%p' A A/B A/C
69319642Sngie	atf_check chmod -RL 0600 A/C
70319642Sngie	atf_check -o inline:'40700\n40600\n120777\n' stat -f '%p' A A/B A/C
71319642Sngie}
72319642Sngie
73319642Sngieatf_test_case RP_flag
74319642SngieRP_flag_head()
75319642Sngie{
76319642Sngie	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
77319642Sngie			"affect symlinks specified via the arguments when -P " \
78319642Sngie			"is specified"
79319642Sngie}
80319642SngieRP_flag_body()
81319642Sngie{
82319642Sngie	atf_check mkdir -m 0777 -p A/B
83319642Sngie	atf_check ln -s B A/C
84319642Sngie	atf_check chmod -h 0777 A/C
85319642Sngie	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
86319642Sngie	atf_check chmod -RP 0700 A
87319642Sngie	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
88319642Sngie	atf_check chmod -RP 0600 A/C
89319642Sngie	atf_check -o inline:'40700\n40700\n120600\n' stat -f '%p' A A/B A/C
90319642Sngie}
91319642Sngie
92319642Sngieatf_test_case f_flag cleanup
93319642Sngief_flag_head()
94319642Sngie{
95319642Sngie	atf_set	"descr" "Verify that setting a mode for a file with -f " \
96319642Sngie			"doesn't emit an error message/exit with a non-zero " \
97319642Sngie			"code"
98319642Sngie}
99319642Sngie
100319642Sngief_flag_body()
101319642Sngie{
102319642Sngie	atf_check truncate -s 0 foo bar
103319642Sngie	atf_check chmod 0750 foo bar
104322759Sgjb	case "$(get_filesystem .)" in
105322759Sgjb	zfs)
106322759Sgjb		atf_expect_fail "ZFS doesn't support UF_IMMUTABLE; returns EPERM - bug 221189"
107322759Sgjb		;;
108322759Sgjb	esac
109319642Sngie	atf_check chflags uchg foo
110319642Sngie	atf_check -e not-empty -s not-exit:0 chmod 0700 foo bar
111319642Sngie	atf_check -o inline:'100750\n100700\n' stat -f '%p' foo bar
112319642Sngie	atf_check -s exit:0 chmod -f 0600 foo bar
113319642Sngie	atf_check -o inline:'100750\n100600\n' stat -f '%p' foo bar
114319642Sngie}
115319642Sngie
116319642Sngief_flag_cleanup()
117319642Sngie{
118322759Sgjb	chflags 0 foo || :
119319642Sngie}
120319642Sngie
121319642Sngieatf_test_case h_flag
122319642Sngieh_flag_head()
123319642Sngie{
124319642Sngie	atf_set	"descr" "Verify that setting a mode for a file with -f " \
125319642Sngie			"doesn't emit an error message/exit with a non-zero " \
126319642Sngie			"code"
127319642Sngie}
128319642Sngie
129319642Sngieh_flag_body()
130319642Sngie{
131319642Sngie	atf_check truncate -s 0 foo
132319642Sngie	atf_check chmod 0600 foo
133319642Sngie	atf_check -o inline:'100600\n' stat -f '%p' foo
134319642Sngie	umask 0077
135319642Sngie	atf_check ln -s foo bar
136319642Sngie	atf_check -o inline:'100600\n120700\n' stat -f '%p' foo bar
137319642Sngie	atf_check chmod -h 0500 bar
138319642Sngie	atf_check -o inline:'100600\n120500\n' stat -f '%p' foo bar
139319642Sngie	atf_check chmod 0660 bar
140319642Sngie	atf_check -o inline:'100660\n120500\n' stat -f '%p' foo bar
141319642Sngie}
142319642Sngie
143319642Sngieatf_test_case v_flag
144319642Sngiev_flag_head()
145319642Sngie{
146319642Sngie	atf_set	"descr" "Verify that setting a mode with -v emits the file when " \
147319642Sngie			"doesn't emit an error message/exit with a non-zero " \
148319642Sngie			"code"
149319642Sngie}
150319642Sngiev_flag_body()
151319642Sngie{
152319642Sngie	atf_check truncate -s 0 foo bar
153319642Sngie	atf_check chmod 0600 foo
154319642Sngie	atf_check chmod 0750 bar
155322759Sgjb	case "$(get_filesystem .)" in
156322759Sgjb	zfs)
157322759Sgjb		atf_expect_fail "ZFS updates mode for foo unnecessarily - bug 221188"
158322759Sgjb		;;
159322759Sgjb	esac
160319642Sngie	atf_check -o 'inline:bar\n' chmod -v 0600 foo bar
161319642Sngie	atf_check chmod -v 0600 foo bar
162319642Sngie	for f in foo bar; do
163319642Sngie		echo "$f: 0100600 [-rw------- ] -> 0100700 [-rwx------ ]";
164319642Sngie	done > output.txt
165319642Sngie	atf_check -o file:output.txt chmod -vv 0700 foo bar
166319642Sngie	atf_check chmod -vv 0700 foo bar
167319642Sngie}
168319642Sngie
169319642Sngieatf_init_test_cases()
170319642Sngie{
171319642Sngie	atf_add_test_case RH_flag
172319642Sngie	atf_add_test_case RL_flag
173319642Sngie	atf_add_test_case RP_flag
174319642Sngie	atf_add_test_case f_flag
175319642Sngie	atf_add_test_case h_flag
176319642Sngie	atf_add_test_case v_flag
177319642Sngie}
178