Passed
Branch graph-look (3081c1)
by Jan
03:46
created

test_graph.test_bigOvalTree()   B

Complexity

Conditions 1

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 52
rs 8.9919
c 0
b 0
f 0
cc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import graph.oval_graph
2
import graph.evaluate
3
import graph.xml_parser
4
import pytest
5
import os
6
import py
7
8
9
results_counts = {
10
    'true_cnt': -1,
11
    'false_cnt': -1,
12
    'error_cnt': -1,
13
    'unknown_cnt': -1,
14
    'noteval_cnt': -1,
15
    'notappl_cnt': -1
16
}
17
18
results_counts1 = {
19
    'true_cnt': 3,
20
    'false_cnt': 3,
21
    'error_cnt': 3,
22
    'unknown_cnt': 0,
23
    'noteval_cnt': -1,
24
    'notappl_cnt': 3
25
}
26
27
28
def test_bad_tree():
29
    with pytest.raises(ValueError) as e:
30
        badTree()
31
    assert str(
32
        e.value) == 'err- true, false, error, unknown. noteval, notappl have not child!'
33
34
    with pytest.raises(ValueError) as e:
35
        treeOnlyAnd()
36
    assert str(e.value) == 'err- OR, XOR, ONE, AND have child!'
37
38
    with pytest.raises(ValueError) as e:
39
        treeOnlyOr()
40
    assert str(e.value) == 'err- OR, XOR, ONE, AND have child!'
41
42
    with pytest.raises(ValueError) as e:
43
        treeWithBadType()
44
    assert str(e.value) == 'err- unknown type'
45
46
    with pytest.raises(ValueError) as e:
47
        treeWithBadValueOfOperator()
48
    assert str(e.value) == 'err- unknown operator'
49
50
    with pytest.raises(ValueError) as e:
51
        treeWithBadValueOfValue()
52
    assert str(e.value) == 'err- unknown value'
53
54
55
# degenered trees
56
57
def badTree():
58
    """
59
         t
60
         |
61
        and
62
         |
63
         t
64
    """
65
    t = graph.oval_graph.OvalNode(
66
        1, "value", "true", [
67
            graph.oval_graph.OvalNode(
68
                2, "operator", "and", [
69
                    graph.oval_graph.OvalNode(
70
                        3, "value", "true")])])
71
    return
72
73
74
def treeOnlyOr():
75
    """
76
        or
77
    """
78
    Tree = graph.oval_graph.OvalNode(1, "operator", 'or')
79
    return
80
81
82
def treeOnlyAnd():
83
    """
84
        and
85
    """
86
    Tree = graph.oval_graph.OvalNode(1, "operator", 'and')
87
    return
88
89
90
def treeWithBadValueOfOperator():
91
    Tree = graph.oval_graph.OvalNode(1, "operator", 'nad')
92
    return
93
94
95
def treeWithBadValueOfValue():
96
    Tree = graph.oval_graph.OvalNode(1, "value", 'and')
97
    return
98
99
100
def treeWithBadType():
101
    Tree = graph.oval_graph.OvalNode(1, "auto", 'and')
102
    return
103
104
# normal trees
105
106
107
def test_UPPERCASETree():
108
    t = graph.oval_graph.OvalNode(
109
        1, "OPERATOR", "AND", [
110
            graph.oval_graph.OvalNode(
111
                2, "VALUE", "TRUE",), graph.oval_graph.OvalNode(
112
                3, "VALUE", "NOTAPPL")])
113
114
# AND operator
115
116
117
def test_ANDTreeTrue():
118
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
119
        graph.oval_graph.OvalNode(2, 'value', "true"),
120
        graph.oval_graph.OvalNode(3, 'value', "true"),
121
        graph.oval_graph.OvalNode(4, 'value', "notappl")
122
    ]
123
    )
124
125
    any_test_treeEvaluation(Tree, "true")
126
127
128 View Code Duplication
def test_ANDTreeFalse():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
129
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
130
        graph.oval_graph.OvalNode(2, 'value', "false"),
131
        graph.oval_graph.OvalNode(3, 'value', "false"),
132
133
        graph.oval_graph.OvalNode(4, 'value', "true"),
134
        graph.oval_graph.OvalNode(5, 'value', "error"),
135
        graph.oval_graph.OvalNode(6, 'value', "unknown"),
136
        graph.oval_graph.OvalNode(7, 'value', "noteval"),
137
        graph.oval_graph.OvalNode(8, 'value', "notappl")
138
    ]
139
    )
140
141
    any_test_treeEvaluation(Tree, "false")
142
143
144 View Code Duplication
def test_ANDTreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
145
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
146
        graph.oval_graph.OvalNode(2, 'value', "error"),
147
        graph.oval_graph.OvalNode(3, 'value', "error"),
148
        graph.oval_graph.OvalNode(4, 'value', "true"),
149
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
150
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
151
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
152
        graph.oval_graph.OvalNode(8, 'value', "error")
153
    ])
154
155
    any_test_treeEvaluation(Tree, "error")
156
157
158 View Code Duplication
def test_ANDTreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
159
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
160
        graph.oval_graph.OvalNode(2, 'value', "unknown"),
161
        graph.oval_graph.OvalNode(3, 'value', "unknown"),
162
        graph.oval_graph.OvalNode(4, 'value', "true"),
163
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
164
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
165
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
166
        graph.oval_graph.OvalNode(8, 'value', "notappl")
167
    ]
168
    )
169
170
    any_test_treeEvaluation(Tree, "unknown")
171
172
173 View Code Duplication
def test_ANDTreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
174
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
175
        graph.oval_graph.OvalNode(2, 'value', "noteval"),
176
        graph.oval_graph.OvalNode(3, 'value', "noteval"),
177
        graph.oval_graph.OvalNode(4, 'value', "true"),
178
        graph.oval_graph.OvalNode(5, 'value', "true"),
179
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
180
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
181
        graph.oval_graph.OvalNode(8, 'value', "notappl")
182
    ]
183
    )
