diff --git a/cyclonedx/model/bom.py b/cyclonedx/model/bom.py index 3f76c94a..0800b079 100644 --- a/cyclonedx/model/bom.py +++ b/cyclonedx/model/bom.py @@ -14,6 +14,7 @@ # # SPDX-License-Identifier: Apache-2.0 # Copyright (c) OWASP Foundation. All Rights Reserved. +# Copyright (C) Siemens 2026. All Rights Reserved. from collections.abc import Generator, Iterable @@ -45,6 +46,7 @@ from . import _BOM_LINK_PREFIX, ExternalReference, Property from .bom_ref import BomRef from .component import Component +from .composition import Composition from .contact import OrganizationalContact, OrganizationalEntity from .definition import Definitions from .dependency import Dependable, Dependency @@ -444,6 +446,7 @@ def __init__( vulnerabilities: Optional[Iterable[Vulnerability]] = None, properties: Optional[Iterable[Property]] = None, definitions: Optional[Definitions] = None, + compositions: Optional[Iterable[Composition]] = None, ) -> None: """ Create a new Bom that you can manually/programmatically add data to later. @@ -461,6 +464,7 @@ def __init__( self.dependencies = dependencies or [] self.properties = properties or [] self.definitions = definitions or Definitions() + self.compositions = compositions or [] @property @serializable.type_mapping(UrnUuidHelper) @@ -598,18 +602,27 @@ def dependencies(self) -> 'SortedSet[Dependency]': def dependencies(self, dependencies: Iterable[Dependency]) -> None: self._dependencies = SortedSet(dependencies) - # @property - # ... - # @serializable.view(SchemaVersion1Dot3) - # @serializable.view(SchemaVersion1Dot4) - # @serializable.view(SchemaVersion1Dot5) - # @serializable.xml_sequence(6) - # def compositions(self) -> ...: - # ... # TODO Since CDX 1.3 - # - # @compositions.setter - # def compositions(self, ...) -> None: - # ... # TODO Since CDX 1.3 + @property + @serializable.view(SchemaVersion1Dot3) + @serializable.view(SchemaVersion1Dot4) + @serializable.view(SchemaVersion1Dot5) + @serializable.view(SchemaVersion1Dot6) + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_array(serializable.XmlArraySerializationType.NESTED, 'composition') + @serializable.xml_sequence(60) + def compositions(self) -> 'SortedSet[Composition]': + """ + A list of compositions that describe constituent parts (including components, services, and dependency + relationships) and their completeness. + + Returns: + Set of `Composition` + """ + return self._compositions + + @compositions.setter + def compositions(self, compositions: Iterable[Composition]) -> None: + self._compositions = SortedSet(compositions) @property # @serializable.view(SchemaVersion1Dot3) @todo: Update py-serializable to support view by OutputFormat filtering @@ -873,6 +886,7 @@ def __comparable_tuple(self) -> _ComparableTuple: _ComparableTuple(self.external_references), _ComparableTuple( self.dependencies), _ComparableTuple(self.properties), _ComparableTuple(self.vulnerabilities), + _ComparableTuple(self.compositions), )) def __eq__(self, other: object) -> bool: diff --git a/cyclonedx/model/composition.py b/cyclonedx/model/composition.py new file mode 100644 index 00000000..c727511a --- /dev/null +++ b/cyclonedx/model/composition.py @@ -0,0 +1,431 @@ +# This file is part of CycloneDX Python Library +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright (C) Siemens 2026. All Rights Reserved. + +""" + This set of classes represents the compositions in the CycloneDX standard. + +.. note:: + Introduced in CycloneDX v1.3 + +.. note:: + See the CycloneDX Schema for compositions: https://cyclonedx.org/docs/1.7/xml/#type_compositionType +""" + +from collections.abc import Iterable +from enum import Enum +from typing import TYPE_CHECKING, Any, Optional, Union +from xml.etree.ElementTree import Element # nosec B405 + +import py_serializable as serializable +from py_serializable.helpers import BaseHelper +from sortedcontainers import SortedSet + +from .._internal.bom_ref import bom_ref_from_str as _bom_ref_from_str +from .._internal.compare import ComparableTuple as _ComparableTuple +from ..schema.schema import ( + SchemaVersion1Dot3, + SchemaVersion1Dot4, + SchemaVersion1Dot5, + SchemaVersion1Dot6, + SchemaVersion1Dot7, +) +from . import _BOM_LINK_PREFIX, XsUri +from .bom_ref import BomRef + +if TYPE_CHECKING: # pragma: no cover + from py_serializable import ViewType + + +def _get_ref_attr(elem: Element) -> str: + """Read the ``ref`` attribute, tolerating namespace-qualified attribute names.""" + for key, value in elem.attrib.items(): + if key == 'ref' or key.endswith('}ref'): + return value + return '' + + +@serializable.serializable_enum +class AggregateType(str, Enum): + """ + Enum object that defines the permissible 'aggregate' types for a Composition according to the CycloneDX schema. + + Specifies an aggregate type that describes how complete a relationship is. + + .. note:: + See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.7/xml/#type_aggregateType + """ + COMPLETE = 'complete' + INCOMPLETE = 'incomplete' + INCOMPLETE_FIRST_PARTY_ONLY = 'incomplete_first_party_only' + INCOMPLETE_FIRST_PARTY_PROPRIETARY_ONLY = 'incomplete_first_party_proprietary_only' + INCOMPLETE_FIRST_PARTY_OPENSOURCE_ONLY = 'incomplete_first_party_opensource_only' + INCOMPLETE_THIRD_PARTY_ONLY = 'incomplete_third_party_only' + INCOMPLETE_THIRD_PARTY_PROPRIETARY_ONLY = 'incomplete_third_party_proprietary_only' + INCOMPLETE_THIRD_PARTY_OPENSOURCE_ONLY = 'incomplete_third_party_opensource_only' + UNKNOWN = 'unknown' + NOT_SPECIFIED = 'not_specified' + + +class _AggregateTypeSerializationHelper(BaseHelper): + """ THIS CLASS IS NON-PUBLIC API + + Down-maps `AggregateType` values that are not supported by a given schema version to a supported value. + """ + + __CASES: dict[type['ViewType'], frozenset[AggregateType]] = dict() + __CASES[SchemaVersion1Dot3] = frozenset({ + AggregateType.COMPLETE, + AggregateType.INCOMPLETE, + AggregateType.INCOMPLETE_FIRST_PARTY_ONLY, + AggregateType.INCOMPLETE_THIRD_PARTY_ONLY, + AggregateType.UNKNOWN, + AggregateType.NOT_SPECIFIED, + }) + __CASES[SchemaVersion1Dot4] = __CASES[SchemaVersion1Dot3] + __CASES[SchemaVersion1Dot5] = __CASES[SchemaVersion1Dot4] | { + AggregateType.INCOMPLETE_FIRST_PARTY_PROPRIETARY_ONLY, + AggregateType.INCOMPLETE_FIRST_PARTY_OPENSOURCE_ONLY, + AggregateType.INCOMPLETE_THIRD_PARTY_PROPRIETARY_ONLY, + AggregateType.INCOMPLETE_THIRD_PARTY_OPENSOURCE_ONLY, + } + __CASES[SchemaVersion1Dot6] = __CASES[SchemaVersion1Dot5] + __CASES[SchemaVersion1Dot7] = __CASES[SchemaVersion1Dot6] + + @classmethod + def __normalize(cls, aggregate: AggregateType, view: type['ViewType']) -> str: + return ( + aggregate + if aggregate in cls.__CASES.get(view, ()) + else AggregateType.INCOMPLETE + ).value + + @classmethod + def json_normalize(cls, o: Any, *, + view: Optional[type['ViewType']], + **__: Any) -> str: + assert view is not None + return cls.__normalize(o, view) + + @classmethod + def xml_normalize(cls, o: Any, *, + view: Optional[type['ViewType']], + **__: Any) -> str: + assert view is not None + return cls.__normalize(o, view) + + @classmethod + def json_denormalize(cls, o: Any, **__: Any) -> AggregateType: + return AggregateType(o) + + @classmethod + def xml_denormalize(cls, o: Any, **__: Any) -> AggregateType: + return AggregateType(o.text if hasattr(o, 'text') else o) + + +_Assembly = Union[BomRef, XsUri] +"""TypeAlias for a single assembly reference. + +An assembly is either a plain ``bom-ref`` (:class:`BomRef`) or a BOM-Link element (:class:`XsUri`). +""" + + +if TYPE_CHECKING: # pragma: no cover + # workaround for https://github.com/python/mypy/issues/5264 + class _AssemblyRepository(SortedSet[_Assembly]): + """Collection of assembly references. + + This is a `set`, not a `list`. Order MUST NOT matter here. + """ + + class _BomRefRepository(SortedSet[BomRef]): + """Collection of `BomRef`. + + This is a `set`, not a `list`. Order MUST NOT matter here. + """ +else: + class _AssemblyRepository(SortedSet): + """Collection of assembly references. + + This is a `set`, not a `list`. Order MUST NOT matter here. + """ + + def __init__(self, iterable: Any = None) -> None: + # `BomRef` and `XsUri` are not mutually comparable; sort by string value. + super().__init__(iterable, key=str) + + class _BomRefRepository(SortedSet): + """Collection of `BomRef`. + + This is a `set`, not a `list`. Order MUST NOT matter here. + """ + + +class _AssemblyRepositoryHelper(BaseHelper): + @classmethod + def json_normalize(cls, o: _AssemblyRepository, *, + view: Optional[type['ViewType']], + **__: Any) -> Any: + if len(o) == 0: + return None + return [str(a) for a in o] + + @classmethod + def json_denormalize(cls, o: list[Any], + **__: Any) -> _AssemblyRepository: + repo = _AssemblyRepository() + for a in o: + s = str(a) + if s.startswith(_BOM_LINK_PREFIX): + repo.add(XsUri(s)) + else: + repo.add(BomRef(value=s)) + return repo + + @classmethod + def xml_normalize(cls, o: _AssemblyRepository, *, + element_name: str, + view: Optional[type['ViewType']], + xmlns: Optional[str], + **__: Any) -> Optional[Element]: + if len(o) == 0: + return None + elem = Element(element_name) + tag = f'{{{xmlns}}}assembly' if xmlns else 'assembly' + for a in o: + child = Element(tag) + child.set(f'{{{xmlns}}}ref' if xmlns else 'ref', str(a)) + elem.append(child) + return elem + + @classmethod + def xml_denormalize(cls, o: Element, + default_ns: Optional[str], + **__: Any) -> _AssemblyRepository: + repo = _AssemblyRepository() + ns_map = {'bom': default_ns or ''} + for a in o.iterfind('bom:assembly', ns_map): + ref = _get_ref_attr(a) + if ref.startswith(_BOM_LINK_PREFIX): + repo.add(XsUri(ref)) + else: + repo.add(BomRef(value=ref)) + return repo + + +class _BomRefRepositoryHelper(BaseHelper): + """Serialize a `SortedSet` of `BomRef` as a nested XML wrapper / JSON list of strings.""" + + _element_item_name = 'item' + + @classmethod + def json_normalize(cls, o: _BomRefRepository, *, + view: Optional[type['ViewType']], + **__: Any) -> Any: + if len(o) == 0: + return None + return [str(r) for r in o] + + @classmethod + def json_denormalize(cls, o: list[Any], + **__: Any) -> _BomRefRepository: + repo = _BomRefRepository() + for r in o: + repo.add(BomRef(value=str(r))) + return repo + + @classmethod + def xml_normalize(cls, o: _BomRefRepository, *, + element_name: str, + view: Optional[type['ViewType']], + xmlns: Optional[str], + **__: Any) -> Optional[Element]: + if len(o) == 0: + return None + elem = Element(element_name) + tag = f'{{{xmlns}}}{cls._element_item_name}' if xmlns else cls._element_item_name + for r in o: + child = Element(tag) + child.set(f'{{{xmlns}}}ref' if xmlns else 'ref', str(r)) + elem.append(child) + return elem + + @classmethod + def xml_denormalize(cls, o: Element, + default_ns: Optional[str], + **__: Any) -> _BomRefRepository: + repo = _BomRefRepository() + ns_map = {'bom': default_ns or ''} + for r in o.iterfind(f'bom:{cls._element_item_name}', ns_map): + repo.add(BomRef(value=_get_ref_attr(r))) + return repo + + +class _DependencyRefRepositoryHelper(_BomRefRepositoryHelper): + _element_item_name = 'dependency' + + +class _VulnerabilityRefRepositoryHelper(_BomRefRepositoryHelper): + _element_item_name = 'vulnerability' + + +@serializable.serializable_class(ignore_unknown_during_deserialization=True) +class Composition: + """ + Models a Composition within a BOM. + + .. note:: + See https://cyclonedx.org/docs/1.7/xml/#type_compositionType + """ + + def __init__( + self, *, + aggregate: AggregateType = AggregateType.NOT_SPECIFIED, + assemblies: Optional[Iterable[_Assembly]] = None, + dependencies: Optional[Iterable[BomRef]] = None, + vulnerabilities: Optional[Iterable[BomRef]] = None, + bom_ref: Optional[Union[str, BomRef]] = None, + ) -> None: + self._bom_ref = _bom_ref_from_str(bom_ref, optional=True) + self.aggregate = aggregate + self.assemblies = assemblies or [] # type:ignore[assignment] + self.dependencies = dependencies or [] + self.vulnerabilities = vulnerabilities or [] + + @property + @serializable.json_name('bom-ref') + @serializable.type_mapping(BomRef) + @serializable.view(SchemaVersion1Dot6) + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_attribute() + @serializable.xml_name('bom-ref') + def bom_ref(self) -> Optional[BomRef]: + """ + An optional identifier which can be used to reference the composition elsewhere in the BOM. + Every bom-ref MUST be unique within the BOM. + + Returns: + `BomRef` + """ + return self._bom_ref + + @property + @serializable.type_mapping(_AggregateTypeSerializationHelper) + @serializable.view(SchemaVersion1Dot3) + @serializable.view(SchemaVersion1Dot4) + @serializable.view(SchemaVersion1Dot5) + @serializable.view(SchemaVersion1Dot6) + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_sequence(1) + def aggregate(self) -> AggregateType: + """ + Specifies an aggregate type that describes how complete a relationship is. + + Returns: + `AggregateType` + """ + return self._aggregate + + @aggregate.setter + def aggregate(self, aggregate: AggregateType) -> None: + self._aggregate = aggregate + + @property + @serializable.type_mapping(_AssemblyRepositoryHelper) + @serializable.view(SchemaVersion1Dot3) + @serializable.view(SchemaVersion1Dot4) + @serializable.view(SchemaVersion1Dot5) + @serializable.view(SchemaVersion1Dot6) + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_sequence(2) + def assemblies(self) -> _AssemblyRepository: + """ + The bom-ref identifiers of the components or services being described. Assemblies refer to nested + relationships whereby a constituent part may include other constituent parts. + + Returns: + Set of `BomRef`/`XsUri` + """ + return self._assemblies + + @assemblies.setter + def assemblies(self, assemblies: Iterable[_Assembly]) -> None: + self._assemblies = _AssemblyRepository(assemblies) + + @property + @serializable.type_mapping(_DependencyRefRepositoryHelper) + @serializable.view(SchemaVersion1Dot3) + @serializable.view(SchemaVersion1Dot4) + @serializable.view(SchemaVersion1Dot5) + @serializable.view(SchemaVersion1Dot6) + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_sequence(3) + def dependencies(self) -> _BomRefRepository: + """ + The bom-ref identifiers of the components or services being described. Dependencies refer to a relationship + whereby an independent constituent part requires another independent constituent part. + + Returns: + Set of `BomRef` + """ + return self._dependencies + + @dependencies.setter + def dependencies(self, dependencies: Iterable[BomRef]) -> None: + self._dependencies = _BomRefRepository(dependencies) + + @property + @serializable.type_mapping(_VulnerabilityRefRepositoryHelper) + @serializable.view(SchemaVersion1Dot5) + @serializable.view(SchemaVersion1Dot6) + @serializable.view(SchemaVersion1Dot7) + @serializable.xml_sequence(4) + def vulnerabilities(self) -> _BomRefRepository: + """ + The bom-ref identifiers of the vulnerabilities being described. + + Returns: + Set of `BomRef` + """ + return self._vulnerabilities + + @vulnerabilities.setter + def vulnerabilities(self, vulnerabilities: Iterable[BomRef]) -> None: + self._vulnerabilities = _BomRefRepository(vulnerabilities) + + def __comparable_tuple(self) -> _ComparableTuple: + return _ComparableTuple(( + self._aggregate, + _ComparableTuple(self._assemblies), + _ComparableTuple(self._dependencies), + _ComparableTuple(self._vulnerabilities), + )) + + def __hash__(self) -> int: + return hash(self.__comparable_tuple()) + + def __eq__(self, other: object) -> bool: + if isinstance(other, Composition): + return self.__comparable_tuple() == other.__comparable_tuple() + return False + + def __lt__(self, other: Any) -> bool: + if isinstance(other, Composition): + return self.__comparable_tuple() < other.__comparable_tuple() + return NotImplemented + + def __repr__(self) -> str: + return f'' diff --git a/docs/schema-support.rst b/docs/schema-support.rst index fe37633d..de211601 100644 --- a/docs/schema-support.rst +++ b/docs/schema-support.rst @@ -43,7 +43,7 @@ This is a snapshot. See the API docs for the latest state. +----------------------------+---------------+---------------------------------------------------------------------------------------------------+ | ``bom.dependencies`` | Yes | Since ``2.3.0`` | +----------------------------+---------------+---------------------------------------------------------------------------------------------------+ -| ``bom.compositions`` | No | | +| ``bom.compositions`` | Yes | | +----------------------------+---------------+---------------------------------------------------------------------------------------------------+ | ``bom.properties`` | Yes | Supported when outputting to Schema Version >= 1.5. See `schema specification bug 130`_ | +----------------------------+---------------+---------------------------------------------------------------------------------------------------+ diff --git a/tests/_data/models.py b/tests/_data/models.py index e2052878..8017c7ed 100644 --- a/tests/_data/models.py +++ b/tests/_data/models.py @@ -14,6 +14,7 @@ # # SPDX-License-Identifier: Apache-2.0 # Copyright (c) OWASP Foundation. All Rights Reserved. +# Copyright (C) Siemens 2026. All Rights Reserved. import base64 import sys @@ -67,6 +68,7 @@ Method, Occurrence, ) +from cyclonedx.model.composition import AggregateType, Composition from cyclonedx.model.contact import OrganizationalContact, OrganizationalEntity, PostalAddress from cyclonedx.model.crypto import ( AlgorithmProperties, @@ -1679,6 +1681,32 @@ def get_bom_for_issue941_nested_dependencies_irreversible_migrate() -> Bom: # --- +def get_bom_with_compositions() -> Bom: + return _make_bom( + compositions=[ + Composition( + aggregate=AggregateType.INCOMPLETE, + assemblies=[ + BomRef('my-specific-bom-ref-for-first-assembly'), + XsUri('urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1#some-ref'), + ], + dependencies=[ + BomRef('my-specific-bom-ref-for-dependency'), + ], + vulnerabilities=[ + BomRef('my-specific-bom-ref-for-vulnerability'), + ], + bom_ref='my-specific-bom-ref-for-composition', + ), + Composition( + aggregate=AggregateType.COMPLETE, + ), + ] + ) + +# --- + + all_get_bom_funct_valid = tuple( (n, f) for n, f in getmembers(sys.modules[__name__], isfunction) if n.startswith('get_bom_') and not n.endswith('_invalid') diff --git a/tests/_data/snapshots/enum_AggregateType-1.0.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.0.xml.bin new file mode 100644 index 00000000..acb06612 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.0.xml.bin @@ -0,0 +1,4 @@ + + + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.1.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.1.xml.bin new file mode 100644 index 00000000..55ef5cda --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.1.xml.bin @@ -0,0 +1,4 @@ + + + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.2.json.bin b/tests/_data/snapshots/enum_AggregateType-1.2.json.bin new file mode 100644 index 00000000..8f473bd3 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.2.json.bin @@ -0,0 +1,10 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.2" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_AggregateType-1.2.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.2.xml.bin new file mode 100644 index 00000000..df1938ec --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.2.xml.bin @@ -0,0 +1,6 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.3.json.bin b/tests/_data/snapshots/enum_AggregateType-1.3.json.bin new file mode 100644 index 00000000..1a2301a7 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.3.json.bin @@ -0,0 +1,72 @@ +{ + "compositions": [ + { + "aggregate": "complete", + "dependencies": [ + "dep-0" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-1" + ] + }, + { + "aggregate": "incomplete_first_party_only", + "dependencies": [ + "dep-2" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-4" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-3" + ] + }, + { + "aggregate": "incomplete_third_party_only", + "dependencies": [ + "dep-5" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-7" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-6" + ] + }, + { + "aggregate": "not_specified", + "dependencies": [ + "dep-9" + ] + }, + { + "aggregate": "unknown", + "dependencies": [ + "dep-8" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.3" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_AggregateType-1.3.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.3.xml.bin new file mode 100644 index 00000000..a3b04c11 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.3.xml.bin @@ -0,0 +1,68 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + + + + incomplete + + + + + + incomplete_first_party_only + + + + + + incomplete + + + + + + incomplete + + + + + + incomplete_third_party_only + + + + + + incomplete + + + + + + incomplete + + + + + + not_specified + + + + + + unknown + + + + + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.4.json.bin b/tests/_data/snapshots/enum_AggregateType-1.4.json.bin new file mode 100644 index 00000000..93e553be --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.4.json.bin @@ -0,0 +1,72 @@ +{ + "compositions": [ + { + "aggregate": "complete", + "dependencies": [ + "dep-0" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-1" + ] + }, + { + "aggregate": "incomplete_first_party_only", + "dependencies": [ + "dep-2" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-4" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-3" + ] + }, + { + "aggregate": "incomplete_third_party_only", + "dependencies": [ + "dep-5" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-7" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-6" + ] + }, + { + "aggregate": "not_specified", + "dependencies": [ + "dep-9" + ] + }, + { + "aggregate": "unknown", + "dependencies": [ + "dep-8" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.4" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_AggregateType-1.4.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.4.xml.bin new file mode 100644 index 00000000..a9eca6b9 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.4.xml.bin @@ -0,0 +1,68 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + + + + incomplete + + + + + + incomplete_first_party_only + + + + + + incomplete + + + + + + incomplete + + + + + + incomplete_third_party_only + + + + + + incomplete + + + + + + incomplete + + + + + + not_specified + + + + + + unknown + + + + + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.5.json.bin b/tests/_data/snapshots/enum_AggregateType-1.5.json.bin new file mode 100644 index 00000000..d20784fb --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.5.json.bin @@ -0,0 +1,82 @@ +{ + "compositions": [ + { + "aggregate": "complete", + "dependencies": [ + "dep-0" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-1" + ] + }, + { + "aggregate": "incomplete_first_party_only", + "dependencies": [ + "dep-2" + ] + }, + { + "aggregate": "incomplete_first_party_opensource_only", + "dependencies": [ + "dep-4" + ] + }, + { + "aggregate": "incomplete_first_party_proprietary_only", + "dependencies": [ + "dep-3" + ] + }, + { + "aggregate": "incomplete_third_party_only", + "dependencies": [ + "dep-5" + ] + }, + { + "aggregate": "incomplete_third_party_opensource_only", + "dependencies": [ + "dep-7" + ] + }, + { + "aggregate": "incomplete_third_party_proprietary_only", + "dependencies": [ + "dep-6" + ] + }, + { + "aggregate": "not_specified", + "dependencies": [ + "dep-9" + ] + }, + { + "aggregate": "unknown", + "dependencies": [ + "dep-8" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.5" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_AggregateType-1.5.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.5.xml.bin new file mode 100644 index 00000000..29fd3453 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.5.xml.bin @@ -0,0 +1,72 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + + + + incomplete + + + + + + incomplete_first_party_only + + + + + + incomplete_first_party_opensource_only + + + + + + incomplete_first_party_proprietary_only + + + + + + incomplete_third_party_only + + + + + + incomplete_third_party_opensource_only + + + + + + incomplete_third_party_proprietary_only + + + + + + not_specified + + + + + + unknown + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.6.json.bin b/tests/_data/snapshots/enum_AggregateType-1.6.json.bin new file mode 100644 index 00000000..500e964a --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.6.json.bin @@ -0,0 +1,82 @@ +{ + "compositions": [ + { + "aggregate": "complete", + "dependencies": [ + "dep-0" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-1" + ] + }, + { + "aggregate": "incomplete_first_party_only", + "dependencies": [ + "dep-2" + ] + }, + { + "aggregate": "incomplete_first_party_opensource_only", + "dependencies": [ + "dep-4" + ] + }, + { + "aggregate": "incomplete_first_party_proprietary_only", + "dependencies": [ + "dep-3" + ] + }, + { + "aggregate": "incomplete_third_party_only", + "dependencies": [ + "dep-5" + ] + }, + { + "aggregate": "incomplete_third_party_opensource_only", + "dependencies": [ + "dep-7" + ] + }, + { + "aggregate": "incomplete_third_party_proprietary_only", + "dependencies": [ + "dep-6" + ] + }, + { + "aggregate": "not_specified", + "dependencies": [ + "dep-9" + ] + }, + { + "aggregate": "unknown", + "dependencies": [ + "dep-8" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.6" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_AggregateType-1.6.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.6.xml.bin new file mode 100644 index 00000000..37368396 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.6.xml.bin @@ -0,0 +1,72 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + + + + incomplete + + + + + + incomplete_first_party_only + + + + + + incomplete_first_party_opensource_only + + + + + + incomplete_first_party_proprietary_only + + + + + + incomplete_third_party_only + + + + + + incomplete_third_party_opensource_only + + + + + + incomplete_third_party_proprietary_only + + + + + + not_specified + + + + + + unknown + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/enum_AggregateType-1.7.json.bin b/tests/_data/snapshots/enum_AggregateType-1.7.json.bin new file mode 100644 index 00000000..8348cb41 --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.7.json.bin @@ -0,0 +1,82 @@ +{ + "compositions": [ + { + "aggregate": "complete", + "dependencies": [ + "dep-0" + ] + }, + { + "aggregate": "incomplete", + "dependencies": [ + "dep-1" + ] + }, + { + "aggregate": "incomplete_first_party_only", + "dependencies": [ + "dep-2" + ] + }, + { + "aggregate": "incomplete_first_party_opensource_only", + "dependencies": [ + "dep-4" + ] + }, + { + "aggregate": "incomplete_first_party_proprietary_only", + "dependencies": [ + "dep-3" + ] + }, + { + "aggregate": "incomplete_third_party_only", + "dependencies": [ + "dep-5" + ] + }, + { + "aggregate": "incomplete_third_party_opensource_only", + "dependencies": [ + "dep-7" + ] + }, + { + "aggregate": "incomplete_third_party_proprietary_only", + "dependencies": [ + "dep-6" + ] + }, + { + "aggregate": "not_specified", + "dependencies": [ + "dep-9" + ] + }, + { + "aggregate": "unknown", + "dependencies": [ + "dep-8" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.7.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.7" +} \ No newline at end of file diff --git a/tests/_data/snapshots/enum_AggregateType-1.7.xml.bin b/tests/_data/snapshots/enum_AggregateType-1.7.xml.bin new file mode 100644 index 00000000..7fca8ffc --- /dev/null +++ b/tests/_data/snapshots/enum_AggregateType-1.7.xml.bin @@ -0,0 +1,72 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + + + + incomplete + + + + + + incomplete_first_party_only + + + + + + incomplete_first_party_opensource_only + + + + + + incomplete_first_party_proprietary_only + + + + + + incomplete_third_party_only + + + + + + incomplete_third_party_opensource_only + + + + + + incomplete_third_party_proprietary_only + + + + + + not_specified + + + + + + unknown + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.0.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.0.xml.bin new file mode 100644 index 00000000..acb06612 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.0.xml.bin @@ -0,0 +1,4 @@ + + + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.1.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.1.xml.bin new file mode 100644 index 00000000..55ef5cda --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.1.xml.bin @@ -0,0 +1,4 @@ + + + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.2.json.bin b/tests/_data/snapshots/get_bom_with_compositions-1.2.json.bin new file mode 100644 index 00000000..8f473bd3 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.2.json.bin @@ -0,0 +1,10 @@ +{ + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.2b.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.2" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.2.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.2.xml.bin new file mode 100644 index 00000000..df1938ec --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.2.xml.bin @@ -0,0 +1,6 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.3.json.bin b/tests/_data/snapshots/get_bom_with_compositions-1.3.json.bin new file mode 100644 index 00000000..607110d7 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.3.json.bin @@ -0,0 +1,25 @@ +{ + "compositions": [ + { + "aggregate": "complete" + }, + { + "aggregate": "incomplete", + "assemblies": [ + "my-specific-bom-ref-for-first-assembly", + "urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1#some-ref" + ], + "dependencies": [ + "my-specific-bom-ref-for-dependency" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.3a.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.3" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.3.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.3.xml.bin new file mode 100644 index 00000000..bb72626e --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.3.xml.bin @@ -0,0 +1,21 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + incomplete + + + + + + + + + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.4.json.bin b/tests/_data/snapshots/get_bom_with_compositions-1.4.json.bin new file mode 100644 index 00000000..e0d90424 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.4.json.bin @@ -0,0 +1,25 @@ +{ + "compositions": [ + { + "aggregate": "complete" + }, + { + "aggregate": "incomplete", + "assemblies": [ + "my-specific-bom-ref-for-first-assembly", + "urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1#some-ref" + ], + "dependencies": [ + "my-specific-bom-ref-for-dependency" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.4" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.4.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.4.xml.bin new file mode 100644 index 00000000..edbf5fe4 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.4.xml.bin @@ -0,0 +1,21 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + incomplete + + + + + + + + + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.5.json.bin b/tests/_data/snapshots/get_bom_with_compositions-1.5.json.bin new file mode 100644 index 00000000..e45d35df --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.5.json.bin @@ -0,0 +1,38 @@ +{ + "compositions": [ + { + "aggregate": "complete" + }, + { + "aggregate": "incomplete", + "assemblies": [ + "my-specific-bom-ref-for-first-assembly", + "urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1#some-ref" + ], + "dependencies": [ + "my-specific-bom-ref-for-dependency" + ], + "vulnerabilities": [ + "my-specific-bom-ref-for-vulnerability" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.5.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.5" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.5.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.5.xml.bin new file mode 100644 index 00000000..1fcab7bf --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.5.xml.bin @@ -0,0 +1,28 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + incomplete + + + + + + + + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.6.json.bin b/tests/_data/snapshots/get_bom_with_compositions-1.6.json.bin new file mode 100644 index 00000000..d746d340 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.6.json.bin @@ -0,0 +1,39 @@ +{ + "compositions": [ + { + "aggregate": "complete" + }, + { + "aggregate": "incomplete", + "assemblies": [ + "my-specific-bom-ref-for-first-assembly", + "urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1#some-ref" + ], + "bom-ref": "my-specific-bom-ref-for-composition", + "dependencies": [ + "my-specific-bom-ref-for-dependency" + ], + "vulnerabilities": [ + "my-specific-bom-ref-for-vulnerability" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.6" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.6.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.6.xml.bin new file mode 100644 index 00000000..a8491a1e --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.6.xml.bin @@ -0,0 +1,28 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + incomplete + + + + + + + + + + + + + + val1 + val2 + + diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.7.json.bin b/tests/_data/snapshots/get_bom_with_compositions-1.7.json.bin new file mode 100644 index 00000000..94fea628 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.7.json.bin @@ -0,0 +1,39 @@ +{ + "compositions": [ + { + "aggregate": "complete" + }, + { + "aggregate": "incomplete", + "assemblies": [ + "my-specific-bom-ref-for-first-assembly", + "urn:cdx:f08a6ccd-4dce-4759-bd84-c626675d60a7/1#some-ref" + ], + "bom-ref": "my-specific-bom-ref-for-composition", + "dependencies": [ + "my-specific-bom-ref-for-dependency" + ], + "vulnerabilities": [ + "my-specific-bom-ref-for-vulnerability" + ] + } + ], + "metadata": { + "timestamp": "2023-01-07T13:44:32.312678+00:00" + }, + "properties": [ + { + "name": "key1", + "value": "val1" + }, + { + "name": "key2", + "value": "val2" + } + ], + "serialNumber": "urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac", + "version": 1, + "$schema": "http://cyclonedx.org/schema/bom-1.7.schema.json", + "bomFormat": "CycloneDX", + "specVersion": "1.7" +} \ No newline at end of file diff --git a/tests/_data/snapshots/get_bom_with_compositions-1.7.xml.bin b/tests/_data/snapshots/get_bom_with_compositions-1.7.xml.bin new file mode 100644 index 00000000..321fa0d4 --- /dev/null +++ b/tests/_data/snapshots/get_bom_with_compositions-1.7.xml.bin @@ -0,0 +1,28 @@ + + + + 2023-01-07T13:44:32.312678+00:00 + + + + complete + + + incomplete + + + + + + + + + + + + + + val1 + val2 + + diff --git a/tests/test_enums.py b/tests/test_enums.py index 88ac8e71..0e680f85 100644 --- a/tests/test_enums.py +++ b/tests/test_enums.py @@ -14,6 +14,7 @@ # # SPDX-License-Identifier: Apache-2.0 # Copyright (c) OWASP Foundation. All Rights Reserved. +# Copyright (C) Siemens 2026. All Rights Reserved. import ast from collections.abc import Generator, Iterable @@ -34,8 +35,10 @@ from cyclonedx.exception.serialization import SerializationOfUnsupportedComponentTypeException from cyclonedx.model import AttachedText, ExternalReference, HashType, XsUri from cyclonedx.model.bom import Bom, BomMetaData, DistributionConstraints, TlpClassification +from cyclonedx.model.bom_ref import BomRef from cyclonedx.model.component import Component, Patch, Pedigree from cyclonedx.model.component_evidence import ComponentEvidence, Identity as CEIdentity, Method as CEMethod +from cyclonedx.model.composition import AggregateType, Composition from cyclonedx.model.crypto import ( AlgorithmProperties, CryptoProperties, @@ -514,6 +517,25 @@ def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, super()._test_cases_render(bom, of, sv) +@ddt +class TestEnumAggregateType(_EnumTestCase): + + @idata(set(chain( + dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='aggregateType']"), + dp_cases_from_json_schemas('definitions', 'aggregateType'), + ))) + def test_knows_value(self, value: str) -> None: + super()._test_knows_value(AggregateType, value) + + @named_data(*NAMED_OF_SV) + def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None: + bom = _make_bom(compositions=[ + Composition(aggregate=aggregate, dependencies=[BomRef(f'dep-{i}')]) + for i, aggregate in enumerate(AggregateType) + ]) + super()._test_cases_render(bom, of, sv) + + @ddt class TestEnumLifecyclePhase(_EnumTestCase): diff --git a/tests/test_model_composition.py b/tests/test_model_composition.py new file mode 100644 index 00000000..ab10f21b --- /dev/null +++ b/tests/test_model_composition.py @@ -0,0 +1,87 @@ +# This file is part of CycloneDX Python Library +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# Copyright (C) Siemens 2026. All Rights Reserved. + + +from unittest import TestCase + +from cyclonedx.model import XsUri +from cyclonedx.model.bom_ref import BomRef +from cyclonedx.model.composition import AggregateType, Composition + + +class TestModelComposition(TestCase): + def test_create_default_aggregate(self) -> None: + composition = Composition() + self.assertIs(AggregateType.NOT_SPECIFIED, composition.aggregate) + self.assertIsNone(composition.bom_ref) + self.assertEqual(0, len(composition.assemblies)) + self.assertEqual(0, len(composition.dependencies)) + self.assertEqual(0, len(composition.vulnerabilities)) + + def test_create_full(self) -> None: + composition = Composition( + aggregate=AggregateType.INCOMPLETE, + assemblies=[BomRef('assembly-a'), XsUri('urn:cdx:xyz/1#ref-b')], + dependencies=[BomRef('dep-a')], + vulnerabilities=[BomRef('CVE-1')], + bom_ref='comp-1', + ) + self.assertIs(AggregateType.INCOMPLETE, composition.aggregate) + self.assertEqual('comp-1', str(composition.bom_ref)) + self.assertEqual(2, len(composition.assemblies)) + self.assertEqual(1, len(composition.dependencies)) + self.assertEqual(1, len(composition.vulnerabilities)) + + def test_update_aggregate(self) -> None: + composition = Composition(aggregate=AggregateType.COMPLETE) + composition.aggregate = AggregateType.UNKNOWN + self.assertIs(AggregateType.UNKNOWN, composition.aggregate) + + def test_equal(self) -> None: + a = Composition(aggregate=AggregateType.COMPLETE, dependencies=[BomRef('x')]) + b = Composition(aggregate=AggregateType.COMPLETE, dependencies=[BomRef('x')]) + c = Composition(aggregate=AggregateType.INCOMPLETE, dependencies=[BomRef('x')]) + self.assertEqual(a, b) + self.assertNotEqual(a, c) + self.assertNotEqual(a, 'not-a-composition') + + def test_equal_ignores_bom_ref(self) -> None: + # bom-ref is an identity-only attribute; it does not affect equality + a = Composition(aggregate=AggregateType.COMPLETE, bom_ref='one') + b = Composition(aggregate=AggregateType.COMPLETE, bom_ref='two') + self.assertEqual(a, b) + + def test_hashable(self) -> None: + composition = Composition(aggregate=AggregateType.COMPLETE) + self.assertIsInstance(hash(composition), int) + + def test_sort(self) -> None: + compositions = [ + Composition(aggregate=AggregateType.INCOMPLETE), + Composition(aggregate=AggregateType.COMPLETE), + ] + sorted_compositions = sorted(compositions) + self.assertIs(AggregateType.COMPLETE, sorted_compositions[0].aggregate) + self.assertIs(AggregateType.INCOMPLETE, sorted_compositions[1].aggregate) + + def test_assemblies_distinguishes_ref_and_bom_link(self) -> None: + composition = Composition( + assemblies=[BomRef('plain-ref'), XsUri('urn:cdx:xyz/1#ref-b')], + ) + kinds = {type(a) for a in composition.assemblies} + self.assertIn(BomRef, kinds) + self.assertIn(XsUri, kinds)