Skip to content

Commit

Permalink
Add tests for CPE AL
Browse files Browse the repository at this point in the history
  • Loading branch information
Honny1 committed Mar 30, 2023
1 parent 0cdb616 commit 0cdc7f0
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
from functools import cache
except ImportError:
from functools import lru_cache

cache = lru_cache(maxsize=None)

from lxml import etree

from openscap_report.scap_results_parser import SCAPResultsParser
from openscap_report.scap_results_parser.data_structures import OvalDefinition
from openscap_report.scap_results_parser.namespaces import NAMESPACES
from openscap_report.scap_results_parser.parsers import RuleParser
from openscap_report.scap_results_parser.parsers import (
CPEApplicabilityLanguageParser, RuleParser)

from .constants import (PATH_TO_ARF,
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
from .unit_tests.test_oval_tree_eval import OVAL_TREE_TRUE


@cache
Expand Down Expand Up @@ -78,3 +82,25 @@ def get_rules(file_path=None):
ref_values = get_ref_values(root)
rule_parser = RuleParser(root, test_results, ref_values)
return rule_parser.get_rules()


def get_cpe_al_parser(file_path=PATH_TO_ARF):
root = get_root(file_path)
return CPEApplicabilityLanguageParser(root)


def get_dummy_cpe_oval_definition():
dummy_oval_definition = OvalDefinition(
definition_id="dummy_oval_def",
title="dummy OVAL definition",
oval_tree=OVAL_TREE_TRUE,
)
return {
"oval:ssg-installed_env_is_a_machine:def:1": dummy_oval_definition,
"oval:ssg-installed_env_has_chrony_package:def:1": dummy_oval_definition,
"oval:ssg-installed_env_has_ntp_package:def:1": dummy_oval_definition,
"oval:ssg-installed_env_has_gdm_package:def:1": dummy_oval_definition,
"oval:ssg-installed_OS_is_fedora:def:1": dummy_oval_definition,
"oval:ssg-installed_env_has_zipl_package:def:1": dummy_oval_definition,
"oval:ssg-system_boot_mode_is_uefi:def:1": dummy_oval_definition,
}
109 changes: 109 additions & 0 deletions tests/unit_tests/test_cpe_al_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import pytest
from lxml import etree

from ..test_utils import get_cpe_al_parser, get_dummy_cpe_oval_definition


@pytest.mark.unit_test
@pytest.mark.parametrize(
"str_xml_element, evaluation_result",
[
(
(
'<cpe-lang:logical-test operator="AND" negate="false">'
' <cpe-lang:fact-ref name="cpe:/a:machine"/>'
'</cpe-lang:logical-test>'
),
"true",
),
(
(
'<cpe-lang:logical-test operator="AND" negate="true">'
' <cpe-lang:fact-ref name="cpe:/a:machine"/>'
'</cpe-lang:logical-test>'
),
"false",
),
(
(
'<cpe-lang:logical-test operator="AND" negate="false">'
' <cpe-lang:logical-test operator="OR" negate="false">'
' <cpe-lang:fact-ref name="cpe:/a:chrony"/>'
' <cpe-lang:fact-ref name="cpe:/a:ntp"/>'
' </cpe-lang:logical-test>'
' <cpe-lang:fact-ref name="cpe:/a:machine"/>'
'</cpe-lang:logical-test>'
),
"true",
),
(
(
'<cpe-lang:logical-test operator="AND" negate="true">'
' <cpe-lang:logical-test operator="OR" negate="false">'
' <cpe-lang:logical-test operator="AND" negate="false">'
' <cpe-lang:fact-ref name="cpe:/o:fedoraproject:fedora:32"/>'
' <cpe-lang:fact-ref name="cpe:/a:gdm"/>'
' </cpe-lang:logical-test>'
' <cpe-lang:logical-test operator="AND" negate="false">'
' <cpe-lang:fact-ref name="cpe:/o:fedoraproject:fedora:32"/>'
' <cpe-lang:fact-ref name="cpe:/a:uefi"/>'
' </cpe-lang:logical-test>'
' <cpe-lang:logical-test operator="AND" negate="true">'
' <cpe-lang:fact-ref name="cpe:/a:zipl"/>'
' <cpe-lang:fact-ref name="cpe:/o:fedoraproject:fedora:32"/>'
' </cpe-lang:logical-test>'
' </cpe-lang:logical-test>'
'</cpe-lang:logical-test>'
),
"false",
),
(
(
'<cpe-lang:logical-test operator="AND" negate="false">'
' <cpe-lang:check-fact-ref'
' system="http://oval.mitre.org/XMLSchema/oval-definitions-5"'
' href="ssg-rhel9-cpe-oval.xml"'
' id-ref="oval:ssg-installed_env_is_a_machine:def:1"/>'
'</cpe-lang:logical-test>'
),
"true",
),
(
(
'<cpe-lang:logical-test operator="AND" negate="true">'
' <cpe-lang:check-fact-ref'
' system="http://oval.mitre.org/XMLSchema/oval-definitions-5"'
' href="ssg-rhel9-cpe-oval.xml"'
' id-ref="oval:ssg-installed_env_is_a_machine:def:1"/>'
'</cpe-lang:logical-test>'
),
"false",
),
(
(
'<cpe-lang:logical-test operator="OR" negate="false">'
' <cpe-lang:check-fact-ref'
' system="http://oval.mitre.org/XMLSchema/oval-definitions-5"'
' href="ssg-rhel9-cpe-oval.xml"'
' id-ref="oval:ssg-installed_env_has_chrony_package:def:1"/>'
' <cpe-lang:check-fact-ref'
' system="http://oval.mitre.org/XMLSchema/oval-definitions-5"'
' href="ssg-rhel9-cpe-oval.xml"'
' id-ref="oval:ssg-installed_env_has_ntp_package:def:1"/>'
'</cpe-lang:logical-test>'
),
"true",
),
],
)
def test_get_logical_test(str_xml_element, evaluation_result):
parser = get_cpe_al_parser()
parser.oval_cpe_definitions = get_dummy_cpe_oval_definition()
xml_element = etree.XML(
f'<con xmlns:cpe-lang="http://cpe.mitre.org/language/2.0">{str_xml_element}</con>'
)
logical_test = parser.get_logical_test(xml_element[0])
assert logical_test.evaluate_tree() == evaluation_result

0 comments on commit 0cdc7f0

Please sign in to comment.