| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 4 |
# All Rights Reserved. |
| 5 |
# |
| 6 |
# OpenScap Testing Helpers. |
| 7 |
# |
| 8 |
# Authors: |
| 9 |
# Ondrej Moris <[email protected]> |
| 10 |
|
| 11 |
# Normalized path. |
| 12 |
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin |
| 13 |
|
| 14 |
PREFERRED_PYTHON=@PREFERRED_PYTHON_PATH@ |
| 15 |
|
| 16 |
# Some of the tests rely on the "C" locale and would fail with some locales. |
| 17 |
LC_ALL=C |
| 18 |
export LC_ALL |
| 19 |
|
| 20 |
MALLOC_CHECK_=3 |
| 21 |
export MALLOC_CHECK_ |
| 22 |
|
| 23 |
OSCAP_FULL_VALIDATION=1 |
| 24 |
export OSCAP_FULL_VALIDATION |
| 25 |
|
| 26 |
if [ -z ${CUSTOM_OSCAP+x} ] ; then |
| 27 |
export OSCAP_EXEC="@CMAKE_BINARY_DIR@/utils/oscap" |
| 28 |
export RUN_WRAPPER="@CMAKE_BINARY_DIR@/run" |
| 29 |
enable_valgrind="@ENABLE_VALGRIND@" |
| 30 |
if [ $enable_valgrind != "OFF" ] ; then |
| 31 |
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP="@CMAKE_SOURCE_DIR@/tests/valgrind_test.sh" |
| 32 |
else |
| 33 |
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP="bash $RUN_WRAPPER $OSCAP_EXEC" |
| 34 |
fi |
| 35 |
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP_CHROOTABLE_EXEC="@CMAKE_BINARY_DIR@/utils/oscap-chrootable" |
| 36 |
[ -z "@CMAKE_BINARY_DIR@" ] || export OSCAP_CHROOTABLE="bash @CMAKE_BINARY_DIR@/run $OSCAP_CHROOTABLE_EXEC" |
| 37 |
else |
| 38 |
export OSCAP=${CUSTOM_OSCAP} |
| 39 |
fi |
| 40 |
|
| 41 |
export XMLDIFF="@CMAKE_SOURCE_DIR@/tests/xmldiff.pl" |
| 42 |
|
| 43 |
if ! XPATH_ORIG=`command -v xpath 2>&1`; then |
| 44 |
echo "I require xpath tool but it's not installed. Aborting." >&2 |
| 45 |
exit 1 |
| 46 |
fi |
| 47 |
|
| 48 |
xpath_variant=$(perl -MXML::XPath -e 'print $XML::XPath::VERSION >= 1.34 ? "need_wrapper" : "standard"') |
| 49 |
|
| 50 |
if [ "$xpath_variant" == "need_wrapper" ]; |
| 51 |
then |
| 52 |
export XPATH_ORIG |
| 53 |
xpath_wrapper() { |
| 54 |
if [ "$#" == "1" ]; then |
| 55 |
# read file from stdin |
| 56 |
xpath_expr="$1" |
| 57 |
"$XPATH_ORIG" -e "$xpath_expr" |
| 58 |
elif [ "$#" == "2" ]; then |
| 59 |
file="$1" |
| 60 |
xpath_expr="$2" |
| 61 |
"$XPATH_ORIG" -e "$xpath_expr" "$file" |
| 62 |
else |
| 63 |
echo "Parameters are not supported by xpath wrapper" >&2 |
| 64 |
exit 1 |
| 65 |
fi |
| 66 |
} |
| 67 |
export -f xpath_wrapper |
| 68 |
export XPATH=xpath_wrapper |
| 69 |
else |
| 70 |
export XPATH="$XPATH_ORIG" |
| 71 |
fi |
| 72 |
|
| 73 |
# Overall test result. |
| 74 |
result=0 |
| 75 |
|
| 76 |
# Set-up testing environment. |
| 77 |
function test_init { |
| 78 |
: |
| 79 |
} |
| 80 |
|
| 81 |
# Execute test and report its results. |
| 82 |
function test_run { |
| 83 |
printf "+ %-60s\n" "$1"; |
| 84 |
echo -e "TEST: $1" >&2; |
| 85 |
shift |
| 86 |
( exec 1>&2 ; eval "$@" ) |
| 87 |
ret_val=$? |
| 88 |
if [ $ret_val -eq 0 ]; then |
| 89 |
echo -e "RESULT: PASSED\n" >&2 |
| 90 |
return 0; |
| 91 |
elif [ $ret_val -eq 1 ]; then |
| 92 |
result=$(($result + $ret_val)) |
| 93 |
echo -e "RESULT: FAILED\n" >&2 |
| 94 |
return 1; |
| 95 |
elif [ $ret_val -eq 255 ]; then |
| 96 |
echo -e "RESULT: SKIPPED\n" >&2 |
| 97 |
return 0; |
| 98 |
else |
| 99 |
result=$(($result + $ret_val)) |
| 100 |
echo -e "RESULT: WARNING (unknown exist status $ret_val)\n" >&2 |
| 101 |
return 1; |
| 102 |
fi |
| 103 |
} |
| 104 |
|
| 105 |
# Clean-up testing environment. |
| 106 |
function test_exit { |
| 107 |
if [ $# -eq 1 ] |
| 108 |
then |
| 109 |
( exec 1>&2 ; eval "$@" ) |
| 110 |
fi |
| 111 |
|
| 112 |
[ $result -eq 0 ] && exit 0 |
| 113 |
exit 1 |
| 114 |
} |
| 115 |
|
| 116 |
# Check if requirements are in a path, use it as follows: |
| 117 |
# require 'program' || return 255 |
| 118 |
function require { |
| 119 |
eval "which $1 > /dev/null 2>&1" |
| 120 |
if [ ! $? -eq 0 ]; then |
| 121 |
echo -e "No '$1' found in $PATH!\n" |
| 122 |
return 1; # Test is not applicable. |
| 123 |
fi |
| 124 |
return 0 |
| 125 |
} |
| 126 |
|
| 127 |
# Check if Python module is available. For example: |
| 128 |
# require_python_module 'dbusmock' || return 255 |
| 129 |
function require_python_module { |
| 130 |
eval "python3 -c \"import $1\" >/dev/null 2>&1" |
| 131 |
if [ $? -gt 0 ]; then |
| 132 |
echo -e "No '$1' Python module found!\n" |
| 133 |
return 255 # Test is not applicable. |
| 134 |
fi |
| 135 |
} |
| 136 |
|
| 137 |
# Check if D-Bus service is present. Arguments are: |
| 138 |
# Args: |
| 139 |
# $1: 'system' or 'session' keyword |
| 140 |
# $2: Bus name (e.g. 'org.domain.foo') |
| 141 |
# $3: Object (e.g. '/') |
| 142 |
# require_dbus 'system' 'org.freedesktop.fwupd' '/' || return 255 |
| 143 |
function require_dbus { |
| 144 |
require "gdbus" || return 255 |
| 145 |
eval "gdbus introspect --$1 -d $2 -o $3 >/dev/null 2>&1" |
| 146 |
if [ $? -gt 0 ]; then |
| 147 |
echo -e "No '$2' ($1) D-Bus bus found!\n" |
| 148 |
return 255 # Test is not applicable. |
| 149 |
fi |
| 150 |
} |
| 151 |
|
| 152 |
# Check if probe exists, use it as follows: |
| 153 |
# probecheck 'probe' || return 255 |
| 154 |
function probecheck { |
| 155 |
if ! $OSCAP --version | grep "\<"$1"\>" >/dev/null ; then |
| 156 |
echo -e "Probe $1 does not exist!\n" |
| 157 |
return 255 # Test is not applicable. |
| 158 |
fi |
| 159 |
return 0 |
| 160 |
} |
| 161 |
|
| 162 |
# Check for package names and return a version number |
| 163 |
function package_version { |
| 164 |
# loop through multiple potential package names |
| 165 |
# return first version number found |
| 166 |
for package in $@; do |
| 167 |
ver="" |
| 168 |
|
| 169 |
# check rpm for package version first |
| 170 |
if [ -f "/usr/bin/rpm" ]; then |
| 171 |
ver=$(rpm -q $package --qf="%{version}" 2> /dev/null) |
| 172 |
|
| 173 |
# rpm returns error messages on stdout, check return code |
| 174 |
if [ ! "$?" -eq "0" ]; then |
| 175 |
ver="" |
| 176 |
fi |
| 177 |
fi |
| 178 |
|
| 179 |
# fall back to dpkg for debian systems |
| 180 |
if [ "${ver}" == "" ] && [ -f "/usr/bin/dpkg-query" ]; then |
| 181 |
# for Debian-based systems, return the upstream version |
| 182 |
ver="$(dpkg-query -f '${source:Upstream-Version}' -W $package 2> /dev/null)" |
| 183 |
fi |
| 184 |
|
| 185 |
# return the first match found |
| 186 |
if [ "${ver}" != "" ]; then |
| 187 |
echo "${ver}" |
| 188 |
return 0 |
| 189 |
fi |
| 190 |
done |
| 191 |
|
| 192 |
# package not found |
| 193 |
if [ "${ver}" == "" ]; then |
| 194 |
return 255 |
| 195 |
fi |
| 196 |
} |
| 197 |
|
| 198 |
function require_internet { |
| 199 |
[ -s /etc/resolv.conf ] || return 255 |
| 200 |
} |
| 201 |
|
| 202 |
function require_non_chroot { |
| 203 |
awk 'BEGIN{exit_code=1} $2 == "/" {exit_code=0} END{exit exit_code}' /proc/mounts || return 255 |
| 204 |
} |
| 205 |
|
| 206 |
function verify_results { |
| 207 |
|
| 208 |
require "grep" || return 255 |
| 209 |
|
| 210 |
local ret_val=0; |
| 211 |
local TYPE="$1" |
| 212 |
local CONTENT="$2" |
| 213 |
local RESULTS="$3" |
| 214 |
local COUNT="$4" |
| 215 |
local FULLTYPE="definition" |
| 216 |
|
| 217 |
[ $TYPE == "tst" ] && FULLTYPE="test" |
| 218 |
|
| 219 |
ID=1 |
| 220 |
while [ $ID -le $COUNT ]; do |
| 221 |
|
| 222 |
CON_ITEM=`grep "id=\"oval:[[:digit:]]\+:${TYPE}:${ID}\"" $CONTENT` |
| 223 |
RES_ITEM=`grep "${FULLTYPE}_id=\"oval:[[:digit:]]\+:${TYPE}:${ID}\"" $RESULTS` |
| 224 |
OVAL_ID=`echo ${CON_ITEM} | grep -o "oval:[[:digit:]]\+:${TYPE}:${ID}"` |
| 225 |
if (echo $RES_ITEM | grep "result=\"true\"") >/dev/null; then |
| 226 |
RES="TRUE" |
| 227 |
elif (echo $RES_ITEM | grep "result=\"false\"" >/dev/null); then |
| 228 |
RES="FALSE" |
| 229 |
else |
| 230 |
RES="ERROR" |
| 231 |
fi |
| 232 |
|
| 233 |
if (echo $CON_ITEM | grep "comment=\"true\"" >/dev/null); then |
| 234 |
CMT="TRUE" |
| 235 |
elif (echo $CON_ITEM | grep "comment=\"false\"" >/dev/null); then |
| 236 |
CMT="FALSE" |
| 237 |
else |
| 238 |
CMT="ERROR" |
| 239 |
fi |
| 240 |
|
| 241 |
if [ ! $RES = $CMT ]; then |
| 242 |
echo "Result of ${OVAL_ID} should be ${CMT} and is ${RES}" |
| 243 |
ret_val=$(($ret_val + 1)) |
| 244 |
fi |
| 245 |
|
| 246 |
ID=$(($ID+1)) |
| 247 |
done |
| 248 |
|
| 249 |
return $([ $ret_val -eq 0 ]) |
| 250 |
} |
| 251 |
|
| 252 |
assert_exists() { |
| 253 |
real_cnt="$($XPATH $result 'count('"$2"')' 2>/dev/null)" |
| 254 |
if [ "$real_cnt" != "$1" ]; then |
| 255 |
echo "Failed: expected count: $1, real count: $real_cnt, xpath: '$2'" |
| 256 |
return 1 |
| 257 |
fi |
| 258 |
} |
| 259 |
|
| 260 |
# $1: The chroot directory |
| 261 |
set_chroot_offline_test_mode() { |
| 262 |
if test -n "$_OSCAP_BEFORE"; then |
| 263 |
echo "Already in offline test mode!" >&2 |
| 264 |
return |
| 265 |
fi |
| 266 |
if test -x "$OSCAP_CHROOTABLE_EXEC"; then |
| 267 |
if ! getcap "$OSCAP_CHROOTABLE_EXEC" | grep -q -P 'cap_sys_chroot[=+]+ep'; then |
| 268 |
echo "Skipping test '${FUNCNAME[1]}' as '$OSCAP_CHROOTABLE_EXEC' doesn't have the chroot capability." >&2 |
| 269 |
return 255 |
| 270 |
fi |
| 271 |
_OSCAP_BEFORE="$OSCAP" |
| 272 |
OSCAP="$OSCAP_CHROOTABLE" |
| 273 |
elif test $(id -u) -eq 0; then |
| 274 |
: # Running offline tests as root is acceptable too |
| 275 |
else |
| 276 |
echo "Skipping test '${FUNCNAME[1]}' as '$OSCAP_CHROOTABLE_EXEC' oscap which is supposed to have chroot capability doesn't exist." >&2 |
| 277 |
return 255 |
| 278 |
fi |
| 279 |
set_offline_chroot_dir "$1" |
| 280 |
return 0 |
| 281 |
} |
| 282 |
|
| 283 |
# $1: The chroot directory. If empty, unset the OSCAP_PROBE_ROOT variable |
| 284 |
set_offline_chroot_dir() { |
| 285 |
if test -n "$1"; then |
| 286 |
export OSCAP_PROBE_ROOT="$1" |
| 287 |
else |
| 288 |
unset OSCAP_PROBE_ROOT |
| 289 |
fi |
| 290 |
} |
| 291 |
|
| 292 |
unset_chroot_offline_test_mode() { |
| 293 |
if test -n "$_OSCAP_BEFORE"; then |
| 294 |
OSCAP="$_OSCAP_BEFORE" |
| 295 |
_OSCAP_BEFORE= |
| 296 |
fi |
| 297 |
set_offline_chroot_dir "" |
| 298 |
} |
| 299 |
|
| 300 |
# Wrapper functions to deal with differences across platforms |
| 301 |
make_temp_dir() { |
| 302 |
PARENT_DIR=${1} |
| 303 |
TEMP_NAME=${2} |
| 304 |
|
| 305 |
[ -z "$PARENT_DIR" ] && PARENT_DIR=$(pwd) |
| 306 |
[ -z "$TEMP_NAME" ] && TEMP_NAME="tmp" |
| 307 |
|
| 308 |
TEMP_NAME="${TEMP_NAME}.XXXXXX" |
| 309 |
|
| 310 |
case $(uname) in |
| 311 |
FreeBSD) |
| 312 |
mktemp -d ${PARENT_DIR}/${TEMP_NAME} |
| 313 |
;; |
| 314 |
*) |
| 315 |
mktemp -d -p ${PARENT_DIR} -t ${TEMP_NAME} |
| 316 |
;; |
| 317 |
esac |
| 318 |
} |
| 319 |
|
| 320 |
make_temp_file() { |
| 321 |
PARENT_DIR=${1} |
| 322 |
TEMP_NAME=${2} |
| 323 |
|
| 324 |
[ -z "$PARENT_DIR" ] && PARENT_DIR=$(pwd) |
| 325 |
[ -z "$TEMP_NAME" ] && TEMP_NAME="tmp" |
| 326 |
|
| 327 |
TEMP_NAME="${TEMP_NAME}.XXXXXX" |
| 328 |
|
| 329 |
case $(uname) in |
| 330 |
FreeBSD) |
| 331 |
mktemp ${PARENT_DIR}/${TEMP_NAME} |
| 332 |
;; |
| 333 |
*) |
| 334 |
mktemp -p ${PARENT_DIR} -t ${TEMP_NAME} |
| 335 |
;; |
| 336 |
esac |
| 337 |
} |
| 338 |
|
| 339 |
xsed() { |
| 340 |
case $(uname) in |
| 341 |
FreeBSD) |
| 342 |
gsed "$@" |
| 343 |
;; |
| 344 |
*) |
| 345 |
sed "$@" |
| 346 |
;; |
| 347 |
esac |
| 348 |
} |
| 349 |
|
| 350 |
# Args: |
| 351 |
# $1: The text string to print to stderr |
| 352 |
# $2: The return code (optional, default is 1) |
| 353 |
log_and_fail() { |
| 354 |
printf '%s' "$1" >&2 |
| 355 |
return ${2:-1} |
| 356 |
} |
| 357 |
|
| 358 |
# Args: Same as with log_and_fail |
| 359 |
die() { |
| 360 |
log_and_fail "$1" "$2" |
| 361 |
exit $? |
| 362 |
} |
| 363 |
|
| 364 |
# See https://github.com/martinpitt/python-dbusmock |
| 365 |
# Args: |
| 366 |
# $1: The name of the mock template file without extension. |
| 367 |
init_dbus_mock() { |
| 368 |
require_python_module "dbusmock" || return 255 |
| 369 |
require "gdbus" || return 255 |
| 370 |
printf "%s\n" "Initializing D-Bus mock from template: $1.py" |
| 371 |
python3 -m dbusmock --template "${srcdir}/$1.py" & |
| 372 |
# We have to replace system bus address because mock D-Bus |
| 373 |
# endpoint is created inside the user session |
| 374 |
export DBUS_SYSTEM_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS |
| 375 |
} |
| 376 |
|
| 377 |
# Args: |
| 378 |
# $1: The name of the mock service bus |
| 379 |
clean_dbus_mock() { |
| 380 |
require "gdbus" || return 255 |
| 381 |
printf "%s\n" "Shutting down D-Bus mock: $1" |
| 382 |
gdbus call --session -d "$1" -o / -m "$1.Exit" >/dev/null 2>&1 || true |
| 383 |
export DBUS_SYSTEM_BUS_ADDRESS= |
| 384 |
} |
| 385 |
|
| 386 |
export -f assert_exists |
| 387 |
|
| 388 |
export OPENSCAP_ENABLE_MD5="@OPENSCAP_ENABLE_MD5@" |
| 389 |
export OPENSCAP_ENABLE_SHA1="@OPENSCAP_ENABLE_SHA1@" |