1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
# 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) OWASP Foundation. All Rights Reserved.
from unittest import TestCase
from ddt import ddt, named_data
from cyclonedx.exception.model import InvalidCreIdException
from cyclonedx.model.definition import CreId, Definitions, Level, Requirement, Standard
class TestModelDefinitions(TestCase):
def test_init(self) -> Definitions:
s = Standard(name='test-standard')
dr = Definitions(
standards=(s, ),
)
self.assertEqual(1, len(dr.standards))
self.assertIs(s, tuple(dr.standards)[0])
return dr
def test_filled(self) -> None:
dr = self.test_init()
self.assertIsNotNone(dr.standards)
self.assertTrue(dr)
def test_empty(self) -> None:
dr = Definitions()
self.assertIsNotNone(dr.standards)
self.assertEqual(0, len(dr.standards))
self.assertFalse(dr)
def test_unequal_different_type(self) -> None:
dr = Definitions()
self.assertFalse(dr == 'other')
def test_equal_self(self) -> None:
dr = Definitions()
dr.standards.add(Standard(name='my-standard'))
self.assertTrue(dr == dr)
def test_unequal(self) -> None:
dr1 = Definitions()
dr1.standards.add(Standard(name='my-standard'))
tr2 = Definitions()
self.assertFalse(dr1 == tr2)
def test_equal(self) -> None:
s = Standard(name='my-standard')
dr1 = Definitions()
dr1.standards.add(s)
tr2 = Definitions()
tr2.standards.add(s)
self.assertTrue(dr1 == tr2)
@ddt
class TestModelCreId(TestCase):
def test_different(self) -> None:
id1 = CreId('CRE:123-456')
id2 = CreId('CRE:987-654')
self.assertNotEqual(id(id1), id(id2))
self.assertNotEqual(hash(id1), hash(id2))
self.assertFalse(id1 == id2)
def test_same(self) -> None:
id1 = CreId('CRE:123-456')
id2 = CreId('CRE:123-456')
self.assertNotEqual(id(id1), id(id2))
self.assertEqual(hash(id1), hash(id2))
self.assertTrue(id1 == id2)
def test_invalid_no_id(self) -> None:
with self.assertRaises(TypeError):
CreId()
@named_data(
['empty', ''],
['arbitrary string', 'some string'],
['missing prefix', '123-456'],
['additional part', 'CRE:123-456-789'],
['no numbers', 'CRE:abc-def'],
['no delimiter', 'CRE:123456'],
)
def test_invalid_id(self, wrong_id: str) -> None:
with self.assertRaises(InvalidCreIdException):
CreId(wrong_id)
class TestModelRequirements(TestCase):
def test_bom_ref_is_set_from_value(self) -> None:
r = Requirement(bom_ref='123-456')
self.assertIsNotNone(r.bom_ref)
self.assertEqual('123-456', r.bom_ref.value)
def test_bom_ref_is_set_if_none_given(self) -> None:
r = Requirement()
self.assertIsNotNone(r.bom_ref)
class TestModelLevel(TestCase):
def test_bom_ref_is_set_from_value(self) -> None:
r = Level(bom_ref='123-456')
self.assertIsNotNone(r.bom_ref)
self.assertEqual('123-456', r.bom_ref.value)
def test_bom_ref_is_set_if_none_given(self) -> None:
r = Level()
self.assertIsNotNone(r.bom_ref)
class TestModelStandard(TestCase):
def test_bom_ref_is_set_from_value(self) -> None:
r = Standard(bom_ref='123-456')
self.assertIsNotNone(r.bom_ref)
self.assertEqual('123-456', r.bom_ref.value)
def test_bom_ref_is_set_if_none_given(self) -> None:
r = Standard()
self.assertIsNotNone(r.bom_ref)
|