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

test_ks_oscap.test_rpm_with_wrong_suffix()   A

Complexity

Conditions 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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