Passed
Branch codeStyle (b8cf76)
by Jan
01:52
created

test_graph.get_dict_of_simple_tree()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nop 0
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
def test_bad_tree():
28
    with pytest.raises(ValueError) as e:
29
        badTree()
30
    assert str(
31
        e.value) == 'err- true, false, error, unknown. noteval, notappl have not child!'
32
33
    with pytest.raises(ValueError) as e:
34
        treeOnlyAnd()
35
    assert str(e.value) == 'err- OR, XOR, ONE, AND have child!'
36
37
    with pytest.raises(ValueError) as e:
38
        treeOnlyOr()
39
    assert str(e.value) == 'err- OR, XOR, ONE, AND have child!'
40
41
    with pytest.raises(ValueError) as e:
42
        treeWithBadType()
43
    assert str(e.value) == 'err- unknown type'
44
45
    with pytest.raises(ValueError) as e:
46
        treeWithBadValueOfOperator()
47
    assert str(e.value) == 'err- unknown operator'
48
49
    with pytest.raises(ValueError) as e:
50
        treeWithBadValueOfValue()
51
    assert str(e.value) == 'err- unknown value'
52
53
54
# degenered trees
55
56
def badTree():
57
    """
58
         t
59
         |
60
        and
61
         |
62
         t
63
    """
64
    t = graph.oval_graph.OvalNode(
65
        1, "value", "true", [
66
            graph.oval_graph.OvalNode(
67
                2, "operator", "and", [
68
                    graph.oval_graph.OvalNode(
69
                        3, "value", "true")])])
70
    return
71
72
73
def treeOnlyOr():
74
    """
75
        or
76
    """
77
    Tree = graph.oval_graph.OvalNode(1, "operator", 'or')
78
    return
79
80
81
def treeOnlyAnd():
82
    """
83
        and
84
    """
85
    Tree = graph.oval_graph.OvalNode(1, "operator", 'and')
86
    return
87
88
89
def treeWithBadValueOfOperator():
90
    Tree = graph.oval_graph.OvalNode(1, "operator", 'nad')
91
    return
92
93
94
def treeWithBadValueOfValue():
95
    Tree = graph.oval_graph.OvalNode(1, "value", 'and')
96
    return
97
98
99
def treeWithBadType():
100
    Tree = graph.oval_graph.OvalNode(1, "auto", 'and')
101
    return
102
103
# normal trees
104
105
106
def test_UPPERCASETree():
107
    t = graph.oval_graph.OvalNode(
108
        1, "OPERATOR", "AND", [
109
            graph.oval_graph.OvalNode(
110
                2, "VALUE", "TRUE",), graph.oval_graph.OvalNode(
111
                3, "VALUE", "NOTAPPL")])
112
113
# AND operator
114
115
116
def test_ANDTreeTrue():
117
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
118
        graph.oval_graph.OvalNode(2, 'value', "true"),
119
        graph.oval_graph.OvalNode(3, 'value', "true"),
120
        graph.oval_graph.OvalNode(4, 'value', "notappl")
121
    ]
122
    )
123
124
    any_test_treeEvaluation(Tree, "true")
125
126
127 View Code Duplication
def test_ANDTreeFalse():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
128
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
129
        graph.oval_graph.OvalNode(2, 'value', "false"),
130
        graph.oval_graph.OvalNode(3, 'value', "false"),
131
132
        graph.oval_graph.OvalNode(4, 'value', "true"),
133
        graph.oval_graph.OvalNode(5, 'value', "error"),
134
        graph.oval_graph.OvalNode(6, 'value', "unknown"),
135
        graph.oval_graph.OvalNode(7, 'value', "noteval"),
136
        graph.oval_graph.OvalNode(8, 'value', "notappl")
137
    ]
138
    )
139
140
    any_test_treeEvaluation(Tree, "false")
141
142
143 View Code Duplication
def test_ANDTreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
144
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
145
                graph.oval_graph.OvalNode(2, 'value', "error"),
146
                graph.oval_graph.OvalNode(3, 'value', "error"),
147
                graph.oval_graph.OvalNode(4, 'value', "true"),
148
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
149
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
150
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
151
                graph.oval_graph.OvalNode(8, 'value', "error")
152
                ])
153
154
    any_test_treeEvaluation(Tree, "error")
155
156
157 View Code Duplication
def test_ANDTreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
158
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
159
                graph.oval_graph.OvalNode(2, 'value', "unknown"),
160
                graph.oval_graph.OvalNode(3, 'value', "unknown"),
161
                graph.oval_graph.OvalNode(4, 'value', "true"),
162
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
163
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
164
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
165
                graph.oval_graph.OvalNode(8, 'value', "notappl")
166
                ]
167
                )
168
169
    any_test_treeEvaluation(Tree, "unknown")
170
171
172 View Code Duplication
def test_ANDTreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
173
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
174
                graph.oval_graph.OvalNode(2, 'value', "noteval"),
175
                graph.oval_graph.OvalNode(3, 'value', "noteval"),
176
                graph.oval_graph.OvalNode(4, 'value', "true"),
177
                graph.oval_graph.OvalNode(5, 'value', "true"),
178
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
179
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
180
                graph.oval_graph.OvalNode(8, 'value', "notappl")
181
                ]
182
                )
183
184
    any_test_treeEvaluation(Tree, "noteval")
185
186
187
def test_ANDTreeNotappl():
188
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
189
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
190
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
191
        graph.oval_graph.OvalNode(4, 'value', "notappl")
192
    ]
193
    )
194
195
    any_test_treeEvaluation(Tree, "notappl")
196
197
# ONE operator
198
199
200
def test_ONETreeTrue():
201
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
202
        graph.oval_graph.OvalNode(2, 'value', "true"),
203
        graph.oval_graph.OvalNode(3, 'value', "false"),
204
        graph.oval_graph.OvalNode(4, 'value', "notappl"),
205
        graph.oval_graph.OvalNode(5, 'value', "false")
206
    ]
207
    )
208
209
    any_test_treeEvaluation(Tree, "true")
