Completed
Push — master ( 79f3e5...52b895 )
by Matěj
27s queued 14s
created

test_kickstart.check_ks_output()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
#
2
# Copyright (C) 2021  Red Hat, Inc.
3
#
4
# This copyrighted material is made available to anyone wishing to use,
5
# modify, copy, or redistribute it subject to the terms and conditions of
6
# the GNU General Public License v.2, or (at your option) any later version.
7
# This program is distributed in the hope that it will be useful, but WITHOUT
8
# ANY WARRANTY expressed or implied, including the implied warranties of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
10
# Public License for more details.  You should have received a copy of the
11
# GNU General Public License along with this program; if not, write to the
12
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
13
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
14
# source code or documentation are not subject to the GNU General Public
15
# License and may only be used or replicated with the express permission of
16
# Red Hat, Inc.
17
#
18
import pytest
19
from textwrap import dedent
20
from unittest.mock import Mock
21
from org_fedora_oscap.service.oscap import OSCAPService
22
23
24
@pytest.fixture()
25
def service():
26
    return OSCAPService()
27
28
29
@pytest.fixture()
30
def mock_ssg_available(monkeypatch):
31
    mocked_function = Mock(return_value=True)
32
    monkeypatch.setattr("org_fedora_oscap.common.ssg_available", mocked_function)
33
    return mocked_function
34
35
36
def check_ks_input(ks_service, ks_in, errors=None, warnings=None):
37
    """Read a provided kickstart string.
38
39
    :param ks_service: the kickstart service
40
    :param ks_in: a kickstart string
41
    :param errors: a list of expected errors
42
    :param warnings: a list of expected warning
43
    """
44
    errors = errors or []
45
    warnings = warnings or []
46
    report = ks_service.read_kickstart(ks_in)
47
48
    for index, error in enumerate(report.error_messages):
49
        assert errors[index] in error.message
50
51
    for index, warning in enumerate(report.warning_messages):
52
        assert warnings[index] in warning.message
53
54
55
def check_ks_output(ks_service, ks_out):
56
    """Generate a new kickstart string.
57
58
    :param ks_service: a kickstart service
59
    :param ks_out: an expected kickstart string
60
    """
61
    output = ks_service.generate_kickstart()
62
    assert output.strip() == dedent(ks_out).strip()
63
64
65
def test_default(service):
66
    check_ks_output(service, "")
67
68
69
def test_data(service):
70
    ks_in = """
71
    %addon com_redhat_oscap
72
        content-type = datastream
73
        content-url = "https://example.com/hardening.xml"
74
    %end
75
    """
76
    check_ks_input(service, ks_in)
77
78
    assert service.policy_data.content_type == "datastream"
79
    assert service.policy_data.content_url == "https://example.com/hardening.xml"
80
81
82
def test_datastream(service):
83
    ks_in = """
84
    %addon com_redhat_oscap
85
        content-type = datastream
86
        content-url = "https://example.com/hardening.xml"
87
        datastream-id = id_datastream_1
88
        xccdf-id = id_xccdf_new
89
        content-path = /usr/share/oscap/testing_ds.xml
90
        cpe-path = /usr/share/oscap/cpe.xml
91
        tailoring-path = /usr/share/oscap/tailoring.xml
92
        profile = "Web Server"
93
    %end
94
    """
95
    check_ks_input(service, ks_in)
96
97
    ks_out = """
98
    %addon com_redhat_oscap
99
        content-type = datastream
100
        content-url = https://example.com/hardening.xml
101
        datastream-id = id_datastream_1
102
        xccdf-id = id_xccdf_new
103
        content-path = /usr/share/oscap/testing_ds.xml
104
        cpe-path = /usr/share/oscap/cpe.xml
105
        tailoring-path = /usr/share/oscap/tailoring.xml
106
        profile = Web Server
107
    %end
108
    """
109
    check_ks_output(service, ks_out)
110
111
112
def test_no_content_type(service):
113
    ks_in = """
114
    %addon com_redhat_oscap
115
        content-url = http://example.com/test_ds.xml
116
        profile = Web Server
117
    %end
118
    """
119
    check_ks_input(service, ks_in, errors=[
120
        "content-type missing for the com_redhat_oscap addon"
121
    ])
122
123
124
def test_no_content_url(service):
125
    ks_in = """
126
    %addon com_redhat_oscap
127
        content-type = datastream
128
        profile = Web Server
129
    %end
130
    """
131
    check_ks_input(service, ks_in, errors=[
132
        "content-url missing for the com_redhat_oscap addon"
133
    ])
134
135
136
def test_no_profile(service):
137
    ks_in = """
138
    %addon com_redhat_oscap
139
        content-url = http://example.com/test_ds.xml
140
        content-type = datastream
141
    %end
142
    """
143
    check_ks_input(service, ks_in)
144
145
    ks_out = """
146
    %addon com_redhat_oscap
147
        content-type = datastream
148
        content-url = http://example.com/test_ds.xml
149
        profile = default
150
    %end
151
    """
152
    check_ks_output(service, ks_out)
153
154
    assert service.policy_data.profile_id == "default"
