forked from OpenSCAP/openscap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common.sh.in
executable file
·195 lines (166 loc) · 4.57 KB
/
test_common.sh.in
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/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
PREFERRED_PYTHON=@preferred_python@
# Some of the tests rely on the "C" locale and would fail with some locales.
LC_ALL=C
export LC_ALL
if [ -z ${CUSTOM_OSCAP+x} ] ; then
enable_valgrind=@vgcheck@
if [ $enable_valgrind = "yes" ] ; then
actualdir=@abs_top_srcdir@
export actualdir
[ -z "$builddir" ] || export OSCAP="@abs_top_srcdir@/tests/valgrind_test.sh"
else
[ -z "$builddir" ] || export OSCAP=$(cd $builddir/utils/.libs; pwd)/oscap
fi
else
export OSCAP=${CUSTOM_OSCAP}
fi
export XMLDIFF="@abs_top_srcdir@/tests/xmldiff.pl"
if ! XPATH_ORIG=`command -v xpath 2>&1`; then
echo "I require xpath tool but it's not installed. Aborting." >&2
exit 1
fi
xpath_variant=$(perl -MXML::XPath -e 'print $XML::XPath::VERSION >= 1.34 ? "need_wrapper" : "standard"')
if [ "$xpath_variant" == "need_wrapper" ];
then
export XPATH_ORIG
xpath_wrapper() {
if [ "$#" == "1" ]; then
# read file from stdin
xpath_expr="$1"
"$XPATH_ORIG" -e "$xpath_expr"
elif [ "$#" == "2" ]; then
file="$1"
xpath_expr="$2"
"$XPATH_ORIG" -e "$xpath_expr" "$file"
else
echo "Parameters are not supported by xpath wrapper" >&2
exit 1
fi
}
export -f xpath_wrapper
export XPATH=xpath_wrapper
else
export XPATH="$XPATH_ORIG"
fi
# 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:[[:digit:]]\+:${TYPE}:${ID}\"" $CONTENT`
RES_ITEM=`grep "${FULLTYPE}_id=\"oval:[[:digit:]]\+:${TYPE}:${ID}\"" $RESULTS`
OVAL_ID=`echo ${CON_ITEM} | grep -o "oval:[[:digit:]]\+:${TYPE}:${ID}"`
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_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"')' 2>/dev/null)"
if [ "$real_cnt" != "$1" ]; then
echo "Failed: expected count: $1, real count: $real_cnt, xpath: '$2'"
return 1
fi
}
export -f assert_exists