210
211
212 View Code Duplication
def test_ONETreeFalse():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
213
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
214
        graph.oval_graph.OvalNode(2, 'value', "true"),
215
        graph.oval_graph.OvalNode(3, 'value', "true"),
216
217
        graph.oval_graph.OvalNode(4, 'value', "false"),
218
        graph.oval_graph.OvalNode(5, 'value', "error"),
219
        graph.oval_graph.OvalNode(6, 'value', "unknown"),
220
        graph.oval_graph.OvalNode(7, 'value', "noteval"),
221
        graph.oval_graph.OvalNode(8, 'value', "notappl")
222
    ]
223
    )
224
225
    any_test_treeEvaluation(Tree, "false")
226
227
228
def test_ONETreeFalse1():
229
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
230
        graph.oval_graph.OvalNode(2, 'value', "false"),
231
        graph.oval_graph.OvalNode(3, 'value', "false"),
232
        graph.oval_graph.OvalNode(4, 'value', "notappl")
233
    ]
234
    )
235
236
    any_test_treeEvaluation(Tree, "false")
237
238
239 View Code Duplication
def test_ONETreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
240
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
241
                graph.oval_graph.OvalNode(2, 'value', "error"),
242
                graph.oval_graph.OvalNode(3, 'value', "error"),
243
                graph.oval_graph.OvalNode(4, 'value', "true"),
244
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
245
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
246
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
247
                graph.oval_graph.OvalNode(8, 'value', "false")
248
                ]
249
                )
250
251
    any_test_treeEvaluation(Tree, "error")
252
253
254 View Code Duplication
def test_ONETreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
255
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
256
                graph.oval_graph.OvalNode(2, 'value', "unknown"),
257
                graph.oval_graph.OvalNode(3, 'value', "unknown"),
258
                graph.oval_graph.OvalNode(4, 'value', "true"),
259
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
260
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
261
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
262
                graph.oval_graph.OvalNode(8, 'value', "false")
263
                ])
264
265
    any_test_treeEvaluation(Tree, "unknown")
266
267
268 View Code Duplication
def test_ONETreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
269
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
270
                graph.oval_graph.OvalNode(2, 'value', "noteval"),
271
                graph.oval_graph.OvalNode(3, 'value', "noteval"),
272
                graph.oval_graph.OvalNode(4, 'value', "true"),
273
                graph.oval_graph.OvalNode(5, 'value', "false"),
274
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
275
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
276
                graph.oval_graph.OvalNode(8, 'value', "notappl")
277
                ]
278
                )
279
280
    any_test_treeEvaluation(Tree, "noteval")
281
282
283
def test_ONETreeNotappl():
284
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'one', [
285
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
286
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
287
        graph.oval_graph.OvalNode(4, 'value', "notappl")
288
    ]
289
    )
290
291
    any_test_treeEvaluation(Tree, "notappl")
292
293
# OR operator
294
295
296 View Code Duplication
def test_ORTreeTrue():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
297
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
298
        graph.oval_graph.OvalNode(2, 'value', "true"),
299
        graph.oval_graph.OvalNode(3, 'value', "false"),
300
301
        graph.oval_graph.OvalNode(4, 'value', "true"),
302
        graph.oval_graph.OvalNode(5, 'value', "error"),
303
        graph.oval_graph.OvalNode(6, 'value', "unknown"),
304
        graph.oval_graph.OvalNode(7, 'value', "noteval"),
305
        graph.oval_graph.OvalNode(8, 'value', "notappl")
306
    ]
307
    )
308
309
    any_test_treeEvaluation(Tree, "true")
310
311
312
def test_ORTreeFalse():
313
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
314
        graph.oval_graph.OvalNode(2, 'value', "false"),
315
        graph.oval_graph.OvalNode(3, 'value', "false"),
316
        graph.oval_graph.OvalNode(4, 'value', "notappl")
317
    ]
318
    )
319
320
    any_test_treeEvaluation(Tree, "false")
321
322
323 View Code Duplication
def test_ORTreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
324
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
325
                graph.oval_graph.OvalNode(2, 'value', "error"),
326
                graph.oval_graph.OvalNode(3, 'value', "error"),
327
                graph.oval_graph.OvalNode(4, 'value', "false"),
328
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
329
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
330
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
331
                graph.oval_graph.OvalNode(8, 'value', "error")
332
                ]
333
                )
334
335
    any_test_treeEvaluation(Tree, "error")
336
337
338 View Code Duplication
def test_ORTreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
339
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
340
                graph.oval_graph.OvalNode(2, 'value', "unknown"),
341
                graph.oval_graph.OvalNode(3, 'value', "unknown"),
342
                graph.oval_graph.OvalNode(4, 'value', "false"),
343
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
344
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
345
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
346
                graph.oval_graph.OvalNode(8, 'value', "notappl")
347
                ]
348
                )
349
350
    any_test_treeEvaluation(Tree, "unknown")
351
352
353 View Code Duplication
def test_ORTreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
354
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
355
                graph.oval_graph.OvalNode(2, 'value', "noteval"),
356
                graph.oval_graph.OvalNode(3, 'value', "noteval"),
357
                graph.oval_graph.OvalNode(4, 'value', "false"),
358
                graph.oval_graph.OvalNode(5, 'value', "false"),
359
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
360
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
361
                graph.oval_graph.OvalNode(8, 'value', "notappl")
362
                ]
363
                )
364
365
    any_test_treeEvaluation(Tree, "noteval")
366
367
368
def test_ORTreeNotappl():
369
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'or', [
370
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
371
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
372
        graph.oval_graph.OvalNode(4, 'value', "notappl")
373
    ]
374
    )
375
376
    any_test_treeEvaluation(Tree, "notappl")
377
378
# XOR operator
379
380
381 View Code Duplication
def test_XORTreeTrue():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
382
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
383
        graph.oval_graph.OvalNode(2, 'value', "true"),
384
        graph.oval_graph.OvalNode(3, 'value', "false"),
385
386
        graph.oval_graph.OvalNode(4, 'value', "false"),
387
        graph.oval_graph.OvalNode(5, 'value', "false"),
