|
1
|
|
|
import graph.oval_graph |
|
2
|
|
|
import os |
|
3
|
|
|
import py |
|
4
|
|
|
import json |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
def any_test_treeEvaluation(tree, expect, file_name=None): |
|
8
|
|
|
if file_name is not None and tree is None: |
|
9
|
|
|
if file_name.startswith('AND'): |
|
10
|
|
|
dir = 'test_data_and' |
|
11
|
|
|
elif file_name.startswith('OR'): |
|
12
|
|
|
dir = 'test_data_or' |
|
13
|
|
|
elif file_name.startswith('XOR'): |
|
14
|
|
|
dir = 'test_data_xor' |
|
15
|
|
|
elif file_name.startswith('ONE'): |
|
16
|
|
|
dir = 'test_data_one' |
|
17
|
|
|
else: |
|
18
|
|
|
dir = 'test_data_NONE' |
|
19
|
|
|
|
|
20
|
|
|
src = 'test_data/' + dir + '/' + file_name |
|
21
|
|
|
data = dict() |
|
22
|
|
|
with open(get_src(src), "r") as f: |
|
23
|
|
|
data = json.load(f) |
|
24
|
|
|
assert graph.oval_graph.restore_dict_to_tree( |
|
25
|
|
|
data).evaluate_tree() == expect |
|
26
|
|
|
else: |
|
27
|
|
|
assert tree.evaluate_tree() == expect |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def any_test_parsing_and_evaluate_scan_rule(src, rule_id, result): |
|
31
|
|
|
oval_tree = graph.oval_graph.build_nodes_form_xml( |
|
32
|
|
|
get_src(src), rule_id) |
|
33
|
|
|
any_test_treeEvaluation(oval_tree, result) |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def any_get_test_data_json(src): |
|
37
|
|
|
with open(get_src(src), 'r') as f: |
|
38
|
|
|
data = json.load(f) |
|
39
|
|
|
return data |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def any_test_create_node_dict_for_sigmaJs(Tree, out): |
|
43
|
|
|
print(Tree._create_node(0, 0)) |
|
44
|
|
|
assert Tree._create_node(0, 0) == out |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def get_simple_tree(): |
|
48
|
|
|
return graph.oval_graph.OvalNode(1, 'operator', 'and', False, [ |
|
49
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true", False), |
|
50
|
|
|
graph.oval_graph.OvalNode(3, 'value', "false", False), |
|
51
|
|
|
graph.oval_graph.OvalNode(4, 'operator', 'or', False, [ |
|
52
|
|
|
graph.oval_graph.OvalNode(5, 'value', "false", False), |
|
53
|
|
|
graph.oval_graph.OvalNode(6, 'value', "true", False) |
|
54
|
|
|
] |
|
55
|
|
|
) |
|
56
|
|
|
] |
|
57
|
|
|
) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
def get_dict_of_simple_tree(): |
|
61
|
|
|
return get_simple_tree().save_tree_to_dict() |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
def any_test_transformation_tree_to_Json_for_SigmaJs( |
|
65
|
|
|
src, test_data_src, rule_id): |
|
66
|
|
|
test_data = any_get_test_data_json(test_data_src) |
|
67
|
|
|
|
|
68
|
|
|
oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id) |
|
69
|
|
|
|
|
70
|
|
|
if oval_tree.node_id == rule_id: |
|
71
|
|
|
out_data = oval_tree.to_sigma_dict(0, 0) |
|
72
|
|
|
for i in range(len(out_data['nodes'])): |
|
73
|
|
|
assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label'] |
|
74
|
|
|
assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text'] |
|
75
|
|
|
assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url'] |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
def any_test_tree_to_dict_of_tree(tree, dict_of_tree): |
|
79
|
|
|
assert tree.save_tree_to_dict() == dict_of_tree |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
def find_any_node(Tree, node_id): |
|
83
|
|
|
findTree = Tree.find_node_with_ID(node_id) |
|
84
|
|
|
assert findTree.node_id == node_id |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def any_test_treeEvaluation_with_tree(tree, expect): |
|
88
|
|
|
assert tree.evaluate_tree() == expect |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
def any_test_dict_to_tree(dict_of_tree): |
|
92
|
|
|
treedict_of_tree = graph.oval_graph.restore_dict_to_tree(dict_of_tree) |
|
93
|
|
|
assert treedict_of_tree.save_tree_to_dict() == dict_of_tree |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
def get_parser(src): |
|
97
|
|
|
return graph.xml_parser.xml_parser(get_src(src)) |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
def get_src(src): |
|
101
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
|
102
|
|
|
FIXTURE_DIR = py.path.local(_dir) / src |
|
103
|
|
|
return str(FIXTURE_DIR) |
|
104
|
|
|
|