Passed
Push — codeStyle ( 8912bb...a542d4 )
by Jan
02:08
created

test_sigmaJs   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 220
Duplicated Lines 12.73 %

Importance

Changes 0
Metric Value
wmc 22
eloc 161
dl 28
loc 220
rs 10
c 0
b 0
f 0

17 Functions

Rating   Name   Duplication   Size   Complexity  
A test_transformation_tree_to_Json_for_SigmaJs_with_duplicated_test() 14 14 3
A test_parsing_and_evaluate_scan_with_fail_rule() 0 6 1
A test_parsing_and_evaluate_scan_with_extend_def() 0 6 1
A test_create_node_dict_for_sigmaJs_0() 0 14 1
A test_get_def_id_by_rule_id() 0 11 2
A test_parsing_full_can_XML_and_evaluate() 0 6 1
A test_parsing_and_evaluate_scan_with_pasing_rule() 0 6 1
A test_create_array_of_ids_form_tree() 0 3 1
A test_parsing_and_evaluate_scan_with_11_rules() 0 6 1
A test_create_node_dict_for_sigmaJs_1() 0 18 1
A test_create_node_dict_for_sigmaJs_5() 0 15 1
A test_create_node_dict_for_sigmaJs_2() 0 18 1
A test_transformation_tree_to_Json_for_SigmaJs_0() 14 14 3
A test_parsing_and_evaluate_scan_with_rule_with_XOR() 0 6 1
A test_create_node_dict_for_sigmaJs_3() 0 15 1
A test_create_edge_dict_for_sigmaJs() 0 10 1
A test_create_node_dict_for_sigmaJs_4() 0 15 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import tests.any_test_help
2
import graph.oval_graph
3
import os
4
import py
5
import pytest
6
7
def test_create_node_dict_for_sigmaJs_0():
8
    out = {
9
        'color': '#ff0000',
10
        'id': 1,
11
        'label': 'and',
12
        'size': 3,
13
        'text': 'null',
14
        'url': 'null',
15
        'title': 1,
16
        'x': 0,
17
        'y': 0
18
    }
19
    Tree = tests.any_test_help.get_simple_tree()
20
    tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out)
21
22
23
def test_create_node_dict_for_sigmaJs_1():
24
    out = {
25
        'color': '#00ff00',
26
        'id': 1,
27
        'label': 'and',
28
        'size': 3,
29
        'text': 'null',
30
        'url': 'null',
31
        'title': 1,
32
        'x': 0,
33
        'y': 0
34
    }
35
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
36
        graph.oval_graph.OvalNode(2, 'value', "true")
37
    ]
38
    )
39
40
    tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out)
41
42
43
def test_create_node_dict_for_sigmaJs_2():
44
    out = {
45
        'color': '#000000',
46
        'id': 1,
47
        'label': 'and',
48
        'size': 3,
49
        'text': 'null',
50
        'url': 'null',
51
        'title': '1 and',
52
        'x': 0,
53
        'y': 0
54
    }
55
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
56
        graph.oval_graph.OvalNode(2, 'value', "noteval")
57
    ]
58
    )
59
60
    tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out)
61
62
63
def test_create_node_dict_for_sigmaJs_3():
64
    out = {
65
        'color': '#ff0000',
66
        'id': 1,
67
        'label': '1',
68
        'size': 3,
69
        'text': 'null',
70
        'url': 'null',
71
        'title': 1,
72
        'x': 0,
73
        'y': 0
74
    }
75
    Tree = graph.oval_graph.OvalNode(1, 'value', 'false')
76
77
    tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out)
78
79
80
def test_create_node_dict_for_sigmaJs_4():
81
    out = {
82
        'color': '#00ff00',
83
        'id': 1,
84
        'label': '1',
85
        'size': 3,
86
        'text': 'null',
87
        'url': 'null',
88
        'title': 1,
89
        'x': 0,
90
        'y': 0
91
    }
92
    Tree = graph.oval_graph.OvalNode(1, 'value', 'true')
93
94
    tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out)
95
96
97
def test_create_node_dict_for_sigmaJs_5():
98
    out = {
99
        'color': '#000000',
100
        'id': 1,
101
        'label': 'error',
102
        'size': 3,
103
        'text': 'null',
104
        'url': 'null',
105
        'title': '1 error',
106
        'x': 0,
107
        'y': 0
108
    }
109
    Tree = graph.oval_graph.OvalNode(1, 'value', 'error')
