|
1 | 1 | from io import StringIO |
2 | 2 | import json |
3 | | -from typing import List |
| 3 | +from typing import List, Union |
4 | 4 | from collections import namedtuple |
5 | 5 |
|
6 | 6 | import pytest |
7 | 7 | from packageurl import PackageURL |
8 | 8 | from spdx_tools.spdx.writer.json.json_writer import write_document_to_stream |
9 | 9 |
|
10 | | -from sbom.create_product_sbom import ReleaseNotes, create_sbom |
| 10 | +from sbom.create_product_sbom import ReleaseNotes, create_sbom, parse_release_notes |
11 | 11 | from sbom.sbomlib import Component, Image, IndexImage, Snapshot |
12 | 12 |
|
13 | 13 | Digests = namedtuple("Digests", ["single_arch", "multi_arch"]) |
|
17 | 17 | ) |
18 | 18 |
|
19 | 19 |
|
20 | | -def verify_cpe(sbom, cpe: str) -> None: |
| 20 | +@pytest.mark.parametrize( |
| 21 | + ["data", "expected_rn"], |
| 22 | + [ |
| 23 | + pytest.param( |
| 24 | + { |
| 25 | + "unrelated": "field", |
| 26 | + "releaseNotes": { |
| 27 | + "product_name": "Product", |
| 28 | + "product_version": "1.0", |
| 29 | + "cpe": "cpe", |
| 30 | + }, |
| 31 | + }, |
| 32 | + ReleaseNotes( |
| 33 | + product_name="Product", |
| 34 | + product_version="1.0", |
| 35 | + cpe="cpe", |
| 36 | + ), |
| 37 | + id="cpe-single", |
| 38 | + ), |
| 39 | + pytest.param( |
| 40 | + { |
| 41 | + "unrelated": "field", |
| 42 | + "releaseNotes": { |
| 43 | + "product_name": "Product", |
| 44 | + "product_version": "1.0", |
| 45 | + "cpe": ["cpe1", "cpe2"], |
| 46 | + }, |
| 47 | + }, |
| 48 | + ReleaseNotes( |
| 49 | + product_name="Product", |
| 50 | + product_version="1.0", |
| 51 | + cpe=["cpe1", "cpe2"], |
| 52 | + ), |
| 53 | + id="cpe-list", |
| 54 | + ), |
| 55 | + ], |
| 56 | +) |
| 57 | +def test_parse_release_notes(data: dict, expected_rn: ReleaseNotes) -> None: |
| 58 | + actual = parse_release_notes(json.dumps(data)) |
| 59 | + assert expected_rn == actual |
| 60 | + |
| 61 | + |
| 62 | +def verify_cpe(sbom, expected_cpe: Union[str, List[str]]) -> None: |
21 | 63 | """ |
22 | | - Verify that the CPE externalRef is in the first package. |
| 64 | + Verify that all CPE externalRefs are in the first package. |
23 | 65 | """ |
24 | | - assert { |
25 | | - "referenceCategory": "SECURITY", |
26 | | - "referenceLocator": cpe, |
27 | | - "referenceType": "cpe22Type", |
28 | | - } in sbom["packages"][0]["externalRefs"] |
| 66 | + all_cpes = expected_cpe if isinstance(expected_cpe, list) else [expected_cpe] |
| 67 | + for cpe in all_cpes: |
| 68 | + assert { |
| 69 | + "referenceCategory": "SECURITY", |
| 70 | + "referenceLocator": cpe, |
| 71 | + "referenceType": "cpe22Type", |
| 72 | + } in sbom["packages"][0]["externalRefs"] |
29 | 73 |
|
30 | 74 |
|
31 | 75 | def verify_purls(sbom, expected: List[str]) -> None: |
@@ -98,6 +142,19 @@ def verify_package_licenses(sbom) -> None: |
98 | 142 | assert package["licenseDeclared"] == "NOASSERTION" |
99 | 143 |
|
100 | 144 |
|
| 145 | +@pytest.mark.parametrize( |
| 146 | + "cpe", |
| 147 | + [ |
| 148 | + pytest.param("cpe:/a:redhat:discovery:1.0::el9", id="cpe-single"), |
| 149 | + pytest.param( |
| 150 | + [ |
| 151 | + "cpe:/a:redhat:discovery:1.0::el9", |
| 152 | + "cpe:/a:redhat:discovery:1.0::el10", |
| 153 | + ], |
| 154 | + id="cpe-list", |
| 155 | + ), |
| 156 | + ], |
| 157 | +) |
101 | 158 | @pytest.mark.parametrize( |
102 | 159 | ["snapshot", "purls"], |
103 | 160 | [ |
@@ -176,12 +233,11 @@ def verify_package_licenses(sbom) -> None: |
176 | 233 | ), |
177 | 234 | ], |
178 | 235 | ) |
179 | | -def test_create_sbom(snapshot, purls): |
| 236 | +def test_create_sbom(snapshot: Snapshot, purls: List[str], cpe: Union[str, List[str]]): |
180 | 237 | """ |
181 | 238 | Create an SBOM from release notes and a snapshot and verify that the |
182 | 239 | expected properties hold. |
183 | 240 | """ |
184 | | - cpe = "cpe:/a:redhat:discovery:1.0::el9" |
185 | 241 | release_notes = ReleaseNotes( |
186 | 242 | product_name="Product", |
187 | 243 | product_version="1.0", |
|
0 commit comments