184
185
    any_test_treeEvaluation(Tree, "noteval")
186
187
188
def test_ANDTreeNotappl():
189
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
190
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
191
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
192
        graph.oval_graph.OvalNode(4, 'value', "notappl")
193
    ]
194
    )
195
196
    any_test_treeEvaluation(Tree, "notappl")
197
198
# ONE operator
199
200
201
def test_ONETreeTrue():
202
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
203
        graph.oval_graph.OvalNode(2, 'value', "true"),
204
        graph.oval_graph.OvalNode(3, 'value', "false"),
205
        graph.oval_graph.OvalNode(4, 'value', "notappl"),
206
        graph.oval_graph.OvalNode(5, 'value', "false")
207
    ]
208
    )
209
210
    any_test_treeEvaluation(Tree, "true")
211
212
213 View Code Duplication
def test_ONETreeFalse():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
214
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
215
        graph.oval_graph.OvalNode(2, 'value', "true"),
216
        graph.oval_graph.OvalNode(3, 'value', "true"),
217
218
        graph.oval_graph.OvalNode(4, 'value', "false"),
219
        graph.oval_graph.OvalNode(5, 'value', "error"),
220
        graph.oval_graph.OvalNode(6, 'value', "unknown"),
221
        graph.oval_graph.OvalNode(7, 'value', "noteval"),
222
        graph.oval_graph.OvalNode(8, 'value', "notappl")
223
    ]
224
    )
225
226
    any_test_treeEvaluation(Tree, "false")
227
228
229
def test_ONETreeFalse1():
230
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
231
        graph.oval_graph.OvalNode(2, 'value', "false"),
232
        graph.oval_graph.OvalNode(3, 'value', "false"),
233
        graph.oval_graph.OvalNode(4, 'value', "notappl")
234
    ]
235
    )
236
237
    any_test_treeEvaluation(Tree, "false")
238
239
240 View Code Duplication
def test_ONETreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
241
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
242
        graph.oval_graph.OvalNode(2, 'value', "error"),
243
        graph.oval_graph.OvalNode(3, 'value', "error"),
244
        graph.oval_graph.OvalNode(4, 'value', "true"),
245
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
246
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
247
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
248
        graph.oval_graph.OvalNode(8, 'value', "false")
249
    ]
250
    )
251
252
    any_test_treeEvaluation(Tree, "error")
253
254
255 View Code Duplication
def test_ONETreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
256
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
257
        graph.oval_graph.OvalNode(2, 'value', "unknown"),
258
        graph.oval_graph.OvalNode(3, 'value', "unknown"),
259
        graph.oval_graph.OvalNode(4, 'value', "true"),
260
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
261
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
262
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
263
        graph.oval_graph.OvalNode(8, 'value', "false")
264
    ])
265
266
    any_test_treeEvaluation(Tree, "unknown")
267
268
269 View Code Duplication
def test_ONETreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
270
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
271
        graph.oval_graph.OvalNode(2, 'value', "noteval"),
272
        graph.oval_graph.OvalNode(3, 'value', "noteval"),
273
        graph.oval_graph.OvalNode(4, 'value', "true"),
274
        graph.oval_graph.OvalNode(5, 'value', "false"),
275
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
276
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
277
        graph.oval_graph.OvalNode(8, 'value', "notappl")
278
    ]
279
    )
280
281
    any_test_treeEvaluation(Tree, "noteval")
282
283
284
def test_ONETreeNotappl():
285
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
286
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
287
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
288
        graph.oval_graph.OvalNode(4, 'value', "notappl")
289
    ]
290
    )
291
292
    any_test_treeEvaluation(Tree, "notappl")
293
294
# OR operator
295
296
297 View Code Duplication
def test_ORTreeTrue():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
298
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
299
        graph.oval_graph.OvalNode(2, 'value', "true"),
300
        graph.oval_graph.OvalNode(3, 'value', "false"),
301
302
        graph.oval_graph.OvalNode(4, 'value', "true"),
303
        graph.oval_graph.OvalNode(5, 'value', "error"),
304
        graph.oval_graph.OvalNode(6, 'value', "unknown"),
305
        graph.oval_graph.OvalNode(7, 'value', "noteval"),
306
        graph.oval_graph.OvalNode(8, 'value', "notappl")
307
    ]
308
    )
309
310
    any_test_treeEvaluation(Tree, "true")
311
312
313
def test_ORTreeFalse():
314
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
315
        graph.oval_graph.OvalNode(2, 'value', "false"),
316
        graph.oval_graph.OvalNode(3, 'value', "false"),
317
        graph.oval_graph.OvalNode(4, 'value', "notappl")
318
    ]
319
    )
320
321
    any_test_treeEvaluation(Tree, "false")
322
323
324 View Code Duplication
def test_ORTreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
325
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
326
        graph.oval_graph.OvalNode(2, 'value', "error"),
327
        graph.oval_graph.OvalNode(3, 'value', "error"),
328
        graph.oval_graph.OvalNode(4, 'value', "false"),
329
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
330
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
331
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
332
        graph.oval_graph.OvalNode(8, 'value', "error")
333
    ]
334
    )
335
336
    any_test_treeEvaluation(Tree, "error")
337
338
339 View Code Duplication
def test_ORTreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
340
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
341
        graph.oval_graph.OvalNode(2, 'value', "unknown"),
342
        graph.oval_graph.OvalNode(3, 'value', "unknown"),
343
        graph.oval_graph.OvalNode(4, 'value', "false"),
344
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
345
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
346
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
347
        graph.oval_graph.OvalNode(8, 'value', "notappl")
348
    ]
349
    )
350
351
    any_test_treeEvaluation(Tree, "unknown")
352
353
354 View Code Duplication
def test_ORTreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
355
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
356
        graph.oval_graph.OvalNode(2, 'value', "noteval"),
357
        graph.oval_graph.OvalNode(3, 'value', "noteval"),
358
        graph.oval_graph.OvalNode(4, 'value', "false"),
359
        graph.oval_graph.OvalNode(5, 'value', "false"),
