|
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(data).evaluate_tree() == expect |
|
27
|
|
|
else: |
|
28
|
|
|
assert tree.evaluate_tree() == expect |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
def any_test_parsing_and_evaluate_scan_rule(src, rule_id, result): |
|
32
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
|
33
|
|
|
FIXTURE_DIR = py.path.local(_dir) / src |
|
34
|
|
|
|
|
35
|
|
|
oval_tree = graph.oval_graph.build_nodes_form_xml( |
|
36
|
|
|
str(FIXTURE_DIR), rule_id) |
|
37
|
|
|
any_test_treeEvaluation(oval_tree, result) |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
def any_get_test_data_json(src): |
|
41
|
|
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
|
42
|
|
|
FIXTURE_DIR = py.path.local(_dir) / src |
|
43
|
|
|
|
|
44
|
|
|
with open(str(FIXTURE_DIR), 'r') as f: |
|
45
|
|
|
data = json.load(f) |
|
46
|
|
|
return data |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def any_test_create_node_dict_for_sigmaJs(Tree, out): |
|
50
|
|
|
assert Tree._create_node(0, 0) == out |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def get_simple_tree(): |
|
54
|
|
|
return graph.oval_graph.OvalNode(1, 'operator', 'and', [ |
|
55
|
|
|
graph.oval_graph.OvalNode(2, 'value', "true"), |
|
56
|
|
|
graph.oval_graph.OvalNode(3, 'value', "false"), |
|
57
|
|
|
graph.oval_graph.OvalNode(4, 'operator', 'or', [ |
|
58
|
|
|
graph.oval_graph.OvalNode(5, 'value', "false"), |
|
59
|
|
|
graph.oval_graph.OvalNode(6, 'value', "true") |
|
60
|
|
|
] |
|
61
|
|
|
) |
|
62
|
|
|
] |
|
63
|
|
|
) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
def get_dict_of_simple_tree(): |
|
67
|
|
|
return get_simple_tree().save_tree_to_dict() |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
def any_test_transformation_tree_to_Json_for_SigmaJs( |
|
71
|
|
|
src, test_data_src, rule_id): |
|
72
|
|
|
test_data = any_get_test_data_json(test_data_src) |
|
73
|
|
|
|
|
74
|
|
|
oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id) |
|
75
|
|
|
|
|
76
|
|
|
if oval_tree.node_id == rule_id: |
|
77
|
|
|
out_data = oval_tree.to_sigma_dict(0, 0) |
|
78
|
|
|
for i in range(len(out_data['nodes'])): |
|
79
|
|
|
assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label'] |
|
80
|
|
|
assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text'] |
|
81
|
|
|
assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url'] |
|
82
|
|
|
|