Skip to content

Commit

Permalink
test filtering
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Horton <paul.horton@owasp.org>
  • Loading branch information
madpah committed Apr 5, 2024
1 parent b23df1f commit 14f699f
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 4 deletions.
17 changes: 15 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.

import re
from os import getenv, path
from os.path import join
from typing import TYPE_CHECKING, Any, Generator, Iterable, List, Optional, TypeVar, Union
Expand Down Expand Up @@ -160,6 +160,19 @@ def _make_unique(self) -> str:
}


def mksname(purpose: Union[Any], sv: SchemaVersion, f: OutputFormat) -> str:
_LIMIT_GET_BOM_BY_VERSION_REGEX = re.compile(r'^get_bom_(?P<sv>v(?P<major_version>1)_(?P<minor_version>[0-6]))?(.*)$')

def mksname(purpose: Union[Any], sv: SchemaVersion, f: OutputFormat) -> Optional[str]:
purpose = purpose if isinstance(purpose, str) else purpose.__name__
restrict_to_schema = _LIMIT_GET_BOM_BY_VERSION_REGEX.match(purpose)
if restrict_to_schema:
mg = restrict_to_schema.groupdict()
if mg.get('sv') is not None:
# Restrict only to the schema version in the purpose or greater
restricted_to_sv = SchemaVersion.from_version(f'{mg.get("major_version")}.{mg.get("minor_version")}')
if sv >= restricted_to_sv:
return f'{purpose}-{sv.to_version()}.{_SNAME_EXT[f]}'
else:
return None

return f'{purpose}-{sv.to_version()}.{_SNAME_EXT[f]}'
7 changes: 5 additions & 2 deletions tests/_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ def get_component_crypto_asset_protocol_tls_v13(
)


def get_bom_v16_with_crypto() -> Bom:
return _make_bom(components=[get_component_crypto_asset_protocol_tls_v13()])
def get_bom_v1_6_with_crypto() -> Bom:
c = get_component_crypto_asset_protocol_tls_v13()
b = _make_bom(components=[c])
b.register_dependency(c)
return b


