Passed
Pull Request — codeStyle (#2)
by Jan
02:08
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': 0,
856
            'y': 0,
857
            'size': 3,
858
            'color': '#ff0000'},
859
        {
860
            'id': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
861
            'label': 'and',
862
            'url': 'null',
863
            'text': 'null',
864
            'title': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
865
            'x': 0,
866
            'y': 1,
867
            'size': 3,
868
            'color': '#ff0000'},
869
        {
870
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1',
871
            'label': 'accounts_passwords_pam_faillock_preauth_silent_system-auth',
872
            'url': 'null',
873
            'text': 'null',
874
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1',
875
            'x': -4,
876
            'y': 2.36,
877
            'size': 3,
878
            'color': '#ff0000'},
879
        {
880
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1',
881
            'label': 'accounts_passwords_pam_faillock_account_phase_system-auth',
882
            'url': 'null',
883
            'text': 'null',
884
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1',
885
            'x': -2,
886
            'y': 2.7199999999999998,
887
            'size': 3,
888
            'color': '#ff0000'},
889
       {
890
           'id': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1',
891
            'label': 'accounts_passwords_pam_faillock_preauth_silent_password-auth',
892
            'url': 'null',
893
            'text': 'null',
894
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1',
895
            'x': 0,
896
            'y': 3.08,
897
            'size': 3,
898
            'color': '#ff0000'},
899
        {
900
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1',
901
            'label': 'accounts_passwords_pam_faillock_account_phase_password-auth',
902
            'url': 'null',
903
            'text': 'null',
904
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1',
905
            'x': 2,
906
            'y': 3.44,
907
            'size': 3,
908
            'color': '#ff0000'},
909
        {
910
            'id': '53d15de4-262b-469f-8df6-dd93d8d9a2de',
911
            'label': 'and',
912
            'url': 'null',
913
            'text': 'null',
914
            'title': '53d15de4-262b-469f-8df6-dd93d8d9a2de',
915
            'x': 4,
916
            'y': 3.8,
917
            'size': 3,
918
            'color': '#ff0000'},
919
        {
920
            'id': '551bbfca-b55f-4b67-a6ca-fefa9a886ec8',
921
            'label': 'or',
922
            'url': 'null',
923
            'text': 'null',
924
            'title': '551bbfca-b55f-4b67-a6ca-fefa9a886ec8',
925
            'x': -2,
926
            'y': 4.1,
927
            'size': 3,
928
            'color': '#ff0000'},
929
        {
930
            'id': '1b9f74b8-1c58-4b09-969b-2413e9594f08',
931
            'label': 'or',
932
            'url': 'null',
933
            'text': 'null',
934
            'title': '1b9f74b8-1c58-4b09-969b-2413e9594f08',
935
            'x': 2,
936
            'y': 4.1,
937
            'size': 3,
938
            'color': '#ff0000'},
939
        {
940
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1',
941
            'label': 'accounts_passwords_pam_faillock_numeric_default_check_system-auth',
942
            'url': 'null',
943
            'text': 'null',
944
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1',
945
            'x': -4,
946
            'y': 4.36,
947
            'size': 3,
948
            'color': '#ff0000'},
949
        {
950
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1',
951
            'label': 'accounts_passwords_pam_faillock_authfail_deny_system-auth',
952
            'url': 'null',
953
            'text': 'null',
954
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1',
955
            'x': -2,
956
            'y': 4.72,
957
            'size': 3,
958
            'color': '#ff0000'},
959
        {
960
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1',
961
            'label': 'accounts_passwords_pam_faillock_numeric_default_check_password-auth',
962
            'url': 'null',
963
            'text': 'null',
964
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1',
965
            'x': 0,
966
            'y': 5.08,
967
            'size': 3,
968
            'color': '#ff0000'},
969
        {
970
            'id': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1',
971
            'label': 'accounts_passwords_pam_faillock_authfail_deny_password-auth',
972
            'url': 'null',
973
            'text': 'null',
974
            'title': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1',
975
            'x': 2,
976
            'y': 5.4399999999999995,
977
            'size': 3,
978
            'color': '#ff0000'}],
