|
7 | 7 | # See https://aboutcode.org for more information about nexB OSS projects. |
8 | 8 | # |
9 | 9 |
|
| 10 | +import json |
10 | 11 | import os |
11 | 12 | from unittest import TestCase |
12 | 13 | from unittest.mock import patch |
13 | 14 |
|
| 15 | +import pytest |
| 16 | +from packageurl import PackageURL |
| 17 | +from univers.versions import SemverVersion |
| 18 | + |
| 19 | +from vulnerabilities.importers.curl import CurlImporter |
14 | 20 | from vulnerabilities.importers.curl import get_cwe_from_curl_advisory |
15 | 21 | from vulnerabilities.importers.curl import parse_advisory_data |
16 | 22 | from vulnerabilities.tests import util_tests |
@@ -71,3 +77,52 @@ def test_get_cwe_from_curl_advisory(self): |
71 | 77 | for advisory in mock_advisory: |
72 | 78 | mock_cwe_list.extend(get_cwe_from_curl_advisory(advisory)) |
73 | 79 | assert mock_cwe_list == [311] |
| 80 | + |
| 81 | + |
| 82 | +@pytest.fixture |
| 83 | +def mock_curl_api(monkeypatch): |
| 84 | + test_files = [ |
| 85 | + "curl_advisory_mock1.json", |
| 86 | + "curl_advisory_mock2.json", |
| 87 | + "curl_advisory_mock3.json", |
| 88 | + ] |
| 89 | + |
| 90 | + BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 91 | + TEST_DATA = os.path.join(BASE_DIR, "test_data/curl") |
| 92 | + data = [] |
| 93 | + for fname in test_files: |
| 94 | + with open(os.path.join(TEST_DATA, fname)) as f: |
| 95 | + data.append(json.load(f)) |
| 96 | + |
| 97 | + def mock_fetch(self): |
| 98 | + return data |
| 99 | + |
| 100 | + monkeypatch.setattr(CurlImporter, "fetch", mock_fetch) |
| 101 | + |
| 102 | + |
| 103 | +def test_curl_importer_package_first(monkeypatch, mock_curl_api): |
| 104 | + purl = PackageURL(type="generic", namespace="curl.se", name="curl") |
| 105 | + importer = CurlImporter(purl=purl) |
| 106 | + advisories = list(importer.advisory_data()) |
| 107 | + assert len(advisories) == 3 |
| 108 | + for adv in advisories: |
| 109 | + assert any(ap.package.name == "curl" for ap in adv.affected_packages) |
| 110 | + |
| 111 | + |
| 112 | +def test_curl_importer_package_first_version(monkeypatch, mock_curl_api): |
| 113 | + purl = PackageURL(type="generic", namespace="curl.se", name="curl", version="8.6.0") |
| 114 | + importer = CurlImporter(purl=purl) |
| 115 | + advisories = list(importer.advisory_data()) |
| 116 | + |
| 117 | + assert len(advisories) == 1 |
| 118 | + assert advisories[0].aliases[0] == "CVE-2024-2379" |
| 119 | + |
| 120 | + for ap in advisories[0].affected_packages: |
| 121 | + assert ap.affected_version_range.contains(SemverVersion("8.6.0")) |
| 122 | + |
| 123 | + |
| 124 | +def test_curl_importer_package_first_version_not_affected(monkeypatch, mock_curl_api): |
| 125 | + purl = PackageURL(type="generic", namespace="curl.se", name="curl", version="9.9.9") |
| 126 | + importer = CurlImporter(purl=purl) |
| 127 | + advisories = list(importer.advisory_data()) |
| 128 | + assert advisories == [] |
0 commit comments