Skip to content

Commit

Permalink
Merge pull request #11235 from Honny1/add-validation-of-oval-object-m…
Browse files Browse the repository at this point in the history
…odel

Reference validation in OVAL document object
  • Loading branch information
jan-cerny authored Nov 2, 2023
2 parents 1bccd61 + 0a57827 commit bb63938
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
16 changes: 15 additions & 1 deletion ssg/oval_object_model/oval_document.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from __future__ import absolute_import

import platform
import logging

from ..constants import OVAL_NAMESPACES, timestamp, xsi_namespace
from ..utils import required_key
from ..xml import ElementTree
from .oval_container import OVALContainer
from .oval_shorthand import OVALShorthand
from .oval_definition_references import OVALDefinitionReference


def _get_xml_el(tag_name, xml_el):
Expand Down Expand Up @@ -101,7 +103,7 @@ def _skip_if_is_none(value, component_id):
raise MissingOVALComponent(component_id)
return False

def load_shorthand(self, xml_string, product, rule_id=None):
def load_shorthand(self, xml_string, product=None, rule_id=None):
shorthand = OVALShorthand()
shorthand.load_shorthand(xml_string)

Expand All @@ -125,6 +127,18 @@ def finalize_affected_platforms(self, env_yaml):
for definition in self.definitions.values():
definition.metadata.finalize_affected_platforms(type_, full_name)

def validate_references(self):
ref = OVALDefinitionReference()
ref.save_definitions(self.definitions)
try:
self._process_definition_references(ref)
self._process_test_references(ref)
self._process_objects_states_variables_references(ref)
except MissingOVALComponent as error:
logging.warning("Missing OVAL component: {}".format(error))
return False
return True

def get_xml_element(self):
root = self._get_oval_definition_el()
root.append(self._get_generator_el())
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/ssg-module/test_oval_object_model/test_oval_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,24 @@ def test_keep_referenced_components(oval_document):
assert "oval:ssg-state_sshd_rekey_limit:ste:1" not in oval_document.states
assert "oval:ssg-sshd_required:var:1" in oval_document.variables
assert "oval:ssg-var_rekey_limit_size:var:1" not in oval_document.variables


@pytest.mark.parametrize(
"path, expected_result",
[
(
OVAL_DOCUMENT_PATH, True,
),
(
os.path.join(DATA_DIR, "oval_with_broken_extend_definition.xml"),
False
),
(
os.path.join(DATA_DIR, "oval_with_correct_extend_definition.xml"),
True
)
]
)
def test_validation(path, expected_result):
oval_doc = _load_oval_document(path)
assert oval_doc.validate_references() == expected_result

0 comments on commit bb63938

Please sign in to comment.