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