388
        graph.oval_graph.OvalNode(6, 'value', "true"),
389
        graph.oval_graph.OvalNode(7, 'value', "true"),
390
        graph.oval_graph.OvalNode(8, 'value', "notappl")
391
    ]
392
    )
393
394
    any_test_treeEvaluation(Tree, "true")
395
396
397 View Code Duplication
def test_XORTreeFalse():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
398
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
399
        graph.oval_graph.OvalNode(2, 'value', "true"),
400
        graph.oval_graph.OvalNode(3, 'value', "false"),
401
402
        graph.oval_graph.OvalNode(4, 'value', "false"),
403
        graph.oval_graph.OvalNode(5, 'value', "true"),
404
        graph.oval_graph.OvalNode(6, 'value', "true"),
405
        graph.oval_graph.OvalNode(7, 'value', "true"),
406
        graph.oval_graph.OvalNode(8, 'value', "notappl")
407
    ]
408
    )
409
410
    any_test_treeEvaluation(Tree, "false")
411
412
413 View Code Duplication
def test_XORTreeError():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
414
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
415
                graph.oval_graph.OvalNode(2, 'value', "error"),
416
                graph.oval_graph.OvalNode(3, 'value', "error"),
417
                graph.oval_graph.OvalNode(4, 'value', "true"),
418
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
419
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
420
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
421
                graph.oval_graph.OvalNode(8, 'value', "false")
422
                ]
423
                )
424
425
    any_test_treeEvaluation(Tree, "error")
426
427
428 View Code Duplication
def test_xORTreeUnknown():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
429
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
430
                graph.oval_graph.OvalNode(2, 'value', "unknown"),
431
                graph.oval_graph.OvalNode(3, 'value', "unknown"),
432
                graph.oval_graph.OvalNode(4, 'value', "true"),
433
                graph.oval_graph.OvalNode(5, 'value', "unknown"),
434
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
435
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
436
                graph.oval_graph.OvalNode(8, 'value', "notappl")
437
                ]
438
                )
439
440
    any_test_treeEvaluation(Tree, "unknown")
441
442
443 View Code Duplication
def test_XORTreeNoteval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
444
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
445
                graph.oval_graph.OvalNode(2, 'value', "noteval"),
446
                graph.oval_graph.OvalNode(3, 'value', "noteval"),
447
                graph.oval_graph.OvalNode(4, 'value', "true"),
448
                graph.oval_graph.OvalNode(5, 'value', "true"),
449
                graph.oval_graph.OvalNode(6, 'value', "noteval"),
450
                graph.oval_graph.OvalNode(7, 'value', "notappl"),
451
                graph.oval_graph.OvalNode(8, 'value', "notappl")
452
                ]
453
                )
454
455
    any_test_treeEvaluation(Tree, "noteval")
456
457
458
def test_XORTreeNotappl():
459
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'xor', [
460
        graph.oval_graph.OvalNode(2, 'value', "notappl"),
461
        graph.oval_graph.OvalNode(3, 'value', "notappl"),
462
        graph.oval_graph.OvalNode(4, 'value', "notappl")
463
    ]
464
    )
465
466
    any_test_treeEvaluation(Tree, "notappl")
467
468
469
def test_bigOvalTree():
470
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
471
                graph.oval_graph.OvalNode(2, 'value', "false"),
472
                graph.oval_graph.OvalNode(3, 'operator', "xor", [
473
                            graph.oval_graph.OvalNode(4, 'value', 'true'),
474
                            graph.oval_graph.OvalNode(5, 'operator', 'one', [
475
                                        graph.oval_graph.OvalNode(6, 'value', 'noteval'),
476
                                        graph.oval_graph.OvalNode(7, 'value', 'true'), 
477
                                        graph.oval_graph.OvalNode(8, 'value', 'notappl')
478
                                        ]
479
                                        ),
480
                            graph.oval_graph.OvalNode(9, 'value', 'error')
481
                            ]
482
                            ),
483
                graph.oval_graph.OvalNode(10, 'operator', 'or', [
484
                                        graph.oval_graph.OvalNode(11, 'value', "unknown"),
485
                                        graph.oval_graph.OvalNode(12, 'value', "true")
486
                                        ]
487
                                        )
488
                ]
489
                )
490
491
    dict_of_tree = {'node_id': 1, 'type': 'operator', 'value': 'and',
492
                    'child': [
493
                        {'node_id': 2, 'type': 'value', 'value': "false", 'child':  None},
494
                        {'node_id': 3, 'type': 'operator', 'value': "xor", 'child': [
495
                            {'node_id': 4, 'type': 'value', 'value': "true", 'child':  None},
496
                            {'node_id': 5, 'type': 'operator', 'value': "one", 'child': [
497
                                {'node_id': 6, 'type': 'value', 'value': "noteval", 'child':  None},
498
                                {'node_id': 7, 'type': 'value', 'value': "true", 'child':  None},
499
                                {'node_id': 8, 'type': 'value', 'value': "notappl", 'child':  None}
500
                            ]},
501
                            {'node_id': 9, 'type': 'value', 'value': "error", 'child':  None}]},
502
                        {'node_id': 10, 'type': 'operator', 'value': 'or', 'child': [
503
                            {'node_id': 11, 'type': 'value', 'value': "unknown", 'child':  None},
504
                            {'node_id': 12, 'type': 'value', 'value': "true", 'child':  None}
505
                        ]
506
                        }
507
                    ]
508
                    }
509
510
    any_test_treeEvaluation(Tree, "false")
511
    any_test_tree_to_dict_of_tree(Tree, dict_of_tree)
512
    find_any_node(Tree, 5)
513
    any_test_dict_to_tree(dict_of_tree)
514
515
###################################################
516
517
518
def any_test_tree_to_dict_of_tree(tree, dict_of_tree):
519
    assert tree.save_tree_to_dict() == dict_of_tree
520
521
522
def find_any_node(Tree, node_id):
523
    findTree = Tree.find_node_with_ID(node_id)
524
    assert findTree.node_id == node_id
525
526
527
def any_test_treeEvaluation(tree, expect):
528
    assert tree.evaluate_tree() == expect
