Completed
Push — master ( f4924a...c72d79 )
by Matěj
16s queued 13s
created

test_kickstart.test_rpm()   A

Complexity

Conditions 1

Size

Total Lines 20
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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