Passed
Pull Request — master (#33)
by Jan
05:53
created

tests.unit_tests.test_scap_result_parser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 112
dl 0
loc 146
rs 10
c 0
b 0
f 0
wmc 11

7 Functions

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