529
530
531
def any_test_dict_to_tree(dict_of_tree):
532
    treedict_of_tree = graph.oval_graph.restore_dict_to_tree(dict_of_tree)
533
    assert treedict_of_tree.save_tree_to_dict() == dict_of_tree
534
535
536
def test_treeRepr():
537
    """
538
        and
539
         |
540
         f
541
    """
542
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
543
        graph.oval_graph.OvalNode(2, 'value', "false")
544
    ]
545
    )
546
    assert str(Tree) == "and"
547
548
549
def test_add_to_tree():
550
    """
551
        and
552
         |
553
         f
554
    """
555
556
    dict_of_tree = {'node_id': 1,
557
                    'type': 'operator',
558
                    'value': 'and',
559
                    'child': [{'node_id': 2,
560
                               'type': 'value',
561
                               'value': "false",
562
                               'child':  None},
563
                              {'node_id': 3,
564
                               'type': 'value',
565
                               'value': "true",
566
                               'child':  None},
567
                              ]}
568
569
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
570
        graph.oval_graph.OvalNode(2, 'value', "false")
571
    ]
572
    )
573
    Tree1 = graph.oval_graph.OvalNode(3, 'value', "true")
574
    Tree.add_to_tree(1, Tree1)
575
    assert Tree.save_tree_to_dict() == dict_of_tree
576
577
578
def test_ChangeValueTree():
579
    """
580
        and
581
        /|\
582
       t t or
583
          / \
584
         f   t
585
    """
586
    Tree = graph.oval_graph.OvalNode(1, 'operator', 'and', [
587
        graph.oval_graph.OvalNode(2, 'value', "true"),
588
        graph.oval_graph.OvalNode(3, 'value', "false"),
589
        graph.oval_graph.OvalNode(4, 'operator', 'or', [
590
            graph.oval_graph.OvalNode(5, 'value', "false"),
591
            graph.oval_graph.OvalNode(6, 'value', "true")
592
        ]
593
        )
594
    ]
595
    )
596
597
    Tree.change_tree_value(3, "true")
598
    any_test_treeEvaluation(Tree, "true")
599
600
601
def test_bad_results_counts_for_operator_and():
602
    assert graph.evaluate.oval_operator_and(results_counts) is None
603
604
605
def test_bad_results_counts_for_operator_one():
606
    assert graph.evaluate.oval_operator_one(results_counts) is None
607
608
609
def test_bad_results_counts_for_operator_or():
610
    assert graph.evaluate.oval_operator_or(results_counts) is None
611
612
613
def test_bad_results_counts_for_operator_xor():
614
    assert graph.evaluate.oval_operator_xor(results_counts) is None
615
616
617
def test_false_noteval_greater_zero():
618
    assert graph.evaluate.greater_zero(results_counts,'noteval_cnt') == False
619
620
621
def test_false_smaller_then_two():
622
    assert graph.evaluate.smaller_than_two(results_counts1, 'true_cnt') == False
623
624
625
def test_false_eq_or_greater_zero_unknown_noteval_notappl():
626
    assert graph.evaluate.eq_or_greater_zero_unknown_noteval_notappl(results_counts1) == False
627
628
629
def test_false_error_unknown_eq_noteval_greater_zero():
630
    assert graph.evaluate.error_unknown_eq_noteval_greater_zero(results_counts) == False
631
632
633
def any_test_parsing_and_evaluate_scan_rule(src, rule_id, result):
634
    _dir = os.path.dirname(os.path.realpath(__file__))
635
    FIXTURE_DIR = py.path.local(_dir) / src
636
    
637
    oval_tree = graph.oval_graph.build_nodes_form_xml(str(FIXTURE_DIR), rule_id)
638
    any_test_treeEvaluation(oval_tree, result)
639
640
641
def get_simple_tree():
642
    return graph.oval_graph.OvalNode(1, 'operator', 'and', [
643
        graph.oval_graph.OvalNode(2, 'value', "true"),
644
        graph.oval_graph.OvalNode(3, 'value', "false"),
645
        graph.oval_graph.OvalNode(4, 'operator', 'or', [
646
            graph.oval_graph.OvalNode(5, 'value', "false"),
647
            graph.oval_graph.OvalNode(6, 'value', "true")
648
        ]
649
        )
650
    ]
651
    )
652
653
def get_dict_of_simple_tree():
654
    return get_simple_tree().save_tree_to_dict()
655
656
def any_test_create_node_dict_for_sigmaJs(Tree,out):
657
    assert Tree._create_node(0,0)==out
658
659
def test_create_node_dict_for_sigmaJs_0():
660
    out = {
661
        'color': '#ff0000',
662
        'id': 1,
663
        'label': 'and',
664
        'size': 3,
665
        'text': 'null',
666
        'url': 'null',
667
        'title': 1,
668
        'x': 0,
669
        'y': 0
670
    }
671
    Tree=get_simple_tree()
672
    any_test_create_node_dict_for_sigmaJs(Tree,out)
673
674
def test_create_node_dict_for_sigmaJs_1():
675
    out = {
676
        'color': '#00ff00',
677
        'id': 1,
678
        'label': 'and',
679
        'size': 3,
680
        'text': 'null',
681
        'url': 'null',
682
        'title': 1,
683
        'x': 0,
684
        'y': 0
685
    }
686
    Tree=graph.oval_graph.OvalNode(1, 'operator', 'and', [
687
                graph.oval_graph.OvalNode(2, 'value', "true")
688
            ]
689
            )
690
691
    any_test_create_node_dict_for_sigmaJs(Tree,out)
692
693
def test_create_node_dict_for_sigmaJs_2():
694
    out = {
695
        'color': '#000000',
696
        'id': 1,
697
        'label': 'and',
698
        'size': 3,
699
        'text': 'null',
700
        'url': 'null',
701
        'title': '1 and',
702
        'x': 0,
703
        'y': 0
704
    }
705
    Tree=graph.oval_graph.OvalNode(1, 'operator', 'and', [
706
                graph.oval_graph.OvalNode(2, 'value', "noteval")
707
            ]
708
            )
