Skip to content

Commit d1fcf66

Browse files
test python api introspection features
1 parent ec6625f commit d1fcf66

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
# Author:
4+
# Dominique Blaze <[email protected]>
5+
#
6+
# Description:
7+
# Test introspection features of python the api
8+
# - Test the presence or the absence of expected builtins functions
9+
# - Indentify a constant name by its value and prefix (prefix XCCDF_RESULT
10+
# with value 1 (usually) should identify XCCDF_RESULT_PASS
11+
# - Check presence of constants filtered by a prefix (XCCDF_RESULT with
12+
# value = None should returns all XCCDF_RESULT_* constants).
13+
#
14+
15+
16+
from import_handler import oscap
17+
from pprint import pprint
18+
19+
oval_funcs=oscap.oval.introspect()
20+
21+
if oval_funcs.get('oval_variable_get_values') is None:
22+
raise Exception("method 'oval_variable_get_values' not found in builtins"
23+
"functions of oval object (should be in oscap.oval.introspect())")
24+
25+
if oval_funcs.get('xccdf_check_get_id') is not None:
26+
raise Exception("method 'xccdf_check_get_id' should not be found in "
27+
"oscap.oval.introspect(), because the prefix isn't 'oval')")
28+
29+
if oscap.oval.introspect_all().get('xccdf_check_get_id') is None:
30+
raise Exception("method 'xccdf_check_get_id' should be present in "
31+
"oscap.oval.introspect_all(), which returns all available builtins functions")
32+
33+
print("Introspection of builtins functions seems working.")
34+
35+
36+
# should returns {'XCCDF_RESULT_PASS': 1}
37+
# but it's not impossible that the numeric value change (in fact we don't care of it)
38+
pass_val=oscap.oval.XCCDF_RESULT_PASS # is usually 1
39+
40+
var1=oscap.oval.introspect_constants(pass_val, "XCCDF_RESULT")
41+
if len(var1) == 1 and list(var1.keys())[0]=="XCCDF_RESULT_PASS":
42+
print("Introspection of oscap.oval.XCCDF_RESULT_PASS ok");
43+
44+
45+
xccdf_results=oscap.oval.introspect_constants(None, "XCCDF_RESULT")
46+
# should returns something like {
47+
# 'XCCDF_RESULT_ERROR': 3,
48+
# 'XCCDF_RESULT_FAIL': 2,
49+
# 'XCCDF_RESULT_FIXED': 9,
50+
# 'XCCDF_RESULT_INFORMATIONAL': 8,
51+
# 'XCCDF_RESULT_NOT_APPLICABLE': 5,
52+
# 'XCCDF_RESULT_NOT_CHECKED': 6,
53+
# 'XCCDF_RESULT_NOT_SELECTED': 7,
54+
# 'XCCDF_RESULT_PASS': 1,
55+
# 'XCCDF_RESULT_UNKNOWN': 4}
56+
57+
expected_constants = ('XCCDF_RESULT_ERROR', 'XCCDF_RESULT_FAIL', 'XCCDF_RESULT_FIXED',
58+
'XCCDF_RESULT_INFORMATIONAL', 'XCCDF_RESULT_NOT_APPLICABLE',
59+
'XCCDF_RESULT_NOT_CHECKED', 'XCCDF_RESULT_PASS',
60+
'XCCDF_RESULT_NOT_SELECTED', 'XCCDF_RESULT_UNKNOWN')
61+
62+
for c in expected_constants:
63+
if c not in xccdf_results:
64+
raise Exception("oscap.oval.introspect_constants(None, 'XCCDF_RESULT) should"
65+
"contains the constant {0}.".format(c))
66+
67+
print("Introspection of constants ok with prefix XCCDF_RESULT_*");

tests/bindings/test_python.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ if [ -z ${CUSTOM_OSCAP+x} ] ; then
3434

3535
test_run "python_benchmark_import_results" run_pyfile benchmark_import_results.py
3636
test_run "python_oval_eval" run_pyfile oval_eval.py
37+
test_run "python_introspection_features" run_pyfile introspection_features.py
3738
fi
3839

3940
test_exit

0 commit comments

Comments
 (0)