Passed
Pull Request — rhel8-branch (#147)
by Jan
01:46
created

test_scap_content_handler   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 127
dl 0
loc 162
rs 10
c 0
b 0
f 0

12 Functions

Rating   Name   Duplication   Size   Complexity  
A test_default_profile() 0 13 1
A test_init_tailoring_of_sds() 0 3 1
A test_init_sds() 0 3 1
A test_init_tailoring_of_xccdf() 0 3 1
A test_init_xccdf() 0 3 1
A test_xccdf() 0 18 1
A test_sds_get_profiles_fails() 0 20 4
A test_xccdf_get_profiles_fails() 0 7 2
A test_init_invalid_file_path() 0 4 2
A test_init_unsupported_scap_content_type() 0 5 2
A test_sds() 0 26 1
A test_tailoring() 0 16 1
1
from org_fedora_oscap.scap_content_handler import SCAPContentHandler
2
from org_fedora_oscap.scap_content_handler import SCAPContentHandlerError
3
from org_fedora_oscap.scap_content_handler import ProfileInfo
4
import os
5
import pytest
6
7
TESTING_FILES_PATH = os.path.join(
8
    os.path.dirname(__file__), os.path.pardir, "testing_files")
9
DS_FILEPATH = os.path.join(TESTING_FILES_PATH, "testing_ds.xml")
10
XCCDF_FILEPATH = os.path.join(TESTING_FILES_PATH, "xccdf.xml")
11
TAILORING_FILEPATH = os.path.join(TESTING_FILES_PATH, "tailoring.xml")
12
OVAL_FILEPATH = os.path.join(TESTING_FILES_PATH, "scap-mycheck-oval.xml")
13
14
DS_IDS = "scap_org.open-scap_datastream_tst"
15
CHK_FIRST_ID = "scap_org.open-scap_cref_first-xccdf.xml"
16
CHK_SECOND_ID = "scap_org.open-scap_cref_second-xccdf.xml"
17
18
19
def test_init_invalid_file_path():
20
    with pytest.raises(FileNotFoundError) as excinfo:
21
        SCAPContentHandler("blbl")
22
    assert "No such file or directory: 'blbl'" in str(excinfo.value)
23
24
25
def test_init_sds():
26
    ch = SCAPContentHandler(DS_FILEPATH)
27
    assert ch.scap_type == "SCAP_SOURCE_DATA_STREAM"
28
29
30
def test_init_xccdf():
31
    ch = SCAPContentHandler(XCCDF_FILEPATH)
32
    assert ch.scap_type == "XCCDF"
33
34
35
def test_init_tailoring_of_sds():
36
    ch = SCAPContentHandler(TAILORING_FILEPATH)
37
    assert ch.scap_type == "TAILORING"
38
39
40
def test_init_tailoring_of_xccdf():
41
    ch = SCAPContentHandler(TAILORING_FILEPATH)
42
    assert ch.scap_type == "TAILORING"
43
44
45
def test_init_unsupported_scap_content_type():
46
    # the class SCAPContentHandler shouldn't support OVAL files
47
    with pytest.raises(SCAPContentHandlerError) as excinfo:
48
        SCAPContentHandler(OVAL_FILEPATH)
49
    assert "Unsupported SCAP content type" in str(excinfo.value)
50
51
52
def test_xccdf():
53
    ch = SCAPContentHandler(XCCDF_FILEPATH)
54
55
    checklists = ch.get_data_streams_checklists()
56
    assert checklists is None
57
58
    profiles = ch.get_profiles()
59
    assert len(profiles) == 2
60
    pinfo1 = ProfileInfo(
61
        id="xccdf_com.example_profile_my_profile",
62
        title="My testing profile",
63
        description="A profile for testing purposes.")
64
    assert pinfo1 in profiles
65
    pinfo2 = ProfileInfo(
66
        id="xccdf_com.example_profile_my_profile2",
67
        title="My testing profile2",
68
        description="Another profile for testing purposes.")
69
    assert pinfo2 in profiles
70
71
72
def test_xccdf_get_profiles_fails():
73
    ch = SCAPContentHandler(XCCDF_FILEPATH)
74
    with pytest.raises(SCAPContentHandlerError) as excinfo:
75
        ch.select_checklist("", "")
76
        profiles = ch.get_profiles()
77
    assert "For XCCDF documents, the data_stream_id and " \
78
        "checklist_id must be both None." in str(excinfo.value)
79
80
81
def test_sds():
82
    ch = SCAPContentHandler(DS_FILEPATH)
83
    checklists = ch.get_data_streams_checklists()
84
    assert checklists == {DS_IDS: [CHK_FIRST_ID, CHK_SECOND_ID]}
85
86
    ch.select_checklist(DS_IDS, CHK_FIRST_ID)
87
    profiles = ch.get_profiles()
88
    assert len(profiles) == 2
89
    pinfo1 = ProfileInfo(
90
        id="xccdf_com.example_profile_my_profile",
91
        title="My testing profile",
92
        description="A profile for testing purposes.")
93
    assert pinfo1 in profiles
94
    pinfo2 = ProfileInfo(
95
        id="xccdf_com.example_profile_my_profile2",
96
        title="My testing profile2",
97
        description="Another profile for testing purposes.")
98
    assert pinfo2 in profiles
99
100
    ch.select_checklist(DS_IDS, CHK_SECOND_ID)
101
    profiles2 = ch.get_profiles()
102
    assert len(profiles2) == 1
103
    pinfo3 = ProfileInfo(
104
        id="xccdf_com.example_profile_my_profile3",
105
        title="My testing profile3",
106
        description="Yet another profile for testing purposes.")
107
108
109
def test_sds_get_profiles_fails():
110
    ch = SCAPContentHandler(DS_FILEPATH)
111
112
    with pytest.raises(SCAPContentHandlerError) as excinfo:
113
        profiles = ch.get_profiles()
114
    assert "For SCAP source data streams, data_stream_id and " \
115
        "checklist_id must be both different than None" in str(excinfo.value)
116
117
    with pytest.raises(SCAPContentHandlerError) as excinfo:
118
        ch.select_checklist(DS_IDS, checklist_id=None)
119
        profiles = ch.get_profiles()
120
    assert "For SCAP source data streams, data_stream_id and " \
121
        "checklist_id must be both different than None" in str(excinfo.value)
122
123
    with pytest.raises(SCAPContentHandlerError) as excinfo:
124
        wrong_cref = "scap_org.open-scap_cref_seventh-xccdf.xml"
125
        ch.select_checklist(DS_IDS, wrong_cref)
126
        profiles = ch.get_profiles()
127
    assert f"Can't find ds:component-ref with id='{wrong_cref}' in " \
128
        f"ds:datastream with id='{DS_IDS}'" in str(excinfo.value)
129
130
131
def test_tailoring():
132
    ch = SCAPContentHandler(TAILORING_FILEPATH)
133
    checklists = ch.get_data_streams_checklists()
134
    assert checklists is None
135
    profiles = ch.get_profiles()
136
    assert len(profiles) == 2
137
    pinfo1 = ProfileInfo(
138
        id="xccdf_com.example_profile_my_profile_tailored",
139
        title="My testing profile tailored",
140
        description="")
141
    assert pinfo1 in profiles
142
    pinfo2 = ProfileInfo(
143
        id="xccdf_com.example_profile_my_profile2_tailored",
144
        title="My testing profile2 tailored",
145
        description="")
146
    assert pinfo2 in profiles
147
148
149
def test_default_profile():
150
    xccdf_filepath = os.path.join(TESTING_FILES_PATH, "testing_xccdf.xml")
151
    ch = SCAPContentHandler(xccdf_filepath)
152
    checklists = ch.get_data_streams_checklists()
153
    assert checklists is None
154
    profiles = ch.get_profiles()
155
    assert len(profiles) == 1
156
    pinfo1 = ProfileInfo(
157
        id="default",
158
        title="Default",
159
        description="The implicit XCCDF profile. Usually, the default profile "
160
        "contains no rules.")
161
    assert pinfo1 in profiles