709
710
    any_test_create_node_dict_for_sigmaJs(Tree,out)
711
712
def test_create_node_dict_for_sigmaJs_3():
713
    out = {
714
        'color': '#ff0000',
715
        'id': 1,
716
        'label': 'false',
717
        'size': 3,
718
        'text': 'null',
719
        'url': 'null',
720
        'title': 1,
721
        'x': 0,
722
        'y': 0
723
    }
724
    Tree=graph.oval_graph.OvalNode(1, 'value', 'false')
725
726
    any_test_create_node_dict_for_sigmaJs(Tree,out)
727
728
def test_create_node_dict_for_sigmaJs_4():
729
    out = {
730
        'color': '#00ff00',
731
        'id': 1,
732
        'label': 'true',
733
        'size': 3,
734
        'text': 'null',
735
        'url': 'null',
736
        'title': 1,
737
        'x': 0,
738
        'y': 0
739
    }
740
    Tree=graph.oval_graph.OvalNode(1, 'value', 'true')
741
742
    any_test_create_node_dict_for_sigmaJs(Tree,out)
743
744
745
def test_create_node_dict_for_sigmaJs_5():
746
    out = {
747
        'color': '#000000',
748
        'id': 1,
749
        'label': 'error',
750
        'size': 3,
751
        'text': 'null',
752
        'url': 'null',
753
        'title': '1 error',
754
        'x': 0,
755
        'y': 0
756
    }
757
    Tree=graph.oval_graph.OvalNode(1, 'value', 'error')
758
759
    any_test_create_node_dict_for_sigmaJs(Tree,out)
760
761
762
def test_create_edge_dict_for_sigmaJs():
763
    print(get_simple_tree()._create_edge(1,2))
764
    out = {
765
        'id': 'random_ID',
766
        'source': 1,
767
        'target': 2
768
        }
769
770
    assert get_simple_tree()._create_edge(1,2)['source']==out['source']
771
    assert get_simple_tree()._create_edge(1,2)['target']==out['target']   
772
773
def test_create_array_of_ids_form_tree():
774
     array=get_simple_tree().create_list_of_id()
775
     assert array==[1,2,3,4,5,6]
776
777
def test_parsing_full_can_XML_and_evaluate():
778
    src = 'test_data/ssg-fedora-ds-arf.xml'
779
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
780
    result = 'false'
781
782
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
783
784
785
def test_parsing_and_evaluate_scan_with_extend_def():
786
    src = 'test_data/ssg-fedora-ds-arf-scan-with-extend-definitions.xml'
787
    rule_id = 'xccdf_org.ssgproject.content_rule_sysctl_net_ipv6_conf_all_disable_ipv6'
788
    result = 'false'
789
790
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
791
792
793
def test_parsing_and_evaluate_scan_with_pasing_rule():
794
    src = 'test_data/ssg-fedora-ds-arf-passing-scan.xml'
795
    rule_id = 'xccdf_org.ssgproject.content_rule_service_debug-shell_disabled'
796
    result = 'true'
797
798
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
799
800
801
def test_parsing_and_evaluate_scan_with_fail_rule():
802
    src = 'test_data/ssg-fedora-ds-arf-scan-fail.xml'
803
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec'
804
    result = 'false'
805
806
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
807
808
809
def test_parsing_and_evaluate_scan_with_rule_with_XOR():
810
    src = 'test_data/ssg-fedora-ds-arf-scan-with-xor.xml'
811
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_nosuid_removable_partitions'
812
    result = 'true'
813
814
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
815
816
817
def test_parsing_and_evaluate_scan_with_11_rules():
818
    src = 'test_data/ssg-fedora-ds-arf-scan-with-11-rules.xml'
819
    rule_id = 'xccdf_org.ssgproject.content_rule_mount_option_tmp_nosuid'
820
    result = 'true'
821
822
    any_test_parsing_and_evaluate_scan_rule(src, rule_id, result)
