|
| 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/nexB/vulnerablecode for support or download. |
| 7 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 8 | +# |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +from vulnerabilities.importer import AdvisoryData |
| 13 | +from vulnerabilities.importer import Reference |
| 14 | +from vulnerabilities.improver import MAX_CONFIDENCE |
| 15 | +from vulnerabilities.improver import Inference |
| 16 | +from vulnerabilities.improver import PackageURL |
| 17 | + |
| 18 | + |
| 19 | +def test_empty_inference_raises_exception(): |
| 20 | + with pytest.raises(AssertionError): |
| 21 | + Inference() |
| 22 | + |
| 23 | + |
| 24 | +def test_inference_to_dict_method_with_vulnerability_id(): |
| 25 | + inference = Inference(vulnerability_id="vulcoid-1337") |
| 26 | + expected = { |
| 27 | + "vulnerability_id": "vulcoid-1337", |
| 28 | + "aliases": [], |
| 29 | + "confidence": MAX_CONFIDENCE, |
| 30 | + "summary": "", |
| 31 | + "affected_purls": [], |
| 32 | + "fixed_purl": None, |
| 33 | + "references": [], |
| 34 | + } |
| 35 | + assert expected == inference.to_dict() |
| 36 | + |
| 37 | + |
| 38 | +def test_inference_to_dict_method_with_purls(): |
| 39 | + purl = PackageURL(type="dummy", namespace="rick", name="jalebi", version="1") |
| 40 | + inference = Inference(affected_purls=[purl], fixed_purl=purl) |
| 41 | + expected = { |
| 42 | + "vulnerability_id": None, |
| 43 | + "aliases": [], |
| 44 | + "confidence": MAX_CONFIDENCE, |
| 45 | + "summary": "", |
| 46 | + "affected_purls": [purl.to_dict()], |
| 47 | + "fixed_purl": purl.to_dict(), |
| 48 | + "references": [], |
| 49 | + } |
| 50 | + assert expected == inference.to_dict() |
| 51 | + |
| 52 | + |
| 53 | +def test_inference_to_dict_method_with_versionless_purls_raises_exception(): |
| 54 | + versionless_purl = PackageURL(type="dummy", namespace="rick", name="gulabjamun") |
| 55 | + with pytest.raises(AssertionError): |
| 56 | + Inference(affected_purls=[versionless_purl], fixed_purl=versionless_purl) |
| 57 | + |
| 58 | + |
| 59 | +def test_inference_from_advisory_data(): |
| 60 | + aliases = ["lalmohan", "gulabjamun"] |
| 61 | + summary = "really tasty sweets" |
| 62 | + references = [Reference(url="http://localhost")] |
| 63 | + advisory_data = AdvisoryData(aliases=aliases, summary=summary, references=references) |
| 64 | + fixed_purl = PackageURL(name="mithai", version="1", type="sweets") |
| 65 | + inference = Inference.from_advisory_data( |
| 66 | + advisory_data=advisory_data, fixed_purl=fixed_purl, confidence=MAX_CONFIDENCE |
| 67 | + ) |
| 68 | + assert inference == Inference( |
| 69 | + aliases=aliases, summary=summary, references=references, fixed_purl=fixed_purl |
| 70 | + ) |
0 commit comments