Skip to content

Commit 75ecd2a

Browse files
committed
Add unit test for cyclonedx
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent b65117a commit 75ecd2a

1 file changed

Lines changed: 172 additions & 0 deletions

File tree

scanpipe/tests/test_cyclonedx.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# http://nexb.com and https://github.com/nexB/scancode.io
4+
# The ScanCode.io software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode.io is provided as-is without warranties.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
16+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
17+
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
18+
# for any legal advice.
19+
#
20+
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/scancode.io for support and download.
22+
23+
import json
24+
from pathlib import Path
25+
26+
from django.test import TestCase
27+
28+
from hoppr_cyclonedx_models.cyclonedx_1_4 import (
29+
CyclonedxSoftwareBillOfMaterialsStandard as Bom_1_4,
30+
)
31+
from hoppr_cyclonedx_models.cyclonedx_1_4 import License
32+
from hoppr_cyclonedx_models.cyclonedx_1_4 import License1
33+
from hoppr_cyclonedx_models.cyclonedx_1_4 import LicenseChoice
34+
from hoppr_cyclonedx_models.cyclonedx_1_4 import LicenseChoice1
35+
36+
from scanpipe import cyclonedx
37+
38+
39+
class CycloneDXUnitTest(TestCase):
40+
bom_file = Path(__file__).parent / "data/cyclonedx/nested.bom.json"
41+
bom_json = bom_file.read_text()
42+
bom_parsed = json.loads(bom_json)
43+
bom = Bom_1_4(**bom_parsed)
44+
45+
def test_scanpipe_cyclonedx_get_bom(self):
46+
bom = cyclonedx.get_bom(self.bom_parsed)
47+
result = bom.json(exclude_unset=True, by_alias=True)
48+
49+
self.assertJSONEqual(result, self.bom_json)
50+
51+
def test_scanpipe_cyclonedx_bom_attributes_to_dict(self):
52+
component_level1 = self.bom.components[0]
53+
component_level2 = component_level1.components[0]
54+
components = component_level2.components
55+
56+
expected = [
57+
{
58+
"type": "library",
59+
"bom-ref": "pkg:pypi/fictional@9.10.2",
60+
"name": "fictional",
61+
"version": "0.10.2",
62+
"hashes": [
63+
{
64+
"alg": "SHA-256",
65+
"content": (
66+
"960343ae5bfb6a3c6e736a764057db0e"
67+
"6a0e05e338b5630894a5f779cabb4f9b"
68+
),
69+
}
70+
],
71+
"licenses": [
72+
{
73+
"expression": (
74+
"LGPL-3.0-or-later AND "
75+
"LicenseRef-scancode-openssl-exception-lgpl3.0plus"
76+
)
77+
}
78+
],
79+
"purl": "pkg:pypi/fictional@9.10.2",
80+
"externalReferences": [
81+
{
82+
"url": "https://cyclonedx.org",
83+
"comment": "No comment",
84+
"type": "distribution",
85+
"hashes": [
86+
{
87+
"alg": "SHA-256",
88+
"content": (
89+
"960343ae5bfb6a3c6e736a764057d"
90+
"b0e6a0e05e338b5630894a5f779cabb4f9b"
91+
),
92+
}
93+
],
94+
}
95+
],
96+
}
97+
]
98+
result = cyclonedx.bom_attributes_to_dict(components)
99+
100+
self.assertEqual(result, expected)
101+
102+
def test_scanpipe_cyclonedx_recursive_component_collector(self):
103+
component_level1 = self.bom.components[0]
104+
component_level2 = component_level1.components[0]
105+
component_level3 = component_level2.components[0]
106+
107+
expected = [
108+
{
109+
"cdx_package": component_level1,
110+
"nested_components": cyclonedx.bom_attributes_to_dict(
111+
component_level1.components
112+
),
113+
},
114+
{
115+
"cdx_package": component_level2,
116+
"nested_components": cyclonedx.bom_attributes_to_dict(
117+
component_level2.components
118+
),
119+
},
120+
{"cdx_package": component_level3, "nested_components": {}},
121+
]
122+
result = cyclonedx.recursive_component_collector(self.bom.components, [])
123+
124+
self.assertEqual(result, expected)
125+
126+
def test_scanpipe_cyclonedx_resolve_license(self):
127+
hopper_cdx_licensechoice_id = LicenseChoice(
128+
__root__=LicenseChoice1(license=License1(__root__=License(id="OFL-1.1")))
129+
)
130+
license_choice_dict = json.loads(
131+
hopper_cdx_licensechoice_id.json(exclude_unset=True, by_alias=True)
132+
)
133+
134+
result = cyclonedx.resolve_license(license_choice_dict)
135+
expected = "OFL-1.1"
136+
137+
self.assertEqual(result, expected)
138+
139+
def test_scanpipe_cyclonedx_get_declared_licenses(self):
140+
component = self.bom.components[0]
141+
142+
result = cyclonedx.get_declared_licenses(component.licenses)
143+
expected = "OFL-1.1\nApache-2.0"
144+
145+
self.assertEqual(result, expected)
146+
147+
def test_scanpipe_cyclonedx_get_checksums(self):
148+
component = self.bom.components[0]
149+
150+
result = cyclonedx.get_checksums(component)
151+
expected = {
152+
"sha256": "806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"
153+
}
154+
155+
self.assertEqual(result, expected)
156+
157+
def test_scanpipe_cyclonedx_get_external_refrences(self):
158+
component = self.bom.components[0]
159+
result = cyclonedx.get_external_refrences(component.externalReferences)
160+
expected = {
161+
"vcs": ["https://cyclonedx.org/vcs"],
162+
"issue-tracker": ["https://cyclonedx.org/issue-tracker"],
163+
"website": ["https://cyclonedx.org/website"],
164+
"advisories": ["https://cyclonedx.org/advisories"],
165+
"bom": ["https://cyclonedx.org/bom"],
166+
"mailing-list": ["https://cyclonedx.org/mailing-list"],
167+
}
168+
169+
self.assertEqual(result, expected)
170+
171+
def test_scanpipe_cyclonedx_validate_document(self):
172+
cyclonedx.validate_document(self.bom_json)

0 commit comments

Comments
 (0)