Passed
Pull Request — rhel8-branch (#178)
by Matěj
01:59
created

test_kickstart   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 304
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 94
dl 0
loc 304
rs 10
c 0
b 0
f 0
wmc 23

18 Functions

Rating   Name   Duplication   Size   Complexity  
A test_data() 0 11 1
A test_no_content_type() 0 9 1
A test_fingerprints() 0 26 3
A test_no_content_url() 0 9 1
A test_archive() 0 20 1
A test_rpm_without_path() 0 10 1
A test_rpm() 0 20 1
A test_rpm_with_wrong_suffix() 0 11 1
A check_ks_output() 0 8 1
A test_scap_security_guide() 0 12 2
A check_ks_input() 0 17 3
A test_no_profile() 0 19 1
A test_archive_without_path() 0 10 1
A test_org_fedora_oscap() 0 9 1
A test_default() 0 2 1
A service() 0 3 1
A test_datastream() 0 28 1
A test_section_confusion() 0 14 1
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 org_fedora_oscap.service.oscap import OSCAPService
21
from org_fedora_oscap import common
22
23
24
@pytest.fixture()
25
def service():
26
    return OSCAPService()
27
28
29
def check_ks_input(ks_service, ks_in, errors=None, warnings=None):
30
    """Read a provided kickstart string.
31
32
    :param ks_service: the kickstart service
33
    :param ks_in: a kickstart string
34
    :param errors: a list of expected errors
35
    :param warnings: a list of expected warning
36
    """
37
    errors = errors or []
38
    warnings = warnings or []
39
    report = ks_service.read_kickstart(ks_in)
40
41
    for index, error in enumerate(report.error_messages):
42
        assert errors[index] in error.message
43
44
    for index, warning in enumerate(report.warning_messages):
45
        assert warnings[index] in warning.message
46
47
48
def check_ks_output(ks_service, ks_out):
49
    """Generate a new kickstart string.
50
51
    :param ks_service: a kickstart service
52
    :param ks_out: an expected kickstart string
53
    """
54
    output = ks_service.generate_kickstart()
55
    assert output.strip() == dedent(ks_out).strip()
56
57
58
def test_default(service):
59
    check_ks_output(service, "")
60
61
62
def test_data(service):
63
    ks_in = """
64
    %addon com_redhat_oscap
65
        content-type = datastream
66
        content-url = "https://example.com/hardening.xml"
67
    %end
68
    """
69
    check_ks_input(service, ks_in)
70
71
    assert service.policy_data.content_type == "datastream"
72
    assert service.policy_data.content_url == "https://example.com/hardening.xml"
73
74
75
def test_datastream(service):
76
    ks_in = """
77
    %addon com_redhat_oscap
78
        content-type = datastream
79
        content-url = "https://example.com/hardening.xml"
80
        datastream-id = id_datastream_1
81
        xccdf-id = id_xccdf_new
82
        content-path = /usr/share/oscap/testing_ds.xml
83
        cpe-path = /usr/share/oscap/cpe.xml
84
        tailoring-path = /usr/share/oscap/tailoring.xml
85
        profile = "Web Server"
86
    %end
87
    """
88
    check_ks_input(service, ks_in)
89
90
    ks_out = """
91
    %addon com_redhat_oscap
92
        content-type = datastream
93
        content-url = https://example.com/hardening.xml
94
        datastream-id = id_datastream_1
95
        xccdf-id = id_xccdf_new
96
        content-path = /usr/share/oscap/testing_ds.xml
97
        cpe-path = /usr/share/oscap/cpe.xml
98
        tailoring-path = /usr/share/oscap/tailoring.xml
99
        profile = Web Server
100
    %end
101
    """
102
    check_ks_output(service, ks_out)
103
104
105
def test_no_content_type(service):
106
    ks_in = """
107
    %addon com_redhat_oscap
108
        content-url = http://example.com/test_ds.xml
109
        profile = Web Server
110
    %end
111
    """
112
    check_ks_input(service, ks_in, errors=[
113
        "content-type missing for the com_redhat_oscap addon"
114
    ])
115
116
117
def test_no_content_url(service):
118
    ks_in = """
119
    %addon com_redhat_oscap
120
        content-type = datastream
121
        profile = Web Server
122
    %end
123
    """
124
    check_ks_input(service, ks_in, errors=[
125
        "content-url missing for the com_redhat_oscap addon"
126
    ])
127
128
129
def test_no_profile(service):
130
    ks_in = """
131
    %addon com_redhat_oscap
132
        content-url = http://example.com/test_ds.xml
133
        content-type = datastream
134
    %end
135
    """
136
    check_ks_input(service, ks_in)
137
138
    ks_out = """
139
    %addon com_redhat_oscap
140
        content-type = datastream
141
        content-url = http://example.com/test_ds.xml
142
        profile = default
143
    %end
144
    """
145
    check_ks_output(service, ks_out)
146
147
    assert service.policy_data.profile_id == "default"
148
149
150
def test_rpm(service):
151
    ks_in = """
152
    %addon com_redhat_oscap
153
        content-url = http://example.com/oscap_content.rpm
154
        content-type = RPM
155
        profile = Web Server
156
        xccdf-path = /usr/share/oscap/xccdf.xml
157
    %end
158
    """
159
    check_ks_input(service, ks_in)
160
161
    ks_out = """
162
    %addon com_redhat_oscap
163
        content-type = rpm
164
        content-url = http://example.com/oscap_content.rpm
165
        content-path = /usr/share/oscap/xccdf.xml
166
        profile = Web Server
167
    %end
168
    """
169
    check_ks_output(service, ks_out)
170
171
172
def test_rpm_without_path(service):
173
    ks_in = """
174
    %addon com_redhat_oscap
175
        content-url = http://example.com/oscap_content.rpm
176
        content-type = RPM
177
        profile = Web Server
178
    %end
179
    """
180
    check_ks_input(service, ks_in, errors=[
181
        "Path to the XCCDF file has to be given if content in RPM or archive is used"
182
    ])
183
184
185
def test_rpm_with_wrong_suffix(service):
186
    ks_in = """
187
    %addon com_redhat_oscap
188
        content-url = http://example.com/oscap_content.xml
189
        content-type = RPM
190
        profile = Web Server
191
        xccdf-path = /usr/share/oscap/xccdf.xml
192
    %end
193
    """
194
    check_ks_input(service, ks_in, errors=[
195
        "Content type set to RPM, but the content URL doesn't end with '.rpm'"
196
    ])
197
198
199
def test_archive(service):
200
    ks_in = """
201
    %addon com_redhat_oscap
202
        content-url = http://example.com/oscap_content.tar
203
        content-type = archive
204
        profile = Web Server
205
        xccdf-path = oscap/xccdf.xml
206
    %end
207
    """
208
    check_ks_input(service, ks_in)
209
210
    ks_out = """
211
    %addon com_redhat_oscap
212
        content-type = archive
213
        content-url = http://example.com/oscap_content.tar
214
        content-path = oscap/xccdf.xml
215
        profile = Web Server
216
    %end
217
    """
218
    check_ks_output(service, ks_out)
219
220
221
def test_archive_without_path(service):
222
    ks_in = """
223
    %addon com_redhat_oscap
224
        content-url = http://example.com/oscap_content.tar
225
        content-type = archive
226
        profile = Web Server
227
    %end
228
    """
229
    check_ks_input(service, ks_in, errors=[
230
        "Path to the XCCDF file has to be given if content in RPM or archive is used"
231
    ])
232
233
234
def test_org_fedora_oscap(service):
235
    ks_in = """
236
    %addon org_fedora_oscap
237
        content-type = datastream
238
        content-url = "https://example.com/hardening.xml"
239
    %end
240
    """
241
    check_ks_input(service, ks_in, warnings=[
242
        "org_fedora_oscap"
243
    ])
244
245
246
def test_section_confusion(service):
247
    ks_in = """
248
    %addon org_fedora_oscap
249
        content-type = datastream
250
        content-url = "https://example.com/hardening.xml"
251
    %end
252
253
    %addon com_redhat_oscap
254
        content-type = datastream
255
        content-url = "https://example.com/hardening.xml"
256
    %end
257
    """
258
    check_ks_input(service, ks_in, errors=[
259
        "You have used more than one oscap addon sections in the kickstart."
260
    ])
261
262
263
def test_scap_security_guide(service):
264
    if common.ssg_available():
265
        pytest.skip("Test works only if scap-security-guide is not installed")
266
267
    ks_in = """
268
    %addon com_redhat_oscap
269
        content-type = scap-security-guide
270
        profile = Web Server
271
    %end
272
    """
273
    check_ks_input(service, ks_in, errors=[
274
        "SCAP Security Guide not found on the system"
275
    ])
276
277
278
def test_fingerprints(service):
279
    ks_template = """
280
    %addon com_redhat_oscap
281
        content-url = http://example.com/test_ds.xml
282
        content-type = datastream
283
        fingerprint = {}
284
    %end
285
    """
286
287
    # invalid character
288
    ks_in = ks_template.format("a" * 31 + "?")
289
    check_ks_input(service, ks_in, errors=[
290
        "Unsupported or invalid fingerprint"
291
    ])
292
293
    # invalid lengths (odd and even)
294
    for repetitions in (31, 41, 54, 66, 98, 124):
295
        ks_in = ks_template.format("a" * repetitions)
296
        check_ks_input(service, ks_in, errors=[
297
            "Unsupported fingerprint"
298
        ])
299
300
    # valid values
301
    for repetitions in (32, 40, 56, 64, 96, 128):
302
        ks_in = ks_template.format("a" * repetitions)
303
        check_ks_input(service, ks_in)
304