# Copyright (C) 2022 Intel Corporation
# SPDX-License-Identifier: GPL-3.0-or-later

import pytest

from cve_bin_tool.cve_scanner import CVEScanner
from cve_bin_tool.util import ProductInfo


class TestExploitScanner:
    @pytest.mark.parametrize(
        "check_exploits, exploits_list, product_info, triage_info, expected_result",
        (
            (
                True,
                ["CVE-2018-19664"],
                ProductInfo(
                    vendor="libjpeg-turbo", product="libjpeg-turbo", version="2.0.1"
                ),
                {
                    "CVE-2018-19664": {
                        "severity": "CRITICAL",
                    },
                    "paths": {""},
                },
                "CRITICAL-EXPLOIT",
            ),
            (
                False,
                ["CVE-2018-19664"],
                ProductInfo(
                    vendor="libjpeg-turbo", product="libjpeg-turbo", version="2.0.1"
                ),
                {
                    "CVE-2018-19664": {
                        "severity": "CRITICAL",
                    },
                    "paths": {""},
                },
                "CRITICAL",
            ),
            (
                True,
                ["CVE-2019-19664"],
                ProductInfo(
                    vendor="libjpeg-turbo", product="libjpeg-turbo", version="2.0.1"
                ),
                {
                    "CVE-2018-19664": {
                        "severity": "CRITICAL",
                    },
                    "paths": {""},
                },
                "CRITICAL",
            ),
            (
                False,
                ["CVE-2019-19664"],
                ProductInfo(
                    vendor="libjpeg-turbo", product="libjpeg-turbo", version="2.0.1"
                ),
                {
                    "CVE-2018-19664": {
                        "severity": "CRITICAL",
                    },
                    "paths": {""},
                },
                "CRITICAL",
            ),
        ),
    )
    def test_exploit_checker(
        self, check_exploits, exploits_list, product_info, triage_info, expected_result
    ):
        with CVEScanner(
            check_exploits=check_exploits, exploits_list=exploits_list
        ) as scanner:
            scanner.get_cves(product_info, triage_info)
            assert (
                scanner.all_cve_data[product_info]["cves"][0].severity
                == expected_result
            )
