Path: blob/main/usr.sbin/chown/tests/chown_test.sh
106138 views
#1# Copyright (c) 2017 Dell EMC2# All rights reserved.3#4# Redistribution and use in source and binary forms, with or without5# modification, are permitted provided that the following conditions6# are met:7# 1. Redistributions of source code must retain the above copyright8# notice, this list of conditions and the following disclaimer.9# 2. Redistributions in binary form must reproduce the above copyright10# notice, this list of conditions and the following disclaimer in the11# documentation and/or other materials provided with the distribution.12#13# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23# SUCH DAMAGE.24#2526get_filesystem()27{28local mountpoint=$12930df -T $mountpoint | tail -n 1 | cut -wf 231}3233atf_test_case RH_flag34RH_flag_head()35{36atf_set "descr" "Verify that setting ownership recursively via -R doesn't " \37"affect symlinks specified via the arguments when -H " \38"is specified"39atf_set "require.user" "root"40}41RH_flag_body()42{43atf_check mkdir -p A/B44atf_check ln -s B A/C45atf_check chown -h 42:42 A/C46atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C47atf_check chown -RH 84:84 A48atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C49atf_check chown -RH 126:126 A/C50atf_check -o inline:'84:84\n126:126\n84:84\n' stat -f '%u:%g' A A/B A/C51}5253atf_test_case RL_flag54RL_flag_head()55{56atf_set "descr" "Verify that setting ownership recursively via -R doesn't " \57"affect symlinks specified via the arguments when -L " \58"is specified"59atf_set "require.user" "root"60}61RL_flag_body()62{63atf_check mkdir -p A/B64atf_check ln -s B A/C65atf_check chown -h 42:42 A/C66atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C67atf_check chown -RL 84:84 A68atf_check -o inline:'84:84\n84:84\n42:42\n' stat -f '%u:%g' A A/B A/C69atf_check chown -RL 126:126 A/C70atf_check -o inline:'84:84\n126:126\n42:42\n' stat -f '%u:%g' A A/B A/C71}7273atf_test_case RP_flag74RP_flag_head()75{76atf_set "descr" "Verify that setting ownership recursively via -R " \77"doesn't affect symlinks specified via the arguments " \78"when -P is specified"79atf_set "require.user" "root"80}81RP_flag_body()82{83atf_check mkdir -p A/B84atf_check ln -s B A/C85atf_check chown -h 42:42 A/C86atf_check -o inline:'0:0\n0:0\n42:42\n' stat -f '%u:%g' A A/B A/C87atf_check chown -RP 84:84 A88atf_check -o inline:'84:84\n84:84\n84:84\n' stat -f '%u:%g' A A/B A/C89atf_check chown -RP 126:126 A/C90atf_check -o inline:'84:84\n84:84\n126:126\n' stat -f '%u:%g' A A/B A/C91}9293atf_test_case f_flag cleanup94f_flag_head()95{96atf_set "descr" "Verify that setting a mode for a file with -f " \97"doesn't emit an error message/exit with a non-zero " \98"code"99atf_set "require.user" "root"100}101102f_flag_body()103{104atf_check truncate -s 0 foo bar105atf_check chown 0:0 foo bar106case "$(get_filesystem .)" in107zfs)108atf_expect_fail "ZFS does not support UF_IMMUTABLE; returns EPERM"109;;110esac111atf_check chflags uchg foo112atf_check -e not-empty -s not-exit:0 chown 42:42 foo bar113atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar114atf_check -s exit:0 chown -f 84:84 foo bar115atf_check -o inline:'0:0\n84:84\n' stat -f '%u:%g' foo bar116}117118f_flag_cleanup()119{120chflags 0 foo || :121}122123atf_test_case h_flag124h_flag_head()125{126atf_set "descr" "Verify that setting a mode for a file with -f " \127"doesn't emit an error message/exit with a non-zero " \128"code"129atf_set "require.user" "root"130}131132h_flag_body()133{134atf_check truncate -s 0 foo135atf_check -o inline:'0:0\n' stat -f '%u:%g' foo136atf_check ln -s foo bar137atf_check -o inline:'0:0\n0:0\n' stat -f '%u:%g' foo bar138atf_check chown -h 42:42 bar139atf_check -o inline:'0:0\n42:42\n' stat -f '%u:%g' foo bar140atf_check chown 84:84 bar141atf_check -o inline:'84:84\n42:42\n' stat -f '%u:%g' foo bar142}143144atf_test_case v_flag145v_flag_head()146{147atf_set "descr" "Verify that setting ownership with -v emits the " \148"file doesn't emit an error message/exit with a " \149"non-zero code"150atf_set "require.user" "root"151}152v_flag_body()153{154atf_check truncate -s 0 foo bar155atf_check chown 0:0 foo156atf_check chown 42:42 bar157atf_check -o 'inline:bar\n' chown -v 0:0 foo bar158atf_check chown -v 0:0 foo bar159for f in foo bar; do160echo "$f: 0:0 -> 84:84";161done > output.txt162atf_check -o file:output.txt chown -vv 84:84 foo bar163atf_check chown -vv 84:84 foo bar164}165166md_file="md.out"167atf_test_case x_flag cleanup168x_flag_head()169{170atf_set "descr" "Verify that setting a mode with -x doesn't set " \171"ownership across mountpoints"172atf_set "require.user" "root"173}174x_flag_body()175{176atf_check -o save:$md_file mdconfig -a -t malloc -s 20m177if ! md_device=$(cat $md_file); then178atf_fail "cat $md_file failed"179fi180atf_check -o not-empty newfs /dev/$md_device181atf_check mkdir mnt182atf_check mount /dev/$md_device mnt183atf_check truncate -s 0 foo bar mnt/bazbaz184atf_check ln -s bar mnt/barbaz185atf_check ln -s ../foo mnt/foobaz186cd mnt187test_files="../foo ../bar barbaz bazbaz foobaz"188atf_check -o inline:'0:0\n0:0\n0:0\n0:0\n0:0\n' \189stat -f '%u:%g' $test_files190atf_check chown -Rx 42:42 .191atf_check -o inline:'0:0\n0:0\n42:42\n42:42\n42:42\n' \192stat -f '%u:%g' $test_files193atf_check chown -R 84:84 .194atf_check -o inline:'0:0\n0:0\n84:84\n84:84\n84:84\n' \195stat -f '%u:%g' $test_files196}197x_flag_cleanup()198{199if ! md_device=$(cat $md_file) || [ -z "$md_device" ]; then200echo "Couldn't get device from $md_file"201exit 0202fi203umount mnt204mdconfig -d -u $md_device205}206207atf_init_test_cases()208{209atf_add_test_case RH_flag210atf_add_test_case RL_flag211atf_add_test_case RP_flag212atf_add_test_case f_flag213atf_add_test_case h_flag214atf_add_test_case v_flag215atf_add_test_case x_flag216}217218219