vmojzis / tests / selinux

Forked from tests/selinux 4 years ago
Clone
78a1b24
#!/bin/bash
78a1b24
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
78a1b24
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78a1b24
#
78a1b24
#   runtest.sh of /CoreOS/policycoreutils/Sanity/file-contexts
78a1b24
#   Description: Test semanage fcontext, restorecon, fixfiles, chcon
78a1b24
#   Author: Jan Zarsky <jzarsky@redhat.com>
78a1b24
#
78a1b24
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78a1b24
#
78a1b24
#   Copyright (c) 2018 Red Hat, Inc.
78a1b24
#
78a1b24
#   This program is free software: you can redistribute it and/or
78a1b24
#   modify it under the terms of the GNU General Public License as
78a1b24
#   published by the Free Software Foundation, either version 2 of
78a1b24
#   the License, or (at your option) any later version.
78a1b24
#
78a1b24
#   This program is distributed in the hope that it will be
78a1b24
#   useful, but WITHOUT ANY WARRANTY; without even the implied
78a1b24
#   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
78a1b24
#   PURPOSE.  See the GNU General Public License for more details.
78a1b24
#
78a1b24
#   You should have received a copy of the GNU General Public License
78a1b24
#   along with this program. If not, see http://www.gnu.org/licenses/.
78a1b24
#
78a1b24
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78a1b24
78a1b24
# Include Beaker environment
78a1b24
. /usr/bin/rhts-environment.sh || exit 1
78a1b24
. /usr/share/beakerlib/beakerlib.sh || exit 1
78a1b24
78a1b24
PACKAGE="policycoreutils"
78a1b24
78a1b24
rlJournalStart
78a1b24
    rlPhaseStartSetup
78a1b24
        rlAssertRpm $PACKAGE
78a1b24
78a1b24
        RUN_CON="system_u:object_r:var_run_t:s0"
78a1b24
        HOME_BIN_CON="unconfined_u:object_r:home_bin_t:s0"
78a1b24
        FILE_CON="user_tmp_t"
78a1b24
        TEST_CON="user_home_t"
78a1b24
        TEST_CON2="tmp_t"
78a1b24
78a1b24
        function checkFC {
78a1b24
            ls -dZ $1
78a1b24
            ls -dZ $1 | grep $2
78a1b24
            return $?
78a1b24
        }
78a1b24
    rlPhaseEnd
78a1b24
78a1b24
    rlPhaseStartTest "semanage fcontext -l"
78a1b24
        rlRun "semanage fcontext -l >stdout"
78a1b24
        rlAssertGrep "SELinux fcontext" stdout
78a1b24
        rlAssertGrep "SELinux Distribution fcontext Equivalence" stdout
78a1b24
        rlAssertGrep "/run/\.\* *all files *$RUN_CON" stdout
78a1b24
        rlAssertGrep "/run *directory *$RUN_CON" stdout
78a1b24
        rlAssertGrep "/home/\[\^/\]+/bin(/\.\*)? *all files *$HOME_BIN_CON" stdout
78a1b24
        rlAssertGrep "/run = /var/run" stdout
78a1b24
    rlPhaseEnd
78a1b24
78a1b24
    rlPhaseStartTest "semanage fcontext add and delete"
78a1b24
        rlRun "mkdir /tmp/test"
78a1b24
        rlRun "pushd /tmp/test"
78a1b24
        DIR=$(pwd)
78a1b24
78a1b24
        # add custom file context
78a1b24
        rlRun "semanage fcontext -a -t $TEST_CON '$DIR/asdf'"
78a1b24
78a1b24
        rlRun "semanage fcontext -l -C | grep '$DIR/asdf *all files *system_u:object_r:$TEST_CON'"
78a1b24
78a1b24
        # test for regular file
78a1b24
        rlRun "touch asdf"
78a1b24
        rlRun "checkFC asdf $FILE_CON"