def get_bom_with_component_setuptools_no_component_version() -> Bom:
Expand Down
108 changes: 108 additions & 0 deletions tests/_data/snapshots/get_bom_v1_6_with_crypto-1.6.json.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"components": [
{
"bom-ref": "26b1ce0f-bec6-4bfe-9db1-03b75a4ed1ec",
"cryptoProperties": {
"assetType": "protocol",
"oid": "an-oid-here",
"protocolProperties": {
"cipherSuites": [
{
"identifiers": [
"TLS_AES_128_CCM_8_SHA256"
],
"name": "TLS_AES_128_CCM_8_SHA256"
},
{
"identifiers": [
"TLS_AES_128_CCM_SHA256"
],
"name": "TLS_AES_128_CCM_SHA256"
},
{
"identifiers": [
"TLS_AES_128_GCM_SHA256"
],
"name": "TLS_AES_128_GCM_SHA256"
},
{
"identifiers": [
"TLS_AES_256_GCM_SHA384"
],
"name": "TLS_AES_256_GCM_SHA384"
},
{
"identifiers": [
"TLS_CHACHA20_POLY1305_SHA256"
],
"name": "TLS_CHACHA20_POLY1305_SHA256"
}
],
"type": "tls",
"version": "1.3"
}
},
"name": "TLS",
"tags": [
"protocl",
"tls"
],
"type": "cryptographic-asset",
"version": "v1.3"
}
],
"dependencies": [
{
"ref": "26b1ce0f-bec6-4bfe-9db1-03b75a4ed1ec"
}
],
"metadata": {
"timestamp": "2023-01-07T13:44:32.312678+00:00",
"tools": [
{
"externalReferences": [
{
"type": "build-system",
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/actions"
},
{
"type": "distribution",
"url": "https://pypi.org/project/cyclonedx-python-lib/"
},
{
"type": "documentation",
"url": "https://cyclonedx-python-library.readthedocs.io/"
},
{
"type": "issue-tracker",
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/issues"
},
{
"type": "license",
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE"
},
{
"type": "release-notes",
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md"
},
{
"type": "vcs",
"url": "https://github.com/CycloneDX/cyclonedx-python-lib"
},
{
"type": "website",
"url": "https://github.com/CycloneDX/cyclonedx-python-lib/#readme"
}
],
"name": "cyclonedx-python-lib",
"vendor": "CycloneDX",
"version": "TESTING"
}
]
},
"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"
}
92 changes: 92 additions & 0 deletions tests/_data/snapshots/get_bom_v1_6_with_crypto-1.6.xml.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" ?>
<bom xmlns="http://cyclonedx.org/schema/bom/1.6" serialNumber="urn:uuid:1441d33a-e0fc-45b5-af3b-61ee52a88bac" version="1">
<metadata>
<timestamp>2023-01-07T13:44:32.312678+00:00</timestamp>
<tools>
<tool>
<vendor>CycloneDX</vendor>
<name>cyclonedx-python-lib</name>
<version>TESTING</version>
<externalReferences>
<reference type="build-system">
<url>https://github.com/CycloneDX/cyclonedx-python-lib/actions</url>
</reference>
<reference type="distribution">
<url>https://pypi.org/project/cyclonedx-python-lib/</url>
</reference>
<reference type="documentation">
<url>https://cyclonedx-python-library.readthedocs.io/</url>
</reference>
<reference type="issue-tracker">
<url>https://github.com/CycloneDX/cyclonedx-python-lib/issues</url>
</reference>
<reference type="license">
<url>https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/LICENSE</url>
</reference>
<reference type="release-notes">
<url>https://github.com/CycloneDX/cyclonedx-python-lib/blob/main/CHANGELOG.md</url>
</reference>
<reference type="vcs">
<url>https://github.com/CycloneDX/cyclonedx-python-lib</url>
</reference>
<reference type="website">
<url>https://github.com/CycloneDX/cyclonedx-python-lib/#readme</url>
</reference>
</externalReferences>
</tool>
</tools>
</metadata>
<components>
<component type="cryptographic-asset" bom-ref="26b1ce0f-bec6-4bfe-9db1-03b75a4ed1ec">
<name>TLS</name>
<version>v1.3</version>
<cryptoProperties>
<assetType>protocol</assetType>
<protocolProperties>
<type>tls</type>
<version>1.3</version>
<cipherSuites>
<cipherSuite>
<name>TLS_AES_128_CCM_8_SHA256</name>
<identifiers>
<identifier>TLS_AES_128_CCM_8_SHA256</identifier>
</identifiers>
</cipherSuite>
<cipherSuite>
<name>TLS_AES_128_CCM_SHA256</name>
<identifiers>
<identifier>TLS_AES_128_CCM_SHA256</identifier>
</identifiers>
</cipherSuite>
<cipherSuite>
<name>TLS_AES_128_GCM_SHA256</name>
<identifiers>
<identifier>TLS_AES_128_GCM_SHA256</identifier>
</identifiers>
</cipherSuite>
<cipherSuite>
<name>TLS_AES_256_GCM_SHA384</name>
<identifiers>
<identifier>TLS_AES_256_GCM_SHA384</identifier>
</identifiers>
</cipherSuite>
<cipherSuite>
<name>TLS_CHACHA20_POLY1305_SHA256</name>
<identifiers>
<identifier>TLS_CHACHA20_POLY1305_SHA256</identifier>
</identifiers>
</cipherSuite>
</cipherSuites>
</protocolProperties>
<oid>an-oid-here</oid>
</cryptoProperties>
<tags>
<tag>protocl</tag>
<tag>tls</tag>
</tags>
</component>
</components>
<dependencies>
<dependency ref="26b1ce0f-bec6-4bfe-9db1-03b75a4ed1ec"/>
</dependencies>
</bom>
2 changes: 2 additions & 0 deletions tests/test_output_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ def test_unsupported_schema_raises(self, sv: SchemaVersion) -> None:
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_valid(self, get_bom: Callable[[], Bom], sv: SchemaVersion, *_: Any, **__: Any) -> None:
snapshot_name = mksname(get_bom, sv, OutputFormat.JSON)
if snapshot_name is None:
return
bom = get_bom()
json = BY_SCHEMA_VERSION[sv](bom).output_as_string(indent=2)
try:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_output_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class TestOutputXml(TestCase, SnapshotMixin):
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_valid(self, get_bom: Callable[[], Bom], sv: SchemaVersion, *_: Any, **__: Any) -> None:
snapshot_name = mksname(get_bom, sv, OutputFormat.XML)
if snapshot_name is None:
return
bom = get_bom()
xml = BY_SCHEMA_VERSION[sv](bom).output_as_string(indent=2)
try:
Expand Down

0 comments on commit 14f699f

Please sign in to comment.