Completed
Push — master ( 8229ad...0db8a8 )
by Jan
17s queued 13s
created

test_scap_content_handler.test_init_sds()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 0
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
def test_xccdf_1_1():
72
    file_path = os.path.join(TESTING_FILES_PATH, "xccdf-1.1.xml")
73
    ch = SCAPContentHandler(file_path)
74
75
    checklists = ch.get_data_streams_checklists()
76
    assert checklists is None
77
78
    profiles = ch.get_profiles()
79
    assert len(profiles) == 2
80
    pinfo1 = ProfileInfo(
81
        id="xccdf_com.example_profile_my_profile",
82
        title="My testing profile",
83
        description="A profile for testing purposes.")
84
    assert pinfo1 in profiles
85
    pinfo2 = ProfileInfo(
86
        id="xccdf_com.example_profile_my_profile2",
87
        title="My testing profile2",
88
        description="Another profile for testing purposes.")
89
    assert pinfo2 in profiles
90
91
92
def test_xccdf_get_profiles_fails():
93
    ch = SCAPContentHandler(XCCDF_FILEPATH)
94
    with pytest.raises(SCAPContentHandlerError) as excinfo:
95
        ch.select_checklist("", "")
96
        profiles = ch.get_profiles()
97
    assert "For XCCDF documents, the data_stream_id and " \
98
        "checklist_id must be both None." in str(excinfo.value)
99
100
101
def test_sds():
102
    ch = SCAPContentHandler(DS_FILEPATH)
103
    checklists = ch.get_data_streams_checklists()
104
    assert checklists == {DS_IDS: [CHK_FIRST_ID, CHK_SECOND_ID]}
105
106
    ch.select_checklist(DS_IDS, CHK_FIRST_ID)
107
    profiles = ch.get_profiles()
108
    assert len(profiles) == 2
109
    pinfo1 = ProfileInfo(
110
        id="xccdf_com.example_profile_my_profile",
111
        title="My testing profile",
112
        description="A profile for testing purposes.")
113
    assert pinfo1 in profiles
114
    pinfo2 = ProfileInfo(
115
        id="xccdf_com.example_profile_my_profile2",
116
        title="My testing profile2",
117
        description="Another profile for testing purposes.")
118
    assert pinfo2 in profiles
119
120
    ch.select_checklist(DS_IDS, CHK_SECOND_ID)
121
    profiles2 = ch.get_profiles()
122
    assert len(profiles2) == 1
123
    pinfo3 = ProfileInfo(
124
        id="xccdf_com.example_profile_my_profile3",
125
        title="My testing profile3",
126
        description="Yet another profile for testing purposes.")
127
128
129
def test_sds_get_profiles_fails():
130
    ch = SCAPContentHandler(DS_FILEPATH)
131
132
    with pytest.raises(SCAPContentHandlerError) as excinfo:
133
        profiles = ch.get_profiles()
134
    assert "For SCAP source data streams, data_stream_id and " \
135
        "checklist_id must be both different than None" in str(excinfo.value)
136
137
    with pytest.raises(SCAPContentHandlerError) as excinfo:
138
        ch.select_checklist(DS_IDS, checklist_id=None)
139
        profiles = ch.get_profiles()
140
    assert "For SCAP source data streams, data_stream_id and " \
141
        "checklist_id must be both different than None" in str(excinfo.value)
142
143
    with pytest.raises(SCAPContentHandlerError) as excinfo:
144
        wrong_cref = "scap_org.open-scap_cref_seventh-xccdf.xml"
145
        ch.select_checklist(DS_IDS, wrong_cref)
146
        profiles = ch.get_profiles()
147
    assert f"Can't find ds:component-ref with id='{wrong_cref}' in " \
148
        f"ds:datastream with id='{DS_IDS}'" in str(excinfo.value)
149
150
151
def test_tailoring():
152
    ch = SCAPContentHandler(DS_FILEPATH, TAILORING_FILEPATH)
153
    checklists = ch.get_data_streams_checklists()
154
    assert checklists == {DS_IDS: [CHK_FIRST_ID, CHK_SECOND_ID]}
155
    ch.select_checklist(DS_IDS, CHK_FIRST_ID)
156
    profiles = ch.get_profiles()
157
    assert len(profiles) == 4
158
    pinfo1 = ProfileInfo(
159
        id="xccdf_com.example_profile_my_profile_tailored",
160
        title="My testing profile tailored",
161
        description="")
162
    assert pinfo1 in profiles
163
    pinfo2 = ProfileInfo(
164
        id="xccdf_com.example_profile_my_profile2_tailored",
165
        title="My testing profile2 tailored",
166
        description="")
167
    assert pinfo2 in profiles
168
    # it should also include the profiles of the original benchmark
169
    pinfo3 = ProfileInfo(
170
        id="xccdf_com.example_profile_my_profile",
171
        title="My testing profile",
172
        description="A profile for testing purposes.")
173
    assert pinfo3 in profiles
174
    pinfo4 = ProfileInfo(
175
        id="xccdf_com.example_profile_my_profile2",
176
        title="My testing profile2",
177
        description="Another profile for testing purposes.")
178
    assert pinfo4 in profiles
179
180
181
def test_default_profile():
182
    xccdf_filepath = os.path.join(TESTING_FILES_PATH, "testing_xccdf.xml")
183
    ch = SCAPContentHandler(xccdf_filepath)
184
    checklists = ch.get_data_streams_checklists()
185
    assert checklists is None
186
    profiles = ch.get_profiles()
187
    assert len(profiles) == 1
188
    pinfo1 = ProfileInfo(
189
        id="default",
190
        title="Default",
191
        description="The implicit XCCDF profile. Usually, the default profile "
192
        "contains no rules.")
193
    assert pinfo1 in profiles
194