|
| 1 | +# This file is part of CycloneDX Python Library |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# SPDX-License-Identifier: Apache-2.0 |
| 16 | +# Copyright (c) OWASP Foundation. All Rights Reserved. |
| 17 | + |
| 18 | +from unittest import TestCase |
| 19 | + |
| 20 | +from cyclonedx.exception.model import LicenseExpressionAlongWithOthersException, UnknownComponentDependencyException |
| 21 | +from cyclonedx.model.bom import Bom |
| 22 | +from cyclonedx.model.component import Component |
| 23 | +from cyclonedx.model.dependency import Dependency |
| 24 | +from cyclonedx.model.license import DisjunctiveLicense, LicenseExpression |
| 25 | +from cyclonedx.validation.model import ModelValidator |
| 26 | + |
| 27 | + |
| 28 | +class TestModelValidator(TestCase): |
| 29 | + def test_validate_multiple_errors(self) -> None: |
| 30 | + bom = Bom() |
| 31 | + # Error 1: Component with multiple licenses including expression |
| 32 | + comp = Component(name='test', version='1.0', bom_ref='test-comp') |
| 33 | + comp.licenses.update([ |
| 34 | + DisjunctiveLicense(id='MIT'), |
| 35 | + LicenseExpression(value='Apache-2.0 OR MIT') |
| 36 | + ]) |
| 37 | + bom.components.add(comp) |
| 38 | + |
| 39 | + # Error 2: Unknown dependency reference |
| 40 | + bom.dependencies.add(Dependency('test-comp', dependencies=[Dependency('non-existent-ref')])) |
| 41 | + |
| 42 | + validator = ModelValidator() |
| 43 | + errors = list(validator.validate(bom)) |
| 44 | + |
| 45 | + self.assertEqual(len(errors), 2) |
| 46 | + error_types = [type(e.data) for e in errors] |
| 47 | + self.assertIn(UnknownComponentDependencyException, error_types) |
| 48 | + self.assertIn(LicenseExpressionAlongWithOthersException, error_types) |
| 49 | + |
| 50 | + def test_validate_clean_bom(self) -> None: |
| 51 | + bom = Bom() |
| 52 | + bom.metadata.component = Component(name='root', version='1.0', bom_ref='root') |
| 53 | + validator = ModelValidator() |
| 54 | + errors = list(validator.validate(bom)) |
| 55 | + self.assertEqual(len(errors), 0) |
| 56 | + |
| 57 | + def test_bom_validate_deprecated_behavior(self) -> None: |
| 58 | + bom = Bom() |
| 59 | + bom.metadata.component = Component(name='root', version='1.0', bom_ref='root') |
| 60 | + |
| 61 | + # Verify side effect: register_dependency is called by Bom.validate |
| 62 | + self.assertEqual(len(bom.dependencies), 0) |
| 63 | + with self.assertWarns(DeprecationWarning): |
| 64 | + bom.validate() |
| 65 | + self.assertEqual(len(bom.dependencies), 1) |
| 66 | + self.assertEqual(next(iter(bom.dependencies)).ref.value, 'root') |
| 67 | + |
| 68 | + def test_model_validator_no_side_effects(self) -> None: |
| 69 | + bom = Bom() |
| 70 | + bom.metadata.component = Component(name='root', version='1.0', bom_ref='root') |
| 71 | + |
| 72 | + # Verify NO side effect: ModelValidator should not call register_dependency |
| 73 | + self.assertEqual(len(bom.dependencies), 0) |
| 74 | + validator = ModelValidator() |
| 75 | + list(validator.validate(bom)) |
| 76 | + self.assertEqual(len(bom.dependencies), 0) |
0 commit comments