1#! /usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2017 Nexenta Systems, Inc. All rights reserved.
16# Copyright 2019 Alexander Pyhalov
17#
18
19CHOWN=${CHOWN:=/usr/bin/chown}
20FILEDIR=/opt/util-tests/tests/files
21
22fail() {
23	echo $1
24	exit -1
25}
26
27create_test_hier() {
28	mkdir -p $1/src $1/dst
29	touch $1/target
30	touch $1/file
31	touch $1/dst/file1
32	touch $1/src/file2
33	ln -s ../target $1/src/tlink
34	ln -s ../dst $1/src/dstlink
35	ln -s target $1/tlink
36}
37
38SUCCESSFLAGS="
39-f
40-h
41-fhR
42-R
43-RH
44-RP
45-RL
46-Rh
47-RHL
48-RPH
49-RLP"
50
51FAILFLAGS="-RPh
52-RLh
53-RHh
54-P
55-H
56-L"
57
58NEWOWNER=daemon
59
60# We set PATH to /bin to try get ksh chown builtin
61# and to ensure that /usr/bin/chown is used instead.
62export PATH=/bin
63
64# We want unified output from tools
65export LC_ALL=en_US.UTF-8
66
67i=0
68echo "$SUCCESSFLAGS" | while read flags; do
69	print -n "test $i: chown $flags: "
70	TD=$(mktemp -d  -t)
71	if [ -d "$TD" ]; then
72		create_test_hier $TD
73		chown $flags $NEWOWNER $TD/src   || fail "chown $flags $NEWOWNER $TD/src failed on exit"
74		chown $flags $NEWOWNER $TD/tlink || fail "chown $flags $NEWOWNER $TD/tlink failed on exit"
75		chown $flags $NEWOWNER $TD/file  || fail "chown $flags $NEWOWNER $TD/file failed on exit"
76		(cd $TD ; find . -ls |\
77			awk ' { print $3 " " $5 " " $11 }'|\
78			sort -k 3 > /tmp/out.$$)
79		if [ -n "$(diff /tmp/out.$$ $FILEDIR/cout$i)" ]; then
80			print "$(diff -u /tmp/out.$$ $FILEDIR/cout$i)"
81			fail "result is different"
82		fi
83		echo "passed"
84		rm -fr $TD /tmp/out.$$
85	else
86		fail "couldn't create $TD"
87	fi
88	((i++))
89done
90
91echo "$FAILFLAGS" | while read flags; do
92	print -n "test $i: chown $flags: "
93	TD=$(mktemp -d  -t)
94	if [ -d "$TD" ]; then
95		create_test_hier $TD
96		chown $flags $NEWOWNER $TD/file && fail "chown $flags $NEWOWNER $TD/file should have failed"
97		echo "passed"
98		rm -fr $TD
99	else
100		fail "couldn't create $TD"
101	fi
102	((i++))
103done
104