1
|
|
|
# Copyright 2022, Red Hat, Inc. |
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
import pytest |
6
|
|
|
|
7
|
|
|
from openscap_report.scap_results_parser.data_structures.json_transformation import ( |
8
|
|
|
is_not_empty, rearrange_identifiers, rearrange_references, |
9
|
|
|
remove_empty_values, remove_not_selected_rules) |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
@pytest.mark.unit_test |
13
|
|
|
@pytest.mark.parametrize( |
14
|
|
|
"dictionary_json, result", |
15
|
|
|
[ |
16
|
|
|
( |
17
|
|
|
{ |
18
|
|
|
"rules": { |
19
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
20
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
21
|
|
|
"identifiers": [ |
22
|
|
|
{ |
23
|
|
|
"text": "CCE-90841-8", |
24
|
|
|
"system": "random-link.com", |
25
|
|
|
} |
26
|
|
|
], |
27
|
|
|
"references": [ |
28
|
|
|
{ |
29
|
|
|
"text": "11", |
30
|
|
|
"href": "idk-link.com", |
31
|
|
|
} |
32
|
|
|
], |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
}, |
36
|
|
|
{ |
37
|
|
|
"rules": { |
38
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
39
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
40
|
|
|
"identifiers": [ |
41
|
|
|
{ |
42
|
|
|
"text": "CCE-90841-8", |
43
|
|
|
"system": "random-link.com", |
44
|
|
|
} |
45
|
|
|
], |
46
|
|
|
"references": [ |
47
|
|
|
"11", |
48
|
|
|
], |
49
|
|
|
} |
50
|
|
|
}, |
51
|
|
|
"references": {"11": "idk-link.com"}, |
52
|
|
|
}, |
53
|
|
|
) |
54
|
|
|
], |
55
|
|
|
) |
56
|
|
|
def test_rearrange_references(dictionary_json, result): |
57
|
|
|
rearrange_references(dictionary_json) |
58
|
|
|
assert dictionary_json == result |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
@pytest.mark.unit_test |
62
|
|
|
@pytest.mark.parametrize( |
63
|
|
|
"dictionary_json, result", |
64
|
|
|
[ |
65
|
|
|
( |
66
|
|
|
{ |
67
|
|
|
"rules": { |
68
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
69
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
70
|
|
|
"identifiers": [ |
71
|
|
|
{ |
72
|
|
|
"text": "CCE-90841-8", |
73
|
|
|
"system": "random-link.com", |
74
|
|
|
} |
75
|
|
|
], |
76
|
|
|
"references": [ |
77
|
|
|
{ |
78
|
|
|
"text": "11", |
79
|
|
|
"href": "idk-link.com", |
80
|
|
|
} |
81
|
|
|
], |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
}, |
85
|
|
|
{ |
86
|
|
|
"rules": { |
87
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
88
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
89
|
|
|
"identifiers": ["CCE-90841-8"], |
90
|
|
|
"references": [ |
91
|
|
|
{ |
92
|
|
|
"text": "11", |
93
|
|
|
"href": "idk-link.com", |
94
|
|
|
} |
95
|
|
|
], |
96
|
|
|
} |
97
|
|
|
}, |
98
|
|
|
"identifiers": {"CCE-90841-8": "random-link.com"}, |
99
|
|
|
}, |
100
|
|
|
) |
101
|
|
|
], |
102
|
|
|
) |
103
|
|
|
def test_rearrange_identifiers(dictionary_json, result): |
104
|
|
|
rearrange_identifiers(dictionary_json) |
105
|
|
|
print(dictionary_json) |
106
|
|
|
print("aaaa") |
107
|
|
|
print(result) |
108
|
|
|
assert dictionary_json == result |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
@pytest.mark.unit_test |
112
|
|
|
@pytest.mark.parametrize( |
113
|
|
|
"val, result", |
114
|
|
|
[ |
115
|
|
|
(None, False), |
116
|
|
|
([], False), |
117
|
|
|
({}, False), |
118
|
|
|
(1.3, True), |
119
|
|
|
(0.0, True), |
120
|
|
|
(-1.3, True), |
121
|
|
|
(["a", "b"], True), |
122
|
|
|
({"a": "b", "c": "d"}, True), |
123
|
|
|
], |
124
|
|
|
) |
125
|
|
|
def test_is_not_empty(val, result): |
126
|
|
|
assert is_not_empty(val) == result |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
@pytest.mark.unit_test |
130
|
|
|
@pytest.mark.parametrize( |
131
|
|
|
"dictionary_json, ids_of_selected_rules, result", |
132
|
|
|
[ |
133
|
|
|
( |
134
|
|
|
{ |
135
|
|
|
"rules": { |
136
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
137
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
138
|
|
|
"result": "pass", |
139
|
|
|
}, |
140
|
|
|
"rule_a": { |
141
|
|
|
"rule_id": "rule_a", |
142
|
|
|
"result": "notselected", |
143
|
|
|
}, |
144
|
|
|
} |
145
|
|
|
}, |
146
|
|
|
["xccdf_org.ssgproject.content_rule_rpm_verify_hashes"], |
147
|
|
|
{ |
148
|
|
|
"rules": { |
149
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
150
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
151
|
|
|
"result": "pass", |
152
|
|
|
}, |
153
|
|
|
} |
154
|
|
|
}, |
155
|
|
|
), |
156
|
|
|
( |
157
|
|
|
{ |
158
|
|
|
"rules": { |
159
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
160
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
161
|
|
|
"result": "pass", |
162
|
|
|
}, |
163
|
|
|
"rule_a": { |
164
|
|
|
"rule_id": "rule_a", |
165
|
|
|
"result": "notselected", |
166
|
|
|
}, |
167
|
|
|
} |
168
|
|
|
}, |
169
|
|
|
[], |
170
|
|
|
{ |
171
|
|
|
"rules": { |
172
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
173
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
174
|
|
|
"result": "pass", |
175
|
|
|
}, |
176
|
|
|
} |
177
|
|
|
}, |
178
|
|
|
), |
179
|
|
|
( |
180
|
|
|
{ |
181
|
|
|
"rules": { |
182
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
183
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
184
|
|
|
"result": "pass", |
185
|
|
|
}, |
186
|
|
|
"rule_a": { |
187
|
|
|
"rule_id": "rule_a", |
188
|
|
|
"result": "fail", |
189
|
|
|
}, |
190
|
|
|
} |
191
|
|
|
}, |
192
|
|
|
["xccdf_org.ssgproject.content_rule_rpm_verify_hashes"], |
193
|
|
|
{ |
194
|
|
|
"rules": { |
195
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
196
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
197
|
|
|
"result": "pass", |
198
|
|
|
}, |
199
|
|
|
} |
200
|
|
|
}, |
201
|
|
|
), |
202
|
|
|
( |
203
|
|
|
{ |
204
|
|
|
"rules": { |
205
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
206
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
207
|
|
|
"result": "pass", |
208
|
|
|
}, |
209
|
|
|
"rule_a": { |
210
|
|
|
"rule_id": "rule_a", |
211
|
|
|
"result": "fail", |
212
|
|
|
}, |
213
|
|
|
} |
214
|
|
|
}, |
215
|
|
|
["xccdf_org.ssgproject.content_rule_rpm_verify_hashes", "rule_a"], |
216
|
|
|
{ |
217
|
|
|
"rules": { |
218
|
|
|
"xccdf_org.ssgproject.content_rule_rpm_verify_hashes": { |
219
|
|
|
"rule_id": "xccdf_org.ssgproject.content_rule_rpm_verify_hashes", |
220
|
|
|
"result": "pass", |
221
|
|
|
}, |
222
|
|
|
"rule_a": { |
223
|
|
|
"rule_id": "rule_a", |
224
|
|
|
"result": "fail", |
225
|
|
|
}, |
226
|
|
|
} |
227
|
|
|
}, |
228
|
|
|
), |
229
|
|
|
], |
230
|
|
|
) |
231
|
|
|
def test_remove_not_selected_rules(dictionary_json, ids_of_selected_rules, result): |
232
|
|
|
remove_not_selected_rules(dictionary_json, ids_of_selected_rules) |
233
|
|
|
assert dictionary_json == result |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
@pytest.mark.unit_test |
237
|
|
|
@pytest.mark.parametrize( |
238
|
|
|
"dictionary, result", |
239
|
|
|
[ |
240
|
|
|
({"a": "", "b": {"c": ""}, "x": "x"}, {"x": "x"}), |
241
|
|
|
({"a": "a", "b": {"c": ""}, "x": "x"}, {"a": "a", "x": "x"}), |
242
|
|
|
({"a": [], "b": {"c": ""}, "x": "x"}, {"x": "x"}), |
243
|
|
|
({"a": {}, "b": {"c": ""}, "x": "x"}, {"x": "x"}), |
244
|
|
|
({"a": 3.14, "b": {"c": ""}, "x": "x"}, {"a": 3.14, "x": "x"}), |
245
|
|
|
( |
246
|
|
|
{"a": "", "b": {"c": {"z": "y"}, "m": "o"}, "x": "x"}, |
247
|
|
|
{"b": {"c": {"z": "y"}, "m": "o"}, "x": "x"}, |
248
|
|
|
), |
249
|
|
|
], |
250
|
|
|
) |
251
|
|
|
def test_remove_empty_values(dictionary, result): |
252
|
|
|
assert remove_empty_values(dictionary) == result |
253
|
|
|
|