Passed
Pull Request — master (#30)
by Jan
05:51
created

test_description()   A

Complexity

Conditions 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nop 2
dl 0
loc 24
rs 9.85
c 0
b 0
f 0
1
import pytest
2
3
from oscap_report.scap_results_parser.data_structures import Report, Rule
4
from oscap_report.scap_results_parser.scap_results_parser import \
5
    SCAPResultsParser
6
7
from ..constants import (PATH_TO_ARF, PATH_TO_ARF_WITH_MULTI_CHECK,
8
                         PATH_TO_ARF_WITH_REMOVED_INFO, PATH_TO_EMPTY_XML_FILE,
9
                         PATH_TO_RULE_AND_CPE_CHECK_ARF,
10
                         PATH_TO_RULE_AND_CPE_CHECK_XCCDF,
11
                         PATH_TO_SIMPLE_RULE_FAIL_ARF,
12
                         PATH_TO_SIMPLE_RULE_FAIL_XCCDF,
13
                         PATH_TO_SIMPLE_RULE_PASS_ARF,
14
                         PATH_TO_SIMPLE_RULE_PASS_XCCDF, PATH_TO_XCCDF,
15
                         PATH_TO_XCCDF_WITH_MULTI_CHECK,
16
                         PATH_TO_XCCDF_WITH_REMOVED_INFO)
17
18
19
def get_parser(file_path):
20
    xml_data = None
21
    with open(file_path, "r", encoding="utf-8") as xml_report:
22
        xml_data = xml_report.read().encode()
23
    return SCAPResultsParser(xml_data)
24
25
26
@pytest.mark.parametrize("file_path, result", [
27
    (PATH_TO_ARF, True),
28
    (PATH_TO_SIMPLE_RULE_PASS_ARF, True),
29
    (PATH_TO_SIMPLE_RULE_FAIL_ARF, True),
30
    (PATH_TO_RULE_AND_CPE_CHECK_ARF, True),
31
    (PATH_TO_ARF_WITH_REMOVED_INFO, True),
32
    (PATH_TO_XCCDF, False),
33
    (PATH_TO_SIMPLE_RULE_PASS_XCCDF, False),
34
    (PATH_TO_SIMPLE_RULE_FAIL_XCCDF, False),
35
    (PATH_TO_RULE_AND_CPE_CHECK_XCCDF, False),
36
    (PATH_TO_XCCDF_WITH_REMOVED_INFO, False),
37
    (PATH_TO_EMPTY_XML_FILE, False),
38
])
39
def test_validation(file_path, result):
40
    parser = get_parser(file_path)
41
    assert parser.validate(parser.arf_schemas_path) == result
42
43
44
@pytest.mark.parametrize("file_path, number_of_cpe_platforms", [
45
    (PATH_TO_ARF, 13),
46
    (PATH_TO_XCCDF, 13),
47
    (PATH_TO_SIMPLE_RULE_PASS_ARF, 0),
48
    (PATH_TO_SIMPLE_RULE_FAIL_ARF, 0),
49
    (PATH_TO_ARF_WITH_REMOVED_INFO, 0),
50
    (PATH_TO_RULE_AND_CPE_CHECK_ARF, 1),
51
    (PATH_TO_SIMPLE_RULE_PASS_XCCDF, 0),
52
    (PATH_TO_SIMPLE_RULE_FAIL_XCCDF, 0),
53
    (PATH_TO_XCCDF_WITH_REMOVED_INFO, 0),
54
    (PATH_TO_RULE_AND_CPE_CHECK_XCCDF, 1),
55
])
56
def test_get_profile_info(file_path, number_of_cpe_platforms):
57
    parser = get_parser(file_path)
58
    report = parser.get_profile_info()
59
    assert len(report.cpe_platforms) == number_of_cpe_platforms
60
61
62
@pytest.mark.parametrize("file_path, number_of_rules", [
63
    (PATH_TO_ARF, 714),
64
    (PATH_TO_XCCDF, 714),
65
    (PATH_TO_SIMPLE_RULE_PASS_ARF, 1),
66
    (PATH_TO_SIMPLE_RULE_FAIL_ARF, 1),
67
    (PATH_TO_ARF_WITH_REMOVED_INFO, 1),
68
    (PATH_TO_RULE_AND_CPE_CHECK_ARF, 3),
69
    (PATH_TO_SIMPLE_RULE_PASS_XCCDF, 1),
70
    (PATH_TO_SIMPLE_RULE_FAIL_XCCDF, 1),
71
    (PATH_TO_XCCDF_WITH_REMOVED_INFO, 1),
72
    (PATH_TO_RULE_AND_CPE_CHECK_XCCDF, 3),
73
])
74
def test_get_info_about_rules_in_profile(file_path, number_of_rules):
75
    parser = get_parser(file_path)
76
    rules = parser.get_info_about_rules_in_profile()
77
    assert len(rules.keys()) == number_of_rules
78
    for rule in rules.values():
79
        assert isinstance(rule, Rule)
80
81
82
@pytest.mark.parametrize("file_path, contains_oval_tree", [
83
    (PATH_TO_ARF, True),
84
    (PATH_TO_XCCDF, False),
85
])
86
def test_parse_report(file_path, contains_oval_tree):
87
    parser = get_parser(file_path)
88
    report = parser.parse_report()
89
    assert isinstance(report, Report)
90
    assert report.profile_name is not None
91
    assert report.rules is not None
92
    rule_id = "xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny"
93
    assert isinstance(report.rules[rule_id], Rule)
94
    assert (report.rules[rule_id].oval_tree is not None) == contains_oval_tree
95
96
97
@pytest.mark.parametrize("file_path, contains_rules_some_multi_check_rule", [
98
    (PATH_TO_ARF, False),
99
    (PATH_TO_XCCDF, False),
100
    (PATH_TO_XCCDF_WITH_MULTI_CHECK, True),
101
    (PATH_TO_ARF_WITH_MULTI_CHECK, True),
102
])
103
def test_multi_check(file_path, contains_rules_some_multi_check_rule):
104
    parser = get_parser(file_path)
105
    rules = parser.get_info_about_rules_in_profile()
106
    result = False
107
    for rule in rules.values():
108
        if rule.multi_check:
109
            result = True
110
    assert result == contains_rules_some_multi_check_rule
111
112
113
@pytest.mark.parametrize("rule, result", [
114
    (
115
        "xccdf_org.ssgproject.content_rule_prefer_64bit_os",
116
        "Prefer installation of 64-bit operating systems when the CPU supports it."
117
    ),
118
    (
119
        "xccdf_org.ssgproject.content_rule_dconf_gnome_screensaver_lock_enabled",
120
        (
121
            "\nTo activate locking of the screensaver in the GNOME3 desktop when it is activated,\n"
122
            "add or set <code>lock-enabled</code> to <code>true</code> in\n"
123
            "<code>/etc/dconf/db/local.d/00-security-settings</code>. For example:\n"
124
            "<pre>[org/gnome/desktop/screensaver]\nlock-enabled=true\n</pre>\n"
125
            "Once the settings have been added, add a lock to\n"
126
            "<code>/etc/dconf/db/local.d/locks/00-security-settings-lock</code> "
127
            "to prevent user modification.\nFor example:\n"
128
            "<pre>/org/gnome/desktop/screensaver/lock-enabled</pre>\n"
129
            "After the settings have been set, run <code>dconf update</code>."
130
        )
131
    )
132
])
133
def test_description(rule, result):
134
    parser = get_parser(PATH_TO_ARF)
135
    rules = parser.get_info_about_rules_in_profile()
136
    assert rules[rule].description == result
137