360
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
361
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
362
        graph.oval_graph.OvalNode(8, 'value', "notappl")
363
    ]
364
    )
365
366
    any_test_treeEvaluation(Tree, "noteval")
367
368
369
def test_ORTreeNotappl():
370
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
371
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
372
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
373
        graph.oval_graph.OvalNode(4, 'value', "notappl")
374
    ]
375
    )
376
377
    any_test_treeEvaluation(Tree, "notappl")
378
379
# XOR operator
380
381
382 View Code Duplication
def test_XORTreeTrue():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
383
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
384
        graph.oval_graph.OvalNode(2, 'value', "true"),
385
        graph.oval_graph.OvalNode(3, 'value', "false"),
386
387
        graph.oval_graph.OvalNode(4, 'value', "false"),
388
        graph.oval_graph.OvalNode(5, 'value', "false"),
389
        graph.oval_graph.OvalNode(6, 'value', "true"),
390
        graph.oval_graph.OvalNode(7, 'value', "true"),
391
        graph.oval_graph.OvalNode(8, 'value', "notappl")
392
    ]
393
    )
394
395
    any_test_treeEvaluation(Tree, "true")
396
397
398 View Code Duplication
def test_XORTreeFalse():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
399
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
400
        graph.oval_graph.OvalNode(2, 'value', "true"),
401
        graph.oval_graph.OvalNode(3, 'value', "false"),
402
403
        graph.oval_graph.OvalNode(4, 'value', "false"),
404
        graph.oval_graph.OvalNode(5, 'value', "true"),
405
        graph.oval_graph.OvalNode(6, 'value', "true"),
406
        graph.oval_graph.OvalNode(7, 'value', "true"),
407
        graph.oval_graph.OvalNode(8, 'value', "notappl")
408
    ]
409
    )
410
411
    any_test_treeEvaluation(Tree, "false")
412
413
414 View Code Duplication
def test_XORTreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
415
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
416
        graph.oval_graph.OvalNode(2, 'value', "error"),
417
        graph.oval_graph.OvalNode(3, 'value', "error"),
418
        graph.oval_graph.OvalNode(4, 'value', "true"),
419
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
420
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
421
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
422
        graph.oval_graph.OvalNode(8, 'value', "false")
423
    ]
424
    )
425
426
    any_test_treeEvaluation(Tree, "error")
427
428
429 View Code Duplication
def test_xORTreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
430
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
431
        graph.oval_graph.OvalNode(2, 'value', "unknown"),
432
        graph.oval_graph.OvalNode(3, 'value', "unknown"),
433
        graph.oval_graph.OvalNode(4, 'value', "true"),
434
        graph.oval_graph.OvalNode(5, 'value', "unknown"),
435
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
436
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
437
        graph.oval_graph.OvalNode(8, 'value', "notappl")
438
    ]
439
    )
440
441
    any_test_treeEvaluation(Tree, "unknown")
442
443
444 View Code Duplication
def test_XORTreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
445
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
446
        graph.oval_graph.OvalNode(2, 'value', "noteval"),
447
        graph.oval_graph.OvalNode(3, 'value', "noteval"),
448
        graph.oval_graph.OvalNode(4, 'value', "true"),
449
        graph.oval_graph.OvalNode(5, 'value', "true"),
450
        graph.oval_graph.OvalNode(6, 'value', "noteval"),
451
        graph.oval_graph.OvalNode(7, 'value', "notappl"),
452
        graph.oval_graph.OvalNode(8, 'value', "notappl")
453
    ]
454
    )
455
456
    any_test_treeEvaluation(Tree, "noteval")
457
458
459
def test_XORTreeNotappl():
460
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
461
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
462
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
463
        graph.oval_graph.OvalNode(4, 'value', "notappl")
464
    ]
465
    )
466
467
    any_test_treeEvaluation(Tree, "notappl")
468
469
470
def test_bigOvalTree():
471
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
472
        graph.oval_graph.OvalNode(2, 'value', "false"),
473
        graph.oval_graph.OvalNode(3, 'operator', "xor", [
474
            graph.oval_graph.OvalNode(4, 'value', 'true'),
475
            graph.oval_graph.OvalNode(5, 'operator', 'one', [
476
                graph.oval_graph.OvalNode(6, 'value', 'noteval'),
477
                graph.oval_graph.OvalNode(7, 'value', 'true'),
478
                graph.oval_graph.OvalNode(8, 'value', 'notappl')
479
            ]
480
            ),
481
            graph.oval_graph.OvalNode(9, 'value', 'error')
482
        ]
483
        ),
484
        graph.oval_graph.OvalNode(10, 'operator', 'or', [
485
            graph.oval_graph.OvalNode(11, 'value', "unknown"),
486
            graph.oval_graph.OvalNode(12, 'value', "true")
487
        ]
488
        )
489
    ]
490
    )
491
492
    dict_of_tree = {'node_id': 1, 'type': 'operator', 'value': 'and',
493
                    'child': [
494
                        {'node_id': 2, 'type': 'value',
495
                            'value': "false", 'child': None},
496
                        {'node_id': 3, 'type': 'operator', 'value': "xor", 'child': [
497
                            {'node_id': 4, 'type': 'value',
498
                                'value': "true", 'child': None},
499
                            {'node_id': 5, 'type': 'operator', 'value': "one", 'child': [
500
                                {'node_id': 6, 'type': 'value',
501
                                    'value': "noteval", 'child': None},
502
                                {'node_id': 7, 'type': 'value',
503
                                    'value': "true", 'child': None},
504
                                {'node_id': 8, 'type': 'value',
505
                                    'value': "notappl", 'child': None}
506
                            ]},
507
                            {'node_id': 9, 'type': 'value', 'value': "error", 'child': None}]},
508
                        {'node_id': 10, 'type': 'operator', 'value': 'or', 'child': [
509
                            {'node_id': 11, 'type': 'value',
510
                                'value': "unknown", 'child': None},
511
                            {'node_id': 12, 'type': 'value',
512
                                'value': "true", 'child': None}
513
                        ]
514
                        }
515
                    ]
516
                    }
