Debian Codegraph @

tests/API/XCCDF/unittests/test_reference.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash
2 . $builddir/tests/test_common.sh
3
4 set -e
5 set -o pipefail
6
7 result=$(mktempscip-shell system . . mktemp. -t ${name}.out.XXXXXX)
8 stderr=$(mktempscip-shell system . . mktemp. -t ${name}.out.XXXXXX)
9 stdout=$(mktempscip-shell system . . mktemp. -t ${name}.out.XXXXXX)
10
11 ds="$srcdir/test_reference_ds.xml"
12 p1="xccdf_com.example.www_profile_P1"
13 r1="xccdf_com.example.www_rule_R1"
14 r2="xccdf_com.example.www_rule_R2"
15 r3="xccdf_com.example.www_rule_R3"
16 r4="xccdf_com.example.www_rule_R4"
17
18 # Tests if references are correctly shown in oscap info output
19 $OSCAP info --references $ds > $stdout 2> $stderr
20 [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; :> $stderr
21 grepscip-shell system . . grep. -q "References:" $stdout
22 grepscip-shell system . . grep. -q "animals: https://www.animals.com" $stdout
23 grepscip-shell system . . grep. -q "fruit: https://www.fruit.com" $stdout
24 :> $stdout
25
26 # Tests that all rules from profile P1 (profile contains only 4 rules) are
27 # evaluated when '--reference' option is not specified.
28 $OSCAP xccdf eval --results $result --profile $p1 $ds > $stdout 2> $stderr
29
30 [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; :> $stderr
31 assert_exists 1 "//rule-result[@idref=\"$r1\"]/result[text()=\"pass\"]"
32 assert_exists 1 "//rule-result[@idref=\"$r2\"]/result[text()=\"pass\"]"
33 assert_exists 1 "//rule-result[@idref=\"$r3\"]/result[text()=\"pass\"]"
34 assert_exists 1 "//rule-result[@idref=\"$r4\"]/result[text()=\"pass\"]"
35 :> $stdout
36 :> $result
37
38 # Tests that rule R1 from profile P1 is evaluated when '--reference' option
39 # matches the rule R1.
40 $OSCAP xccdf eval --results $result --profile $p1 --reference "animals:3.14" $ds > $stdout 2> $stderr
41
42 [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; :> $stderr
43 assert_exists 1 "//rule-result[@idref=\"$r1\"]/result[text()=\"pass\"]"
44 assert_exists 1 "//rule-result[@idref=\"$r2\"]/result[text()=\"notselected\"]"
45 assert_exists 1 "//rule-result[@idref=\"$r3\"]/result[text()=\"notselected\"]"
46 assert_exists 1 "//rule-result[@idref=\"$r4\"]/result[text()=\"notselected\"]"
47 :> $stdout
48 :> $result
49
50 # Tests that rule R1 from profile P1 is evaluated when '--reference' option
51 # matches the rule R1. This test uses a different reference key than the
52 # previous test.
53 $OSCAP xccdf eval --results $result --profile $p1 --reference "fruit:42.42" $ds > $stdout 2> $stderr
54
55 [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; :> $stderr
56 assert_exists 1 "//rule-result[@idref=\"$r1\"]/result[text()=\"pass\"]"
57 assert_exists 1 "//rule-result[@idref=\"$r2\"]/result[text()=\"notselected\"]"
58 assert_exists 1 "//rule-result[@idref=\"$r3\"]/result[text()=\"notselected\"]"
59 assert_exists 1 "//rule-result[@idref=\"$r4\"]/result[text()=\"notselected\"]"
60 :> $stdout
61 :> $result
62
63 # Tests that only rules R2 and R3 from profile P1 are evaluated when
64 # '--reference' option matches the rule R2 and R3, both rules have
65 # the same reference item.
66 $OSCAP xccdf eval --results $result --profile $p1 --reference "animals:17.71.777" $ds > $stdout 2> $stderr
67
68 [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; :> $stderr
69 assert_exists 1 "//rule-result[@idref=\"$r1\"]/result[text()=\"notselected\"]"
70 assert_exists 1 "//rule-result[@idref=\"$r2\"]/result[text()=\"pass\"]"
71 assert_exists 1 "//rule-result[@idref=\"$r3\"]/result[text()=\"pass\"]"
72 assert_exists 1 "//rule-result[@idref=\"$r4\"]/result[text()=\"notselected\"]"
73 :> $stdout
74 :> $result
75
76 # Tests that no rule from profile P1 is evaluated when '--reference' option
77 # doesn't match any reference in any rule.
78 $OSCAP xccdf eval --results $result --profile $p1 --reference "animals:99.66.33" $ds > $stdout 2> $stderr
79
80 [scip-shell system . . `[`. -f $stderr ]; [scip-shell system . . `[`. ! -s $stderr ]; :> $stderr
81 assert_exists 1 "//rule-result[@idref=\"$r1\"]/result[text()=\"notselected\"]"
82 assert_exists 1 "//rule-result[@idref=\"$r2\"]/result[text()=\"notselected\"]"
83 assert_exists 1 "//rule-result[@idref=\"$r3\"]/result[text()=\"notselected\"]"
84 assert_exists 1 "//rule-result[@idref=\"$r4\"]/result[text()=\"notselected\"]"
85 :> $stdout
86 :> $result
87
88 # Tests that when a wrong '--reference' option is provided OpenSCAP ignores it,
89 # evaluates all rules and prints a nice error messsage.
90 $OSCAP xccdf eval --results $result --profile $p1 --reference "aliens:XXX" $ds > $stdout 2> $stderr
91 grepscip-shell system . . grep. -q "OpenSCAP Error: Reference type 'aliens' isn't available in this benchmark" $stderr
92 assert_exists 1 "//rule-result[@idref=\"$r1\"]/result[text()=\"pass\"]"
93 assert_exists 1 "//rule-result[@idref=\"$r2\"]/result[text()=\"pass\"]"
94 assert_exists 1 "//rule-result[@idref=\"$r3\"]/result[text()=\"pass\"]"
95 assert_exists 1 "//rule-result[@idref=\"$r4\"]/result[text()=\"pass\"]"
96 :> $stdout
97 :> $result
98
99 # Tests that when a wrong '--reference' option with a valid name but missing
100 # identifier is provided OpenSCAP prints an errror message.
101 $OSCAP xccdf eval --results $result --profile $p1 --reference "animals" $ds > $stdout 2> $stderr || [[ $? -eq 1 ]]
102 grepscip-shell system . . grep. -q "The --reference argument needs to be in form NAME:IDENTIFIER, using a colon as a separator." $stderr
103 :> $stdout
104 :> $result