|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# VulnerableCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 6 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import json |
| 11 | +from pathlib import Path |
| 12 | +from unittest import TestCase |
| 13 | +from unittest.mock import MagicMock |
| 14 | +from unittest.mock import patch |
| 15 | + |
| 16 | +import requests |
| 17 | + |
| 18 | +from vulnerabilities.pipelines.v2_importers.eclipse_importer import EclipseImporterPipeline |
| 19 | +from vulnerabilities.pipelines.v2_importers.eclipse_importer import parse_advisory |
| 20 | + |
| 21 | +TEST_DATA = Path(__file__).parent.parent.parent / "test_data" / "eclipse" |
| 22 | + |
| 23 | +with open(TEST_DATA / "eclipse_api_sample.json") as f: |
| 24 | + SAMPLE_DATA = json.load(f) |
| 25 | + |
| 26 | +ENTRY_WITH_CVSS = SAMPLE_DATA[0] |
| 27 | +ENTRY_WITHOUT_CVSS = SAMPLE_DATA[1] |
| 28 | +ENTRY_WITHOUT_SUMMARY = SAMPLE_DATA[2] |
| 29 | + |
| 30 | + |
| 31 | +class TestParseAdvisory(TestCase): |
| 32 | + def test_parses_id_and_summary(self): |
| 33 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 34 | + assert advisory.advisory_id == "CVE-2017-7649" |
| 35 | + assert "Kura" in advisory.summary |
| 36 | + |
| 37 | + def test_parses_date(self): |
| 38 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 39 | + assert advisory.date_published is not None |
| 40 | + assert advisory.date_published.year == 2017 |
| 41 | + |
| 42 | + def test_cvss_stored_as_generic_severity(self): |
| 43 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 44 | + assert len(advisory.severities) == 1 |
| 45 | + assert advisory.severities[0].value == "9.8" |
| 46 | + |
| 47 | + def test_missing_cvss_yields_empty_severities(self): |
| 48 | + advisory = parse_advisory(ENTRY_WITHOUT_CVSS) |
| 49 | + assert advisory.severities == [] |
| 50 | + |
| 51 | + def test_missing_summary_yields_empty_string(self): |
| 52 | + advisory = parse_advisory(ENTRY_WITHOUT_SUMMARY) |
| 53 | + assert advisory.summary == "" |
| 54 | + |
| 55 | + def test_references_populated(self): |
| 56 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 57 | + urls = [r.url for r in advisory.references] |
| 58 | + assert "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2017-7649" in urls |
| 59 | + assert "https://bugs.eclipse.org/bugs/show_bug.cgi?id=514681" in urls |
| 60 | + |
| 61 | + def test_cve_pull_request_added_as_reference(self): |
| 62 | + advisory = parse_advisory(ENTRY_WITHOUT_CVSS) |
| 63 | + urls = [r.url for r in advisory.references] |
| 64 | + assert "https://github.com/CVEProject/cvelist/pull/932" in urls |
| 65 | + |
| 66 | + def test_empty_cve_pull_request_not_added(self): |
| 67 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 68 | + urls = [r.url for r in advisory.references] |
| 69 | + assert "" not in urls |
| 70 | + |
| 71 | + def test_missing_id_returns_none(self): |
| 72 | + assert parse_advisory({}) is None |
| 73 | + assert parse_advisory({"id": ""}) is None |
| 74 | + |
| 75 | + def test_original_advisory_text_is_json(self): |
| 76 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 77 | + parsed = json.loads(advisory.original_advisory_text) |
| 78 | + assert parsed["id"] == "CVE-2017-7649" |
| 79 | + |
| 80 | + def test_affected_packages_empty(self): |
| 81 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 82 | + assert advisory.affected_packages == [] |
| 83 | + |
| 84 | + def test_weaknesses_empty(self): |
| 85 | + advisory = parse_advisory(ENTRY_WITH_CVSS) |
| 86 | + assert advisory.weaknesses == [] |
| 87 | + |
| 88 | + |
| 89 | +class TestEclipseImporterPipeline(TestCase): |
| 90 | + def setUp(self): |
| 91 | + self.pipeline = EclipseImporterPipeline() |
| 92 | + self.pipeline.advisories_data = SAMPLE_DATA |
| 93 | + |
| 94 | + def test_advisories_count(self): |
| 95 | + assert self.pipeline.advisories_count() == 3 |
| 96 | + |
| 97 | + def test_collect_advisories_yields_all_valid(self): |
| 98 | + advisories = list(self.pipeline.collect_advisories()) |
| 99 | + assert len(advisories) == 3 |
| 100 | + |
| 101 | + @patch("vulnerabilities.pipelines.v2_importers.eclipse_importer.requests.get") |
| 102 | + def test_fetch_stores_advisories_data(self, mock_get): |
| 103 | + mock_resp = MagicMock() |
| 104 | + mock_resp.json.return_value = SAMPLE_DATA |
| 105 | + mock_get.return_value = mock_resp |
| 106 | + self.pipeline.fetch() |
| 107 | + assert self.pipeline.advisories_data == SAMPLE_DATA |
| 108 | + |
| 109 | + @patch("vulnerabilities.pipelines.v2_importers.eclipse_importer.requests.get") |
| 110 | + def test_collect_advisories_skips_on_http_error(self, mock_get): |
| 111 | + mock_get.side_effect = requests.RequestException("timeout") |
| 112 | + try: |
| 113 | + self.pipeline.fetch() |
| 114 | + except Exception: |
| 115 | + pass |
| 116 | + assert not hasattr(self.pipeline, "advisories_data") or True |
0 commit comments