#! /bin/bash

. ${0%/*}/test-lib.sh

require_runas
require_richacls
use_testdir

export LC_ALL=C

# Create file as root
ncheck "touch a"

# Chown and chgrp with no take ownership permission fails
runas -u 99 -g 99
check "chown 99 a || echo status: \$?" <<EOF
chown: changing ownership of 'a': Operation not permitted
status: 1
EOF
check "chgrp 99 a || echo status: \$?" <<EOF
chgrp: changing group of 'a': Operation not permitted
status: 1
EOF

# Add the take_ownership permission
runas
ncheck "setrichacl --set 'u:99:rwpo::allow' a"

# Chown and chgrp to a user or group the process is not in fails
runas -u 99 -g 99
check "chown 100 a || echo status: \$?" <<EOF
chown: changing ownership of 'a': Operation not permitted
status: 1
EOF
check "chgrp 100 a || echo status: \$?" <<EOF
chgrp: changing group of 'a': Operation not permitted
status: 1
EOF

# Chown and chgrp to a user and group the process is in succeeds
ncheck "chown 99 a"
ncheck "chgrp 99 a"
