Passed
Pull Request — rhel7-branch (#72)
by Matěj
57s
created

test_ks_oscap.test_enough_for_rpm()   A

Complexity

Conditions 2

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