517
518
    any_test_treeEvaluation(Tree, "false")
519
    any_test_tree_to_dict_of_tree(Tree, dict_of_tree)
520
    find_any_node(Tree, 5)
521
    any_test_dict_to_tree(dict_of_tree)
522
523
###################################################
524
525
526
def any_test_tree_to_dict_of_tree(tree, dict_of_tree):
527
    assert tree.save_tree_to_dict() == dict_of_tree
528
529
530
def find_any_node(Tree, node_id):
531
    findTree = Tree.find_node_with_ID(node_id)
532
    assert findTree.node_id == node_id
533
534
535
def any_test_treeEvaluation(tree, expect):
536
    assert tree.evaluate_tree() == expect
537
538
539
def any_test_dict_to_tree(dict_of_tree):
540
    treedict_of_tree = graph.oval_graph.restore_dict_to_tree(dict_of_tree)
541
    assert treedict_of_tree.save_tree_to_dict() == dict_of_tree
542
543
544
def test_treeRepr():
545
    """
546
        and
547
         |
548
         f
549
    """
550
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
551
        graph.oval_graph.OvalNode(2, 'value', "false")
552
    ]
553
    )
554
    assert str(Tree) == "and"
555
556
557
def test_add_to_tree():
558
    """
559
        and
560
         |
561
         f
562
    """
563
564
    dict_of_tree = {'node_id': 1,
565
                    'type': 'operator',
566
                    'value': 'and',
567
                    'child': [{'node_id': 2,
568
                               'type': 'value',
569
                               'value': "false",
570
                               'child': None},
571
                              {'node_id': 3,
572
                               'type': 'value',
573
                               'value': "true",
574
                               'child': None},
575
                              ]}
576
577
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
578
        graph.oval_graph.OvalNode(2, 'value', "false")
579
    ]
580
    )
581
    Tree1 = graph.oval_graph.OvalNode(3, 'value', "true")
582
    Tree.add_to_tree(1, Tree1)
583
    assert Tree.save_tree_to_dict() == dict_of_tree
584
585
586
def test_ChangeValueTree():
587
    """
588
        and
589
        /|\
590
       t t or
591
          / \
592
         f   t
593
    """
594
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
595
        graph.oval_graph.OvalNode(2, 'value', "true"),
596
        graph.oval_graph.OvalNode(3, 'value', "false"),
597
        graph.oval_graph.OvalNode(4, 'operator', 'or', [
598
            graph.oval_graph.OvalNode(5, 'value', "false"),
599
            graph.oval_graph.OvalNode(6, 'value', "true")
600
        ]
601
        )
602
    ]
603
    )
604
605
    Tree.change_tree_value(3, "true")
606
    any_test_treeEvaluation(Tree, "true")
607
608
609
def test_bad_results_counts_for_operator_and():
610
    assert graph.evaluate.oval_operator_and(results_counts) is None
611
612
613
def test_bad_results_counts_for_operator_one():
614
    assert graph.evaluate.oval_operator_one(results_counts) is None
615
616
617
def test_bad_results_counts_for_operator_or():
618
    assert graph.evaluate.oval_operator_or(results_counts) is None
619
620
621
def test_bad_results_counts_for_operator_xor():
622
    assert graph.evaluate.oval_operator_xor(results_counts) is None
623
624
625
def test_false_noteval_greater_zero():
626
    assert graph.evaluate.greater_zero(results_counts, 'noteval_cnt') == False
627
628
629
def test_false_smaller_then_two():
630
    assert graph.evaluate.smaller_than_two(
631
        results_counts1, 'true_cnt') == False
632
633
634
def test_false_eq_or_greater_zero_unknown_noteval_notappl():
635
    assert graph.evaluate.eq_or_greater_zero_unknown_noteval_notappl(
636
        results_counts1) == False
637
638
639
def test_false_error_unknown_eq_noteval_greater_zero():
640
    assert graph.evaluate.error_unknown_eq_noteval_greater_zero(
641
        results_counts) == False
642
643
644
def any_test_parsing_and_evaluate_scan_rule(src, rule_id, result):
645
    _dir = os.path.dirname(os.path.realpath(__file__))
646
    FIXTURE_DIR = py.path.local(_dir) / src
647
648
    oval_tree = graph.oval_graph.build_nodes_form_xml(
649
        str(FIXTURE_DIR), rule_id)
650
    any_test_treeEvaluation(oval_tree, result)
651
652
653
def get_simple_tree():
654
    return graph.oval_graph.OvalNode(1, 'operator', 'and', [
655
        graph.oval_graph.OvalNode(2, 'value', "true"),
656
        graph.oval_graph.OvalNode(3, 'value', "false"),
657
        graph.oval_graph.OvalNode(4, 'operator', 'or', [
658
            graph.oval_graph.OvalNode(5, 'value', "false"),
659
            graph.oval_graph.OvalNode(6, 'value', "true")
660
        ]
661
        )
662
    ]
663
    )
664
665
666
def get_dict_of_simple_tree():
667
    return get_simple_tree().save_tree_to_dict()
668
669
670
def any_test_create_node_dict_for_sigmaJs(Tree, out):
671
672
    assert Tree._create_node(0, 0) == out
673
674
675
def test_create_node_dict_for_sigmaJs_0():
676
    out = {
677
        'color': '#ff0000',
678
        'id': 1,
679
        'label': 'and',
680
        'size': 3,
681
        'text': 'null',
682
        'url': 'null',
683
        'title': 1,
684
        'x': 0,
685
        'y': 0
686
    }
687
    Tree = get_simple_tree()
688
    any_test_create_node_dict_for_sigmaJs(Tree, out)
689
690
691
def test_create_node_dict_for_sigmaJs_1():
692
    out = {
693
        'color': '#00ff00',
694
        'id': 1,
695
        'label': 'and',
696
        'size': 3,
697
        'text': 'null',
698
        'url': 'null',
699
        'title': 1,
700
        'x': 0,
701
        'y': 0
702
    }
