from boefjes.plugins.kat_cve_2023_35078.normalize import VULNERABLE_RANGES, is_vulnerable_version, run
from packaging import version
from tests.loading import get_dummy_data


def test_vulnerable_version_11_8():
    detected_version = version.parse("11.8.0.0")
    vulnerable_ranges = [(version.parse(start), version.parse(end)) for start, end in VULNERABLE_RANGES]
    assert is_vulnerable_version(vulnerable_ranges, detected_version) is True


def test_vulnerable_version_11_9():
    detected_version = version.parse("11.9.1.0")
    vulnerable_ranges = [(version.parse(start), version.parse(end)) for start, end in VULNERABLE_RANGES]
    assert is_vulnerable_version(vulnerable_ranges, detected_version) is True


def test_vulnerable_version_11_10():
    detected_version = version.parse("11.10.0.1")
    vulnerable_ranges = [(version.parse(start), version.parse(end)) for start, end in VULNERABLE_RANGES]
    assert is_vulnerable_version(vulnerable_ranges, detected_version) is True


def test_patched_version_11_9():
    detected_version = version.parse("11.9.2.0")
    vulnerable_ranges = [(version.parse(start), version.parse(end)) for start, end in VULNERABLE_RANGES]
    assert is_vulnerable_version(vulnerable_ranges, detected_version) is False


def test_equal_to_patched_version_11_9():
    detected_version = version.parse("11.9.1.1")
    vulnerable_ranges = [(version.parse(start), version.parse(end)) for start, end in VULNERABLE_RANGES]
    assert is_vulnerable_version(vulnerable_ranges, detected_version) is False


def test_cve_2023_35078_vulnerable(mocker):
    oois = set(run(mocker.MagicMock(), get_dummy_data("cve_2023_35078_vulnerable.html")))

    assert any(ooi.object_type == "Finding" for ooi in oois)


def test_cve_2023_35078_not_vulnerable(mocker):
    oois = set(run(mocker.MagicMock(), get_dummy_data("cve_2023_35078_not_vulnerable.html")))

    assert not any(ooi.object_type == "Finding" for ooi in oois)
