#!/bin/bash # vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # runtest.sh of /CoreOS/libselinux/Sanity/selabel-functions # Description: Test selabel functions # Author: Jan Zarsky # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # Copyright (c) 2016 Red Hat, Inc. # # This program is free software: you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation, either version 2 of # the License, or (at your option) any later version. # # This program is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see http://www.gnu.org/licenses/. # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Include Beaker environment . /usr/bin/rhts-environment.sh || exit 1 . /usr/share/beakerlib/beakerlib.sh || exit 1 PACKAGE="libselinux" rlJournalStart rlPhaseStartSetup rlAssertRpm ${PACKAGE} rlAssertRpm ${PACKAGE}-devel rlAssertRpm "glibc" rlAssertRpm "gcc" if rlIsRHEL 6; then rlRun -l "gcc test_open.c -o test_open -lselinux -Wall -Wextra -std=c99 -DRHEL6" rlRun -l "gcc test_lookup.c -o test_lookup -lselinux -Wall -Wextra -std=c99 -DRHEL6" rlRun -l "gcc test_stats.c -o test_stats -lselinux -Wall -Wextra -std=c99 -DRHEL6" else rlRun -l "gcc test_open.c -o test_open -lselinux -Wall -Wextra -std=c99" rlRun -l "gcc test_lookup.c -o test_lookup -lselinux -Wall -Wextra -std=c99" rlRun -l "gcc test_partial.c -o test_partial -lselinux -Wall -Wextra -std=c99" rlRun -l "gcc test_best.c -o test_best -lselinux -Wall -Wextra -std=c99" rlRun -l "gcc test_stats.c -o test_stats -lselinux -Wall -Wextra -std=c99" rlRun -l "gcc test_digest.c -o test_digest -lselinux -Wall -Wextra -std=c99" fi rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory" rlPhaseEnd rlPhaseStartTest "selabel_open" # syntax: ./test_open BACKEND OPT_PATH OPT_SUBSET OPT_VALIDATE OPT_BASEONLY [nopt] rlLogInfo "Normal run" rlRun "./test_open CTX_FILE NULL NULL 0 0" rlLogInfo "Backends" rlRun "./test_open CTX_MEDIA NULL NULL 0 0" 0 rlRun "./test_open CTX_X NULL NULL 0 0" 0 rlRun "./test_open CTX_DB NULL NULL 0 0" 0 if rlIsRHEL "7" || rlIsFedora "<26"; then rlRun "./test_open CTX_ANDROID_PROP NULL NULL 0 0" 0 rlRun "./test_open 5 NULL NULL 0 0" 22 fi rlRun "./test_open 2147483647 NULL NULL 0 0" 22 rlLogInfo "Parameter nopt" rlRun "./test_open CTX_FILE NULL NULL 0 0 2147483647" 22,139 rlRun "./test_open CTX_MEDIA NULL NULL 0 0 2147483647" 22,139 rlRun "./test_open CTX_X NULL NULL 0 0 2147483647" 22,139 rlRun "./test_open CTX_DB NULL NULL 0 0 2147483647" 22,139 if rlIsRHEL "7" || rlIsFedora "<26"; then rlRun "./test_open CTX_ANDROID_PROP NULL NULL 0 0 2147483647" 22,139 fi rlRun "./test_open CTX_FILE NULL NULL 0 0 1" rlRun "./test_open CTX_MEDIA NULL NULL 0 0 1" rlRun "./test_open CTX_X NULL NULL 0 0 1" rlRun "./test_open CTX_DB NULL NULL 0 0 1" if rlIsRHEL "7" || rlIsFedora "<26"; then rlRun "./test_open CTX_ANDROID_PROP NULL NULL 0 0 1" fi rlRun "./test_open CTX_FILE NULL NULL 0 0 0" rlRun "./test_open CTX_MEDIA NULL NULL 0 0 0" rlRun "./test_open CTX_X NULL NULL 0 0 0" rlRun "./test_open CTX_DB NULL NULL 0 0 0" if rlIsRHEL "7" || rlIsFedora "<26"; then rlRun "./test_open CTX_ANDROID_PROP NULL NULL 0 0 0" fi rlRun "./test_open CTX_FILE NULL NULL 0 0 -1" 22,139 rlRun "./test_open CTX_MEDIA NULL NULL 0 0 -1" 22,139 rlRun "./test_open CTX_X NULL NULL 0 0 -1" 22,139 rlRun "./test_open CTX_DB NULL NULL 0 0 -1" 22,139 if rlIsRHEL "7" || rlIsFedora "<26"; then rlRun "./test_open CTX_ANDROID_PROP NULL NULL 0 0 -1" 22,139 fi rlLogInfo "Path option" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < >(tee output >&2)" 0 rlRun "grep 'line' output" 1 rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 0 else rlRun "./test_open CTX_FILE $TmpDir/my_contexts NULL 1 0 2> >(tee output >&2)" 22 fi rlRun "grep 'line 1 is missing fields' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 0 else rlRun "./test_open CTX_FILE $TmpDir/my_contexts NULL 1 0 2> >(tee output >&2)" 22 fi rlRun "grep 'line 1 has invalid context my_user_u:my_role_r:my_type_t:s0' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 0 else rlRun "./test_open CTX_FILE $TmpDir/my_contexts NULL 1 0 2> >(tee output >&2)" 22 fi rlRun "grep 'line 1 has invalid file type invalid_file_type' output" rlRun "cat > $TmpDir/my_contexts <> EOF" rlRun "./test_open CTX_FILE $TmpDir/my_contexts NULL 1 0 2> >(tee output >&2)" 0 rlRun "grep 'line 1' output" 1 rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 0 rlRun "grep 'Multiple same specifications' output" 1 rlLogInfo "Two same rules for the same path" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 22 rlRun "grep 'Multiple same specifications' output" rlLogInfo "Two different rules for the same path" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 22 rlRun "grep 'Multiple different specifications' output" rlLogInfo "Two different rules for same path but with different file type" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 0 rlRun "grep 'Multiple different specifications' output" 1 rlLogInfo "Two different rules for same path one general and one with file type" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 22 rlRun "grep 'Multiple different specifications' output" rlPhaseEnd if rlIsRHEL ">=7" || rlIsFedora; then rlPhaseStartTest "file contexts files" rlLogInfo "subs file" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts.subs < $TmpDir/my_contexts < $TmpDir/my_contexts.subs_dist < $TmpDir/my_contexts < $TmpDir/my_contexts.local < $TmpDir/my_contexts < $TmpDir/my_contexts.homedirs <>" rlRun "./test_lookup CTX_FILE NULL NULL 0 0 /tmp/somefile 0 2> >(tee output >&2)" 2 \ "Run selabel_lookup on file with default context <>" rlRun "grep 'selabel_lookup - ERROR: No such file or directory' output" 0 rlPhaseEnd fi rlPhaseStartTest "media contexts files" rlLogInfo "Valid entries" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'selabel_lookup - ERROR: No such file or directory' output" rlLogInfo "Invalid entries" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 is missing fields' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 22 rlRun "grep 'has invalid context some_u:some_r:some_t:s0' output" # defaultContext=$(cat /etc/selinux/targeted/contexts/removable_context) # rlLogInfo "empty contexts file" # rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 is missing fields' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 is missing fields' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 has invalid object type some_property' output" rlLogInfo "Wildcard matching" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts <=7" || rlIsFedora; then rlRun "cat >> $TmpDir/my_contexts <=7" || rlIsFedora; then rlRun "./test_lookup CTX_DB $TmpDir/my_contexts NULL 1 0 my_language DB_LANGUAGE | tee output" 0 rlRun "grep 'selabel_lookup context: system_u:object_r:sepgsql_lang_t:s0' output" rlRun "./test_lookup CTX_DB $TmpDir/my_contexts NULL 1 0 my_exception DB_EXCEPTION | tee output" 0 rlRun "grep 'selabel_lookup context: system_u:object_r:usr_t:s0' output" rlRun "./test_lookup CTX_DB $TmpDir/my_contexts NULL 1 0 my_datatype DB_DATATYPE | tee output" 0 rlRun "grep 'selabel_lookup context: system_u:object_r:bin_t:s0' output" fi rlLogInfo "Comments and empty lines" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 has invalid format' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 has invalid format' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 has invalid object type one' output" rlRun "cat > $TmpDir/my_contexts < >(tee output >&2)" 2 rlRun "grep 'line 1 has invalid format' output" rlLogInfo "Normal run" rlRun "./test_lookup CTX_DB NULL NULL 0 0 my_database DB_DATABASE | tee output" rlRun "grep 'selabel_lookup context: system_u:object_r:sepgsql_db_t:s0' output" rlRun "grep 'selabel_lookup_raw context: system_u:object_r:sepgsql_db_t:s0' output" rlPhaseEnd if rlIsRHEL ">=7" || rlIsFedora; then rlPhaseStartTest "baseonly option" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts.subs < $TmpDir/my_contexts.local < $TmpDir/my_contexts.homedirs < $TmpDir/my_contexts < $TmpDir/my_contexts <=7" || rlIsFedora; then rlPhaseStartTest "selabel_partial_match" # syntax: ./test_partial BACKEND OPT_PATH OPT_SUBSET OPT_VALIDATE OPT_BASEONLY path [nohandle] rlLogInfo "nonsupporting backends" rlRun "./test_partial CTX_MEDIA NULL NULL 0 0 /somedir | tee output" 0 rlRun "grep 'selabel_partial_match: true' output" 0 rlRun "./test_partial CTX_DB NULL NULL 0 0 /somedir | tee output" 0 rlRun "grep 'selabel_partial_match: true' output" 0 rlRun "./test_partial CTX_X NULL NULL 0 0 /somedir | tee output" 0 rlRun "grep 'selabel_partial_match: true' output" 0 rlLogInfo "null as handle" rlRun "./test_partial CTX_FILE NULL NULL 0 0 /somedir nohandle" 22,139 rlLogInfo "nonexisting entry" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts <=7" || rlIsFedora; then rlPhaseStartTest "selabel_best_match" # syntax: ./test_best BACKEND OPT_PATH OPT_SUBSET OPT_VALIDATE OPT_BASEONLY path mode [nohandle] rlLogInfo "nonsupported backends" rlRun "./test_best CTX_MEDIA NULL NULL 0 0 /somedir 0" 95 rlRun "./test_best CTX_DB NULL NULL 0 0 /somedir 0" 95 rlRun "./test_best CTX_X NULL NULL 0 0 /somedir 0" 95 rlLogInfo "null as handle" rlRun "./test_best CTX_FILE NULL NULL 0 0 /somedir 0 nohandle" 22,139 rlLogInfo "null as key" rlRun "./test_best CTX_FILE NULL NULL 0 0 NULL 0" 22 rlLogInfo "nonexisting entry" rlRun "cat > $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts < $TmpDir/my_contexts <=7" || rlIsFedora; then rlPhaseStartTest "selabel_digest" # syntax: ./test_digest BACKEND OPT_PATH OPT_VALIDATE OPT_DIGEST [nohandle] rlRun "./test_digest CTX_FILE NULL 0 0" 22 rlRun "./test_digest CTX_FILE NULL 0 0 nohandle" 139 rlRun "./test_digest CTX_FILE NULL 0 1" 0 rlRun "./test_digest CTX_MEDIA NULL 0 1" 0 rlRun "./test_digest CTX_X NULL 0 1" 0 rlRun "./test_digest CTX_DB NULL 0 1" 0 rlRun "cat > $TmpDir/my_contexts <