110
111
    tests.any_test_help.any_test_create_node_dict_for_sigmaJs(Tree, out)
112
113
114
def test_create_edge_dict_for_sigmaJs():
115
    print(tests.any_test_help.get_simple_tree()._create_edge(1, 2))
116
    out = {
117
        'id': 'random_ID',
118
        'source': 1,
119
        'target': 2
120
    }
121
122
    assert tests.any_test_help.get_simple_tree()._create_edge(1, 2)['source'] == out['source']
123
    assert tests.any_test_help.get_simple_tree()._create_edge(1, 2)['target'] == out['target']
124
125
126
def test_create_array_of_ids_form_tree():
127
    array = tests.any_test_help.get_simple_tree().create_list_of_id()
128
    assert array == [1, 2, 3, 4, 5, 6]
129
130
def test_parsing_full_can_XML_and_evaluate():
131
    src = 'test_data/ssg-fedora-ds-arf.xml'
132
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
133
    result = 'false'
134
135
    tests.any_test_help.any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
136
137
138
def test_parsing_and_evaluate_scan_with_extend_def():
139
    src = 'test_data/ssg-fedora-ds-arf-scan-with-extend-definitions.xml'
140
    rule_id = 'xccdf_org.ssgproject.content_rule_sysctl_net_ipv6_conf_all_disable_ipv6'
141
    result = 'false'
142
143
    tests.any_test_help.any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
144
145
146
def test_parsing_and_evaluate_scan_with_pasing_rule():
147
    src = 'test_data/ssg-fedora-ds-arf-passing-scan.xml'
148
    rule_id = 'xccdf_org.ssgproject.content_rule_service_debug-shell_disabled'
149
    result = 'true'
150
151
    tests.any_test_help.any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
152
153
154
def test_parsing_and_evaluate_scan_with_fail_rule():
155
    src = 'test_data/ssg-fedora-ds-arf-scan-fail.xml'
156
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec'
157
    result = 'false'
158
159
    tests.any_test_help.any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
160
161
162
def test_parsing_and_evaluate_scan_with_rule_with_XOR():
163
    src = 'test_data/ssg-fedora-ds-arf-scan-with-xor.xml'
164
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_nosuid_removable_partitions'
165
    result = 'true'
166
167
    tests.any_test_help.any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
168
169
170
def test_parsing_and_evaluate_scan_with_11_rules():
171
    src = 'test_data/ssg-fedora-ds-arf-scan-with-11-rules.xml'
172
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_tmp_nosuid'
173
    result = 'true'
174
175
    tests.any_test_help.any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
176
177
178 View Code Duplication
def test_transformation_tree_to_Json_for_SigmaJs_0():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
179
    test_data_src='test_data/sigmaJs_json0.json'
180
    test_data = tests.any_test_help.any_get_test_data_json(test_data_src)
181
    src = 'data/ssg-fedora-ds-arf.xml'
182
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
183
184
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
185
    if oval_tree.node_id == rule_id:
186
        out_data = oval_tree.to_sigma_dict(0, 0)
187
        print(out_data)
188
        for i in range(len(out_data['nodes'])):
189
            assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label']
190
            assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text']
191
            assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url']
192
193
194 View Code Duplication
def test_transformation_tree_to_Json_for_SigmaJs_with_duplicated_test():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
195
    test_data_src='test_data/sigmaJs_json1.json'
196
    test_data = tests.any_test_help.any_get_test_data_json(test_data_src)
197
    src = 'data/ssg-fedora-ds-arf.xml'
198
    rule_id = 'xccdf_org.ssgproject.content_rule_disable_host_auth'
199
200
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
201
202
    if oval_tree.node_id == rule_id:
203
        out_data = oval_tree.to_sigma_dict(0, 0)
204
        for i in range(len(out_data['nodes'])):
205
            assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label']
206
            assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text']
207
            assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url']
208
209
def test_get_def_id_by_rule_id():
210
    src = 'test_data/ssg-fedora-ds-arf.xml'
211
    _dir = os.path.dirname(os.path.realpath(__file__))
212
    FIXTURE_DIR = py.path.local(_dir) / src
213
214
    parser = graph.xml_parser.xml_parser(str(FIXTURE_DIR))
215
216
    with pytest.raises(ValueError) as e:
217
        parser.get_def_id_by_rule_id('hello')
218
    assert str(
219
        e.value) == 'err- 404 rule not found!'
220