155
156
157
def test_rpm(service):
158
    ks_in = """
159
    %addon com_redhat_oscap
160
        content-url = http://example.com/oscap_content.rpm
161
        content-type = RPM
162
        profile = Web Server
163
        xccdf-path = /usr/share/oscap/xccdf.xml
164
    %end
165
    """
166
    check_ks_input(service, ks_in)
167
168
    ks_out = """
169
    %addon com_redhat_oscap
170
        content-type = rpm
171
        content-url = http://example.com/oscap_content.rpm
172
        content-path = /usr/share/oscap/xccdf.xml
173
        profile = Web Server
174
    %end
175
    """
176
    check_ks_output(service, ks_out)
177
178
179
def test_rpm_without_path(service):
180
    ks_in = """
181
    %addon com_redhat_oscap
182
        content-url = http://example.com/oscap_content.rpm
183
        content-type = RPM
184
        profile = Web Server
185
    %end
186
    """
187
    check_ks_input(service, ks_in, errors=[
188
        "Path to the XCCDF file has to be given if content in RPM or archive is used"
189
    ])
190
191
192
def test_rpm_with_wrong_suffix(service):
193
    ks_in = """
194
    %addon com_redhat_oscap
195
        content-url = http://example.com/oscap_content.xml
196
        content-type = RPM
197
        profile = Web Server
198
        xccdf-path = /usr/share/oscap/xccdf.xml
199
    %end
200
    """
201
    check_ks_input(service, ks_in, errors=[
202
        "Content type set to RPM, but the content URL doesn't end with '.rpm'"
203
    ])
204
205
206
def test_archive(service):
207
    ks_in = """
208
    %addon com_redhat_oscap
209
        content-url = http://example.com/oscap_content.tar
210
        content-type = archive
211
        profile = Web Server
212
        xccdf-path = oscap/xccdf.xml
213
    %end
214
    """
215
    check_ks_input(service, ks_in)
216
217
    ks_out = """
218
    %addon com_redhat_oscap
219
        content-type = archive
220
        content-url = http://example.com/oscap_content.tar
221
        content-path = oscap/xccdf.xml
222
        profile = Web Server
223
    %end
224
    """
225
    check_ks_output(service, ks_out)
226
227
228
def test_archive_without_path(service):
229
    ks_in = """
230
    %addon com_redhat_oscap
231
        content-url = http://example.com/oscap_content.tar
232
        content-type = archive
233
        profile = Web Server
234
    %end
235
    """
236
    check_ks_input(service, ks_in, errors=[
237
        "Path to the XCCDF file has to be given if content in RPM or archive is used"
238
    ])
239
240
241
def test_org_fedora_oscap(service):
242
    ks_in = """
243
    %addon org_fedora_oscap
244
        content-type = datastream
245
        content-url = "https://example.com/hardening.xml"
246
    %end
247
    """
248
    check_ks_input(service, ks_in, warnings=[
249
        "org_fedora_oscap"
250
    ])
251
252
253
def test_section_confusion(service):
254
    ks_in = """
255
    %addon org_fedora_oscap
256
        content-type = datastream
257
        content-url = "https://example.com/hardening.xml"
258
    %end
259
260
    %addon com_redhat_oscap
261
        content-type = datastream
262
        content-url = "https://example.com/hardening.xml"
263
    %end
264
    """
265
    check_ks_input(service, ks_in, errors=[
266
        "You have used more than one oscap addon sections in the kickstart."
267
    ])
268
269
270
def test_scap_security_guide(service, mock_ssg_available):
271
    ks_in = """
272
    %addon com_redhat_oscap
273
        content-type = scap-security-guide
274
        profile = Web Server
275
    %end
276
    """
277
278
    mock_ssg_available.return_value = False
279
    check_ks_input(service, ks_in, errors=[
280
        "SCAP Security Guide not found on the system"
281
    ])
282
283
    ks_out = """
284
    %addon com_redhat_oscap
285
        content-type = scap-security-guide
286
        profile = Web Server
287
    %end
288
    """
289
290
    mock_ssg_available.return_value = True
291
    check_ks_input(service, ks_in, ks_out)
292
293
294
def test_fingerprints(service):
295
    ks_template = """
296
    %addon com_redhat_oscap
297
        content-url = http://example.com/test_ds.xml
298
        content-type = datastream
299
        fingerprint = {}
300
    %end
301
    """
302
303
    # invalid character
304
    ks_in = ks_template.format("a" * 31 + "?")
305
    check_ks_input(service, ks_in, errors=[
306
        "Unsupported or invalid fingerprint"
307
    ])
308
309
    # invalid lengths (odd and even)
310
    for repetitions in (31, 41, 54, 66, 98, 124):
311
        ks_in = ks_template.format("a" * repetitions)
312
        check_ks_input(service, ks_in, errors=[
313
            "Unsupported fingerprint"
314
        ])
315
316
    # valid values
317
    for repetitions in (32, 40, 56, 64, 96, 128):
318
        ks_in = ks_template.format("a" * repetitions)
319
        check_ks_input(service, ks_in)
320