from pathlib import Path

import pytest

from oval_graph.arf_xml_parser.arf_xml_parser import ARFXMLParser


def get_arf_report_path(src="global_test_data/ssg-fedora-ds-arf.xml"):
    return str(Path(__file__).parent.parent / src)


@pytest.mark.parametrize("rule_id, result", [
    (
        "xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny",
        "false",
    ),
    (
        "xccdf_org.ssgproject.content_rule_sshd_disable_gssapi_auth",
        "false",
    ),
    (
        "xccdf_org.ssgproject.content_rule_service_debug-shell_disabled",
        "true",
    ),
    (
        "xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec",
        "false",
    ),
    (
        "xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_creat",
        "false",
    ),
    (
        "xccdf_org.ssgproject.content_rule_audit_rules_file_deletion_events_rmdir",
        "false",
    ),
    (
        "xccdf_org.ssgproject.content_rule_require_singleuser_auth",
        "true",
    ),
])
def test_parsing_and_evaluate_scan_rule(rule_id, result):
    path = get_arf_report_path()

    parser = ARFXMLParser(path)
    oval_tree = parser.get_oval_tree(rule_id)
    assert oval_tree.evaluate_tree() == result


def test_parsing_arf_report_without_system_data():
    path = get_arf_report_path("global_test_data/arf_no_system_data.xml")
    rule_id = "xccdf_com.example.www_rule_test-fail"
    parser = ARFXMLParser(path)
    oval_tree = parser.get_oval_tree(rule_id)
    assert oval_tree.evaluate_tree() == "false"


@pytest.mark.parametrize("rule_id, pattern", [
    ("hello", "404 rule \"hello\" not found!"),
    ("xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server", "notselected"),
    ("xccdf_org.ssgproject.content_rule_configure_bind_crypto_policy", "notchecked"),
    ("xccdf_org.ssgproject.content_rule_ensure_gpgcheck_local_packages", "notapplicable"),
])
def test_parsing_bad_rule(rule_id, pattern):
    path = get_arf_report_path()
    parser = ARFXMLParser(path)

    with pytest.raises(Exception, match=pattern):
        assert parser.get_oval_tree(rule_id)


def test_use_bad_report_file():
    src = 'global_test_data/xccdf_org.ssgproject.content_profile_ospp-results-initial.xml'
    path = get_arf_report_path(src)
    with pytest.raises(Exception, match=r"arf\b|ARF\b"):
        assert ARFXMLParser(path)

[((234, 897), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""rule_id, result"""', "[('xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny',\n    'false'), ('xccdf_org.ssgproject.content_rule_sshd_disable_gssapi_auth',\n    'false'), (\n    'xccdf_org.ssgproject.content_rule_service_debug-shell_disabled',\n    'true'), (\n    'xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec',\n    'false'), (\n    'xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_creat'\n    , 'false'), (\n    'xccdf_org.ssgproject.content_rule_audit_rules_file_deletion_events_rmdir',\n    'false'), ('xccdf_org.ssgproject.content_rule_require_singleuser_auth',\n    'true')]"], {}), "('rule_id, result', [(\n    'xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny',\n    'false'), ('xccdf_org.ssgproject.content_rule_sshd_disable_gssapi_auth',\n    'false'), (\n    'xccdf_org.ssgproject.content_rule_service_debug-shell_disabled',\n    'true'), (\n    'xccdf_org.ssgproject.content_rule_mount_option_dev_shm_noexec',\n    'false'), (\n    'xccdf_org.ssgproject.content_rule_audit_rules_unsuccessful_file_modification_creat'\n    , 'false'), (\n    'xccdf_org.ssgproject.content_rule_audit_rules_file_deletion_events_rmdir',\n    'false'), ('xccdf_org.ssgproject.content_rule_require_singleuser_auth',\n    'true')])\n", (257, 897), False, 'import pytest\n'), ((1562, 1930), 'pytest.mark.parametrize', 'pytest.mark.parametrize', (['"""rule_id, pattern"""', '[(\'hello\', \'404 rule "hello" not found!\'), (\n    \'xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server\',\n    \'notselected\'), (\n    \'xccdf_org.ssgproject.content_rule_configure_bind_crypto_policy\',\n    \'notchecked\'), (\n    \'xccdf_org.ssgproject.content_rule_ensure_gpgcheck_local_packages\',\n    \'notapplicable\')]'], {}), '(\'rule_id, pattern\', [(\'hello\',\n    \'404 rule "hello" not found!\'), (\n    \'xccdf_org.ssgproject.content_rule_ntpd_specify_remote_server\',\n    \'notselected\'), (\n    \'xccdf_org.ssgproject.content_rule_configure_bind_crypto_policy\',\n    \'notchecked\'), (\n    \'xccdf_org.ssgproject.content_rule_ensure_gpgcheck_local_packages\',\n    \'notapplicable\')])\n', (1585, 1930), False, 'import pytest\n'), ((1141, 1159), 'oval_graph.arf_xml_parser.arf_xml_parser.ARFXMLParser', 'ARFXMLParser', (['path'], {}), '(path)\n', (1153, 1159), False, 'from oval_graph.arf_xml_parser.arf_xml_parser import ARFXMLParser\n'), ((1446, 1464), 'oval_graph.arf_xml_parser.arf_xml_parser.ARFXMLParser', 'ARFXMLParser', (['path'], {}), '(path)\n', (1458, 1464), False, 'from oval_graph.arf_xml_parser.arf_xml_parser import ARFXMLParser\n'), ((2012, 2030), 'oval_graph.arf_xml_parser.arf_xml_parser.ARFXMLParser', 'ARFXMLParser', (['path'], {}), '(path)\n', (2024, 2030), False, 'from oval_graph.arf_xml_parser.arf_xml_parser import ARFXMLParser\n'), ((2041, 2080), 'pytest.raises', 'pytest.raises', (['Exception'], {'match': 'pattern'}), '(Exception, match=pattern)\n', (2054, 2080), False, 'import pytest\n'), ((2297, 2344), 'pytest.raises', 'pytest.raises', (['Exception'], {'match': '"""arf\\\\b|ARF\\\\b"""'}), "(Exception, match='arf\\\\b|ARF\\\\b')\n", (2310, 2344), False, 'import pytest\n'), ((2360, 2378), 'oval_graph.arf_xml_parser.arf_xml_parser.ARFXMLParser', 'ARFXMLParser', (['path'], {}), '(path)\n', (2372, 2378), False, 'from oval_graph.arf_xml_parser.arf_xml_parser import ARFXMLParser\n'), ((195, 209), 'pathlib.Path', 'Path', (['__file__'], {}), '(__file__)\n', (199, 209), False, 'from pathlib import Path\n')]