"""VMware Tools + wildcard version floors — run: python tests/test_vmware_tools.py

VMware Tools is the case that proves both sources are needed. NVD enriched
CVE-2025-41244 with a CPE and marked CVE-2025-41246, -41239, -22247 and -22230
"NOT SCHEDULED" — the CPE path can never see those four. cvelistV5 has them
all, under three spellings including vendor "n/a".

Their versions are written as placeholders, and "12.x.x, 11.x.x" names TWO
release lines in one field. Read as digits that becomes (12, 11), which is not
a version at all: a 12.4 host compares below it and falls out of its own range.
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.services.app_cve_scanner_service import resolve_product
from app.services import cvelistv5_scan_service as c5
from app.services.cvelistv5_scan_service import (
    _affected, _ranges_from_affected, _wildcard_floor,
)


def demo():
    # Both paths recognise the Windows inventory name.
    assert (resolve_product("VMware Tools") or {}).get("key") == "cpe:vmware:tools"
    assert c5.resolve("VMware Tools") == "vmware-tools"
    # Not the Linux package, and not Citrix's lookalike.
    assert resolve_product("Open VM Tools") is None
    assert c5.resolve("XenServer VM Tools for Windows") is None

    # All three vendor/product spellings resolve to one key.
    for v, p in (("VMware", "Tools"), ("n/a", "VMware Tools"),
                 ("VMware", "VMware tools")):
        assert c5._pair_key(v, p) == "vmware-tools", (v, p)

    # Wildcard floors: the concrete part in front of each x, lowest wins.
    assert _wildcard_floor("13.x.x.x") == "13"
    assert _wildcard_floor("12.x.x, 11.x.x") == "11"   # two release lines
    assert _wildcard_floor("12.5.x") == "12.5"
    assert _wildcard_floor("12.5.4") is None           # no wildcard, untouched

    # CVE-2025-22230 verbatim: 12.x and 11.x below 12.5.1.
    rg = _ranges_from_affected({
        "vendor": "n/a", "product": "VMware Tools",
        "versions": [{"version": "12.x.x, 11.x.x", "lessThan": "12.5.1",
                      "versionType": "custom", "status": "affected"}]})
    hit = lambda v: any(_affected(v, s, lt, lte) for s, lt, lte in rg)
    assert hit("12.4.0") and hit("11.3.5")     # both were reported unaffected
    assert not hit("12.5.1") and not hit("12.5.2")
    assert not hit("10.9.0")                   # below the stated floor

    print("vmware tools OK")


if __name__ == "__main__":
    demo()
