1#!/bin/sh
2# $FreeBSD: head/tools/regression/pjdfstest/tests/chmod/00.t 211352 2010-08-15 21:24:17Z pjd $
3
4desc="chmod changes permission"
5
6dir=`dirname $0`
7. ${dir}/../misc.sh
8
9if supported lchmod; then
10	echo "1..203"
11else
12	echo "1..119"
13fi
14
15n0=`namegen`
16n1=`namegen`
17n2=`namegen`
18
19expect 0 mkdir ${n2} 0755
20cdir=`pwd`
21cd ${n2}
22
23for type in regular dir fifo block char socket symlink; do
24	if [ "${type}" != "symlink" ]; then
25		create_file ${type} ${n0}
26		expect 0 chmod ${n0} 0111
27		expect 0111 stat ${n0} mode
28
29		expect 0 symlink ${n0} ${n1}
30		mode=`${fstest} lstat ${n1} mode`
31		expect 0 chmod ${n1} 0222
32		expect 0222 stat ${n1} mode
33		expect 0222 stat ${n0} mode
34		expect ${mode} lstat ${n1} mode
35		expect 0 unlink ${n1}
36
37		if [ "${type}" = "dir" ]; then
38			expect 0 rmdir ${n0}
39		else
40			expect 0 unlink ${n0}
41		fi
42	fi
43
44	if supported lchmod; then
45		create_file ${type} ${n0}
46		expect 0 lchmod ${n0} 0111
47		expect 0111 lstat ${n0} mode
48		if [ "${type}" = "dir" ]; then
49			expect 0 rmdir ${n0}
50		else
51			expect 0 unlink ${n0}
52		fi
53	fi
54done
55
56# successful chmod(2) updates ctime.
57for type in regular dir fifo block char socket symlink; do
58	if [ "${type}" != "symlink" ]; then
59		create_file ${type} ${n0}
60		ctime1=`${fstest} stat ${n0} ctime`
61		sleep 1
62		expect 0 chmod ${n0} 0111
63		ctime2=`${fstest} stat ${n0} ctime`
64		test_check $ctime1 -lt $ctime2
65		if [ "${type}" = "dir" ]; then
66			expect 0 rmdir ${n0}
67		else
68			expect 0 unlink ${n0}
69		fi
70	fi
71
72	if supported lchmod; then
73		create_file ${type} ${n0}
74		ctime1=`${fstest} lstat ${n0} ctime`
75		sleep 1
76		expect 0 lchmod ${n0} 0111
77		ctime2=`${fstest} lstat ${n0} ctime`
78		test_check $ctime1 -lt $ctime2
79		if [ "${type}" = "dir" ]; then
80			expect 0 rmdir ${n0}
81		else
82			expect 0 unlink ${n0}
83		fi
84	fi
85done
86
87# unsuccessful chmod(2) does not update ctime.
88for type in regular dir fifo block char socket symlink; do
89	if [ "${type}" != "symlink" ]; then
90		create_file ${type} ${n0}
91		ctime1=`${fstest} stat ${n0} ctime`
92		sleep 1
93		expect EPERM -u 65534 chmod ${n0} 0111
94		ctime2=`${fstest} stat ${n0} ctime`
95		test_check $ctime1 -eq $ctime2
96		if [ "${type}" = "dir" ]; then
97			expect 0 rmdir ${n0}
98		else
99			expect 0 unlink ${n0}
100		fi
101	fi
102
103	if supported lchmod; then
104		create_file ${type} ${n0}
105		ctime1=`${fstest} lstat ${n0} ctime`
106		sleep 1
107		expect EPERM -u 65534 lchmod ${n0} 0321
108		ctime2=`${fstest} lstat ${n0} ctime`
109		test_check $ctime1 -eq $ctime2
110		if [ "${type}" = "dir" ]; then
111			expect 0 rmdir ${n0}
112		else
113			expect 0 unlink ${n0}
114		fi
115	fi
116done
117
118# POSIX: If the calling process does not have appropriate privileges, and if
119# the group ID of the file does not match the effective group ID or one of the
120# supplementary group IDs and if the file is a regular file, bit S_ISGID
121# (set-group-ID on execution) in the file's mode shall be cleared upon
122# successful return from chmod().
123
124expect 0 create ${n0} 0755
125expect 0 chown ${n0} 65535 65535
126expect 0 -u 65535 -g 65535 chmod ${n0} 02755
127expect 02755 stat ${n0} mode
128expect 0 -u 65535 -g 65535 chmod ${n0} 0755
129expect 0755 stat ${n0} mode
130
131todo FreeBSD "S_ISGID should be removed and chmod(2) should success and FreeBSD returns EPERM."
132expect 0 -u 65535 -g 65534 chmod ${n0} 02755
133expect 0755 stat ${n0} mode
134
135expect 0 unlink ${n0}
136
137cd ${cdir}
138expect 0 rmdir ${n2}
139