|
7 | 7 | # See https://aboutcode.org for more information about nexB OSS projects. |
8 | 8 | # |
9 | 9 |
|
| 10 | +from datetime import datetime |
| 11 | +from datetime import timedelta |
| 12 | + |
| 13 | +from django.test import TestCase |
10 | 14 | from fetchcode.package_versions import PackageVersion |
11 | 15 | from packageurl import PackageURL |
12 | 16 | from univers.version_constraint import VersionConstraint |
13 | 17 | from univers.version_range import GemVersionRange |
| 18 | +from univers.version_range import VersionRange |
14 | 19 | from univers.versions import RubygemsVersion |
15 | 20 |
|
| 21 | +from vulnerabilities import utils |
| 22 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 23 | +from vulnerabilities.importer import AffectedPackageV2 |
| 24 | +from vulnerabilities.importer import PackageCommitPatchData |
| 25 | +from vulnerabilities.importer import PatchData |
| 26 | +from vulnerabilities.importer import VulnerabilitySeverity |
| 27 | +from vulnerabilities.models import AdvisoryV2 |
| 28 | +from vulnerabilities.pipelines import insert_advisory_v2 |
| 29 | +from vulnerabilities.references import XsaReferenceV2 |
| 30 | +from vulnerabilities.references import ZbxReferenceV2 |
16 | 31 | from vulnerabilities.utils import AffectedPackage |
17 | 32 | from vulnerabilities.utils import get_item |
18 | 33 | from vulnerabilities.utils import get_severity_range |
@@ -151,3 +166,88 @@ def test_resolve_version_range_without_ignorable_versions(): |
151 | 166 | def test_get_severity_range(): |
152 | 167 | assert get_severity_range({""}) is None |
153 | 168 | assert get_severity_range({}) is None |
| 169 | + |
| 170 | + |
| 171 | +class TestComputeContentIdV2(TestCase): |
| 172 | + def setUp(self): |
| 173 | + self.advisory1 = AdvisoryDataV2( |
| 174 | + summary="Test advisory", |
| 175 | + aliases=["CVE-2025-0001", "CVE-2024-0001"], |
| 176 | + references=[ |
| 177 | + XsaReferenceV2.from_number(248), |
| 178 | + ZbxReferenceV2.from_id("ZBX-000"), |
| 179 | + ], |
| 180 | + severities=[ |
| 181 | + VulnerabilitySeverity.from_dict( |
| 182 | + { |
| 183 | + "system": "cvssv4", |
| 184 | + "value": "7.5", |
| 185 | + "scoring_elements": "AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H", |
| 186 | + } |
| 187 | + ), |
| 188 | + VulnerabilitySeverity.from_dict( |
| 189 | + { |
| 190 | + "system": "cvssv3", |
| 191 | + "value": "6.5", |
| 192 | + "scoring_elements": "AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H", |
| 193 | + } |
| 194 | + ), |
| 195 | + ], |
| 196 | + weaknesses=[296, 233], |
| 197 | + affected_packages=[ |
| 198 | + AffectedPackageV2( |
| 199 | + package=PackageURL.from_string("pkg:npm/foobar"), |
| 200 | + affected_version_range=VersionRange.from_string("vers:npm/<=1.2.3"), |
| 201 | + fixed_version_range=VersionRange.from_string("vers:npm/1.2.4"), |
| 202 | + introduced_by_commit_patches=[], |
| 203 | + fixed_by_commit_patches=[], |
| 204 | + ), |
| 205 | + AffectedPackageV2( |
| 206 | + package=PackageURL.from_string("pkg:npm/foobar"), |
| 207 | + affected_version_range=VersionRange.from_string("vers:npm/<=0.2.3"), |
| 208 | + fixed_version_range=VersionRange.from_string("vers:npm/0.2.4"), |
| 209 | + introduced_by_commit_patches=[ |
| 210 | + PackageCommitPatchData( |
| 211 | + vcs_url="https://foobar.vcs/", |
| 212 | + commit_hash="662f801f", |
| 213 | + ), |
| 214 | + PackageCommitPatchData( |
| 215 | + vcs_url="https://foobar.vcs/", |
| 216 | + commit_hash="001f801f", |
| 217 | + ), |
| 218 | + ], |
| 219 | + fixed_by_commit_patches=[ |
| 220 | + PackageCommitPatchData( |
| 221 | + vcs_url="https://foobar.vcs/", |
| 222 | + commit_hash="982f801f", |
| 223 | + ), |
| 224 | + PackageCommitPatchData( |
| 225 | + vcs_url="https://foobar.vcs/", |
| 226 | + commit_hash="081f801f", |
| 227 | + ), |
| 228 | + ], |
| 229 | + ), |
| 230 | + ], |
| 231 | + patches=[ |
| 232 | + PatchData(patch_url="https://foo.bar/", patch_text="test patch"), |
| 233 | + PatchData(patch_url="https://yet-another-foo.bar/", patch_text="some test patch"), |
| 234 | + ], |
| 235 | + advisory_id="ADV-001", |
| 236 | + date_published=datetime.now() - timedelta(days=10), |
| 237 | + url="https://example.com/advisory/1", |
| 238 | + ) |
| 239 | + insert_advisory_v2( |
| 240 | + advisory=self.advisory1, |
| 241 | + pipeline_id="test_pipeline_v2", |
| 242 | + ) |
| 243 | + |
| 244 | + def test_compute_content_id_v2(self): |
| 245 | + result = utils.compute_content_id_v2(self.advisory1) |
| 246 | + self.assertEqual(result, "5211f1e6c3d935759fb288d79a865eeacc06e3e0e352ab7f5b4cb0e76a43a955") |
| 247 | + |
| 248 | + def test_content_id_from_adv_data_and_adv_model_are_same(self): |
| 249 | + id_from_data = utils.compute_content_id_v2(self.advisory1) |
| 250 | + advisory_model = AdvisoryV2.objects.first() |
| 251 | + id_from_model = utils.compute_content_id_v2(advisory_model) |
| 252 | + |
| 253 | + self.assertEqual(id_from_data, id_from_model) |
0 commit comments