979
        'edges': [
980
        {
981
            'id': 'b56a7fd0-f53e-45d3-83af-bd0c4bfa87cb',
982
            'source': 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny',
983
            'target': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1'},
984
        {
985
            'id': '7107c6c9-b22e-4bf0-a67f-9c79f48825be',
986
            'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
987
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1'},
988
        {
989
            'id': '693179e3-2aea-4d9b-b021-65488993e39a',
990
            'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
991
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1'},
992
        {
993
            'id': 'd179dff2-9c03-4455-a59e-6fb2ad15cdf4',
994
            'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
995
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1'},
996
        {
997
            'id': '792c8d34-84f5-49c7-be98-965a977466ac',
998
            'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
999
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1'},
1000
        {
1001
            'id': 'd8eee053-a0f6-4a73-8867-f17ffbc4b095',
1002
            'source': 'oval:ssg-accounts_passwords_pam_faillock_deny:def:1',
1003
            'target': '53d15de4-262b-469f-8df6-dd93d8d9a2de'},
1004
        {
1005
            'id': 'ff763a4b-2a9f-4f72-be96-f8cc0dcbdfbf',
1006
            'source': '53d15de4-262b-469f-8df6-dd93d8d9a2de',
1007
            'target': '551bbfca-b55f-4b67-a6ca-fefa9a886ec8'},
1008
        {
1009
            'id': 'dfdfcb00-97aa-4280-bc16-7f82c151373e',
1010
            'source': '551bbfca-b55f-4b67-a6ca-fefa9a886ec8',
1011
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1'},
1012
        {
1013
            'id': '05e35e95-f42a-419b-acd8-dff9726956f8',
1014
            'source': '551bbfca-b55f-4b67-a6ca-fefa9a886ec8',
1015
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1'},
1016
        {
1017
            'id': 'ddbc2340-bbc1-4a0f-be9f-2a4884b40408',
1018
            'source': '53d15de4-262b-469f-8df6-dd93d8d9a2de',
1019
            'target': '1b9f74b8-1c58-4b09-969b-2413e9594f08'},
1020
        {
1021
            'id': 'b973e01b-33f6-4a75-a2b1-c3b478ef4890',
1022
            'source': '1b9f74b8-1c58-4b09-969b-2413e9594f08',
1023
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1'},
1024
        {
1025
            'id': '6acd02b8-7c44-49c4-8040-94f0305721d6',
1026
            'source': '1b9f74b8-1c58-4b09-969b-2413e9594f08',
1027
            'target': 'oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1'}]}
1028
1029
    src = 'data/ssg-fedora-ds-arf.xml'
1030
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
1031
1032
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
1033
    if oval_tree.node_id == rule_id:
1034
        out_data = oval_tree.to_sigma_dict(0, 0)
1035
        print(out_data)
1036
        for i in range(len(out_data['nodes'])):
1037
            assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label']
1038
            assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text']
1039
            assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url']
1040
1041
1042
def test_transformation_tree_to_Json_for_SigmaJs_with_duplicated_test():
1043
    test_data = {'nodes': [
1044
        {
1045
            'id': 'xccdf_org.ssgproject.content_rule_disable_host_auth',
1046
            'label': 'and',
1047
            'url': 'null',
1048
            'text': 'null',
1049
            'title': 'xccdf_org.ssgproject.content_rule_disable_host_auth',
1050
            'x': 0,
1051
            'y': 0,
1052
            'size': 3,
1053
            'color': '#00ff00'},
1054
        {
1055
            'id': 'oval:ssg-disable_host_auth:def:1',
1056
            'label': 'or',
1057
            'url': 'null',
1058
            'text': 'null',
1059
            'title': 'oval:ssg-disable_host_auth:def:1',
1060
            'x': 0,
1061
            'y': 1,
1062
            'size': 3,
1063
            'color': '#00ff00'},
1064
        {
1065
            'id': '877adf97-04e6-4e2f-af1a-2b75f4b5c0ac',
1066
            'label': 'and',
1067
            'url': 'null',
1068
            'text': 'null',
1069
            'title': '877adf97-04e6-4e2f-af1a-2b75f4b5c0ac',
1070
            'x': -2,
1071
            'y': 2,
1072
            'size': 3,
1073
            'color': '#ff0000'},
1074
        {
1075
            'id': '5eb52597-0ad6-44b0-9df9-1782c8429962',
1076
            'label': 'and',
1077
            'url': 'null',
1078
            'text': 'null',
1079
            'title': '5eb52597-0ad6-44b0-9df9-1782c8429962',
1080
            'x': 2,
1081
            'y': 2,
1082
            'size': 3,
1083
            'color': '#00ff00'},
1084
        {
1085
            'id': '49024905-176a-4acd-9218-4f7966fa4f76',
1086
            'label': 'or',
1087
            'url': 'null',
1088
            'text': 'null',
1089
            'title': '49024905-176a-4acd-9218-4f7966fa4f76',
1090
            'x': -4,
1091
            'y': 3,
1092
            'size': 3,
1093
            'color': '#00ff00'},
1094
        {
1095
            'id': '300c4561-ca86-49db-ac8d-4c7471f2aafd',
1096
            'label': 'and',
1097
            'url': 'null',
1098
            'text': 'null',
1099
            'title': '300c4561-ca86-49db-ac8d-4c7471f2aafd',
1100
            'x': -2,
1101
            'y': 3,
1102
            'size': 3,
1103
            'color': '#ff0000'},
1104
       {
1105
            'id': 'a11311fb-f598-41ef-82da-80f96a4b0db3',
1106
            'label': 'or',
1107
            'url': 'null',
1108
            'text': 'null',
1109
            'title': 'a11311fb-f598-41ef-82da-80f96a4b0db3',
1110
            'x': 0,
1111
            'y': 3,
1112
            'size': 3,
1113
            'color': '#00ff00'},
1114
        {
1115
            'id': '5ccd4201-a08d-4c77-8593-089a30024435',
1116
            'label': 'and',
1117
            'url': 'null',
1118
            'text': 'null',
1119
            'title': '5ccd4201-a08d-4c77-8593-089a30024435',
1120
            'x': 2,
1121
            'y': 3,
1122
            'size': 3,
1123
            'color': '#00ff00'},
1124
        {
1125
            'id': 'oval:ssg-test_sshd_hostbasedauthentication:tst:1',
1126
            'label': 'sshd_hostbasedauthentication',
1127
            'url': 'null',
1128
            'text': 'null',
1129
            'title': 'oval:ssg-test_sshd_hostbasedauthentication:tst:1',
1130
            'x': 4,
1131
            'y': 3.36,
1132
            'size': 3,
1133
            'color': '#00ff00'},
1134
        {
1135
            'id': 'oval:ssg-test_sshd_not_required:tst:1',
1136
            'label': 'sshd_not_required',
1137
            'url': 'null',
1138
            'text': 'null',
1139
            'title': 'oval:ssg-test_sshd_not_required:tst:1',
1140
            'x': -6,
1141
            'y': 4,
1142
            'size': 3,
1143
            'color': '#ff0000'},
1144
        {
1145
            'id': 'e1a2009b-1612-4580-b72b-482873e6ba5e',
1146
            'label': 'and',
1147
            'url': 'null',
1148
            'text': 'null',
1149
            'title': 'e1a2009b-1612-4580-b72b-482873e6ba5e',
1150
            'x': -4,
1151
            'y': 4,
1152
            'size': 3,
1153
            'color': '#00ff00'},
1154
        {
1155
            'id': 'oval:ssg-test_package_openssh-server_removed:tst:1',
1156
            'label': 'package_openssh-server_removed',
1157
            'url': 'null',
1158
            'text': 'null',
1159
            'title': 'oval:ssg-test_package_openssh-server_removed:tst:1',
1160
            'x': -2,
1161
            'y': 4.36,
1162
            'size': 3,
1163
            'color': '#ff0000'},
1164
        {
1165
            'id': 'oval:ssg-test_sshd_required:tst:1',
1166
            'label': 'sshd_required',
1167
            'url': 'null',
1168
            'text': 'null',
1169
            'title': 'oval:ssg-test_sshd_required:tst:1',
1170
            'x': 0,
1171
            'y': 4,
1172
            'size': 3,
1173
            'color': '#ff0000'},
1174
        {
1175
            'id': '54318a01-99bb-4b72-8111-3600e99cbc64',
1176
            'label': 'and',
1177
            'url': 'null',
1178
            'text': 'null',
1179
            'title': '54318a01-99bb-4b72-8111-3600e99cbc64',
1180
            'x': 2,
1181
            'y': 4,
1182
            'size': 3,
1183
            'color': '#00ff00'},
1184
        {
1185
            'id': 'oval:ssg-test_package_openssh-server_installed:tst:1',
1186
            'label': 'package_openssh-server_installed',
1187
            'url': 'null',
1188
            'text': 'null',
1189
            'title': 'oval:ssg-test_package_openssh-server_installed:tst:1',
1190
            'x': 4,
1191
            'y': 4.36,
1192
            'size': 3,
1193
            'color': '#00ff00'},
1194
        {
1195
            'id': 'oval:ssg-test_sshd_requirement_unset:tst:1',
1196
            'label': 'sshd_requirement_unset',
1197
            'url': 'null',
1198
            'text': 'null',
1199
            'title': 'oval:ssg-test_sshd_requirement_unset:tst:1',
1200
            'x': 0,
1201
            'y': 5,
1202
            'size': 3,
1203
            'color': '#00ff00'}],
1204
        'edges': [
1205
            {
1206
                'id': 'eea56f4a-7cef-4be9-b0fc-0f472e56fccf',
1207
                'source': 'xccdf_org.ssgproject.content_rule_disable_host_auth',
1208
                'target': 'oval:ssg-disable_host_auth:def:1'},
1209
            {
1210
                'id': '95d39f35-5e43-417a-9be6-fe2d534648eb',
1211
                'source': 'oval:ssg-disable_host_auth:def:1',
1212
                'target': '877adf97-04e6-4e2f-af1a-2b75f4b5c0ac'},
1213
            {
1214
                'id': '81bee78f-0040-45cd-ac6f-391d7d3b5c4b',
1215
                'source': '877adf97-04e6-4e2f-af1a-2b75f4b5c0ac',
1216
                'target': '49024905-176a-4acd-9218-4f7966fa4f76'},
1217
            {
1218
                'id': '0fa824cf-3a0b-4b2c-8142-c6a1398b8854',
1219
                'source': '49024905-176a-4acd-9218-4f7966fa4f76',
1220
                'target': 'oval:ssg-test_sshd_not_required:tst:1'},
1221
            {
1222
                'id': '2a8aeff8-c9d0-46bf-ba6f-53781a511aa6',
1223
                'source': '49024905-176a-4acd-9218-4f7966fa4f76',
1224
                'target': 'e1a2009b-1612-4580-b72b-482873e6ba5e'},
1225
            {
1226
                'id': '02bcdc1b-bdc2-4562-872b-a2f48c047b4a',
1227
                'source': 'e1a2009b-1612-4580-b72b-482873e6ba5e',
1228
                'target': 'oval:ssg-test_sshd_requirement_unset:tst:1'},
1229
            {
1230
                'id': '5a85614d-ec49-43e4-b193-e4ab9915de37',
1231
                'source': '877adf97-04e6-4e2f-af1a-2b75f4b5c0ac',
1232
                'target': '300c4561-ca86-49db-ac8d-4c7471f2aafd'},
1233
            {
1234
                'id': 'b07ceea7-fbef-4a7d-870f-7b2f732e2dc4',
1235
                'source': '300c4561-ca86-49db-ac8d-4c7471f2aafd',
1236
                'target': 'oval:ssg-test_package_openssh-server_removed:tst:1'},
1237
            {
1238
                'id': '2c82da7f-321f-4cdb-a26e-bda1e0c770eb',
1239
                'source': 'oval:ssg-disable_host_auth:def:1',
1240
                'target': '5eb52597-0ad6-44b0-9df9-1782c8429962'},
1241
            {
1242
                'id': '04e08f99-7dc4-4ec0-9f61-c095a5d31149',
1243
                'source': '5eb52597-0ad6-44b0-9df9-1782c8429962',
1244
                'target': 'a11311fb-f598-41ef-82da-80f96a4b0db3'},
1245
            {
1246
                'id': 'db01fc1a-6575-4501-8694-88799fd6aa22',
1247
                'source': 'a11311fb-f598-41ef-82da-80f96a4b0db3',
1248
                'target': 'oval:ssg-test_sshd_required:tst:1'},
1249
            {
1250
                'id': 'e781cbf0-923f-40a1-8f0d-62af77b6657e',
1251
                'source': 'a11311fb-f598-41ef-82da-80f96a4b0db3',
1252
                'target': '54318a01-99bb-4b72-8111-3600e99cbc64'},
1253
            {
1254
                'id': 'f71355af-8b58-4508-8e4a-fb643a004461',
1255
                'source': '54318a01-99bb-4b72-8111-3600e99cbc64',
1256
                'target': 'oval:ssg-test_sshd_requirement_unset:tst:1'},
1257
            {
1258
                'id': 'f1737acb-0bf9-418f-973a-4148ce971d80',
1259
                'source': '5eb52597-0ad6-44b0-9df9-1782c8429962',
1260
                'target': '5ccd4201-a08d-4c77-8593-089a30024435'},
1261
            {
1262
                'id': '8218b9ac-c8f6-461b-a25e-e57c33bdb96f',
1263
                'source': '5ccd4201-a08d-4c77-8593-089a30024435',
1264
                'target': 'oval:ssg-test_package_openssh-server_installed:tst:1'},
1265
            {
1266
                'id': '6d357f2f-2b98-44f1-a8ae-f7f7d4accaf2',
1267
                'source': '5eb52597-0ad6-44b0-9df9-1782c8429962',
1268
                'target': 'oval:ssg-test_sshd_hostbasedauthentication:tst:1'}]}
1269
1270
    src = 'data/ssg-fedora-ds-arf.xml'
1271
    rule_id = 'xccdf_org.ssgproject.content_rule_disable_host_auth'
1272
1273
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
1274
1275
    if oval_tree.node_id == rule_id:
1276
        out_data = oval_tree.to_sigma_dict(0, 0)
1277
        for i in range(len(out_data['nodes'])):
1278
            assert out_data['nodes'][i]['label'] == test_data['nodes'][i]['label']
1279
            assert out_data['nodes'][i]['text'] == test_data['nodes'][i]['text']
1280
            assert out_data['nodes'][i]['url'] == test_data['nodes'][i]['url']
1281
1282
1283
def test_and_or_eq_zero():
1284
    assert graph.evaluate.and_or_eq_zero('and', results_counts) == False
1285
    assert graph.evaluate.and_or_eq_zero('or', results_counts) == False
1286
    assert graph.evaluate.and_or_eq_zero('xor', results_counts) is None
1287
1288
1289
def test_get_def_id_by_rule_id():
1290
    src = 'test_data/ssg-fedora-ds-arf.xml'
1291
    _dir = os.path.dirname(os.path.realpath(__file__))
1292
    FIXTURE_DIR = py.path.local(_dir) / src
1293
1294
    parser = graph.xml_parser.xml_parser(str(FIXTURE_DIR))
1295
1296
    with pytest.raises(ValueError) as e:
1297
        parser.get_def_id_by_rule_id('hello')
1298
    assert str(
1299
        e.value) == 'err- 404 rule not found!'
1300