| 1 | #!/usr/bin/envscip-shell filesystem . . `/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_evalscip-shell shell shell-project 0.0.0 test_eval(). { |
| 14 | probecheck "rpminfo" || return 255 |
| 15 | [scip-shell system . . `[`. -e "/var/lib/rpmscip-shell filesystem . . `/var/lib/rpm`." ] || return 255 |
| 16 | local stderr=$(mktempscip-shell system . . mktemp. -t ${name}.out.XXXXXX) |
| 17 | $OSCAP xccdf eval "${srcdir}/$1" 2> $stderr |
| 18 | diffscip-shell system . . diff. /dev/nullscip-shell filesystem . . `/dev/null`. $stderr; rmscip-shell system . . rm. $stderr |
| 19 | } |
| 20 | |
| 21 | function test_eval_cpescip-shell shell shell-project 0.0.0 test_eval_cpe(). { |
| 22 | local stdout=$(mktempscip-shell system . . mktemp. -t ${name}.out.XXXXXX) |
| 23 | local stderr=$(mktempscip-shell system . . mktemp. -t ${name}.err.XXXXXX) |
| 24 | local ret=0 |
| 25 | |
| 26 | $OSCAP xccdf eval --progress "${srcdir}/$1" 1> $stdout 2> $stderr || ret=$? |
| 27 | grepscip-shell system . . grep. -q "rule_applicable_pass:pass" $stdout |
| 28 | grepscip-shell system . . grep. -q "rule_applicable_fail:fail" $stdout |
| 29 | grepscip-shell system . . grep. -q "rule_notapplicable:notapplicable" $stdout |
| 30 | diffscip-shell system . . diff. /dev/nullscip-shell filesystem . . `/dev/null`. $stderr |
| 31 | rmscip-shell system . . rm. $stdout $stderr |
| 32 | } |
| 33 | |
| 34 | function test_invalid_evalscip-shell shell shell-project 0.0.0 test_invalid_eval(). { |
| 35 | local ret=0 |
| 36 | $OSCAP xccdf eval "${srcdir}/$1" || ret=$? |
| 37 | return $([scip-shell system . . `[`. $ret -eq 1 ]) |
| 38 | } |
| 39 | |
| 40 | function test_invalid_oval_evalscip-shell shell shell-project 0.0.0 test_invalid_oval_eval(). { |
| 41 | local ret=0 |
| 42 | $OSCAP oval eval "${srcdir}/$1" || ret=$? |
| 43 | return $([scip-shell system . . `[`. $ret -eq 1 ]) |
| 44 | } |
| 45 | |
| 46 | function test_eval_idscip-shell shell shell-project 0.0.0 test_eval_id(). { |
| 47 | |
| 48 | OUT=$($OSCAP xccdf eval --datastream-id $2 --xccdf-id $3 "${srcdir}/$1") |
| 49 | local RET=$? |
| 50 | |
| 51 | if [scip-shell system . . `[`. $RET -ne 0 ]; then |
| 52 | return 1 |
| 53 | fi |
| 54 | |
| 55 | echoscip-shell system . . echo. "$OUT" | grepscip-shell system . . grep. $4 > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 56 | } |
| 57 | |
| 58 | function test_eval_benchmark_idscip-shell shell shell-project 0.0.0 test_eval_benchmark_id(). { |
| 59 | |
| 60 | OUT=$($OSCAP xccdf eval --benchmark-id $2 "${srcdir}/$1") |
| 61 | local RET=$? |
| 62 | |
| 63 | if [scip-shell system . . `[`. $RET -ne 0 ]; then |
| 64 | return 1 |
| 65 | fi |
| 66 | |
| 67 | echoscip-shell system . . echo. "$OUT" | grepscip-shell system . . grep. $3 > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 68 | } |
| 69 | |
| 70 | function test_eval_complexscip-shell shell shell-project 0.0.0 test_eval_complex().() |
| 71 | { |
| 72 | local name=${FUNCNAME} |
| 73 | local arf=$(mktempscip-shell system . . mktemp. -t ${name}.arf.XXXXXX) |
| 74 | local stderr=$(mktempscip-shell system . . mktemp. -t ${name}.err.XXXXXX) |
| 75 | local stdout=$(mktempscip-shell system . . 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 | [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ] |
| 86 | [scip-shell system . . `[`. "`grepscip-shell system . . grep. ^Rule $stdout | wcscip-shell system . . wc. -l`" == "1" ] |
| 87 | grepscip-shell system . . grep. ^Rule $stdout | grepscip-shell system . . grep. xccdf_moc.elpmaxe.www_rule_secon |
| 88 | rmscip-shell system . . rm. $stdout |
| 89 | |
| 90 | # Ensure basic correctness of the ARF |
| 91 | $OSCAP ds rds-validate $arf 2>&1 > $stderr |
| 92 | [scip-shell system . . `[`. -f $srderr ]; [scip-shell system . . `[`. ! -s $stderr ]; rmscip-shell system . . 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[@idref="xccdf_moc.elpmaxe.www_rule_second" and @role="full"]' |
| 100 | assert_exists 1 '//rule-result[@idref="xccdf_moc.elpmaxe.www_rule_second" and @severity="unknown"]' |
| 101 | assert_exists 1 '//rule-result[@idref="xccdf_moc.elpmaxe.www_rule_second" and @weight]' |
| 102 | assert_exists 1 '//rule-result/result' |
| 103 | assert_exists 1 '//rule-result/result[text()="pass"]' |
| 104 | rmscip-shell system . . rm. $arf |
| 105 | } |
| 106 | |
| 107 | function test_oval_evalscip-shell shell shell-project 0.0.0 test_oval_eval(). { |
| 108 | |
| 109 | $OSCAP oval eval "${srcdir}/$1" |
| 110 | } |
| 111 | |
| 112 | function test_oval_eval_idscip-shell shell shell-project 0.0.0 test_oval_eval_id(). { |
| 113 | |
| 114 | OUT=$($OSCAP oval eval --datastream-id $2 --oval-id $3 "${srcdir}/$1") |
| 115 | local RET=$? |
| 116 | |
| 117 | if [scip-shell system . . `[`. $RET -ne 0 ]; then |
| 118 | return 1 |
| 119 | fi |
| 120 | echoscip-shell system . . echo. "out: $OUT" |
| 121 | |
| 122 | echoscip-shell system . . echo. "$OUT" | grepscip-shell system . . grep. $4 > /dev/nullscip-shell filesystem . . `/dev/null`. |
| 123 | } |
| 124 | |
| 125 | function test_sds_external_xccdfscip-shell shell shell-project 0.0.0 test_sds_external_xccdf(). { |
| 126 | local SDS_FILE="${srcdir}/$2" |
| 127 | local XCCDF="$3" |
| 128 | local PROFILE="$4" |
| 129 | local result="${1}-${PROFILE}.xml" |
| 130 | |
| 131 | $OSCAP xccdf eval --xccdf-id "$XCCDF" --profile "$PROFILE" --results "$result" "$SDS_FILE" |
| 132 | |
| 133 | assert_exists 1 '//rule-result/result[text()="pass"]' |
| 134 | |
| 135 | rmscip-shell system . . rm. -f "$result" |
| 136 | } |
| 137 | |
| 138 | function test_sds_tailoringscip-shell shell shell-project 0.0.0 test_sds_tailoring(). { |
| 139 | local SDS_FILE="${srcdir}/$2" |
| 140 | local DATASTREAM_ID="$3" |
| 141 | local TAILORING_ID="$4" |
| 142 | local PROFILE="$5" |
| 143 | local result=$(mktempscip-shell system . . mktemp.) |
| 144 | |
| 145 | $OSCAP info "$SDS_FILE" |
| 146 | |
| 147 | $OSCAP xccdf eval --datastream-id "$DATASTREAM_ID" --tailoring-id "$TAILORING_ID" --profile "$PROFILE" --results "$result" "$SDS_FILE" |
| 148 | |
| 149 | assert_exists 2 '//Rule' |
| 150 | assert_exists 1 '//Rule[@id="xccdf_com.example_rule_1" and @selected="true"]' |
| 151 | assert_exists 1 '//Rule[@id="xccdf_com.example_rule_2" and @selected="false"]' |
| 152 | assert_exists 2 '//rule-result' |
| 153 | assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_1"]' |
| 154 | assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_2"]' |
| 155 | assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_1"]/result[text()="notselected"]' |
| 156 | assert_exists 1 '//rule-result[@idref="xccdf_com.example_rule_2"]/result[text()="pass"]' |
| 157 | |
| 158 | rmscip-shell system . . rm. -f "$result" |
| 159 | } |
| 160 | |
| 161 | # Testing. |
| 162 | test_init |
| 163 | |
| 164 | test_run "sds_external_xccdf" test_sds_external_xccdf sds_external_xccdf sds_external_xccdf/sds.ds.xml scap_org.open-scap_cref_xccdf.xml xccdf_external_profile_datastream_1 |
| 165 | test_run "sds_external_xccdf" test_sds_external_xccdf sds_external_xccdf sds_external_xccdf/sds.ds.xml scap_org.open-scap_cref_xccdf-file.xml xccdf_external_profile_file_1 |
| 166 | |
| 167 | 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 |
| 168 | |
| 169 | test_run "eval_simple" test_eval eval_simple/sds.xml |
| 170 | test_run "cpe_in_ds" test_eval cpe_in_ds/sds.xml |
| 171 | test_run "eval_invalid" test_invalid_eval eval_invalid/sds.xml |
| 172 | test_run "eval_invalid_oval" test_invalid_oval_eval eval_invalid/sds-oval.xml |
| 173 | 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 |
| 174 | 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 |
| 175 | test_run "eval_benchmark_id1" test_eval_benchmark_id eval_xccdf_id/sds.xml xccdf_moc.elpmaxe.www_benchmark_first first |
| 176 | test_run "eval_benchmark_id2" test_eval_benchmark_id eval_xccdf_id/sds.xml xccdf_moc.elpmaxe.www_benchmark_second second |
| 177 | test_run "eval_benchmark_id_conflict" test_eval_benchmark_id eval_benchmark_id_conflict/sds.xml xccdf_moc.elpmaxe.www_benchmark_first first |
| 178 | test_run "eval_just_oval" test_oval_eval eval_just_oval/sds.xml |
| 179 | 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" |
| 180 | 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" |
| 181 | test_run "eval_cpe" test_eval_cpe eval_cpe/sds.xml |
| 182 | |
| 183 | test_run "test_eval_complex" test_eval_complex |
| 184 | |
| 185 | test_exit |