1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/env bash
# Copyright 2009 Red Hat Inc., Durham, North Carolina.
# All Rights Reserved.
#
# OpenScap Testing Helpers.
#
# Authors:
# Ondrej Moris <omoris@redhat.com>
# Normalized path.
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
[ -z "$builddir" ] || export OSCAP=$(cd $builddir/utils/.libs; pwd)/oscap
export XMLDIFF=$(cd $(dirname $BASH_SOURCE); pwd)/xmldiff.pl
if ! XPATH=`command -v xpath 2>&1`; then
echo "I require xpath tool but it's not installed. Aborting." >&2
exit 1
fi
export XPATH
# Overall test result.
result=0
# Logging file (stderr is redirected here).
log=test.log
# Set-up testing environment.
function test_init {
[ $# -eq 1 ] && log="$1"
exec 2>$log
echo ""
echo "----------------------------------------------------------------------"
}
# Execute test and report its results.
function test_run {
printf "+ %-60s" "$1";
echo -e "TEST: $1" >&2;
shift
( exec 1>&2 ; eval "$@" )
ret_val=$?
if [ $ret_val -eq 0 ]; then
echo "[ PASS ]";
echo -e "RESULT: PASSED\n" >&2
return 0;
elif [ $ret_val -eq 1 ]; then
result=$[$result + $ret_val]
echo "[ FAIL ]";
echo -e "RESULT: FAILED\n" >&2
return 1;
elif [ $ret_val -eq 255 ]; then
echo "[ SKIP ]";
echo -e "RESULT: SKIPPED\n" >&2
return 0;
else
result=$[$result + $ret_val]
echo "[ WARN ]";
echo -e "RESULT: WARNING (unknown exist status $ret_val)\n" >&2
return 1;
fi
}
# Clean-up testing environment.
function test_exit {
echo "--------------------------------------------------"
echo -e "See `pwd | sed 's|.*/\(tests/.*\)|\1|'`/${log}.\n"
if [ $# -eq 1 ]
then
( exec 1>&2 ; eval "$@" )
fi
[ $result -eq 0 ] && exit 0
exit 1
}
# Check if requirements are in a path, use it as follows:
# require 'program' || return 255
function require {
eval "which $1 > /dev/null 2>&1"
if [ ! $? -eq 0 ]; then
echo -e "No '$1' found in $PATH!\n"
return 1; # Test is not applicable.
fi
return 0
}
# Check if probe exists, use it as follows:
# probecheck 'probe' || return 255
function probecheck {
if [ ! -f ${OVAL_PROBE_DIR}/probe_${1} ]; then
echo -e "Probe $1 does not exist!\n"
return 255; # Test is not applicable.
fi
return 0
}
function verify_results {
require "grep" || return 255
local ret_val=0;
local TYPE="$1"
local CONTENT="$2"
local RESULTS="$3"
local COUNT="$4"
local FULLTYPE="definition"
[ $TYPE == "tst" ] && FULLTYPE="test"
ID=1
while [ $ID -le $COUNT ]; do
CON_ITEM=`grep "id=\"oval:1:${TYPE}:${ID}\"" $CONTENT`
RES_ITEM=`grep "${FULLTYPE}_id=\"oval:1:${TYPE}:${ID}\"" $RESULTS`
if (echo $RES_ITEM | grep "result=\"true\"") >/dev/null; then
RES="TRUE"
elif (echo $RES_ITEM | grep "result=\"false\"" >/dev/null); then
RES="FALSE"
else
RES="ERROR"
fi
if (echo $CON_ITEM | grep "comment=\"true\"" >/dev/null); then
CMT="TRUE"
elif (echo $CON_ITEM | grep "comment=\"false\"" >/dev/null); then
CMT="FALSE"
else
CMT="ERROR"
fi
if [ ! $RES = $CMT ]; then
echo "Result of oval:1:${TYPE}:${ID} should be ${CMT} and is ${RES}"
ret_val=$[$ret_val + 1]
fi
ID=$[$ID+1]
done
return $([ $ret_val -eq 0 ])
}
assert_exists() {
real_cnt="$($XPATH $result 'count('"$2"')')"
if [ "$real_cnt" != "$1" ]; then
echo "Failed: expected count: $1, real count: $real_cnt, xpath: '$2'"
return 1
fi
}
export -f assert_exists
|