823
824
825
def test_transformation_tree_to_Json_for_SigmaJs_0():
826
    test_data = {
827
        "nodes": [
828
        {
829
            "id": "xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny",
830
            "label": "and",
831
            "url": "null",
832
            "text": "null",
833
            "title": "xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny",
834
            "x": -13,
835
            "y": 0,
836
            "size": 3,
837
            "color": "#ff0000"
838
        },
839
        {
840
            "id": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
841
            "label": "and",
842
            "url": "null",
843
            "text": "null",
844
            "title": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
845
            "x": -13,
846
            "y": 1,
847
            "size": 3,
848
            "color": "#ff0000"
849
        },
850
        {
851
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1",
852
            "label": "false",
853
            "url": "null",
854
            "text": "null",
855
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1",
856
            "x": -11,
857
            "y": 3,
858
            "size": 3,
859
            "color": "#ff0000"
860
        },
861
        {
862
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1",
863
            "label": "false",
864
            "url": "null",
865
            "text": "null",
866
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1",
867
            "x": -9,
868
            "y": 3,
869
            "size": 3,
870
            "color": "#ff0000"
871
        },
872
        {
873
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1",
874
            "label": "false",
875
            "url": "null",
876
            "text": "null",
877
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1",
878
            "x": -7,
879
            "y": 3,
880
            "size": 3,
881
            "color": "#ff0000"
882
        },
883
        {
884
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1",
885
            "label": "false",
886
            "url": "null",
887
            "text": "null",
888
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1",
889
            "x": -5,
890
            "y": 3,
891
            "size": 3,
892
            "color": "#ff0000"
893
        },
894
        {
895
            "id": "b2eae097-2c22-4c0b-8796-4b76e9b1a8c0",
896
            "label": "and",
897
            "url": "null",
898
            "text": "null",
899
            "title": "b2eae097-2c22-4c0b-8796-4b76e9b1a8c0",
900
            "x": -3,
901
            "y": 3,
902
            "size": 3,
903
            "color": "#ff0000"
904
        },
905
        {
906
            "id": "ece33afa-c675-4a43-9366-f946181937f4",
907
            "label": "or",
908
            "url": "null",
909
            "text": "null",
910
            "title": "ece33afa-c675-4a43-9366-f946181937f4",
911
            "x": -1,
912
            "y": 5,
913
            "size": 3,
914
            "color": "#ff0000"
915
        },
916
        {
917
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1",
918
            "label": "false",
919
            "url": "null",
920
            "text": "null",
921
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1",
922
            "x": 1,
923
            "y": 7,
924
            "size": 3,
925
            "color": "#ff0000"
926
        },
927
        {
928
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1",
929
            "label": "false",
930
            "url": "null",
931
            "text": "null",
932
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1",
933
            "x": 3,
934
            "y": 7,
935
            "size": 3,
936
            "color": "#ff0000"
937
        },
938
        {
939
            "id": "c157ad0e-3326-4ae4-88d4-14a9bf7e6a84",
940
            "label": "or",
941
            "url": "null",
942
            "text": "null",
943
            "title": "c157ad0e-3326-4ae4-88d4-14a9bf7e6a84",
944
            "x": 3,
945
            "y": 5,
946
            "size": 3,
947
            "color": "#ff0000"
948
        },
949
        {
950
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1",
951
            "label": "false",
952
            "url": "null",
953
            "text": "null",
954
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1",
955
            "x": 5,
956
            "y": 7,
957
            "size": 3,
958
            "color": "#ff0000"
959
        },
960
        {
961
            "id": "oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1",
962
            "label": "false",
963
            "url": "null",
964
            "text": "null",
965
            "title": "oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1",
966
            "x": 7,
967
            "y": 7,
968
            "size": 3,
969
            "color": "#ff0000"
970
        }
971
        ],
972
        "edges": [
973
        {
974
            "id": "f8e7e5c5-facf-4ba5-aa06-0cad749c1683",
975
            "source": "xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny",
976
            "target": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1"
977
        },
978
        {
979
            "id": "003367ab-2d0e-4ee2-8e04-a8d724c00b4f",
980
            "source": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
981
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_system-auth:tst:1"
982
        },
983
        {
984
            "id": "d89082e6-4f27-4c1f-a1c2-faf6e59a49a0",
985
            "source": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
986
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_account_phase_system-auth:tst:1"
987
        },
988
        {
989
            "id": "a5b2dce5-35de-4060-b499-05dd517c993d",
990
            "source": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
991
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_preauth_silent_password-auth:tst:1"
992
        },
993
        {
994
            "id": "f62a2828-ed80-4f0d-8c1a-2fd6da231d58",
995
            "source": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
996
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_account_phase_password-auth:tst:1"
997
        },
998
        {
999
            "id": "436845ff-994c-4026-adc5-0841a2a61e51",
1000
            "source": "oval:ssg-accounts_passwords_pam_faillock_deny:def:1",
1001
            "target": "b2eae097-2c22-4c0b-8796-4b76e9b1a8c0"
1002
        },
1003
        {
1004
            "id": "466ef345-9f61-4d70-8f0a-c0a3257cc253",
1005
            "source": "b2eae097-2c22-4c0b-8796-4b76e9b1a8c0",
1006
            "target": "ece33afa-c675-4a43-9366-f946181937f4"
1007
        },
1008
        {
1009
            "id": "2bed2247-8155-4a2e-b75a-35d5839d9db4",
1010
            "source": "ece33afa-c675-4a43-9366-f946181937f4",
1011
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_system-auth:tst:1"
1012
        },
1013
        {
1014
            "id": "be4e6a91-b874-4082-b077-ff74e286c91d",
1015
            "source": "ece33afa-c675-4a43-9366-f946181937f4",
1016
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_system-auth:tst:1"
1017
        },
1018
        {
1019
            "id": "29b9563c-2f8f-4554-8159-102ec4897ac1",
1020
            "source": "b2eae097-2c22-4c0b-8796-4b76e9b1a8c0",
1021
            "target": "c157ad0e-3326-4ae4-88d4-14a9bf7e6a84"
1022
        },
1023
        {
1024
            "id": "7d06f894-071e-4383-96e8-934513eb857c",
1025
            "source": "c157ad0e-3326-4ae4-88d4-14a9bf7e6a84",
1026
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_numeric_default_check_password-auth:tst:1"
1027
        },
1028
        {
1029
            "id": "e4c5ee00-5736-4b67-b19d-43472a5d6768",
1030
            "source": "c157ad0e-3326-4ae4-88d4-14a9bf7e6a84",
1031
            "target": "oval:ssg-test_accounts_passwords_pam_faillock_authfail_deny_password-auth:tst:1"
1032
        }
1033
        ]
1034
        }
1035
    src = 'data/ssg-fedora-ds-arf.xml'
1036
    rule_id = 'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny'
1037
    
1038
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
1039
    if oval_tree.node_id == rule_id:
1040
        out_data = oval_tree.to_sigma_dict(0, 0)
1041
        for i in range(len(out_data['nodes'])):
1042
            assert out_data['nodes'][i]['label']==test_data['nodes'][i]['label']
1043
            assert out_data['nodes'][i]['text']==test_data['nodes'][i]['text']
1044
            assert out_data['nodes'][i]['url']==test_data['nodes'][i]['url']
