|
| 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 | +import pytest |
| 10 | +from unittest import mock |
| 11 | + |
| 12 | +import yaml |
| 13 | +from vulnerabilities.pipelines.v2_importers.oss_fuzz import OSSFuzzImporterPipeline |
| 14 | +from vulnerabilities.importer import AdvisoryData |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.django_db |
| 18 | +def test_collect_advisories_parses_yaml_correctly(tmp_path): |
| 19 | + advisory_path = tmp_path / "vulns" / "dummy_project" |
| 20 | + advisory_path.mkdir(parents=True) |
| 21 | + yaml_file = advisory_path / "CVE-2024-1234.yaml" |
| 22 | + |
| 23 | + advisory_dict = { |
| 24 | + "id": "CVE-2024-1234", |
| 25 | + "summary": "Some summary here", |
| 26 | + "affected": [ |
| 27 | + { |
| 28 | + "package": {"name": "some-lib"}, |
| 29 | + "versions": ["1.0.0"] |
| 30 | + } |
| 31 | + ] |
| 32 | + } |
| 33 | + yaml_file.write_text(yaml.dump(advisory_dict), encoding="utf-8") |
| 34 | + |
| 35 | + pipeline = OSSFuzzImporterPipeline() |
| 36 | + pipeline.vcs_response = mock.Mock() |
| 37 | + pipeline.vcs_response.dest_dir = tmp_path |
| 38 | + |
| 39 | + advisories = list(pipeline.collect_advisories()) |
| 40 | + assert len(advisories) == 1 |
| 41 | + assert advisories[0].advisory_id == "CVE-2024-1234" |
| 42 | + assert advisories[0].summary == "Some summary here" |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.django_db |
| 46 | +def test_advisories_count(tmp_path): |
| 47 | + (tmp_path / "vulns" / "project").mkdir(parents=True) |
| 48 | + (tmp_path / "vulns" / "project" / "CVE-2023-0001.yaml").write_text("id: CVE-2023-0001") |
| 49 | + (tmp_path / "vulns" / "project" / "CVE-2023-0002.yaml").write_text("id: CVE-2023-0002") |
| 50 | + |
| 51 | + pipeline = OSSFuzzImporterPipeline() |
| 52 | + pipeline.vcs_response = mock.Mock() |
| 53 | + pipeline.vcs_response.dest_dir = tmp_path |
| 54 | + |
| 55 | + assert pipeline.advisories_count() == 2 |
0 commit comments