703
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
704
        graph.oval_graph.OvalNode(2, 'value', "true")
705
    ]
706
    )
707
708
    any_test_create_node_dict_for_sigmaJs(Tree, out)
709
710
711
def test_create_node_dict_for_sigmaJs_2():
712
    out = {
713
        'color': '#000000',
714
        'id': 1,
715
        'label': 'and',
716
        'size': 3,
717
        'text': 'null',
718
        'url': 'null',
719
        'title': '1 and',
720
        'x': 0,
721
        'y': 0
722
    }
723
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
724
        graph.oval_graph.OvalNode(2, 'value', "noteval")
725
    ]
726
    )
727
728
    any_test_create_node_dict_for_sigmaJs(Tree, out)
729
730
731
def test_create_node_dict_for_sigmaJs_3():
732
    out = {
733
        'color': '#ff0000',
734
        'id': 1,
735
        'label': '1',
736
        'size': 3,
737
        'text': 'null',
738
        'url': 'null',
739
        'title': 1,
740
        'x': 0,
741
        'y': 0
742
    }
743
    Tree = graph.oval_graph.OvalNode(1, 'value', 'false')
744
745
    any_test_create_node_dict_for_sigmaJs(Tree, out)
746
747
748
def test_create_node_dict_for_sigmaJs_4():
749
    out = {
750
        'color': '#00ff00',
751
        'id': 1,
752
        'label': '1',
753
        'size': 3,
754
        'text': 'null',
755
        'url': 'null',
756
        'title': 1,
757
        'x': 0,
758
        'y': 0
759
    }
760
    Tree = graph.oval_graph.OvalNode(1, 'value', 'true')
761
762
    any_test_create_node_dict_for_sigmaJs(Tree, out)
763
764
765
def test_create_node_dict_for_sigmaJs_5():
766
    out = {
767
        'color': '#000000',
768
        'id': 1,
769
        'label': 'error',
770
        'size': 3,
771
        'text': 'null',
772
        'url': 'null',
773
        'title': '1 error',
774
        'x': 0,
775
        'y': 0
776
    }
777
    Tree = graph.oval_graph.OvalNode(1, 'value', 'error')
778
779
    any_test_create_node_dict_for_sigmaJs(Tree, out)
780
781
782
def test_create_edge_dict_for_sigmaJs():
783
    print(get_simple_tree()._create_edge(1, 2))
784
    out = {
785
        'id': 'random_ID',
786
        'source': 1,
787
        'target': 2
788
    }
789
790
    assert get_simple_tree()._create_edge(1, 2)['source'] == out['source']
791
    assert get_simple_tree()._create_edge(1, 2)['target'] == out['target']
792
793
794
def test_create_array_of_ids_form_tree():
795
    array = get_simple_tree().create_list_of_id()
796
    assert array == [1, 2, 3, 4, 5, 6]
797
798
799
def test_parsing_full_can_XML_and_evaluate():
800
    src = 'test_data/ssg-fedora-ds-arf.xml'
801
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
802
    result = 'false'
803
804
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
805
806
807
def test_parsing_and_evaluate_scan_with_extend_def():
808
    src = 'test_data/ssg-fedora-ds-arf-scan-with-extend-definitions.xml'
809
    rule_id = 'xccdf_org.ssgproject.content_rule_sysctl_net_ipv6_conf_all_disable_ipv6'
810
    result = 'false'
811
812
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
813
814
815
def test_parsing_and_evaluate_scan_with_pasing_rule():
816
    src = 'test_data/ssg-fedora-ds-arf-passing-scan.xml'
817
    rule_id = 'xccdf_org.ssgproject.content_rule_service_debug-shell_disabled'
818
    result = 'true'
819
820
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
821
822
823
def test_parsing_and_evaluate_scan_with_fail_rule():
824
    src = 'test_data/ssg-fedora-ds-arf-scan-fail.xml'
825
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec'
826
    result = 'false'
827
828
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
829
830
831
def test_parsing_and_evaluate_scan_with_rule_with_XOR():
832
    src = 'test_data/ssg-fedora-ds-arf-scan-with-xor.xml'
833
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_nosuid_removable_partitions'
834
    result = 'true'
835
836
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
837
838
839
def test_parsing_and_evaluate_scan_with_11_rules():
840
    src = 'test_data/ssg-fedora-ds-arf-scan-with-11-rules.xml'
841
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_tmp_nosuid'
842
    result = 'true'
843
844
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
845
846
847
def test_transformation_tree_to_Json_for_SigmaJs_0():
848
    test_data = {'nodes': [
849
        {
850
            'id': 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny',
851
            'label': 'and',
852
            'url': 'null',
853
            'text': 'null',
854
            'title': 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny',
855
            'x': -13,
856
            'y': 0,
857
            'size': 3,
858
            'color': '#ff0000'},
859
        {'id': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
860
         'label': 'and',
861
         'url': 'null',
862
         'text': 'null',
863
         'title': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
864
         'x': -13,
865
         'y': 1,
866
         'size': 3,
867
         'color': '#ff0000'},
868
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1',
869
         'label': 'test_accounts_passwords_pam_faillock_preauth_silent_system-auth',
870
         'url': 'null',
871
         'text': 'null',
872
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1',
873
         'x': -11,
874
         'y': 3,
875
         'size': 3,
876
         'color': '#ff0000'},
877
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1',
878
         'label': 'test_accounts_passwords_pam_faillock_account_phase_system-auth',
879
         'url': 'null',
880
         'text': 'null',
881
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1',
882
         'x': -9,
883
         'y': 3,
884
         'size': 3,
885
         'color': '#ff0000'},
886
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1',
887
         'label': 'test_accounts_passwords_pam_faillock_preauth_silent_password-auth',
888
         'url': 'null',
889
         'text': 'null',
890
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1',
891
         'x': -7,
892
         'y': 3,
893
         'size': 3,
894
         'color': '#ff0000'},
