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