|
7 | 7 | # See https://aboutcode.org for more information about nexB OSS projects.
|
8 | 8 | #
|
9 | 9 |
|
| 10 | +from collections import Counter |
| 11 | + |
10 | 12 | import pytest
|
| 13 | +from django.utils import timezone |
| 14 | +from packageurl import PackageURL |
| 15 | +from pytest_django.asserts import assertQuerysetEqual |
11 | 16 |
|
| 17 | +from vulnerabilities.importer import Reference |
12 | 18 | from vulnerabilities.improve_runner import create_valid_vulnerability_reference
|
| 19 | +from vulnerabilities.improve_runner import get_or_create_vulnerability_and_aliases |
| 20 | +from vulnerabilities.improve_runner import process_inferences |
| 21 | +from vulnerabilities.improver import Improver |
| 22 | +from vulnerabilities.improver import Inference |
| 23 | +from vulnerabilities.models import Advisory |
| 24 | +from vulnerabilities.models import Alias |
| 25 | +from vulnerabilities.models import Package |
| 26 | +from vulnerabilities.models import PackageRelatedVulnerability |
| 27 | +from vulnerabilities.models import Vulnerability |
| 28 | +from vulnerabilities.models import VulnerabilityReference |
| 29 | +from vulnerabilities.models import VulnerabilityRelatedReference |
| 30 | +from vulnerabilities.models import VulnerabilitySeverity |
13 | 31 |
|
14 | 32 |
|
15 | 33 | @pytest.mark.django_db
|
@@ -37,3 +55,152 @@ def test_create_valid_vulnerability_reference_accepts_long_references():
|
37 | 55 | url="https://foo.bar",
|
38 | 56 | )
|
39 | 57 | assert result
|
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.django_db |
| 61 | +def test_get_or_create_vulnerability_and_aliases_with_new_vulnerability_and_new_aliases(): |
| 62 | + alias_names = ["TAYLOR-1337", "SWIFT-1337"] |
| 63 | + summary = "Melodious vulnerability" |
| 64 | + vulnerability = get_or_create_vulnerability_and_aliases( |
| 65 | + alias_names=alias_names, summary=summary |
| 66 | + ) |
| 67 | + assert vulnerability |
| 68 | + alias_names_in_db = vulnerability.get_aliases.values_list("alias", flat=True) |
| 69 | + assert Counter(alias_names_in_db) == Counter(alias_names) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.django_db |
| 73 | +def test_get_or_create_vulnerability_and_aliases_with_different_vulnerability_and_existing_aliases(): |
| 74 | + existing_vulnerability = Vulnerability(vulnerability_id="VCID-Existing") |
| 75 | + existing_vulnerability.save() |
| 76 | + existing_aliases = [] |
| 77 | + existing_alias_names = ["ALIAS-1", "ALIAS-2"] |
| 78 | + for alias in existing_alias_names: |
| 79 | + existing_aliases.append(Alias(alias=alias, vulnerability=existing_vulnerability)) |
| 80 | + Alias.objects.bulk_create(existing_aliases) |
| 81 | + |
| 82 | + different_vulnerability = Vulnerability(vulnerability_id="VCID-New") |
| 83 | + different_vulnerability.save() |
| 84 | + assert not get_or_create_vulnerability_and_aliases( |
| 85 | + alias_names=existing_alias_names, vulnerability_id=different_vulnerability.vulnerability_id |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.django_db |
| 90 | +def test_get_or_create_vulnerability_and_aliases_with_existing_vulnerability_and_new_aliases(): |
| 91 | + existing_vulnerability = Vulnerability(vulnerability_id="VCID-Existing") |
| 92 | + existing_vulnerability.save() |
| 93 | + |
| 94 | + existing_alias_names = ["ALIAS-1", "ALIAS-2"] |
| 95 | + vulnerability = get_or_create_vulnerability_and_aliases( |
| 96 | + vulnerability_id="VCID-Existing", alias_names=existing_alias_names |
| 97 | + ) |
| 98 | + assert existing_vulnerability == vulnerability |
| 99 | + |
| 100 | + alias_names_in_db = vulnerability.get_aliases.values_list("alias", flat=True) |
| 101 | + assert Counter(alias_names_in_db) == Counter(existing_alias_names) |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.django_db |
| 105 | +def test_get_or_create_vulnerability_and_aliases_with_existing_vulnerability_and_existing_aliases(): |
| 106 | + existing_vulnerability = Vulnerability(vulnerability_id="VCID-Existing") |
| 107 | + existing_vulnerability.save() |
| 108 | + |
| 109 | + existing_aliases = [] |
| 110 | + existing_alias_names = ["ALIAS-1", "ALIAS-2"] |
| 111 | + for alias in existing_alias_names: |
| 112 | + existing_aliases.append(Alias(alias=alias, vulnerability=existing_vulnerability)) |
| 113 | + Alias.objects.bulk_create(existing_aliases) |
| 114 | + |
| 115 | + vulnerability = get_or_create_vulnerability_and_aliases( |
| 116 | + vulnerability_id="VCID-Existing", alias_names=existing_alias_names |
| 117 | + ) |
| 118 | + assert existing_vulnerability == vulnerability |
| 119 | + |
| 120 | + alias_names_in_db = vulnerability.get_aliases.values_list("alias", flat=True) |
| 121 | + assert Counter(alias_names_in_db) == Counter(existing_alias_names) |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.django_db |
| 125 | +def test_get_or_create_vulnerability_and_aliases_with_existing_vulnerability_and_existing_and_new_aliases(): |
| 126 | + existing_vulnerability = Vulnerability(vulnerability_id="VCID-Existing") |
| 127 | + existing_vulnerability.save() |
| 128 | + |
| 129 | + existing_aliases = [] |
| 130 | + existing_alias_names = ["ALIAS-1", "ALIAS-2"] |
| 131 | + for alias in existing_alias_names: |
| 132 | + existing_aliases.append(Alias(alias=alias, vulnerability=existing_vulnerability)) |
| 133 | + Alias.objects.bulk_create(existing_aliases) |
| 134 | + |
| 135 | + new_alias_names = ["ALIAS-3", "ALIAS-4"] |
| 136 | + alias_names = existing_alias_names + new_alias_names |
| 137 | + vulnerability = get_or_create_vulnerability_and_aliases( |
| 138 | + vulnerability_id="VCID-Existing", alias_names=alias_names |
| 139 | + ) |
| 140 | + assert existing_vulnerability == vulnerability |
| 141 | + |
| 142 | + alias_names_in_db = vulnerability.get_aliases.values_list("alias", flat=True) |
| 143 | + assert Counter(alias_names_in_db) == Counter(alias_names) |
| 144 | + |
| 145 | + |
| 146 | +DUMMY_ADVISORY = Advisory(summary="dummy", created_by="tests", date_collected=timezone.now()) |
| 147 | + |
| 148 | + |
| 149 | +@pytest.mark.django_db |
| 150 | +def test_process_inferences_with_no_inference(): |
| 151 | + assert not process_inferences( |
| 152 | + inferences=[], advisory=DUMMY_ADVISORY, improver_name="test_improver" |
| 153 | + ) |
| 154 | + |
| 155 | + |
| 156 | +@pytest.mark.django_db |
| 157 | +def test_process_inferences_with_unknown_but_specified_vulnerability(): |
| 158 | + inference = Inference(vulnerability_id="VCID-Does-Not-Exist-In-DB", aliases=["MATRIX-Neo"]) |
| 159 | + assert not process_inferences( |
| 160 | + inferences=[inference], advisory=DUMMY_ADVISORY, improver_name="test_improver" |
| 161 | + ) |
| 162 | + |
| 163 | + |
| 164 | +INFERENCES = [ |
| 165 | + Inference( |
| 166 | + aliases=["CVE-1", "CVE-2"], |
| 167 | + summary="One upon a time, in a package far far away", |
| 168 | + affected_purls=[ |
| 169 | + PackageURL(type="character", namespace="star-wars", name="anakin", version="1") |
| 170 | + ], |
| 171 | + fixed_purl=PackageURL( |
| 172 | + type="character", namespace="star-wars", name="darth-vader", version="1" |
| 173 | + ), |
| 174 | + references=[Reference(reference_id="imperial-vessel-1", url="https://m47r1x.github.io")], |
| 175 | + ) |
| 176 | +] |
| 177 | + |
| 178 | + |
| 179 | +def get_objects_in_all_tables_used_by_process_inferences(): |
| 180 | + return { |
| 181 | + "vulnerabilities": list(Vulnerability.objects.all()), |
| 182 | + "aliases": list(Alias.objects.all()), |
| 183 | + "references": list(VulnerabilityReference.objects.all()), |
| 184 | + "advisories": list(Advisory.objects.all()), |
| 185 | + "packages": list(Package.objects.all()), |
| 186 | + "references": list(VulnerabilityReference.objects.all()), |
| 187 | + "severity": list(VulnerabilitySeverity.objects.all()), |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | +@pytest.mark.django_db |
| 192 | +def test_process_inferences_idempotency(): |
| 193 | + process_inferences(INFERENCES, DUMMY_ADVISORY, improver_name="test_improver") |
| 194 | + all_objects = get_objects_in_all_tables_used_by_process_inferences() |
| 195 | + process_inferences(INFERENCES, DUMMY_ADVISORY, improver_name="test_improver") |
| 196 | + process_inferences(INFERENCES, DUMMY_ADVISORY, improver_name="test_improver") |
| 197 | + assert all_objects == get_objects_in_all_tables_used_by_process_inferences() |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.django_db |
| 201 | +def test_process_inference_idempotency_with_different_improver_names(): |
| 202 | + process_inferences(INFERENCES, DUMMY_ADVISORY, improver_name="test_improver_one") |
| 203 | + all_objects = get_objects_in_all_tables_used_by_process_inferences() |
| 204 | + process_inferences(INFERENCES, DUMMY_ADVISORY, improver_name="test_improver_two") |
| 205 | + process_inferences(INFERENCES, DUMMY_ADVISORY, improver_name="test_improver_three") |
| 206 | + assert all_objects == get_objects_in_all_tables_used_by_process_inferences() |
0 commit comments