1045
1046
1047
def test_transformation_tree_to_Json_for_SigmaJs_with_duplicated_test():
1048
    test_data = {
1049
        "nodes": [
1050
        {
1051
            "id": "xccdf_org.ssgproject.content_rule_disable_host_auth",
1052
            "label": "and",
1053
            "url": "null",
1054
            "text": "null",
1055
            "title": "xccdf_org.ssgproject.content_rule_disable_host_auth",
1056
            "x": -17,
1057
            "y": 0,
1058
            "size": 3,
1059
            "color": "#00ff00"
1060
        },
1061
        {
1062
            "id": "oval:ssg-disable_host_auth:def:1",
1063
            "label": "or",
1064
            "url": "null",
1065
            "text": "null",
1066
            "title": "oval:ssg-disable_host_auth:def:1",
1067
            "x": -17,
1068
            "y": 1,
1069
            "size": 3,
1070
            "color": "#00ff00"
1071
        },
1072
        {
1073
            "id": "b688b097-7f93-4d38-be83-f8aee611a7ab",
1074
            "label": "and",
1075
            "url": "null",
1076
            "text": "null",
1077
            "title": "b688b097-7f93-4d38-be83-f8aee611a7ab",
1078
            "x": -15,
1079
            "y": 3,
1080
            "size": 3,
1081
            "color": "#ff0000"
1082
        },
1083
        {
1084
            "id": "71e748eb-4f25-481d-8e03-d8b4b18c8cb3",
1085
            "label": "or",
1086
            "url": "null",
1087
            "text": "null",
1088
            "title": "71e748eb-4f25-481d-8e03-d8b4b18c8cb3",
1089
            "x": -13,
1090
            "y": 5,
1091
            "size": 3,
1092
            "color": "#00ff00"
1093
        },
1094
        {
1095
            "id": "oval:ssg-test_sshd_not_required:tst:1",
1096
            "label": "false",
1097
            "url": "null",
1098
            "text": "null",
1099
            "title": "oval:ssg-test_sshd_not_required:tst:1",
1100
            "x": -11,
1101
            "y": 7,
1102
            "size": 3,
1103
            "color": "#ff0000"
1104
        },
1105
        {
1106
            "id": "70d25e1d-3ac9-4f5a-8f86-1ad46b13ce09",
1107
            "label": "and",
1108
            "url": "null",
1109
            "text": "null",
1110
            "title": "70d25e1d-3ac9-4f5a-8f86-1ad46b13ce09",
1111
            "x": -9,
1112
            "y": 7,
1113
            "size": 3,
1114
            "color": "#00ff00"
1115
        },
1116
        {
1117
            "id": "d4ebfbf0-acec-41b7-83be-e98665b6f645",
1118
            "label": "and",
1119
            "url": "null",
1120
            "text": "null",
1121
            "title": "d4ebfbf0-acec-41b7-83be-e98665b6f645",
1122
            "x": -8,
1123
            "y": 5,
1124
            "size": 3,
1125
            "color": "#ff0000"
1126
        },
1127
        {
1128
            "id": "oval:ssg-test_package_openssh-server_removed:tst:1",
1129
            "label": "false",
1130
            "url": "null",
1131
            "text": "null",
1132
            "title": "oval:ssg-test_package_openssh-server_removed:tst:1",
1133
            "x": -6,
1134
            "y": 7,
1135
            "size": 3,
1136
            "color": "#ff0000"
1137
        },
1138
        {
1139
            "id": "fddd65d1-ec4d-4b8f-95d9-45eac3a8d502",
1140
            "label": "and",
1141
            "url": "null",
1142
            "text": "null",
1143
            "title": "fddd65d1-ec4d-4b8f-95d9-45eac3a8d502",
1144
            "x": -7,
1145
            "y": 3,
1146
            "size": 3,
1147
            "color": "#00ff00"
1148
        },
1149
        {
1150
            "id": "bb5da04b-9b2f-4e54-b89f-8c871c404e87",
1151
            "label": "or",
1152
            "url": "null",
1153
            "text": "null",
1154
            "title": "bb5da04b-9b2f-4e54-b89f-8c871c404e87",
1155
            "x": -5,
1156
            "y": 5,
1157
            "size": 3,
1158
            "color": "#00ff00"
1159
        },
1160
        {
1161
            "id": "oval:ssg-test_sshd_required:tst:1",
1162
            "label": "false",
1163
            "url": "null",
1164
            "text": "null",
1165
            "title": "oval:ssg-test_sshd_required:tst:1",
1166
            "x": -3,
1167
            "y": 7,
1168
            "size": 3,
1169
            "color": "#ff0000"
1170
        },
1171
        {
1172
            "id": "20886ece-396f-45fe-992a-563623f35b9b",
1173
            "label": "and",
1174
            "url": "null",
1175
            "text": "null",
1176
            "title": "20886ece-396f-45fe-992a-563623f35b9b",
1177
            "x": -1,
1178
            "y": 7,
1179
            "size": 3,
1180
            "color": "#00ff00"
1181
        },
1182
        {
1183
            "id": "fc1496fd-1097-4bed-8651-f8d7a3b82e3b",
1184
            "label": "and",
1185
            "url": "null",
1186
            "text": "null",
1187
            "title": "fc1496fd-1097-4bed-8651-f8d7a3b82e3b",
1188
            "x": 0,
1189
            "y": 5,
1190
            "size": 3,
1191
            "color": "#00ff00"
1192
        },
1193
        {
1194
            "id": "oval:ssg-test_package_openssh-server_installed:tst:1",
1195
            "label": "true",
1196
            "url": "null",
1197
            "text": "null",
1198
            "title": "oval:ssg-test_package_openssh-server_installed:tst:1",
1199
            "x": 2,
1200
            "y": 7,
1201
            "size": 3,
1202
            "color": "#00ff00"
1203
        },
1204
        {
1205
            "id": "oval:ssg-test_sshd_hostbasedauthentication:tst:1",
1206
            "label": "true",
1207
            "url": "null",
1208
            "text": "null",
1209
            "title": "oval:ssg-test_sshd_hostbasedauthentication:tst:1",
1210
            "x": 3,
1211
            "y": 5,
1212
            "size": 3,
1213
            "color": "#00ff00"
1214
        },
1215
        {
1216
            "id": "oval:ssg-test_sshd_requirement_unset:tst:1",
1217
            "label": "true",
1218
            "url": "null",
1219
            "text": "null",
1220
            "title": "oval:ssg-test_sshd_requirement_unset:tst:1",
1221
            "x": -7,
1222
            "y": 9,
1223
            "size": 3,
1224
            "color": "#00ff00"
1225
        }
1226
        ],
1227
        "edges": [
1228
        {
1229
            "id": "6fa59d60-26e4-439c-be15-0b1b2d69c1e3",
1230
            "source": "xccdf_org.ssgproject.content_rule_disable_host_auth",
1231
            "target": "oval:ssg-disable_host_auth:def:1"
1232
        },
1233
        {
1234
            "id": "79b0a3ff-5102-4c20-a625-a5cdecad378d",
1235
            "source": "oval:ssg-disable_host_auth:def:1",
1236
            "target": "b688b097-7f93-4d38-be83-f8aee611a7ab"
1237
        },
1238
        {
1239
            "id": "5fa8e26a-06a6-4ac9-948d-a7875c6476c7",
1240
            "source": "b688b097-7f93-4d38-be83-f8aee611a7ab",
1241
            "target": "71e748eb-4f25-481d-8e03-d8b4b18c8cb3"
1242
        },
1243
        {
1244
            "id": "b93d6a68-bfca-4baf-9261-15569db9a0b7",
1245
            "source": "71e748eb-4f25-481d-8e03-d8b4b18c8cb3",
1246
            "target": "oval:ssg-test_sshd_not_required:tst:1"
1247
        },
1248
        {
1249
            "id": "b292dd59-38b7-4a82-b437-714b4aabd51a",
1250
            "source": "71e748eb-4f25-481d-8e03-d8b4b18c8cb3",
1251
            "target": "70d25e1d-3ac9-4f5a-8f86-1ad46b13ce09"
1252
        },
1253
        {
1254
            "id": "1895c211-8af2-476d-9a30-db25e5057507",
1255
            "source": "70d25e1d-3ac9-4f5a-8f86-1ad46b13ce09",
1256
            "target": "oval:ssg-test_sshd_requirement_unset:tst:1"
1257
        },
1258
        {
1259
            "id": "aa0316a5-f0ce-44b3-bf79-1459b1cca894",
1260
            "source": "b688b097-7f93-4d38-be83-f8aee611a7ab",
1261
            "target": "d4ebfbf0-acec-41b7-83be-e98665b6f645"
1262
        },
1263
        {
1264
            "id": "42d2ecb9-b45e-4d20-ac92-75f9612f992f",
1265
            "source": "d4ebfbf0-acec-41b7-83be-e98665b6f645",
1266
            "target": "oval:ssg-test_package_openssh-server_removed:tst:1"
1267
        },
1268
        {
1269
            "id": "c4cc7923-02b4-4277-8adb-aa792211faa8",
1270
            "source": "oval:ssg-disable_host_auth:def:1",
1271
            "target": "fddd65d1-ec4d-4b8f-95d9-45eac3a8d502"
1272
        },
1273
        {
1274
            "id": "b648737a-5082-4231-9790-5db1e51df8f9",
1275
            "source": "fddd65d1-ec4d-4b8f-95d9-45eac3a8d502",
1276
            "target": "bb5da04b-9b2f-4e54-b89f-8c871c404e87"
1277
        },
1278
        {
1279
            "id": "13f25b40-7410-4311-9f05-7a28a28e4511",
1280
            "source": "bb5da04b-9b2f-4e54-b89f-8c871c404e87",
1281
            "target": "oval:ssg-test_sshd_required:tst:1"
1282
        },
1283
        {
1284
            "id": "db8a73d8-aecb-4f84-b325-c1799a49af14",
1285
            "source": "bb5da04b-9b2f-4e54-b89f-8c871c404e87",
1286
            "target": "20886ece-396f-45fe-992a-563623f35b9b"
1287
        },
1288
        {
1289
            "id": "28ec9594-32c7-4ee2-9296-ebe728cc88be",
1290
            "source": "20886ece-396f-45fe-992a-563623f35b9b",
1291
            "target": "oval:ssg-test_sshd_requirement_unset:tst:1"
1292
        },
1293
        {
1294
            "id": "5350c294-9036-497a-bea5-2dbd47502c34",
1295
            "source": "fddd65d1-ec4d-4b8f-95d9-45eac3a8d502",
1296
            "target": "fc1496fd-1097-4bed-8651-f8d7a3b82e3b"
1297
        },
1298
        {
1299
            "id": "94492e77-6089-44d6-95d0-ff61421112a4",
1300
            "source": "fc1496fd-1097-4bed-8651-f8d7a3b82e3b",
1301
            "target": "oval:ssg-test_package_openssh-server_installed:tst:1"
1302
        },
1303
        {
1304
            "id": "ae8a5250-93ca-4143-9812-8847dc8fac82",
1305
            "source": "fddd65d1-ec4d-4b8f-95d9-45eac3a8d502",
1306
            "target": "oval:ssg-test_sshd_hostbasedauthentication:tst:1"
1307
        }
1308
        ]
1309
        }    
