Debian Codegraph @

tests/bindings/python/oval_eval.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Author:
4 # Dominique Blaze <[email protected]>
5 #
6
7 '''
8 Basic OVAL evaluation:
9
10 Import an oval file with oscap.oval.definition_model_import_source
11 and run evalutation of some sample checks using a callback
12
13 Tested functions:
14 - oscap.oval.definition_model_import_source
15 - oscap.common.source_new_from_file
16 - TODO oscap.oval.import_model
17 - oscap.oval.agent_new_session
18 - oscap.oval.agent_eval_system + associated callback
19 - oval_result_definition.get_id()
20 - oval_result_definition.get_result()
21 - oval_result_definition.get_criteria()
22
23 Tested in oval_helpers.browse_criteria:
24 - oval_result_criteria.get_type() + some of associated constants OVAL_NODETYPE_*
25 - oval_result_criteria.get_subnodes()
26 - oval_result_criteria.get_test()
27
28 - oval_result_test.get_test()
29 - oval_result_test.get_result()
30
31 - oval_test.get_subtype() + some of associated constants OVAL_SUBYTPE_*
32 - oval_test.get_family() + some of associated constants
33 '''
34
35 import os
36 import time
37 from import_handler import oscap, result2str, get_path
38 from oval_helpers import browse_criteria
39
40
41 '''
42 Intermediate functions
43 '''
44
45
46 # if you return something in callback else than 0, the current session stops
47 def oval_sample_callback(ovdef, usr):
48
49 try:
50 # .eval or .get_result return the same thing
51 usr['results'].append(ovdef.get_id() + " => " + result2str(ovdef.get_result()))
52
53 # retrieve the tests tree and replace test_results by [subtype => result]
54 tests_tree = browse_criteria(ovdef.get_criteria(), 1)
55
56 '''
57 Expected tree for foo_pass: [[(7006, 1)]]
58 = [[(OVAL_INDEPENDENT_TEXT_FILE_CONTENT_54, XCCDF_RESULT_PASS)]]
59 or [[(OVAL_INDEPENDENT_TEXT_FILE_CONTENT_54, XCCDF_RESULT_PASS)]] for foo_fail
60 '''
61
62 expected_trees = {"oval:foo_pass:def:1": [[
63 (oscap.oval.OVAL_FAMILY_INDEPENDENT,
64 oscap.oval.OVAL_INDEPENDENT_TEXT_FILE_CONTENT_54,
65 oscap.oval.XCCDF_RESULT_PASS)]],
66
67 "oval:foo_fail:def:1": [[
68 (oscap.oval.OVAL_FAMILY_INDEPENDENT,
69 oscap.oval.OVAL_INDEPENDENT_TEXT_FILE_CONTENT_54,
70 oscap.oval.XCCDF_RESULT_FAIL)]]
71 }
72
73 expected_tree = expected_trees.get(ovdef.get_id())
74
75 if expected_tree is None:
76 raise ValueError("Unexpected oval def : {0}. No expected tests tree defined for it"
77 .format(ovdef.get_id()))
78
79 elif expected_tree == tests_tree:
80 usr['results'].append("Tests tree of {0} is like expected : {1}".format(ovdef.get_id(),
81 tests_tree))
82
83 else:
84 raise ValueError("Tests tree of {0} doesn't match with the expected tree.\n"
85 "Extracted tree: {1}\nExpected tree: {2}"
86 .format(ovdef.get_id(), tests_tree, expected_tree))
87
88 except Exception as e:
89 usr['results'].append(e)
90
91 return 0
92
93
94 # eval oval defs oval:[foo_pass|fail]:def:1
95 def oval_eval_sample(oval_defs):
96 states = {'false': 0, 'true': 0, 'err': 0, 'unknown': 0, 'neval': 0,
97 'na': 0, 'verbose': True, 'results': []}
98
99 sess = oscap.oval.agent_new_session(oval_defs, "")
100 ret = oscap.oval.agent_eval_system(sess, oval_sample_callback, states)
101
102 for tmp in states['results']:
103 if isinstance(tmp, Exception):
104 raise tmp
105 else:
106 print(tmp)
107
108 '''
109 ================ MAIN TEST ====================
110 '''
111
112
113 print("Opening oval file with original C functions ... ")
114 oval_defs = oscap.oval.definition_model_import_source(
115 oscap.common.source_new_from_file(get_path("samples/oval_sample.xml")))
116
117 oval_eval_sample(oval_defs)
118
119 # TODO: do the same thing with oval_import