| 1 | #!/usr/bin/env bash |
| 2 | . $builddir/tests/test_common.sh |
| 3 | |
| 4 | # Test <refine-rule> element |
| 5 | # 1st phase use xccdf profile with refine-rules |
| 6 | # 2nd phase generate tailoring file from xccdf and use tailoring file's profiles |
| 7 | |
| 8 | # Return XPath expression of <rule-result> |
| 9 | xpath_rule_result() { |
| 10 | local id_ref="$1" |
| 11 | echo '//TestResult/rule-result[ @idref="'"${id_ref}"'" ]' |
| 12 | } |
| 13 | |
| 14 | # Assert <rule_result> has "notchecked" in <result> |
| 15 | assert_notchecked() { |
| 16 | local id_ref="$1" |
| 17 | local rule_result="`xpath_rule_result \"${id_ref}\"`" |
| 18 | assert_exists 1 "${rule_result}"'/result[ text()="notchecked" ]' > /dev/stdout |
| 19 | } |
| 20 | |
| 21 | # Assert <rule_result> has NOT "nonchecked" in <result> |
| 22 | assert_checked() { |
| 23 | local id_ref="$1" |
| 24 | local rule_result="`xpath_rule_result \"${id_ref}\"`" |
| 25 | assert_exists 1 "${rule_result}"'/result[ text()!="notchecked" ]' > /dev/stdout |
| 26 | } |
| 27 | |
| 28 | # Assert weight of <rule-result> |
| 29 | assert_weight() { |
| 30 | local id_ref="$1" |
| 31 | local weight="$2" |
| 32 | assert_exists 1 "//rule-result[ @idref=\"${id_ref}\" and starts-with(@weight, \"${weight}\") ]" > /dev/stdout |
| 33 | } |
| 34 | |
| 35 | # Create tailoring file from xccdf profiles |
| 36 | # Add "tailor-" prefix before profile names |
| 37 | # Return tailoring file content |
| 38 | create_tailoring() { |
| 39 | local xccdf_filename="$1" |
| 40 | echo '<?xml version="1.0" encoding="UTF-8"?>' |
| 41 | echo '<cdf-11-tailoring:Tailoring xmlns:cdf-11-tailoring="http://open-scap.org/page/Xccdf-1.1-tailoring" xmlns="http://checklists.nist.gov/xccdf/1.1" id="xccdf_scap-workbench_tailoring_default">' |
| 42 | echo '<cdf-11-tailoring:version time="2014-03-13T12:22:15">1</cdf-11-tailoring:version>' |
| 43 | cat "$xccdf_filename" |\ |
| 44 | tr '\t' ' ' |\ |
| 45 | tr '\n' '\t ' |\ |
| 46 | sed -E 's;<Profile(.*)</Profile>.*;<NEW><Profile\1</Profile>;' |\ |
| 47 | sed -E 's;^.*<NEW>(.*);\1;' |\ |
| 48 | sed -E 's;"(child|parent|grandparent)";"tailor-\1";g' |\ |
| 49 | tr '\t' '\n' |
| 50 | echo '' |
| 51 | echo '</cdf-11-tailoring:Tailoring>' |
| 52 | } |
| 53 | |
| 54 | |
| 55 | check_results() { |
| 56 | local result="$1" |
| 57 | ### 1. Grandparent refine selector of rule and child refine role of rule |
| 58 | #assert_exists 1 "`xpath_rule_result rule-id-selected-and-role`"'//check-content-ref[ @name="oval:moc.elpmaxe.www:def:2" ]' |
| 59 | assert_checked rule-id-selected-and-role |
| 60 | |
| 61 | ### 2. <refine-rule @weight> is two times overrided by extending of profile |
| 62 | assert_weight rule-id-override-refine 8 |
| 63 | |
| 64 | ### 3. two refine-rules partially override themselves |
| 65 | assert_exists 1 '//rule-result[ @idref="rule-id-partially-refined" ]/result[ text()!="notchecked" ]' |
| 66 | #assert_exists 1 '//rule-result[ @idref="rule-id-partially-refined" and starts-with(@weight, "5") ]/result[ text()!="notchecked" ]' |
| 67 | |
| 68 | ### 4. The rule should be checked (no refine-rule is used) |
| 69 | assert_checked rule-id-checked |
| 70 | |
| 71 | ### 5. <refine-rule> enable check |
| 72 | assert_checked rule-id-checked |
| 73 | |
| 74 | ### 6. <refine-rule> from parent profile enable check |
| 75 | assert_checked rule-id-enable-parent-check |
| 76 | |
| 77 | ### 7. |
| 78 | assert_notchecked rule-id-without-refine |
| 79 | |
| 80 | ### 8. |
| 81 | assert_notchecked rule-id-inherited |
| 82 | |
| 83 | ### 9. |
| 84 | assert_notchecked rule-id-simple |
| 85 | |
| 86 | ### 10. |
| 87 | assert_notchecked rule-id-cluster |
| 88 | |
| 89 | ### 11. <refine-rule> for @weight |
| 90 | assert_weight rule-id-weight 2 |
| 91 | |
| 92 | ### 12. <refine-rule> for @selector |
| 93 | assert_exists 1 "`xpath_rule_result rule-id-multiple-check`"'//check-content-ref[ @name="oval:moc.elpmaxe.www:def:2" ]' |
| 94 | assert_checked rule-id-multiple-check |
| 95 | |
| 96 | ### 13. two refine-rules partially override themselves |
| 97 | assert_exists 1 '//rule-result[ @idref="rule-id-partially-refined-2" ]/result[ text()="notchecked" ]' |
| 98 | #assert_exists 1 '//rule-result[ @idref="rule-id-partially-refined-2" and starts-with(@weight, "5") ]/result[ text()="notchecked" ]' |
| 99 | |
| 100 | ### 14. severity should be redefined |
| 101 | assert_exists 1 '//rule-result[ @idref="rule-id-severity" and @severity="high" ]' |
| 102 | |
| 103 | ### 15. weight should be overrided |
| 104 | assert_exists 1 '//rule-result[ @idref="rule-id-weight-override" and starts-with(@weight, "4") ]' |
| 105 | |
| 106 | ### 16. unscored should be checked |
| 107 | assert_checked rule-id-unscored |
| 108 | |
| 109 | ### 17. should keep parent severity |
| 110 | assert_exists 1 '//rule-result[ @idref="rule-id-keep-parent-severity" and @severity="low" ]' |
| 111 | } |
| 112 | |
| 113 | set -e |
| 114 | set -o pipefail |
| 115 | |
| 116 | name=$(basename $0 .sh) |
| 117 | xccdf="$srcdir/${name}.xccdf.xml" |
| 118 | |
| 119 | ######################################################################## |
| 120 | ## Test refine-rules in profiles |
| 121 | ######################################################################## |
| 122 | echo "Test refine-rules in profiles:" |
| 123 | |
| 124 | result=$(mktemp -t ${name}.res.XXXXXX) |
| 125 | stderr=$(mktemp -t ${name}.err.XXXXXX) |
| 126 | stdout=$(mktemp -t ${name}.out.XXXXXX) |
| 127 | |
| 128 | echo "Stderr file = $stderr" |
| 129 | echo "Result file = $result" |
| 130 | |
| 131 | $OSCAP xccdf eval --profile child --results $result $xccdf > $stdout 2> $stderr || true #we expect exit code != 0 |
| 132 | |
| 133 | [ -f $stderr ]; [ ! -s $stderr ]; rm $stderr |
| 134 | |
| 135 | $OSCAP xccdf validate --skip-schematron $result |
| 136 | |
| 137 | RULE_COUNT=17 |
| 138 | |
| 139 | # Same count of results as rules |
| 140 | assert_exists ${RULE_COUNT} '//Rule' |
| 141 | assert_exists ${RULE_COUNT} '//TestResult/rule-result/result' |
| 142 | |
| 143 | assert_exists 4 '//Rule[ @role="unchecked" ]' # Refine-rule does not modify rules values directly |
| 144 | check_results $result |
| 145 | |
| 146 | rm $result |
| 147 | ######################################################################## |
| 148 | ## Test refine-rules in tailoring file |
| 149 | ######################################################################## |
| 150 | echo "" |
| 151 | echo "Test refine-rules in tailoring file:" |
| 152 | |
| 153 | result=$(mktemp -t ${name}.res.XXXXXX) |
| 154 | stderr=$(mktemp -t ${name}.err.XXXXXX) |
| 155 | stdout=$(mktemp -t ${name}.out.XXXXXX) |
| 156 | tailoring=$(mktemp -t ${name}.in.XXXXXX) |
| 157 | |
| 158 | echo "Stderr file = $stderr" |
| 159 | echo "Stdout file = $stdout" |
| 160 | echo "Result file = $result" |
| 161 | echo "Tailoring file = $tailoring" |
| 162 | |
| 163 | echo "`create_tailoring \"$xccdf\"`" > $tailoring |
| 164 | |
| 165 | $OSCAP xccdf eval --tailoring-file $tailoring --profile tailor-child --results $result $xccdf > $stdout 2> $stderr || true #we expect exit code != 0 |
| 166 | |
| 167 | check_results $result |
| 168 | |
| 169 | [ -f $stderr ]; [ ! -s $stderr ]; rm $stderr |
| 170 | rm $result $tailoring |