1310
    src = 'data/ssg-fedora-ds-arf.xml'
1311
    rule_id = 'xccdf_org.ssgproject.content_rule_disable_host_auth'
1312
1313
    oval_tree = graph.oval_graph.build_nodes_form_xml(src, rule_id)
1314
    
1315
    if oval_tree.node_id == rule_id:
1316
        out_data = oval_tree.to_sigma_dict(0, 0)
1317
        for i in range(len(out_data['nodes'])):
1318
            assert out_data['nodes'][i]['label']==test_data['nodes'][i]['label']
1319
            assert out_data['nodes'][i]['text']==test_data['nodes'][i]['text']
1320
            assert out_data['nodes'][i]['url']==test_data['nodes'][i]['url']
1321
1322
1323
def test_and_or_eq_zero():
1324
    assert graph.evaluate.and_or_eq_zero('and',results_counts) == False
1325
    assert graph.evaluate.and_or_eq_zero('or',results_counts) == False  
1326
    assert graph.evaluate.and_or_eq_zero('xor',results_counts) == None  
1327
1328
1329
def test_get_def_id_by_rule_id():
1330
    src = 'test_data/ssg-fedora-ds-arf.xml'
1331
    _dir = os.path.dirname(os.path.realpath(__file__))
1332
    FIXTURE_DIR = py.path.local(_dir) / src
1333
1334
    parser = graph.xml_parser.xml_parser(str(FIXTURE_DIR))
1335
    
1336
    with pytest.raises(ValueError) as e:
1337
        parser.get_def_id_by_rule_id('hello')
1338
    assert str(
1339
        e.value) == 'err- 404 rule not found!'
1340