"""CPE target_sw is field 10 — run: python tests/test_target_sw_field.py

The platform filter read field 9 (sw_edition) instead of field 10
(target_sw). Field 9 is almost always "*", so every match looked
platform-neutral and the filter never rejected anything — it had no effect at
all since it was written.

What came through: NVD files the Acrobat BROWSER EXTENSIONS under the same
product name as the desktop app, distinguished only by target_sw, and their
versions are BROWSER versions:

    cpe:2.3:a:adobe:acrobat:*:*:*:*:*:edge:*:*   up to 126.0.2592.81

Desktop Acrobat 26.001.21771 compares below that, so every host with Acrobat
installed collected the extension's CVEs (CVE-2026-48294, CVE-2024-39379,
CVE-2024-20721, CVE-2024-20709).
"""
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 _platform_ok

CPE = "cpe:2.3:a:adobe:acrobat:*:*:*:*:*:edge:*:*"


def demo():
    parts = CPE.split(":")
    assert parts[9] == "*", "field 9 is sw_edition"
    assert parts[10] == "edge", "field 10 is target_sw"

    # Browser extensions are rejected on a desktop OS.
    assert _platform_ok(["edge"], "windows") is False
    assert _platform_ok(["chrome"], "windows") is False
    assert _platform_ok(["edge"], "macos") is False

    # Platform-neutral and matching entries still pass.
    assert _platform_ok(["*"], "windows") is True
    assert _platform_ok(["windows"], "windows") is True
    assert _platform_ok(["*", "edge"], "windows") is True   # one neutral is enough

    # Unknown (older cache rows) stays permissive rather than dropping data.
    assert _platform_ok([], "windows") is True
    assert _platform_ok(None, "windows") is True

    print("target_sw field OK")


if __name__ == "__main__":
    demo()
