|
| 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.model.bom_ref import BomRef |
| 21 | +from cyclonedx.model.crypto import ( |
| 22 | + AlgorithmProperties, |
| 23 | + CertificateProperties, |
| 24 | + CryptoPrimitive, |
| 25 | + Ikev2TransformTypes, |
| 26 | + ProtocolProperties, |
| 27 | + ProtocolPropertiesType, |
| 28 | + RelatedCryptoMaterialProperties, |
| 29 | + RelatedCryptoMaterialSecuredBy, |
| 30 | + RelatedCryptoMaterialType, |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +class TestModelAlgorithmProperties(TestCase): |
| 35 | + |
| 36 | + def test_algorithm_properties_sorting(self) -> None: |
| 37 | + """Test that AlgorithmProperties instances can be sorted without triggering TypeError""" |
| 38 | + algo1 = AlgorithmProperties(primitive=CryptoPrimitive.HASH, classical_security_level=128) |
| 39 | + algo2 = AlgorithmProperties(primitive=CryptoPrimitive.SIGNATURE, classical_security_level=256) |
| 40 | + algo3 = AlgorithmProperties(primitive=CryptoPrimitive.BLOCK_CIPHER, classical_security_level=192) |
| 41 | + |
| 42 | + # This should not raise TypeError: '<' not supported between instances |
| 43 | + algo_list = [algo2, algo3, algo1] |
| 44 | + sorted_algos = sorted(algo_list) |
| 45 | + self.assertEqual(len(sorted_algos), 3) |
| 46 | + |
| 47 | + |
| 48 | +class TestModelCertificateProperties(TestCase): |
| 49 | + |
| 50 | + def test_certificate_properties_sorting(self) -> None: |
| 51 | + """Test that CertificateProperties instances can be sorted without triggering TypeError""" |
| 52 | + cert1 = CertificateProperties(subject_name='CN=Test1', certificate_format='X.509') |
| 53 | + cert2 = CertificateProperties(subject_name='CN=Test2', certificate_format='PEM') |
| 54 | + cert3 = CertificateProperties(subject_name='CN=Test3', certificate_format='DER') |
| 55 | + |
| 56 | + # This should not raise TypeError: '<' not supported between instances |
| 57 | + cert_list = [cert2, cert3, cert1] |
| 58 | + sorted_certs = sorted(cert_list) |
| 59 | + self.assertEqual(len(sorted_certs), 3) |
| 60 | + |
| 61 | + |
| 62 | +class TestModelRelatedCryptoMaterialSecuredBy(TestCase): |
| 63 | + |
| 64 | + def test_related_crypto_material_secured_by_sorting(self) -> None: |
| 65 | + """Test that RelatedCryptoMaterialSecuredBy instances can be sorted without triggering TypeError""" |
| 66 | + secured1 = RelatedCryptoMaterialSecuredBy(mechanism='HSM', algorithm_ref=BomRef('algo1')) |
| 67 | + secured2 = RelatedCryptoMaterialSecuredBy(mechanism='TPM', algorithm_ref=BomRef('algo2')) |
| 68 | + secured3 = RelatedCryptoMaterialSecuredBy(mechanism='Software', algorithm_ref=BomRef('algo3')) |
| 69 | + |
| 70 | + # This should not raise TypeError: '<' not supported between instances |
| 71 | + secured_list = [secured3, secured1, secured2] |
| 72 | + sorted_secured = sorted(secured_list) |
| 73 | + self.assertEqual(len(sorted_secured), 3) |
| 74 | + |
| 75 | + |
| 76 | +class TestModelRelatedCryptoMaterialProperties(TestCase): |
| 77 | + |
| 78 | + def test_related_crypto_material_properties_sorting(self) -> None: |
| 79 | + """Test that RelatedCryptoMaterialProperties instances can be sorted without triggering TypeError""" |
| 80 | + material1 = RelatedCryptoMaterialProperties( |
| 81 | + type=RelatedCryptoMaterialType.KEY, |
| 82 | + id='key1', |
| 83 | + size=256 |
| 84 | + ) |
| 85 | + material2 = RelatedCryptoMaterialProperties( |
| 86 | + type=RelatedCryptoMaterialType.PRIVATE_KEY, |
| 87 | + id='key2', |
| 88 | + size=512 |
| 89 | + ) |
| 90 | + material3 = RelatedCryptoMaterialProperties( |
| 91 | + type=RelatedCryptoMaterialType.PUBLIC_KEY, |
| 92 | + id='key3', |
| 93 | + size=1024 |
| 94 | + ) |
| 95 | + |
| 96 | + # This should not raise TypeError: '<' not supported between instances |
| 97 | + material_list = [material3, material1, material2] |
| 98 | + sorted_materials = sorted(material_list) |
| 99 | + self.assertEqual(len(sorted_materials), 3) |
| 100 | + |
| 101 | + |
| 102 | +class TestModelIkev2TransformTypes(TestCase): |
| 103 | + |
| 104 | + def test_ikev2_transform_types_sorting(self) -> None: |
| 105 | + """Test that Ikev2TransformTypes instances can be sorted without triggering TypeError""" |
| 106 | + ikev2_1 = Ikev2TransformTypes( |
| 107 | + encr=[BomRef('encr1')], |
| 108 | + esn=True |
| 109 | + ) |
| 110 | + ikev2_2 = Ikev2TransformTypes( |
| 111 | + encr=[BomRef('encr2')], |
| 112 | + esn=False |
| 113 | + ) |
| 114 | + ikev2_3 = Ikev2TransformTypes( |
| 115 | + encr=[BomRef('encr3')], |
| 116 | + esn=True |
| 117 | + ) |
| 118 | + |
| 119 | + # This should not raise TypeError: '<' not supported between instances |
| 120 | + ikev2_list = [ikev2_3, ikev2_1, ikev2_2] |
| 121 | + sorted_ikev2 = sorted(ikev2_list) |
| 122 | + self.assertEqual(len(sorted_ikev2), 3) |
| 123 | + |
| 124 | + |
| 125 | +class TestModelProtocolProperties(TestCase): |
| 126 | + |
| 127 | + def test_protocol_properties_sorting(self) -> None: |
| 128 | + """Test that ProtocolProperties instances can be sorted without triggering TypeError""" |
| 129 | + proto1 = ProtocolProperties(type=ProtocolPropertiesType.TLS, version='1.2') |
| 130 | + proto2 = ProtocolProperties(type=ProtocolPropertiesType.SSH, version='2.0') |
| 131 | + proto3 = ProtocolProperties(type=ProtocolPropertiesType.IPSEC, version='1.0') |
| 132 | + |
| 133 | + # This should not raise TypeError: '<' not supported between instances |
| 134 | + proto_list = [proto3, proto1, proto2] |
| 135 | + sorted_protos = sorted(proto_list) |
| 136 | + self.assertEqual(len(sorted_protos), 3) |
0 commit comments