895
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1',
896
         'label': 'test_accounts_passwords_pam_faillock_account_phase_password-auth',
897
         'url': 'null',
898
         'text': 'null',
899
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1',
900
         'x': -5,
901
         'y': 3,
902
         'size': 3,
903
         'color': '#ff0000'},
904
        {'id': 'debcac8c-00b4-49f7-ac9d-0b3634c6f42a',
905
         'label': 'and',
906
         'url': 'null',
907
         'text': 'null',
908
         'title': 'debcac8c-00b4-49f7-ac9d-0b3634c6f42a',
909
         'x': -3,
910
         'y': 3,
911
         'size': 3,
912
         'color': '#ff0000'},
913
        {'id': '6ae09564-8357-48ac-8756-7e93d1a76d17',
914
         'label': 'or',
915
         'url': 'null',
916
         'text': 'null',
917
         'title': '6ae09564-8357-48ac-8756-7e93d1a76d17',
918
         'x': -1,
919
         'y': 5,
920
         'size': 3,
921
         'color': '#ff0000'},
922
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1',
923
         'label': 'test_accounts_passwords_pam_faillock_numeric_default_check_system-auth',
924
         'url': 'null',
925
         'text': 'null',
926
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1',
927
         'x': 1,
928
         'y': 7,
929
         'size': 3,
930
         'color': '#ff0000'},
931
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1',
932
         'label': 'test_accounts_passwords_pam_faillock_authfail_deny_system-auth',
933
         'url': 'null',
934
         'text': 'null',
935
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1',
936
         'x': 3,
937
         'y': 7,
938
         'size': 3,
939
         'color': '#ff0000'},
940
        {'id': '85e05733-2ad9-4388-808e-df7f4a6e1af1',
941
         'label': 'or',
942
         'url': 'null',
943
         'text': 'null',
944
         'title': '85e05733-2ad9-4388-808e-df7f4a6e1af1',
945
         'x': 3,
946
         'y': 5,
947
         'size': 3,
948
         'color': '#ff0000'},
949
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1',
950
         'label': 'test_accounts_passwords_pam_faillock_numeric_default_check_password-auth',
951
         'url': 'null',
952
         'text': 'null',
953
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1',
954
         'x': 5,
955
         'y': 7,
956
         'size': 3,
957
         'color': '#ff0000'},
958
        {'id': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1',
959
         'label': 'test_accounts_passwords_pam_faillock_authfail_deny_password-auth',
960
         'url': 'null',
961
         'text': 'null',
962
         'title': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1',
963
         'x': 7,
964
         'y': 7,
965
         'size': 3,
966
         'color': '#ff0000'}],
