| 1 | #!/usr/bin/env bash |
| 2 | . $builddir/tests/test_common.sh |
| 3 | |
| 4 | set -e |
| 5 | set -o pipefail |
| 6 | |
| 7 | stderr=$(mktemp -t ${name}.err.XXXXXX) |
| 8 | stdout=$(mktemp -t ${name}.out.XXXXXX) |
| 9 | |
| 10 | ds="$srcdir/test_reference_ds.xml" |
| 11 | p1="xccdf_com.example.www_profile_P1" |
| 12 | |
| 13 | # Test 1: --list-rules with --profile prints selected rule IDs |
| 14 | $OSCAP info --profile $p1 --list-rules $ds > $stdout 2> $stderr |
| 15 | [[ -f $stderr ]]; [[ ! -s $stderr ]]; :> $stderr |
| 16 | grep -q "xccdf_com.example.www_rule_R1" $stdout |
| 17 | grep -q "xccdf_com.example.www_rule_R2" $stdout |
| 18 | grep -q "xccdf_com.example.www_rule_R3" $stdout |
| 19 | grep -q "xccdf_com.example.www_rule_R4" $stdout |
| 20 | # Verify output contains only rule IDs, one per line (4 rules = 4 lines) |
| 21 | [[ "$(wc -l < $stdout)" -eq 4 ]] |
| 22 | :> $stdout |
| 23 | |
| 24 | # Test 2: --list-rules without --profile produces an error |
| 25 | $OSCAP info --list-rules $ds > $stdout 2> $stderr && exit 1 || true |
| 26 | grep -q "\-\-list-rules option requires \-\-profile" $stderr |
| 27 | :> $stdout |
| 28 | :> $stderr |
| 29 | |
| 30 | # Test 3: --list-rules with standalone XCCDF tailoring file |
| 31 | tailoring="$srcdir/test_tailoring_file.xml" |
| 32 | tp="xccdf_com.example.www_profile_P1_tailored" |
| 33 | $OSCAP info --profile $tp --list-rules $tailoring > $stdout 2> $stderr |
| 34 | [[ -f $stderr ]]; [[ ! -s $stderr ]]; :> $stderr |
| 35 | grep -q "xccdf_com.example.www_rule_R1" $stdout |
| 36 | grep -q "xccdf_com.example.www_rule_R2" $stdout |
| 37 | # R3 and R4 are deselected by tailoring |
| 38 | ! grep -q "xccdf_com.example.www_rule_R3" $stdout |
| 39 | ! grep -q "xccdf_com.example.www_rule_R4" $stdout |
| 40 | [[ "$(wc -l < $stdout)" -eq 2 ]] |
| 41 | :> $stdout |
| 42 | |
| 43 | # Test 4: --list-rules with SDS containing tailoring |
| 44 | ds_tailoring="$srcdir/test_reference_ds_with_tailoring.xml" |
| 45 | $OSCAP info --profile $tp --list-rules $ds_tailoring > $stdout 2> $stderr |
| 46 | [[ -f $stderr ]]; [[ ! -s $stderr ]]; :> $stderr |
| 47 | grep -q "xccdf_com.example.www_rule_R1" $stdout |
| 48 | grep -q "xccdf_com.example.www_rule_R2" $stdout |
| 49 | ! grep -q "xccdf_com.example.www_rule_R3" $stdout |
| 50 | ! grep -q "xccdf_com.example.www_rule_R4" $stdout |
| 51 | [[ "$(wc -l < $stdout)" -eq 2 ]] |
| 52 | :> $stdout |
| 53 | |
| 54 | # Test 5: --list-rules with tailoring referencing SDS via file: prefix + cref |
| 55 | temp_dir="$(mktemp -d)" |
| 56 | cp "$srcdir/test_reference_ds.xml" $temp_dir |
| 57 | tailoring_sds="test_tailoring_file_sds_cref.xml" |
| 58 | sed "s;TEMP_DIRECTORY_PLACEHOLDER;$temp_dir;" "$srcdir/$tailoring_sds" > "$temp_dir/$tailoring_sds" |
| 59 | $OSCAP info --profile $tp --list-rules "$temp_dir/$tailoring_sds" > $stdout 2> $stderr |
| 60 | [[ -f $stderr ]]; [[ ! -s $stderr ]]; :> $stderr |
| 61 | grep -q "xccdf_com.example.www_rule_R1" $stdout |
| 62 | grep -q "xccdf_com.example.www_rule_R2" $stdout |
| 63 | ! grep -q "xccdf_com.example.www_rule_R3" $stdout |
| 64 | ! grep -q "xccdf_com.example.www_rule_R4" $stdout |
| 65 | [[ "$(wc -l < $stdout)" -eq 2 ]] |
| 66 | :> $stdout |
| 67 | rm -rf "$temp_dir" |
| 68 | |
| 69 | # Test 6: --list-rules with tailoring referencing SDS via file: prefix, no cref |
| 70 | temp_dir="$(mktemp -d)" |
| 71 | cp "$srcdir/test_reference_ds.xml" $temp_dir |
| 72 | tailoring_sds_nf="test_tailoring_file_sds.xml" |
| 73 | sed "s;TEMP_DIRECTORY_PLACEHOLDER;$temp_dir;" "$srcdir/$tailoring_sds_nf" > "$temp_dir/$tailoring_sds_nf" |
| 74 | $OSCAP info --profile $tp --list-rules "$temp_dir/$tailoring_sds_nf" > $stdout 2> $stderr |
| 75 | [[ -f $stderr ]]; [[ ! -s $stderr ]]; :> $stderr |
| 76 | grep -q "xccdf_com.example.www_rule_R1" $stdout |
| 77 | grep -q "xccdf_com.example.www_rule_R2" $stdout |
| 78 | ! grep -q "xccdf_com.example.www_rule_R3" $stdout |
| 79 | ! grep -q "xccdf_com.example.www_rule_R4" $stdout |
| 80 | [[ "$(wc -l < $stdout)" -eq 2 ]] |
| 81 | :> $stdout |
| 82 | rm -rf "$temp_dir" |
| 83 | |
| 84 | # Test 7: --list-rules with tailoring referencing SDS via file: prefix, no cref, |
| 85 | # providing the original profile instead of the customized one |
| 86 | temp_dir="$(mktemp -d)" |
| 87 | cp "$srcdir/test_reference_ds.xml" $temp_dir |
| 88 | tailoring_sds_nf="test_tailoring_file_sds.xml" |
| 89 | sed "s;TEMP_DIRECTORY_PLACEHOLDER;$temp_dir;" "$srcdir/$tailoring_sds_nf" > "$temp_dir/$tailoring_sds_nf" |
| 90 | $OSCAP info --profile "xccdf_com.example.www_profile_P1" --list-rules "$temp_dir/$tailoring_sds_nf" > $stdout 2> $stderr |
| 91 | [[ -f $stdout ]]; [[ ! -s $stdout ]]; :> $stdout |
| 92 | grep -q "Profile 'xccdf_com.example.www_profile_P1' not found in the tailoring file. Provide the customized profile ID. Get available profiles using:" "$stderr" |
| 93 | grep -q "$ oscap info $temp_dir/$tailoring_sds_nf" "$stderr" |
| 94 | [[ "$(wc -l < $stderr)" -eq 2 ]] |
| 95 | :> $stderr |
| 96 | rm -rf "$temp_dir" |
| 97 | |
| 98 | rm -f $stdout $stderr |