1
|
|
|
import tests.any_test_help |
2
|
|
|
import graph.oval_graph |
3
|
|
|
import os |
4
|
|
|
import py |
5
|
|
|
import pytest |
6
|
|
|
import json |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def test_create_node_dict_for_sigmaJs_0(): |
10
|
|
|
out = { |
11
|
|
|
'type': 'circle', |
12
|
|
|
'borderColor': '#ff0000', |
13
|
|
|
'color': '#ff0000', |
14
|
|
|
'id': 1, |
15
|
|
|
'label': 'and', |
16
|
|
|
'size': 3, |
17
|
|
|
'text': 'null', |
18
|
|
|
'url': 'null', |
19
|
|
|
'title': 1, |
20
|
|
|
'x': 0, |
21
|
|
|
'y': 0 |
22
|
|
|
} |
23
|
|
|
Tree = tests.any_test_help.get_simple_tree() |
24
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def test_create_node_dict_for_sigmaJs_1(): |
28
|
|
|
out = { |
29
|
|
|
'type': 'circle', |
30
|
|
|
'borderColor': '#00ff00', |
31
|
|
|
'color': '#00ff00', |
32
|
|
|
'id': 1, |
33
|
|
|
'label': 'and', |
34
|
|
|
'size': 3, |
35
|
|
|
'text': 'null', |
36
|
|
|
'url': 'null', |
37
|
|
|
'title': 1, |
38
|
|
|
'x': 0, |
39
|
|
|
'y': 0 |
40
|
|
|
} |
41
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
42
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", False) |
43
|
|
|
] |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
def test_create_node_dict_for_sigmaJs_2(): |
50
|
|
|
out = { |
51
|
|
|
'type': 'circle', |
52
|
|
|
'borderColor': '#000000', |
53
|
|
|
'color': '#000000', |
54
|
|
|
'id': 1, |
55
|
|
|
'label': 'and', |
56
|
|
|
'size': 3, |
57
|
|
|
'text': 'null', |
58
|
|
|
'url': 'null', |
59
|
|
|
'title': '1 and', |
60
|
|
|
'x': 0, |
61
|
|
|
'y': 0 |
62
|
|
|
} |
63
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
64
|
|
|
graph.oval_graph.OvalNode(2, 'value', "noteval", False) |
65
|
|
|
] |
66
|
|
|
) |
67
|
|
|
|
68
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_create_node_dict_for_sigmaJs_3(): |
72
|
|
|
out = { |
73
|
|
|
'type': 'circle', |
74
|
|
|
'borderColor': '#ff0000', |
75
|
|
|
'color': '#ff0000', |
76
|
|
|
'id': 1, |
77
|
|
|
'label': '1', |
78
|
|
|
'size': 3, |
79
|
|
|
'text': 'null', |
80
|
|
|
'url': 'null', |
81
|
|
|
'title': 1, |
82
|
|
|
'x': 0, |
83
|
|
|
'y': 0 |
84
|
|
|
} |
85
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'value', 'false', False) |
86
|
|
|
|
87
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
def test_create_node_dict_for_sigmaJs_4(): |
91
|
|
|
out = { |
92
|
|
|
'type': 'circle', |
93
|
|
|
'borderColor': '#00ff00', |
94
|
|
|
'color': '#00ff00', |
95
|
|
|
'id': 1, |
96
|
|
|
'label': '1', |
97
|
|
|
'size': 3, |
98
|
|
|
'text': 'null', |
99
|
|
|
'url': 'null', |
100
|
|
|
'title': 1, |
101
|
|
|
'x': 0, |
102
|
|
|
'y': 0 |
103
|
|
|
} |
104
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'value', 'true', False) |
105
|
|
|
|
106
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
def test_create_node_dict_for_sigmaJs_5(): |
110
|
|
|
out = { |
111
|
|
|
'type': 'circle', |
112
|
|
|
'borderColor': '#000000', |
113
|
|
|
'color': '#000000', |
114
|
|
|
'id': 1, |
115
|
|
|
'label': '1', |
116
|
|
|
'size': 3, |
117
|
|
|
'text': 'null', |
118
|
|
|
'url': 'null', |
119
|
|
|
'title': '1 error', |
120
|
|
|
'x': 0, |
121
|
|
|
'y': 0 |
122
|
|
|
} |
123
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'value', 'error', False) |
124
|
|
|
|
125
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def test_create_node_with_negation_dict_for_sigmaJs(): |
129
|
|
|
out = { |
130
|
|
|
'type': 'circle', |
131
|
|
|
'borderColor': '#ff0000', |
132
|
|
|
'color': '#00ff00', |
133
|
|
|
'id': 1, |
134
|
|
|
'label': 'and', |
135
|
|
|
'size': 3, |
136
|
|
|
'text': 'null', |
137
|
|
|
'url': 'null', |
138
|
|
|
'title': 1, |
139
|
|
|
'x': 0, |
140
|
|
|
'y': 0 |
141
|
|
|
} |
142
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', True, [ |
143
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", False) |
144
|
|
|
] |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
def test_create_node_with_negation_dict_for_sigmaJs1(): |
151
|
|
|
out = { |
152
|
|
|
'type': 'circle', |
153
|
|
|
'borderColor': '#00ff00', |
154
|
|
|
'color': '#ff0000', |
155
|
|
|
'id': 1, |
156
|
|
|
'label': 'and', |
157
|
|
|
'size': 3, |
158
|
|
|
'text': 'null', |
159
|
|
|
'url': 'null', |
160
|
|
|
'title': 1, |
161
|
|
|
'x': 0, |
162
|
|
|
'y': 0 |
163
|
|
|
} |
164
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', True, [ |
165
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", False) |
166
|
|
|
] |
167
|
|
|
) |
168
|
|
|
|
169
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out) |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
def test_create_node_with_negation_dict_for_sigmaJs2(): |
173
|
|
|
out = { |
174
|
|
|
'type': 'circle', |
175
|
|
|
'borderColor': '#00ff00', |
176
|
|
|
'color': '#ff0000', |
177
|
|
|
'id': 2, |
178
|
|
|
'label': '2', |
179
|
|
|
'size': 3, |
180
|
|
|
'text': 'null', |
181
|
|
|
'url': 'null', |
182
|
|
|
'title': 2, |
183
|
|
|
'x': 0, |
184
|
|
|
'y': 0 |
185
|
|
|
} |
186
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
187
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", True) |
188
|
|
|
] |
189
|
|
|
) |
190
|
|
|
|
191
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs( |
192
|
|
|
Tree.children[0], out) |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
def test_create_node_with_negation_dict_for_sigmaJs3(): |
196
|
|
|
out = { |
197
|
|
|
'type': 'circle', |
198
|
|
|
'borderColor': '#ff0000', |
199
|
|
|
'color': '#00ff00', |
200
|
|
|
'id': 2, |
201
|
|
|
'label': '2', |
202
|
|
|
'size': 3, |
203
|
|
|
'text': 'null', |
204
|
|
|
'url': 'null', |
205
|
|
|
'title': 2, |
206
|
|
|
'x': 0, |
207
|
|
|
'y': 0 |
208
|
|
|
} |
209
|
|
|
Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
210
|
|
|
graph.oval_graph.OvalNode(2, 'value', "false", True) |
211
|
|
|
] |
212
|
|
|
) |
213
|
|
|
|
214
|
|
|
tests.any_test_help.any_test_create_node_dict_for_sigmaJs( |
215
|
|
|
Tree.children[0], out) |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
def test_create_edge_dict_for_sigmaJs(): |
219
|
|
|
out = { |
220
|
|
|
'id': 'random_ID', |
221
|
|
|
'source': 1, |
222
|
|
|
'target': 2, |
223
|
|
|
'color': '#000000' |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
target_node = { |
227
|
|
|
'type': 'circle', |
228
|
|
|
'borderColor': '#000000', |
229
|
|
|
'color': '#000000', |
230
|
|
|
'id': 2, |
231
|
|
|
'label': 'error', |
232
|
|
|
'size': 3, |
233
|
|
|
'text': 'null', |
234
|
|
|
'url': 'null', |
235
|
|
|
'title': '1 error', |
236
|
|
|
'x': 0, |
237
|
|
|
'y': 0 |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
assert tests.any_test_help.get_simple_tree()._create_edge( |
241
|
|
|
1, 2, target_node)['source'] == out['source'] |
242
|
|
|
assert tests.any_test_help.get_simple_tree()._create_edge( |
243
|
|
|
1, 2, target_node)['target'] == out['target'] |
244
|
|
|
assert tests.any_test_help.get_simple_tree()._create_edge( |
245
|
|
|
1, 2, target_node)['color'] == out['color'] |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
def test_create_array_of_ids_form_tree(): |
249
|
|
|
array = tests.any_test_help.get_simple_tree().create_list_of_id() |
250
|
|
|
assert array == [1, 2, 3, 4, 5, 6] |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
def test_parsing_full_scan_XML_and_evaluate(): |
254
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
255
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny' |
256
|
|
|
result = 'false' |
257
|
|
|
|
258
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
259
|
|
|
src, rule_id, result) |
260
|
|
|
|
261
|
|
|
|
262
|
|
|
def test_parsing_and_evaluate_scan_with_extend_def(): |
263
|
|
|
src = 'test_data/ssg-fedora-ds-arf-scan-with-extend-definitions.xml' |
264
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_sysctl_net_ipv6_conf_all_disable_ipv6' |
265
|
|
|
result = 'false' |
266
|
|
|
|
267
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
268
|
|
|
src, rule_id, result) |
269
|
|
|
|
270
|
|
|
|
271
|
|
|
def test_parsing_and_evaluate_scan_with_pasing_rule(): |
272
|
|
|
src = 'test_data/ssg-fedora-ds-arf-passing-scan.xml' |
273
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_service_debug-shell_disabled' |
274
|
|
|
result = 'true' |
275
|
|
|
|
276
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
277
|
|
|
src, rule_id, result) |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
def test_parsing_and_evaluate_scan_with_fail_rule(): |
281
|
|
|
src = 'test_data/ssg-fedora-ds-arf-scan-fail.xml' |
282
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec' |
283
|
|
|
result = 'false' |
284
|
|
|
|
285
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
286
|
|
|
src, rule_id, result) |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
def test_parsing_and_evaluate_scan_with_rule_with_XOR(): |
290
|
|
|
src = 'test_data/ssg-fedora-ds-arf-scan-with-xor.xml' |
291
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_nosuid_removable_partitions' |
292
|
|
|
result = 'true' |
293
|
|
|
|
294
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
295
|
|
|
src, rule_id, result) |
296
|
|
|
|
297
|
|
|
|
298
|
|
|
def test_parsing_and_evaluate_scan_with_11_rules(): |
299
|
|
|
src = 'test_data/ssg-fedora-ds-arf-scan-with-11-rules.xml' |
300
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_tmp_nosuid' |
301
|
|
|
result = 'true' |
302
|
|
|
|
303
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
304
|
|
|
src, rule_id, result) |
305
|
|
|
|
306
|
|
|
|
307
|
|
|
def test_parsing_and_evaluate_scan_0(): |
308
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
309
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_audit_rules_file_deletion_events_rmdir' |
310
|
|
|
result = 'false' |
311
|
|
|
|
312
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
313
|
|
|
src, rule_id, result) |
314
|
|
|
|
315
|
|
|
|
316
|
|
|
def test_parsing_and_evaluate_scan_1(): |
317
|
|
|
src = 'test_data/ssg-fedora-ds-arf-scan-with-negated-extend-definitions.xml' |
318
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_install_PAE_kernel_on_x86-32' |
319
|
|
|
result = 'true' |
320
|
|
|
|
321
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
322
|
|
|
src, rule_id, result) |
323
|
|
|
|
324
|
|
|
|
325
|
|
|
def test_parsing_and_evaluate_scan_1(): |
326
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
327
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_sssd_offline_cred_expiration' |
328
|
|
|
result = 'true' |
329
|
|
|
|
330
|
|
|
tests.any_test_help.any_test_parsing_and_evaluate_scan_rule( |
331
|
|
|
src, rule_id, result) |
332
|
|
|
|
333
|
|
|
|
334
|
|
|
def test_transformation_tree_to_Json_for_SigmaJs_0(): |
335
|
|
|
test_data_src = 'test_data/sigmaJs_json0.json' |
336
|
|
|
src = 'data/ssg-fedora-ds-arf.xml' |
337
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny' |
338
|
|
|
|
339
|
|
|
tests.any_test_help.any_test_transformation_tree_to_Json_for_SigmaJs( |
340
|
|
|
src, test_data_src, rule_id) |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
def test_transformation_tree_to_Json_for_SigmaJs_with_duplicated_test(): |
344
|
|
|
test_data_src = 'test_data/sigmaJs_json1.json' |
345
|
|
|
src = 'data/ssg-fedora-ds-arf.xml' |
346
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_disable_host_auth' |
347
|
|
|
|
348
|
|
|
tests.any_test_help.any_test_transformation_tree_to_Json_for_SigmaJs( |
349
|
|
|
src, test_data_src, rule_id) |
350
|
|
|
|
351
|
|
|
|
352
|
|
|
def test_get_def_id_by_rule_id(): |
353
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
354
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
355
|
|
|
FIXTURE_DIR = py.path.local(_dir) / src |
356
|
|
|
|
357
|
|
|
parser = graph.xml_parser.xml_parser(str(FIXTURE_DIR)) |
358
|
|
|
|
359
|
|
|
with pytest.raises(Exception, match="err- 404 rule not found!"): |
360
|
|
|
assert parser.get_def_id_by_rule_id('hello') |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
def test_get_def_id_by_notselected_rule_id(): |
364
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
365
|
|
|
|
366
|
|
|
parser = tests.any_test_help.get_parser(src) |
367
|
|
|
rule_id = 'xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server' |
368
|
|
|
|
369
|
|
|
with pytest.raises(Exception, match="not selected"): |
370
|
|
|
assert parser.get_def_id_by_rule_id(rule_id) |
371
|
|
|
|
372
|
|
|
|
373
|
|
|
def test_str_to_bool(): |
374
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
375
|
|
|
parser = tests.any_test_help.get_parser(src) |
376
|
|
|
|
377
|
|
|
assert parser._str_to_bool('true') |
378
|
|
|
assert not parser._str_to_bool('false') |
379
|
|
|
with pytest.raises(Exception, match="err- negation is not bool"): |
380
|
|
|
assert parser._str_to_bool('error') |
381
|
|
|
|
382
|
|
|
|
383
|
|
|
def test_use_bat_report_file(): |
384
|
|
|
src = ( |
385
|
|
|
'test_data/xccdf_org.ssgproject.' |
386
|
|
|
'content_rule_sssd_ssh_known_hosts_timeout-comment.' |
387
|
|
|
'fail.sh-xccdf_org.ssgproject.content_profile_ospp-results-initial.xml') |
388
|
|
|
|
389
|
|
|
with pytest.raises(Exception, match=r"(arf\b|ARF\b)"): |
390
|
|
|
assert tests.any_test_help.get_parser(src) |
391
|
|
|
|
392
|
|
|
|
393
|
|
|
def test_get_rule_dict(): |
394
|
|
|
src = 'test_data/ssg-fedora-ds-arf.xml' |
395
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
396
|
|
|
FIXTURE_DIR = py.path.local(_dir) / src |
397
|
|
|
parser = graph.xml_parser.xml_parser(str(FIXTURE_DIR)) |
398
|
|
|
dict = parser.get_rule_dict( |
399
|
|
|
'xccdf_org.ssgproject.content_rule_dconf_gnome_session_idle_user_locks') |
400
|
|
|
src = 'test_data/rule_dict.json' |
401
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
402
|
|
|
FIXTURE_DIR = py.path.local(_dir) / src |
403
|
|
|
with open(str(FIXTURE_DIR), 'r') as f: |
404
|
|
|
data = json.load(f) |
405
|
|
|
assert data == dict |
406
|
|
|
|