1
|
|
|
import os |
|
|
|
|
2
|
|
|
import json |
3
|
|
|
import sys |
4
|
|
|
import mock |
5
|
|
|
import pytest |
6
|
|
|
import uuid |
|
|
|
|
7
|
|
|
import tempfile |
|
|
|
|
8
|
|
|
import re |
|
|
|
|
9
|
|
|
|
10
|
|
|
from oval_graph.oval_node import restore_dict_to_tree, OvalNode |
11
|
|
|
from oval_graph.converter import Converter |
12
|
|
|
from oval_graph.xml_parser import XmlParser |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
def any_test_treeEvaluation(tree, expect, file_name=None): |
|
|
|
|
16
|
|
|
if file_name is not None and tree is None: |
|
|
|
|
17
|
|
|
if file_name.startswith('AND'): |
|
|
|
|
18
|
|
|
dir = 'test_data_and' |
|
|
|
|
19
|
|
|
elif file_name.startswith('OR'): |
|
|
|
|
20
|
|
|
dir = 'test_data_or' |
|
|
|
|
21
|
|
|
elif file_name.startswith('XOR'): |
|
|
|
|
22
|
|
|
dir = 'test_data_xor' |
|
|
|
|
23
|
|
|
elif file_name.startswith('ONE'): |
|
|
|
|
24
|
|
|
dir = 'test_data_one' |
|
|
|
|
25
|
|
|
else: |
|
|
|
|
26
|
|
|
dir = 'test_data_NONE' |
|
|
|
|
27
|
|
|
|
28
|
|
|
src = 'test_data/' + dir + '/' + file_name |
|
|
|
|
29
|
|
|
data = dict() |
|
|
|
|
30
|
|
|
with open(get_src(src), "r") as f: |
|
|
|
|
31
|
|
|
data = json.load(f) |
|
|
|
|
32
|
|
|
assert restore_dict_to_tree( |
|
|
|
|
33
|
|
|
data).evaluate_tree() == expect |
34
|
|
|
else: |
|
|
|
|
35
|
|
|
assert tree.evaluate_tree() == expect |
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
def any_test_parsing_and_evaluate_scan_rule(src, rule_id, result): |
|
|
|
|
39
|
|
|
parser = XmlParser(get_src(src)) |
|
|
|
|
40
|
|
|
oval_tree = parser.get_oval_tree(rule_id) |
|
|
|
|
41
|
|
|
any_test_treeEvaluation(oval_tree, result) |
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
def get_random_dir_in_tmp(): |
|
|
|
|
45
|
|
|
return os.path.join(tempfile.gettempdir(), str(uuid.uuid4())) |
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
def any_get_test_data_json(src): |
|
|
|
|
49
|
|
|
with open(get_src(src), 'r') as f: |
|
|
|
|
50
|
|
|
return json.load(f) |
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def any_get_tested_file(src): |
|
|
|
|
54
|
|
|
with open(get_src(src), 'r') as f: |
|
|
|
|
55
|
|
|
return f.readlines() |
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def get_Converter_simple_tree(): |
|
|
|
|
59
|
|
|
return Converter(get_simple_tree()) |
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
def any_test_create_node_dict_for_JsTree(tree, json_src): |
|
|
|
|
63
|
|
|
data = dict() |
|
|
|
|
64
|
|
|
with open(get_src(json_src), "r") as f: |
|
|
|
|
65
|
|
|
data = json.load(f) |
|
|
|
|
66
|
|
|
assert Converter(tree).to_JsTree_dict() == data |
|
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
def get_simple_tree(): |
|
|
|
|
70
|
|
|
return OvalNode( |
|
|
|
|
71
|
|
|
node_id=1, |
72
|
|
|
node_type='operator', |
73
|
|
|
value='and', |
74
|
|
|
children=[ |
75
|
|
|
OvalNode( |
76
|
|
|
node_id=2, |
77
|
|
|
node_type='value', |
78
|
|
|
value='true', |
79
|
|
|
), |
80
|
|
|
OvalNode( |
81
|
|
|
node_id=3, |
82
|
|
|
node_type='value', |
83
|
|
|
value='false', |
84
|
|
|
), |
85
|
|
|
OvalNode( |
86
|
|
|
node_id=4, |
87
|
|
|
node_type='operator', |
88
|
|
|
value='or', |
89
|
|
|
children=[ |
90
|
|
|
OvalNode( |
91
|
|
|
node_id=5, |
92
|
|
|
node_type='value', |
93
|
|
|
value='false', |
94
|
|
|
), |
95
|
|
|
OvalNode( |
96
|
|
|
node_id=6, |
97
|
|
|
node_type='value', |
98
|
|
|
value="true", |
99
|
|
|
), |
100
|
|
|
] |
101
|
|
|
) |
102
|
|
|
] |
103
|
|
|
) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
def get_dict_of_simple_tree(): |
|
|
|
|
107
|
|
|
return get_simple_tree().save_tree_to_dict() |
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def any_test_transformation_tree_to_Json_for_JsTree( |
|
|
|
|
111
|
|
|
src, test_data_src, rule_id): |
112
|
|
|
test_data = any_get_test_data_json(test_data_src) |
|
|
|
|
113
|
|
|
|
114
|
|
|
parser = XmlParser(get_src(src)) |
|
|
|
|
115
|
|
|
oval_tree = parser.get_oval_tree(rule_id) |
|
|
|
|
116
|
|
|
|
117
|
|
|
assert oval_tree.node_id == rule_id |
|
|
|
|
118
|
|
|
out_data = Converter(oval_tree).to_JsTree_dict() |
|
|
|
|
119
|
|
|
assert out_data == test_data |
|
|
|
|
120
|
|
|
|
121
|
|
|
|
122
|
|
|
def any_test_tree_to_dict_of_tree(tree, dict_of_tree): |
|
|
|
|
123
|
|
|
assert tree.save_tree_to_dict() == dict_of_tree |
|
|
|
|
124
|
|
|
|
125
|
|
|
|
126
|
|
|
def find_any_node(tree, node_id): |
|
|
|
|
127
|
|
|
findTree = tree.find_node_with_ID(node_id) |
|
|
|
|
128
|
|
|
assert findTree.node_id == node_id |
|
|
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
def any_test_treeEvaluation_with_tree(tree, expect): |
|
|
|
|
132
|
|
|
assert tree.evaluate_tree() == expect |
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
def any_test_dict_to_tree(dict_of_tree): |
|
|
|
|
136
|
|
|
treedict_of_tree = restore_dict_to_tree(dict_of_tree) |
|
|
|
|
137
|
|
|
assert treedict_of_tree.save_tree_to_dict() == dict_of_tree |
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
def get_parser(src): |
|
|
|
|
141
|
|
|
return XmlParser(get_src(src)) |
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
|
|
def get_src(src): |
|
|
|
|
145
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
|
|
|
|
146
|
|
|
FIXTURE_DIR = os.path.join(_dir, src) |
|
|
|
|
147
|
|
|
return str(FIXTURE_DIR) |
|
|
|
|
148
|
|
|
|
149
|
|
|
|
150
|
|
|
def compare_results_html(result): |
|
|
|
|
151
|
|
|
result_ = any_get_tested_file(result) |
|
|
|
|
152
|
|
|
reference_pattern = any_get_tested_file( |
|
|
|
|
153
|
|
|
'test_data/referenc_pattern_html_report.txt') |
154
|
|
|
start_pattern = False |
|
|
|
|
155
|
|
|
count_row = 0 |
|
|
|
|
156
|
|
|
for row in result_: |
|
|
|
|
157
|
|
|
if row == '<script>var data_of_tree = {\n': |
|
|
|
|
158
|
|
|
start_pattern = True |
|
|
|
|
159
|
|
|
if start_pattern and "xccdforgssgprojectcontentrulepackageabrtremoved" in row: |
|
|
|
|
160
|
|
|
row = re.sub(r'[0-9]+', '', row) |
|
|
|
|
161
|
|
|
count_row += (1 if start_pattern and row in reference_pattern else 0) |
|
|
|
|
162
|
|
|
if row == '};</script><div>\n': |
|
|
|
|
163
|
|
|
break |
|
|
|
|
164
|
|
|
assert count_row == len(reference_pattern) |
|
|
|
|
165
|
|
|
|
166
|
|
|
|
167
|
|
|
def compare_results_json(result): |
|
|
|
|
168
|
|
|
result = any_get_test_data_json(result) |
|
|
|
|
169
|
|
|
reference_result = any_get_test_data_json( |
|
|
|
|
170
|
|
|
'test_data/referenc_result_data_json.json') |
171
|
|
|
rule_name = "xccdf_org.ssgproject.content_rule_package_abrt_removed" |
|
|
|
|
172
|
|
|
result_rule_name = [ |
|
|
|
|
173
|
|
|
x for x in result.keys() if re.search( |
174
|
|
|
rule_name, x)] |
175
|
|
|
assert (result[result_rule_name[0]] |
|
|
|
|
176
|
|
|
== reference_result[rule_name]) |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
def _find_all_in_string(regex, count, string): |
180
|
|
|
assert len(re.findall(regex, string)) == count |
|
|
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
def get_questions_not_selected(capsys, client): |
|
|
|
|
184
|
|
|
out = client.get_questions()[0].choices |
|
|
|
|
185
|
|
|
outResult = [ |
|
|
|
|
186
|
|
|
'xccdf_org.ssgproject.content_rule_package_abrt_removed', |
187
|
|
|
'xccdf_org.ssgproject.content_rule_package_sendmail_removed'] |
188
|
|
|
assert out == outResult |
|
|
|
|
189
|
|
|
captured = capsys.readouterr() |
|
|
|
|
190
|
|
|
# Problem with CI when si called function test_arf_to_hml.test_get_question_not_selected |
|
|
|
|
191
|
|
|
# other calls work with ==. |
192
|
|
|
regex = r'rule_package_\w+_removed\(Not selected\)' |
|
|
|
|
193
|
|
|
_find_all_in_string(regex, 6, captured.out) |
|
|
|
|
194
|
|
|
|
195
|
|
|
|
196
|
|
|
def get_questions_not_selected_and_show_failed_rules(capsys, client): |
|
|
|
|
197
|
|
|
out = client.get_questions()[0].choices |
|
|
|
|
198
|
|
|
outResult = ['xccdf_org.ssgproject.content_rule_package_abrt_removed'] |
|
|
|
|
199
|
|
|
assert out == outResult |
|
|
|
|
200
|
|
|
captured = capsys.readouterr() |
|
|
|
|
201
|
|
|
regex = r'rule_package_\w+_removed\(Not selected\)' |
|
|
|
|
202
|
|
|
_find_all_in_string(regex, 6, captured.out) |
|
|
|
|
203
|
|
|
|
204
|
|
|
|
205
|
|
|
def get_questions_with_option_show_failed_rules(client): |
|
|
|
|
206
|
|
|
out = client.get_questions()[0].choices |
|
|
|
|
207
|
|
|
rule1 = 'xccdf_org.ssgproject.content_rule_package_abrt_removed' |
|
|
|
|
208
|
|
|
assert out[0] == rule1 |
|
|
|
|
209
|
|
|
with pytest.raises(Exception, match="list index out of range"): |
|
|
|
|
210
|
|
|
assert out[2] is None |
|
|
|
|
211
|
|
|
|
212
|
|
|
|
213
|
|
|
def if_not_installed_inquirer_with_option_show_failed_rules(capsys, client): |
|
|
|
|
214
|
|
|
with mock.patch.dict(sys.modules, {'inquirer': None}): |
|
|
|
|
215
|
|
|
out = client.run_gui_and_return_answers() |
|
|
|
|
216
|
|
|
assert out is None |
|
|
|
|
217
|
|
|
captured = capsys.readouterr() |
|
|
|
|
218
|
|
|
assert "inquirer" in captured.out |
|
|
|
|
219
|
|
|
regex = r'rule_package_\w+_removed\$' |
|
|
|
|
220
|
|
|
_find_all_in_string(regex, 1, captured.out) |
|
|
|
|
221
|
|
|
|
222
|
|
|
|
223
|
|
|
def if_not_installed_inquirer_with_option_show_not_selected_rules( |
|
|
|
|
224
|
|
|
capsys, client, count_of_selected_rule=2): |
225
|
|
|
with mock.patch.dict(sys.modules, {'inquirer': None}): |
|
|
|
|
226
|
|
|
out = client.run_gui_and_return_answers() |
|
|
|
|
227
|
|
|
assert out is None |
|
|
|
|
228
|
|
|
captured = capsys.readouterr() |
|
|
|
|
229
|
|
|
assert "inquirer" in captured.out |
|
|
|
|
230
|
|
|
regex = r'rule_package_\w+_removed\$' |
|
|
|
|
231
|
|
|
_find_all_in_string(regex, count_of_selected_rule, captured.out) |
|
|
|
|
232
|
|
|
regex = r'rule_package_\w+_removed\(Not selected\)' |
|
|
|
|
233
|
|
|
_find_all_in_string(regex, 6, captured.out) |
|
|
|
|
234
|
|
|
|
235
|
|
|
|
236
|
|
|
def if_not_installed_inquirer_with_option_show_not_selected_rules_and_show_failed_rules( |
|
|
|
|
237
|
|
|
capsys, |
238
|
|
|
client): |
239
|
|
|
if_not_installed_inquirer_with_option_show_not_selected_rules( |
|
|
|
|
240
|
|
|
capsys, client, 1) |
241
|
|
|
|
242
|
|
|
def find_files(file_name, search_path): |
|
|
|
|
243
|
|
|
result = [] |
|
|
|
|
244
|
|
|
for root, dir_, files in os.walk(search_path): |
|
|
|
|
245
|
|
|
for filename in files: |
|
|
|
|
246
|
|
|
if file_name in filename: |
|
|
|
|
247
|
|
|
result.append(os.path.abspath(os.path.join(root, filename))) |
|
|
|
|
248
|
|
|
return result |
|
|
|
|
249
|
|
|
|