967
        'edges': [{'id': '794b350c-d6dd-41cf-8d7a-b52b1e14d895',
968
                   'source': 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny',
969
                   'target': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1'},
970
                  {'id': '078b88db-bec0-41cd-80fb-ed06cfeee800',
971
                   'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
972
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1'},
973
                  {'id': '99061b30-6d6b-4129-99f2-14a87b40171a',
974
                   'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
975
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1'},
976
                  {'id': '59b04168-2747-485b-a17e-008c35b411ab',
977
                   'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
978
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1'},
979
                  {'id': '84be8e5f-7005-477c-8cec-0e8dad87c940',
980
                   'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
981
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1'},
982
                  {'id': '4b6990be-94a5-4f0e-b423-19e2147e856d',
983
                   'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
984
                   'target': 'debcac8c-00b4-49f7-ac9d-0b3634c6f42a'},
985
                  {'id': 'c9d4ef13-7444-40d2-b18e-f6f68dce7e9d',
986
                   'source': 'debcac8c-00b4-49f7-ac9d-0b3634c6f42a',
987
                   'target': '6ae09564-8357-48ac-8756-7e93d1a76d17'},
988
                  {'id': '89e28ab1-fc71-42f6-837f-7835d9609034',
989
                   'source': '6ae09564-8357-48ac-8756-7e93d1a76d17',
990
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1'},
991
                  {'id': '194ee0ab-c1e7-4ccf-ab8c-71ac28112e7a',
992
                   'source': '6ae09564-8357-48ac-8756-7e93d1a76d17',
993
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1'},
994
                  {'id': '79f062b0-db9c-4ad9-9b46-86dc9222f7e9',
995
                   'source': 'debcac8c-00b4-49f7-ac9d-0b3634c6f42a',
996
                   'target': '85e05733-2ad9-4388-808e-df7f4a6e1af1'},
997
                  {'id': '043ac902-5962-407c-bdd6-d7cb71438a44',
998
                   'source': '85e05733-2ad9-4388-808e-df7f4a6e1af1',
999
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1'},
1000
                  {'id': '0c0aa354-8da5-4d43-980d-187e20852b34',
1001
                   'source': '85e05733-2ad9-4388-808e-df7f4a6e1af1',
1002
                   'target': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1'}]}
1003
    src = 'data/ssg-fedora-ds-arf.xml'
1004
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
1005
1006
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
1007
    if oval_tree.node_id == rule_id:
1008
        out_data = oval_tree.to_sigma_dict(0, 0)
1009
        for i in range(len(out_data['nodes'])):
1010
            assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label']
1011
            assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text']
1012
            assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url']
1013
1014
1015
def test_transformation_tree_to_Json_for_SigmaJs_with_duplicated_test():
1016
    test_data = {'nodes': [{'id': 'xccdf_org.ssgproject.content_rule_disable_host_auth',
1017
                            'label': 'and',
1018
                            'url': 'null',
1019
                            'text': 'null',
1020
                            'title': 'xccdf_org.ssgproject.content_rule_disable_host_auth',
1021
                            'x': -17,
1022
                            'y': 0,
1023
                            'size': 3,
1024
                            'color': '#00ff00'},
1025
                           {'id': 'oval:ssg-disable_host_auth:def:1',
1026
                            'label': 'or',
1027
                            'url': 'null',
1028
                            'text': 'null',
1029
                            'title': 'oval:ssg-disable_host_auth:def:1',
1030
                            'x': -17,
1031
                            'y': 1,
1032
                            'size': 3,
1033
                            'color': '#00ff00'},
1034
                           {'id': '22ec5137-0406-41a1-871d-3f8e103b9bdb',
1035
                            'label': 'and',
1036
                            'url': 'null',
1037
                            'text': 'null',
1038
                            'title': '22ec5137-0406-41a1-871d-3f8e103b9bdb',
1039
                            'x': -15,
1040
                            'y': 3,
1041
                            'size': 3,
1042
                            'color': '#ff0000'},
1043
                           {'id': '25e0a4f1-5a54-48cf-95d5-8eea945ac7e2',
1044
                            'label': 'or',
1045
                            'url': 'null',
1046
                            'text': 'null',
1047
                            'title': '25e0a4f1-5a54-48cf-95d5-8eea945ac7e2',
1048
                            'x': -13,
1049
                            'y': 5,
1050
                            'size': 3,
1051
                            'color': '#00ff00'},
1052
                           {'id': 'oval:ssg-test_sshd_not_required:tst:1',
1053
                            'label': 'test_sshd_not_required',
1054
                            'url': 'null',
1055
                            'text': 'null',
1056
                            'title': 'oval:ssg-test_sshd_not_required:tst:1',
1057
                            'x': -11,
1058
                            'y': 7,
1059
                            'size': 3,
1060
                            'color': '#ff0000'},
1061
                           {'id': '37db2c90-8fd2-41d1-b87d-ccbe6c8212a9',
1062
                            'label': 'and',
1063
                            'url': 'null',
1064
                            'text': 'null',
1065
                            'title': '37db2c90-8fd2-41d1-b87d-ccbe6c8212a9',
1066
                            'x': -9,
1067
                            'y': 7,
1068
                            'size': 3,
1069
                            'color': '#00ff00'},
1070
                           {'id': '11c91596-51f3-44c1-8d99-d67c50559038',
1071
                            'label': 'and',
1072
                            'url': 'null',
1073
                            'text': 'null',
1074
                            'title': '11c91596-51f3-44c1-8d99-d67c50559038',
1075
                            'x': -8,
1076
                            'y': 5,
1077
                            'size': 3,
1078
                            'color': '#ff0000'},
1079
                           {'id': 'oval:ssg-test_package_openssh-server_removed:tst:1',
1080
                            'label': 'test_package_openssh-server_removed',
1081
                            'url': 'null',
1082
                            'text': 'null',
1083
                            'title': 'oval:ssg-test_package_openssh-server_removed:tst:1',
1084
                            'x': -6,
1085
                            'y': 7,
1086
                            'size': 3,
1087
                            'color': '#ff0000'},
1088
                           {'id': '42c9a387-08a6-4767-a25e-9eabb43b8ae4',
1089
                            'label': 'and',
1090
                            'url': 'null',
1091
                            'text': 'null',
1092
                            'title': '42c9a387-08a6-4767-a25e-9eabb43b8ae4',
1093
                            'x': -7,
1094
                            'y': 3,
1095
                            'size': 3,
1096
                            'color': '#00ff00'},
1097
                           {'id': '89cf4075-0f24-442b-ab01-0bb93d5a19a0',
1098
                            'label': 'or',
1099
                            'url': 'null',
1100
                            'text': 'null',
1101
                            'title': '89cf4075-0f24-442b-ab01-0bb93d5a19a0',
1102
                            'x': -5,
1103
                            'y': 5,
1104
                            'size': 3,
1105
                            'color': '#00ff00'},
1106
                           {'id': 'oval:ssg-test_sshd_required:tst:1',
1107
                            'label': 'test_sshd_required',
1108
                            'url': 'null',
1109
                            'text': 'null',
1110
                            'title': 'oval:ssg-test_sshd_required:tst:1',
1111
                            'x': -3,
1112
                            'y': 7,
1113
                            'size': 3,
1114
                            'color': '#ff0000'},
1115
                           {'id': '10126dbc-be9c-49ce-b071-ab92c2b8a0df',
1116
                            'label': 'and',
1117
                            'url': 'null',
1118
                            'text': 'null',
1119
                            'title': '10126dbc-be9c-49ce-b071-ab92c2b8a0df',
1120
                            'x': -1,
1121
                            'y': 7,
1122
                            'size': 3,
1123
                            'color': '#00ff00'},
1124
                           {'id': '7a6dd54f-4e6c-4057-8c94-6598ee992c89',
1125
                            'label': 'and',
1126
                            'url': 'null',
1127
                            'text': 'null',
1128
                            'title': '7a6dd54f-4e6c-4057-8c94-6598ee992c89',
1129
                            'x': 0,
1130
                            'y': 5,
1131
                            'size': 3,
1132
                            'color': '#00ff00'},
1133
                           {'id': 'oval:ssg-test_package_openssh-server_installed:tst:1',
1134
                            'label': 'test_package_openssh-server_installed',
1135
                            'url': 'null',
1136
                            'text': 'null',
1137
                            'title': 'oval:ssg-test_package_openssh-server_installed:tst:1',
1138
                            'x': 2,
1139
                            'y': 7,
1140
                            'size': 3,
1141
                            'color': '#00ff00'},
1142
                           {'id': 'oval:ssg-test_sshd_hostbasedauthentication:tst:1',
1143
                            'label': 'test_sshd_hostbasedauthentication',
1144
                            'url': 'null',
1145
                            'text': 'null',
1146
                            'title': 'oval:ssg-test_sshd_hostbasedauthentication:tst:1',
1147
                            'x': 3,
1148
                            'y': 5,
1149
                            'size': 3,
1150
                            'color': '#00ff00'},
1151
                           {'id': 'oval:ssg-test_sshd_requirement_unset:tst:1',
1152
                            'label': 'test_sshd_requirement_unset',
1153
                            'url': 'null',
1154
                            'text': 'null',
1155
                            'title': 'oval:ssg-test_sshd_requirement_unset:tst:1',
1156
                            'x': -7,
1157
                            'y': 9,
1158
                            'size': 3,
1159
                            'color': '#00ff00'}],
1160
                 'edges': [{'id': 'f89168c8-7e46-44f0-8a52-04b0af6330f1',
1161
                            'source': 'xccdf_org.ssgproject.content_rule_disable_host_auth',
1162
                            'target': 'oval:ssg-disable_host_auth:def:1'},
1163
                           {'id': '0237cfd3-642e-484e-80be-c53788cc0585',
1164
                            'source': 'oval:ssg-disable_host_auth:def:1',
1165
                            'target': '22ec5137-0406-41a1-871d-3f8e103b9bdb'},
1166
                           {'id': 'f2be3ac1-51f8-4fb3-b836-2fc2426460cd',
1167
                            'source': '22ec5137-0406-41a1-871d-3f8e103b9bdb',
1168
                            'target': '25e0a4f1-5a54-48cf-95d5-8eea945ac7e2'},
1169
                           {'id': '71292795-3b1d-48b4-ba37-0bc1821272c4',
1170
                            'source': '25e0a4f1-5a54-48cf-95d5-8eea945ac7e2',
1171
                            'target': 'oval:ssg-test_sshd_not_required:tst:1'},
1172
                           {'id': '70cf0ff2-0b42-47a7-b0c0-a8b4cacb33db',
1173
                            'source': '25e0a4f1-5a54-48cf-95d5-8eea945ac7e2',
1174
                            'target': '37db2c90-8fd2-41d1-b87d-ccbe6c8212a9'},
1175
                           {'id': '7d6f1fb2-081d-4d86-97a4-9f2292699f25',
1176
                            'source': '37db2c90-8fd2-41d1-b87d-ccbe6c8212a9',
1177
                            'target': 'oval:ssg-test_sshd_requirement_unset:tst:1'},
1178
                           {'id': 'f147b183-f8b7-4ee7-9410-224d6d41f55b',
1179
                            'source': '22ec5137-0406-41a1-871d-3f8e103b9bdb',
1180
                            'target': '11c91596-51f3-44c1-8d99-d67c50559038'},
1181
                           {'id': '458a4e10-69ed-4f6c-a2f4-47ba6dde7905',
1182
                            'source': '11c91596-51f3-44c1-8d99-d67c50559038',
1183
                            'target': 'oval:ssg-test_package_openssh-server_removed:tst:1'},
1184
                           {'id': 'a43d4c25-b1a9-4fe1-ad49-4698b0f5bec7',
1185
                            'source': 'oval:ssg-disable_host_auth:def:1',
1186
                            'target': '42c9a387-08a6-4767-a25e-9eabb43b8ae4'},
1187
                           {'id': '25919b3f-a28a-4e70-ae72-f58aa8457da3',
1188
                            'source': '42c9a387-08a6-4767-a25e-9eabb43b8ae4',
1189
                            'target': '89cf4075-0f24-442b-ab01-0bb93d5a19a0'},
1190
                           {'id': '368263c5-35e2-4bac-b851-dafc737dc4fb',
1191
                            'source': '89cf4075-0f24-442b-ab01-0bb93d5a19a0',
1192
                            'target': 'oval:ssg-test_sshd_required:tst:1'},
1193
                           {'id': 'bd9006b7-f09d-4a78-a3d0-7a4f4420fe01',
1194
                            'source': '89cf4075-0f24-442b-ab01-0bb93d5a19a0',
1195
                            'target': '10126dbc-be9c-49ce-b071-ab92c2b8a0df'},
1196
                           {'id': '094d0dd8-ce8d-4d0a-b502-020a4d2525f6',
1197
                            'source': '10126dbc-be9c-49ce-b071-ab92c2b8a0df',
1198
                            'target': 'oval:ssg-test_sshd_requirement_unset:tst:1'},
1199
                           {'id': '62057baa-2eb8-41d8-b74b-584d69092aca',
1200
                            'source': '42c9a387-08a6-4767-a25e-9eabb43b8ae4',
1201
                            'target': '7a6dd54f-4e6c-4057-8c94-6598ee992c89'},
1202
                           {'id': 'b4abd067-d93c-465c-95c6-d29d38b4ddf0',
1203
                            'source': '7a6dd54f-4e6c-4057-8c94-6598ee992c89',
1204
                            'target': 'oval:ssg-test_package_openssh-server_installed:tst:1'},
1205
                           {'id': '4c5b3aa0-0b1f-411e-b4ca-badf7dd7ab69',
1206
                            'source': '42c9a387-08a6-4767-a25e-9eabb43b8ae4',
1207
                            'target': 'oval:ssg-test_sshd_hostbasedauthentication:tst:1'}]}
1208
1209
    src = 'data/ssg-fedora-ds-arf.xml'
1210
    rule_id = 'xccdf_org.ssgproject.content_rule_disable_host_auth'
1211
1212
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
1213
1214
    if oval_tree.node_id == rule_id:
1215
        out_data = oval_tree.to_sigma_dict(0, 0)
1216
        print(out_data)
1217
        for i in range(len(out_data['nodes'])):
1218
            assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label']
1219
            assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text']
1220
            assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url']
1221
1222
1223
def test_and_or_eq_zero():
1224
    assert graph.evaluate.and_or_eq_zero('and', results_counts) == False
1225
    assert graph.evaluate.and_or_eq_zero('or', results_counts) == False
1226
    assert graph.evaluate.and_or_eq_zero('xor', results_counts) is None
1227
1228
1229
def test_get_def_id_by_rule_id():
1230
    src = 'test_data/ssg-fedora-ds-arf.xml'
1231
    _dir = os.path.dirname(os.path.realpath(__file__))
1232
    FIXTURE_DIR = py.path.local(_dir) / src
1233
1234
    parser = graph.xml_parser.xml_parser(str(FIXTURE_DIR))
1235
1236
    with pytest.raises(ValueError) as e:
1237
        parser.get_def_id_by_rule_id('hello')
1238
    assert str(
1239
        e.value) == 'err- 404 rule not found!'
1240