Passed
Pull Request — master (#202)
by Matěj
01:46 queued 35s
created

test_service_kickstart.test_invalid_fingerprints()   A

Complexity

Conditions 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
"""Module with tests for the ks/oscap.py module."""
2
3
import os
4
5
from pykickstart.errors import KickstartValueError
6
import pytest
7
8
try:
9
    from org_fedora_oscap.service.kickstart import OSCAPKickstartData
10
    from org_fedora_oscap import common
11
except ImportError as exc:
12
    pytestmark = pytest.mark.skip(
13
        "Unable to import modules, possibly due to bad version of Anaconda: {error}"
14
        .format(error=str(exc)))
15
16
17
@pytest.fixture()
18
def blank_oscap_data():
19
    return OSCAPKickstartData()
20
21
22
@pytest.fixture()
23
def filled_oscap_data(blank_oscap_data):
24
    oscap_data = blank_oscap_data
25
    for line in [
26
            "content-type = datastream\n",
27
            "content-url = \"https://example.com/hardening.xml\"\n",
28
            "datastream-id = id_datastream_1\n",
29
            "xccdf-id = id_xccdf_new\n",
30
            "content-path = /usr/share/oscap/testing_ds.xml",
31
            "cpe-path = /usr/share/oscap/cpe.xml",
32
            "tailoring-path = /usr/share/oscap/tailoring.xml",
33
            "profile = \"Web Server\"\n",
34
            ]:
35
        oscap_data.handle_line(line)
36
    return oscap_data
37
38
39
def test_parsing(filled_oscap_data):
40
    data = filled_oscap_data.policy_data
41
    assert data.content_type == "datastream"
42
    assert data.content_url == "https://example.com/hardening.xml"
43
    assert data.datastream_id == "id_datastream_1"
44
    assert data.xccdf_id == "id_xccdf_new"
45
    assert data.content_path == "/usr/share/oscap/testing_ds.xml"
46
    assert data.cpe_path == "/usr/share/oscap/cpe.xml"
47
    assert data.profile_id == "Web Server"
48
    assert data.tailoring_path == "/usr/share/oscap/tailoring.xml"
49
50
51
def test_properties(filled_oscap_data):
52
    data = filled_oscap_data
53
    assert (data.preinst_content_path
54
            == common.INSTALLATION_CONTENT_DIR + data.content_name)
55
    assert (data.postinst_content_path
56
            == common.TARGET_CONTENT_DIR + data.content_name)
57
    assert (data.raw_preinst_content_path
58
            == common.INSTALLATION_CONTENT_DIR + data.content_name)
59
    assert (data.preinst_tailoring_path
60
            == os.path.normpath(common.INSTALLATION_CONTENT_DIR + data.policy_data.tailoring_path))
61
    assert (data.postinst_tailoring_path
62
            == os.path.normpath(common.TARGET_CONTENT_DIR + data.policy_data.tailoring_path))
63
64
65
def test_str(filled_oscap_data):
66
    str_ret = str(filled_oscap_data)
67
    assert (str_ret ==
68
            "%addon org_fedora_oscap\n"
69
            "    content-type = datastream\n"
70
            "    content-url = https://example.com/hardening.xml\n"
71
            "    datastream-id = id_datastream_1\n"
72
            "    xccdf-id = id_xccdf_new\n"
73
            "    content-path = /usr/share/oscap/testing_ds.xml\n"
74
            "    cpe-path = /usr/share/oscap/cpe.xml\n"
75
            "    tailoring-path = /usr/share/oscap/tailoring.xml\n"
76
            "    profile = Web Server\n"
77
            "%end\n\n"
78
            )
79
80
81
def test_str_parse(filled_oscap_data):
82
    our_oscap_data = OSCAPKickstartData()
83
84
    str_ret = str(filled_oscap_data)
85
    for line in str_ret.splitlines()[1:-1]:
86
        if "%end" not in line:
87
            our_oscap_data.handle_line(line)
88
89
    our_str_ret = str(our_oscap_data)
90
    assert str_ret == our_str_ret
91
92
93
def test_nothing_given(blank_oscap_data):
94
    with pytest.raises(KickstartValueError):
95
        blank_oscap_data.handle_end()
96
97
98
def test_no_content_type(blank_oscap_data):
99
    for line in ["content-url = http://example.com/test_ds.xml",
100
                 "profile = Web Server",
101
                 ]:
102
        blank_oscap_data.handle_line(line)
103
104
    with pytest.raises(KickstartValueError):
105
        blank_oscap_data.handle_end()
106
107
108
def test_no_content_url(blank_oscap_data):
109
    for line in ["content-type = datastream",
110
                 "profile = Web Server",
111
                 ]:
112
        blank_oscap_data.handle_line(line)
113
114
    with pytest.raises(KickstartValueError):
115
        blank_oscap_data.handle_end()
116
117
118
def test_no_profile(blank_oscap_data):
119
    for line in ["content-url = http://example.com/test_ds.xml",
120
                 "content-type = datastream",
121
                 ]:
122
        blank_oscap_data.handle_line(line)
123
124
    blank_oscap_data.handle_end()
125
    assert blank_oscap_data.policy_data.profile_id == "default"
126
127
128
def test_rpm_without_path(blank_oscap_data):
129
    for line in ["content-url = http://example.com/oscap_content.rpm",
130
                 "content-type = RPM",
131
                 "profile = Web Server",
132
                 ]:
133
        blank_oscap_data.handle_line(line)
134
135
    with pytest.raises(KickstartValueError):
136
        blank_oscap_data.handle_end()
137
138
139
def test_rpm_with_wrong_suffix(blank_oscap_data):
140
    for line in ["content-url = http://example.com/oscap_content.xml",
141
                 "content-type = RPM",
142
                 "profile = Web Server",
143
                 ]:
144
        blank_oscap_data.handle_line(line)
145
146
    with pytest.raises(KickstartValueError):
147
        blank_oscap_data.handle_end()
148
149
150
def test_archive_without_path(blank_oscap_data):
151
    for line in ["content-url = http://example.com/oscap_content.tar",
152
                 "content-type = archive",
153
                 "profile = Web Server",
154
                 ]:
155
        blank_oscap_data.handle_line(line)
156
157
    with pytest.raises(KickstartValueError):
158
        blank_oscap_data.handle_end()
159
160
161
def test_unsupported_archive_type(blank_oscap_data):
162
    for line in ["content-url = http://example.com/oscap_content.tbz",
163
                 "content-type = archive",
164
                 "profile = Web Server",
165
                 "xccdf-path = xccdf.xml"
166
                 ]:
167
        blank_oscap_data.handle_line(line)
168
169
    with pytest.raises(KickstartValueError):
170
        blank_oscap_data.handle_end()
171
172
173
def test_enough_for_ds(blank_oscap_data):
174
    for line in ["content-url = http://example.com/test_ds.xml",
175
                 "content-type = datastream",
176
                 "profile = Web Server",
177
                 ]:
178
        blank_oscap_data.handle_line(line)
179
180
    blank_oscap_data.handle_end()
181
182
183
def test_enough_for_rpm(blank_oscap_data):
184
    for line in ["content-url = http://example.com/oscap_content.rpm",
185
                 "content-type = RPM",
186
                 "profile = Web Server",
187
                 "xccdf-path = /usr/share/oscap/xccdf.xml"
188
                 ]:
189
        blank_oscap_data.handle_line(line)
190
191
    blank_oscap_data.handle_end()
192
193
194
def test_enough_for_archive(blank_oscap_data):
195
    for line in ["content-url = http://example.com/oscap_content.tar",
196
                 "content-type = archive",
197
                 "profile = Web Server",
198
                 "xccdf-path = /usr/share/oscap/xccdf.xml"
199
                 ]:
200
        blank_oscap_data.handle_line(line)
201
202
    blank_oscap_data.handle_end()
203
204
205
def test_archive_preinst_content_path(blank_oscap_data):
206
    for line in ["content-url = http://example.com/oscap_content.tar",
207
                 "content-type = archive",
208
                 "profile = Web Server",
209
                 "xccdf-path = oscap/xccdf.xml"
210
                 ]:
211
        blank_oscap_data.handle_line(line)
212
213
    blank_oscap_data.handle_end()
214
215
    # content_name should be the archive's name
216
    assert blank_oscap_data.content_name == "oscap_content.tar"
217
218
    # content path should end with the xccdf path
219
    assert blank_oscap_data.preinst_content_path.endswith("oscap/xccdf.xml")
220
221
222
def test_ds_preinst_content_path(blank_oscap_data):
223
    for line in ["content-url = http://example.com/scap_content.xml",
224
                 "content-type = datastream",
225
                 "profile = Web Server",
226
                 ]:
227
        blank_oscap_data.handle_line(line)
228
229
    blank_oscap_data.handle_end()
230
231
    # both content_name and content path should point to the data stream
232
    # XML
233
    assert blank_oscap_data.content_name == "scap_content.xml"
234
    assert blank_oscap_data.preinst_content_path.endswith("scap_content.xml")
235
236
237
def test_archive_raw_content_paths(blank_oscap_data):
238
    for line in ["content-url = http://example.com/oscap_content.tar",
239
                 "content-type = archive",
240
                 "profile = Web Server",
241
                 "xccdf-path = oscap/xccdf.xml",
242
                 "tailoring-path = oscap/tailoring.xml",
243
                 ]:
244
        blank_oscap_data.handle_line(line)
245
246
    blank_oscap_data.handle_end()
247
248
    # content_name should be the archive's name
249
    assert blank_oscap_data.content_name == "oscap_content.tar"
250
251
    # content path should end with the archive's name
252
    assert blank_oscap_data.raw_preinst_content_path.endswith("oscap_content.tar")
253
254
    # tailoring paths should be returned properly
255
    assert (blank_oscap_data.preinst_tailoring_path
256
            == common.INSTALLATION_CONTENT_DIR + blank_oscap_data.policy_data.tailoring_path)
257
258
    assert (blank_oscap_data.postinst_tailoring_path
259
            == common.TARGET_CONTENT_DIR + blank_oscap_data.policy_data.tailoring_path)
260
261
262
def test_rpm_raw_content_paths(blank_oscap_data):
263
    for line in ["content-url = http://example.com/oscap_content.rpm",
264
                 "content-type = rpm",
265
                 "profile = Web Server",
266
                 "xccdf-path = /usr/share/oscap/xccdf.xml",
267
                 "tailoring-path = /usr/share/oscap/tailoring.xml",
268
                 ]:
269
        blank_oscap_data.handle_line(line)
270
271
    blank_oscap_data.handle_end()
272
273
    # content_name should be the rpm's name
274
    assert blank_oscap_data.content_name == "oscap_content.rpm"
275
276
    # content path should end with the rpm's name
277
    assert blank_oscap_data.raw_preinst_content_path.endswith("oscap_content.rpm")
278
279
    # content paths should be returned as expected
280
    assert (blank_oscap_data.preinst_content_path
281
            == os.path.normpath(common.INSTALLATION_CONTENT_DIR + blank_oscap_data.policy_data.content_path))
282
283
    # when using rpm, content_path doesn't change for the post-installation
284
    # phase
285
    assert blank_oscap_data.postinst_content_path == blank_oscap_data.policy_data.content_path
286
287
288
def test_ds_raw_content_paths(blank_oscap_data):
289
    for line in ["content-url = http://example.com/scap_content.xml",
290
                 "content-type = datastream",
291
                 "profile = Web Server",
292
                 ]:
293
        blank_oscap_data.handle_line(line)
294
295
    blank_oscap_data.handle_end()
296
297
    # content_name and content paths should all point to the data stream
298
    # XML
299
    assert blank_oscap_data.content_name == "scap_content.xml"
300
    assert blank_oscap_data.raw_preinst_content_path.endswith("scap_content.xml")
301
302
303
def test_valid_fingerprints(blank_oscap_data):
304
    for repetitions in (32, 40, 56, 64, 96, 128):
305
        blank_oscap_data.handle_line("fingerprint = %s" % ("a" * repetitions))
306
307
308
def test_invalid_fingerprints(blank_oscap_data):
309
    # invalid character
310
    with pytest.raises(KickstartValueError, match="Unsupported or invalid fingerprint"):
311
        blank_oscap_data.handle_line("fingerprint = %s?" % ("a" * 31))
312
313
    # invalid lengths (odd and even)
314
    for repetitions in (31, 41, 54, 66, 98, 124):
315
        with pytest.raises(
316
                KickstartValueError, match="Unsupported fingerprint"):
317
            blank_oscap_data.handle_line("fingerprint = %s" % ("a" * repetitions))
318