| 1 |
#!/usr/bin/env bash |
| 2 |
|
| 3 |
# Author: |
| 4 |
# Martin Preisler <[email protected]> |
| 5 |
|
| 6 |
set -e -o pipefail |
| 7 |
|
| 8 |
. $builddir/tests/test_common.sh |
| 9 |
. $srcdir/test_ds_common.sh |
| 10 |
|
| 11 |
# Test Cases. |
| 12 |
|
| 13 |
function test_eval { |
| 14 |
probecheck "rpminfo" || return 255 |
| 15 |
[ -e "/var/lib/rpm" ] || return 255 |
| 16 |
local stderr=$(mktemp -t ${name}.out.XXXXXX) |
| 17 |
$OSCAP xccdf eval "${srcdir}/$1" 2> $stderr |
| 18 |
diff /dev/null $stderr; rm $stderr |
| 19 |
} |
| 20 |
|
| 21 |
function test_eval_cpe { |
| 22 |
local stdout=$(mktemp -t ${name}.out.XXXXXX) |
| 23 |
local stderr=$(mktemp -t ${name}.err.XXXXXX) |
| 24 |
local ret=0 |
| 25 |
|
| 26 |
$OSCAP xccdf eval --progress "${srcdir}/$1" 1> $stdout 2> $stderr || ret=$? |
| 27 |
grep -q "rule_applicable_pass:pass" $stdout |
| 28 |
grep -q "rule_applicable_fail:fail" $stdout |
| 29 |
grep -q "rule_notapplicable:notapplicable" $stdout |
| 30 |
diff /dev/null $stderr |
| 31 |
rm $stdout $stderr |
| 32 |
} |
| 33 |
|
| 34 |
function test_invalid_eval { |
| 35 |
local ret=0 |
| 36 |
$OSCAP xccdf eval "${srcdir}/$1" || ret=$? |
| 37 |
return $([ $ret -eq 1 ]) |
| 38 |
} |
| 39 |
|
| 40 |
function test_invalid_oval_eval { |
| 41 |
local ret=0 |
| 42 |
$OSCAP oval eval "${srcdir}/$1" || ret=$? |
| 43 |
return $([ $ret -eq 1 ]) |
| 44 |
} |
| 45 |
|
| 46 |
function test_eval_id { |
| 47 |
|
| 48 |
OUT=$($OSCAP xccdf eval --datastream-id $2 --xccdf-id $3 "${srcdir}/$1") |
| 49 |
local RET=$? |
| 50 |
|
| 51 |
if [ $RET -ne 0 ]; then |
| 52 |
return 1 |
| 53 |
fi |
| 54 |
|
| 55 |
echo "$OUT" | grep $4 > /dev/null |
| 56 |
} |
| 57 |
|
| 58 |
function test_eval_benchmark_id { |
| 59 |
|
| 60 |
OUT=$($OSCAP xccdf eval --benchmark-id $2 "${srcdir}/$1") |
| 61 |
local RET=$? |
| 62 |
|
| 63 |
if [ $RET -ne 0 ]; then |
| 64 |
return 1 |
| 65 |
fi |
| 66 |
|
| 67 |
echo "$OUT" | grep $3 > /dev/null |
| 68 |
} |
| 69 |
|
| 70 |
function test_eval_complex() |
| 71 |
{ |
| 72 |
local name=${FUNCNAME} |
| 73 |
local arf=$(mktemp -t ${name}.arf.XXXXXX) |
| 74 |
local stderr=$(mktemp -t ${name}.err.XXXXXX) |
| 75 |
local stdout=$(mktemp -t ${name}.out.XXXXXX) |
| 76 |
|
| 77 |
$OSCAP xccdf eval \ |
| 78 |
--results-arf $arf \ |
| 79 |
--datastream-id scap_org.open-scap_datastream_tst2 \ |
| 80 |
--xccdf-id scap_org.open-scap_cref_second-xccdf.xml2 \ |
| 81 |
--profile xccdf_moc.elpmaxe.www_profile_2 \ |
| 82 |
$srcdir/eval_xccdf_id/sds-complex.xml 2> $stderr > $stdout |
| 83 |
|
| 84 |
# Ensure the sanity of the output. |
| 85 |
[ -f $stderr ]; [ ! -s $stderr ] |
| 86 |
[ "`grep ^Rule $stdout | wc -l`" == "1" ] |
| 87 |
grep ^Rule $stdout | grep xccdf_moc.elpmaxe.www_rule_secon |
| 88 |
rm $stdout |
| 89 |
|
| 90 |
# Ensure basic correctness of the ARF |
| 91 |
$OSCAP ds rds-validate $arf 2>&1 > $stderr |
| 92 |
[ -f $srderr ]; [ ! -s $stderr ]; rm $stderr |
| 93 |
assert_correct_xlinks $arf |
| 94 |
|
| 95 |
# Ensure that results are there |
| 96 |
local result="$arf" |
| 97 |
assert_exists 1 '//rule-result' |
| 98 |
assert_exists 1 '//rule-result[@idref="xccdf_moc.elpmaxe.www_rule_second"]' |
| 99 |
assert_exists 1 '//rule-result/result' |
| 100 |
assert_exists 1 '//rule-result/result[text()="pass"]' |
| 101 |
assert_exists 1 '//TestResult/benchmark[@href="#scap_org.open-scap_comp_second-xccdf.xml2"]' |
| 102 |
rm $arf |
| 103 |
} |
| 104 |
|
| 105 |
function test_oval_eval { |
| 106 |
|
| 107 |
$OSCAP oval eval "${srcdir}/$1" |
| 108 |
} |
| 109 |
|
| 110 |
function test_oval_eval_id { |
| 111 |
|
| 112 |
OUT=$($OSCAP oval eval --datastream-id $2 --oval-id $3 "${srcdir}/$1") |
| 113 |
local RET=$? |
| 114 |
|
| 115 |
if [ $RET -ne 0 ]; then |
| 116 |
return 1 |
| 117 |
fi |
| 118 |
echo "out: $OUT" |
| 119 |
|
| 120 |
echo "$OUT" | grep $4 > /dev/null |
| 121 |
} |
| 122 |
|
| 123 |
function test_sds_external_xccdf_in_ds { |
| 124 |
local SDS_FILE="${srcdir}/sds_external_xccdf/sds.ds.xml" |
| 125 |
local XCCDF="scap_org.open-scap_cref_xccdf.xml" |
| 126 |
local PROFILE="xccdf_external_profile_datastream_1" |
| 127 |
local result="$(mktemp)" |
| 128 |
|
| 129 |
$OSCAP xccdf eval --xccdf-id "$XCCDF" --profile "$PROFILE" --results-arf "$result" "$SDS_FILE" |
| 130 |
|
| 131 |
assert_exists 1 '//rule-result/result[text()="pass"]' |
| 132 |
assert_exists 1 '//TestResult/benchmark[@href="file:xccdf.sds.xml#scap_1_comp_xccdf.xml"]' |
| 133 |
|
| 134 |
rm -f "$result" |
| 135 |
} |
| 136 |
|
| 137 |
function test_sds_external_xccdf { |
| 138 |
local SDS_FILE="${srcdir}/sds_external_xccdf/sds.ds.xml" |
| 139 |
local XCCDF="scap_org.open-scap_cref_xccdf-file.xml" |
| 140 |
local PROFILE="xccdf_external_profile_file_1" |
| 141 |
local result="$(mktemp)" |
| 142 |
|
| 143 |
$OSCAP xccdf eval --xccdf-id "$XCCDF" --profile "$PROFILE" --results-arf "$result" "$SDS_FILE" |
| 144 |
|
| 145 |
assert_exists 1 '//rule-result/result[text()="pass"]' |
| 146 |
assert_exists 1 '//TestResult/benchmark[@href="file:xccdf.xml"]' |
| 147 |
|
| 148 |
rm -f "$result" |
| 149 |
} |
| 150 |
|
| 151 |
function test_sds_tailoring { |
| 152 |
local SDS_FILE="${srcdir}/$2" |
| 153 |
local DATASTREAM_ID="$3" |
| 154 |
local TAILORING_ID="$4" |
| 155 |
local PROFILE="$5" |
| 156 |
local result=$(mktemp) |
| 157 |
|
| 158 |
$OSCAP info "$SDS_FILE" |
| 159 |
|
| 160 |
$OSCAP xccdf eval --datastream-id "$DATASTREAM_ID" --tailoring-id "$TAILORING_ID" --profile "$PROFILE" --results "$result" "$SDS_FILE" |
| 161 |
|
| 162 |
assert_exists 2 '//Rule' |
| 163 |
assert_exists 1 '//Rule[@id="xccdf_com.example_rule_1" and @selected="true"]' |
| 164 |
assert_exists 1 '//Rule[@id="xccdf_com.example_rule_2" and @selected="false"]' |
| 165 |
assert_exists 2 '//rule-result' |
| 166 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_1"]' |
| 167 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_2"]' |
| 168 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_1"]/result[text()="notselected"]' |
| 169 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_2"]/result[text()="pass"]' |
| 170 |
|
| 171 |
rm -f "$result" |
| 172 |
} |
| 173 |
|
| 174 |
function test_ds_continue_without_remote_resources() { |
| 175 |
local DS="${srcdir}/$1" |
| 176 |
local PROFILE="$2" |
| 177 |
local result=$(mktemp) |
| 178 |
local oval_result="test_single_rule.oval.xml.result.xml" |
| 179 |
|
| 180 |
$OSCAP xccdf eval --oval-results --profile "$PROFILE" --results "$result" "$DS" |
| 181 |
|
| 182 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_test-pass"]/result[text()="pass"]' |
| 183 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_test-remote_res"]/result[text()="notchecked"]' |
| 184 |
assert_exists 1 '//rule-result[@idref="xccdf_com.example.www_rule_test-pass2"]/result[text()="pass"]' |
| 185 |
|
| 186 |
rm -f "$result" "$oval_result" |
| 187 |
} |
| 188 |
|
| 189 |
function test_ds_error_remote_resources() { |
| 190 |
# --fetch-remote-resources uses internet |
| 191 |
require_internet || return 255 |
| 192 |
|
| 193 |
local DS="${srcdir}/$1" |
| 194 |
local PROFILE="$2" |
| 195 |
local result=$(mktemp) |
| 196 |
local stderr=$(mktemp) |
| 197 |
|
| 198 |
$OSCAP xccdf eval --fetch-remote-resources --profile "$PROFILE" --results "$result" "$DS" 2>"$stderr" || ret=$? |
| 199 |
grep -q "Downloading: https://www.example.com/security/data/oval/oval.xml.bz2 ... error" "$stderr" |
| 200 |
grep -q "OpenSCAP Error: Download failed" "$stderr" |
| 201 |
|
| 202 |
rm -f "$result" "$stderr" |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
# Testing. |
| 207 |
test_init |
| 208 |
|
| 209 |
test_run "sds_external_xccdf_in_ds" test_sds_external_xccdf_in_ds |
| 210 |
test_run "sds_external_xccdf" test_sds_external_xccdf |
| 211 |
test_run "sds_tailoring" test_sds_tailoring sds_tailoring sds_tailoring/sds.ds.xml scap_com.example_datastream_with_tailoring xccdf_com.example_cref_tailoring_01 xccdf_com.example_profile_tailoring |
| 212 |
|
| 213 |
test_run "eval_simple" test_eval eval_simple/sds.xml |
| 214 |
test_run "cpe_in_ds" test_eval cpe_in_ds/sds.xml |
| 215 |
test_run "eval_invalid" test_invalid_eval eval_invalid/sds.xml |
| 216 |
test_run "eval_invalid_oval" test_invalid_oval_eval eval_invalid/sds-oval.xml |
| 217 |
test_run "eval_xccdf_id1" test_eval_id eval_xccdf_id/sds.xml scap_org.open-scap_datastream_tst scap_org.open-scap_cref_first-xccdf.xml first |
| 218 |
test_run "eval_xccdf_id2" test_eval_id eval_xccdf_id/sds.xml scap_org.open-scap_datastream_tst scap_org.open-scap_cref_second-xccdf.xml second |
| 219 |
test_run "eval_benchmark_id1" test_eval_benchmark_id eval_xccdf_id/sds.xml xccdf_moc.elpmaxe.www_benchmark_first first |
| 220 |
test_run "eval_benchmark_id2" test_eval_benchmark_id eval_xccdf_id/sds.xml xccdf_moc.elpmaxe.www_benchmark_second second |
| 221 |
test_run "eval_benchmark_id_conflict" test_eval_benchmark_id eval_benchmark_id_conflict/sds.xml xccdf_moc.elpmaxe.www_benchmark_first first |
| 222 |
test_run "eval_just_oval" test_oval_eval eval_just_oval/sds.xml |
| 223 |
test_run "eval_oval_id1" test_oval_eval_id eval_oval_id/sds.xml scap_org.open-scap_datastream_just_oval scap_org.open-scap_cref_scap-oval1.xml "oval:x:def:1" |
| 224 |
test_run "eval_oval_id2" test_oval_eval_id eval_oval_id/sds.xml scap_org.open-scap_datastream_just_oval scap_org.open-scap_cref_scap-oval2.xml "oval:x:def:2" |
| 225 |
test_run "eval_cpe" test_eval_cpe eval_cpe/sds.xml |
| 226 |
|
| 227 |
test_run "test_eval_complex" test_eval_complex |
| 228 |
test_run "test_ds_1_2_continue_without_remote_resources" test_ds_continue_without_remote_resources ds_continue_without_remote_resources/remote_content_1.2.ds.xml xccdf_com.example.www_profile_test_remote_res |
| •229 |
#test_run "test_ds_1_2_error_remote_resources" test_ds_error_remote_resources ds_continue_without_remote_resources/remote_content_1.2.ds.xml xccdf_com.example.www_profile_test_remote_res |
| 230 |
test_run "test_ds_1_3_continue_without_remote_resources" test_ds_continue_without_remote_resources ds_continue_without_remote_resources/remote_content_1.3.ds.xml xccdf_com.example.www_profile_test_remote_res |
| •231 |
#test_run "test_ds_1_3_error_remote_resources" test_ds_error_remote_resources ds_continue_without_remote_resources/remote_content_1.3.ds.xml xccdf_com.example.www_profile_test_remote_res |
| 232 |
|
| 233 |
test_exit |
| 234 |
|