78a1b24
        rlRun "restorecon asdf"
78a1b24
        rlRun "checkFC asdf $TEST_CON"
78a1b24
        rlRun "rm asdf"
78a1b24
78a1b24
        # test for directory
78a1b24
        rlRun "mkdir asdf"
78a1b24
        rlRun "checkFC asdf $FILE_CON"
78a1b24
        rlRun "restorecon asdf"
78a1b24
        rlRun "checkFC asdf $TEST_CON"
78a1b24
        rlRun "rmdir asdf"
78a1b24
78a1b24
        # modify the context
78a1b24
        rlRun "semanage fcontext -m -t $TEST_CON2 '$DIR/asdf'"
78a1b24
78a1b24
        rlRun "semanage fcontext -l -C | grep '$DIR/asdf *all files *system_u:object_r:$TEST_CON2'"
78a1b24
78a1b24
        # test
78a1b24
        rlRun "touch asdf"
78a1b24
        rlRun "checkFC asdf $FILE_CON"
78a1b24
        rlRun "restorecon asdf"
78a1b24
        rlRun "checkFC asdf $TEST_CON2"
78a1b24
        rlRun "rm asdf"
78a1b24
78a1b24
        # delete the context
78a1b24
        rlRun "semanage fcontext -d '$DIR/asdf'"
78a1b24
78a1b24
        rlRun "popd"
78a1b24
        rlRun "rm -rf /tmp/test"
78a1b24
    rlPhaseEnd
78a1b24
78a1b24
    rlPhaseStartTest "semanage fcontext add and delete with file type"
78a1b24
        rlRun "mkdir /tmp/test"
78a1b24
        rlRun "pushd /tmp/test"
78a1b24
        DIR=$(pwd)
78a1b24
78a1b24
        # add custom file context for regular files
78a1b24
        rlRun "semanage fcontext -a -f f -t $TEST_CON '$DIR/asdf'"
78a1b24
        # add custom file context for directories
78a1b24
        rlRun "semanage fcontext -a -f d -t $TEST_CON2 '$DIR/asdf'"
78a1b24
78a1b24
        rlRun "semanage fcontext -l | grep '$DIR/asdf *regular file *system_u:object_r:$TEST_CON'"
78a1b24
        rlRun "semanage fcontext -l | grep '$DIR/asdf *directory *system_u:object_r:$TEST_CON2'"
78a1b24
78a1b24
        # test regular file
78a1b24
        rlRun "touch asdf"
78a1b24
        rlRun "checkFC asdf $FILE_CON"
78a1b24
        rlRun "restorecon asdf"
78a1b24
        rlRun "checkFC asdf $TEST_CON"
78a1b24
        rlRun "rm asdf"
78a1b24
78a1b24
        # test directory
78a1b24
        rlRun "mkdir asdf"
78a1b24
        rlRun "checkFC asdf $FILE_CON"
78a1b24
        rlRun "restorecon asdf"
78a1b24
        rlRun "checkFC asdf $TEST_CON2"
78a1b24
        rlRun "rmdir asdf"
78a1b24
78a1b24
        # delete the context
78a1b24
        rlRun "semanage fcontext -d -f f '$DIR/asdf'"
78a1b24
        rlRun "semanage fcontext -d -f d '$DIR/asdf'"
78a1b24
78a1b24
        rlRun "popd"
78a1b24
        rlRun "rm -rf /tmp/test"
78a1b24
    rlPhaseEnd
78a1b24
78a1b24
    rlPhaseStartTest "chcon"
78a1b24
        rlRun "mkdir /tmp/test"
78a1b24
        rlRun "pushd /tmp/test"
78a1b24
78a1b24
        rlRun "touch asdf"
78a1b24
        rlRun "checkFC asdf $FILE_CON"
78a1b24
78a1b24
        rlRun "chcon -t $TEST_CON asdf"
78a1b24
        rlRun "checkFC asdf $TEST_CON"
78a1b24
78a1b24
        rlRun "popd"
78a1b24
        rlRun "rm -rf /tmp/test"
78a1b24
    rlPhaseEnd
78a1b24
78a1b24
    rlPhaseStartTest "restorecon"
78a1b24
        rlRun "mkdir /tmp/test"
78a1b24
        rlRun "pushd /tmp/test"
78a1b24
        DIR=$(pwd)
78a1b24
78a1b24
        # add a custom file context for whole directory
78a1b24
        rlRun "semanage fcontext -a -t $TEST_CON '$DIR/.*'"
78a1b24
78a1b24
        # create test files and directories
78a1b24
        rlRun "touch a"
78a1b24
        rlRun "checkFC a $FILE_CON"
78a1b24
        rlRun "mkdir dir"
78a1b24
        rlRun "checkFC dir $FILE_CON"
78a1b24
        rlRun "touch dir/a"
78a1b24
        rlRun "checkFC dir/a $FILE_CON"
78a1b24
        rlRun "touch dir/b"
78a1b24
        rlRun "checkFC dir/b $FILE_CON"
78a1b24
        rlRun "mkdir dir/dir"
78a1b24
        rlRun "checkFC dir/dir $FILE_CON"
78a1b24
        rlRun "touch dir/dir/a"
78a1b24
        rlRun "checkFC dir/dir/a $FILE_CON"
78a1b24
78a1b24
        function prepareStr {
78a1b24
            echo -n ".* $DIR/$1 .* [[:alnum:]:_]*$2:[[:alnum:]:_]*.*[[:alnum:]:_]*$3:[[:alnum:]:_]*"
78a1b24
        }
78a1b24
78a1b24
        # run restorecon in dry-run mode for a single file
78a1b24
        rlRun "restorecon -v -n dir/a >stdout"
78a1b24
        rlRun "cat stdout"
78a1b24
        rlAssertGrep "$(prepareStr dir/a $FILE_CON $TEST_CON)" stdout
78a1b24
        rlRun "[ $(cat stdout | wc -l) -eq 1 ]"
78a1b24
78a1b24
        # run restorecon in recursive mode
78a1b24
        rlRun "restorecon -r -v dir >stdout"
78a1b24
        rlRun "cat stdout"
78a1b24
        rlAssertGrep "$(prepareStr dir $FILE_CON $TEST_CON)" stdout
78a1b24
        rlAssertGrep "$(prepareStr dir/a $FILE_CON $TEST_CON)" stdout
78a1b24
        rlAssertGrep "$(prepareStr dir/b $FILE_CON $TEST_CON)" stdout
78a1b24
        rlAssertGrep "$(prepareStr dir/dir $FILE_CON $TEST_CON)" stdout
78a1b24
        rlAssertGrep "$(prepareStr dir/dir/a $FILE_CON $TEST_CON)" stdout
78a1b24
        rlRun "[ $(cat stdout | wc -l) -eq 5 ]"
78a1b24
78a1b24
        rlRun "checkFC dir $TEST_CON"
78a1b24
        rlRun "checkFC dir/a $TEST_CON"
78a1b24
        rlRun "checkFC dir/b $TEST_CON"
78a1b24
        rlRun "checkFC dir/dir $TEST_CON"
78a1b24
        rlRun "checkFC dir/dir/a $TEST_CON"
78a1b24
78a1b24
        # delete the custom context
78a1b24
        rlRun "semanage fcontext -d '$DIR/.*'"
78a1b24
78a1b24
        rlRun "popd"
78a1b24
        rlRun "rm -rf /tmp/test"
78a1b24
    rlPhaseEnd
78a1b24
78a1b24
    rlPhaseStartCleanup
78a1b24
        rlRun "rm -rf stdout"
78a1b24
    rlPhaseEnd
78a1b24
rlJournalPrintText
78a1b24
rlJournalEnd