-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtest_enums.py
489 lines (401 loc) · 18.7 KB
/
test_enums.py
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# 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 enum import Enum
from itertools import chain
from json import load as json_load
from typing import Any, Generator, Iterable, Tuple, Type
from unittest import TestCase
from unittest.mock import patch
from warnings import warn
from xml.etree.ElementTree import parse as xml_parse # nosec B405
from ddt import ddt, idata, named_data
from cyclonedx.exception import MissingOptionalDependencyException
from cyclonedx.exception.serialization import SerializationOfUnsupportedComponentTypeException
from cyclonedx.model import AttachedText, ExternalReference, HashType, XsUri
from cyclonedx.model.bom import Bom
from cyclonedx.model.component import Component, Patch, Pedigree
from cyclonedx.model.issue import IssueType
from cyclonedx.model.license import DisjunctiveLicense
from cyclonedx.model.service import Data, Service
from cyclonedx.model.vulnerability import (
BomTarget,
BomTargetVersionRange,
Vulnerability,
VulnerabilityAnalysis,
VulnerabilityRating,
)
from cyclonedx.output import make_outputter
from cyclonedx.schema import OutputFormat, SchemaVersion
from cyclonedx.schema._res import BOM_JSON as SCHEMA_JSON, BOM_XML as SCHEMA_XML
from cyclonedx.validation import make_schemabased_validator
from tests import SnapshotMixin
from tests._data.models import _make_bom
# region SUT: all the enums
from cyclonedx.model import ( # isort:skip
DataFlow,
Encoding,
ExternalReferenceType,
HashAlgorithm,
)
from cyclonedx.model.component import ( # isort:skip
ComponentScope,
ComponentType,
PatchClassification,
)
from cyclonedx.model.impact_analysis import ( # isort:skip
ImpactAnalysisAffectedStatus,
ImpactAnalysisJustification,
ImpactAnalysisResponse,
ImpactAnalysisState,
)
from cyclonedx.model.issue import ( # isort:skip
IssueClassification,
)
from cyclonedx.model.vulnerability import ( # isort:skip
VulnerabilityScoreSource,
VulnerabilitySeverity,
)
# endregion SUT
SCHEMA_NS = '{http://www.w3.org/2001/XMLSchema}'
def dp_cases_from_xml_schema(sf: str, xpath: str) -> Generator[str, None, None]:
for el in xml_parse(sf).iterfind(f'{xpath}/{SCHEMA_NS}restriction/{SCHEMA_NS}enumeration'): # nosec B314
yield el.get('value')
def dp_cases_from_xml_schemas(xpath: str) -> Generator[str, None, None]:
for sf in SCHEMA_XML.values():
if sf is None:
continue
yield from dp_cases_from_xml_schema(sf, xpath)
def dp_cases_from_json_schema(sf: str, jsonpointer: Iterable[str]) -> Generator[str, None, None]:
with open(sf) as sfh:
data = json_load(sfh)
try:
for pp in jsonpointer:
data = data[pp]
except KeyError:
return
for value in data['enum']:
yield value
def dp_cases_from_json_schemas(*jsonpointer: str) -> Generator[str, None, None]:
for sf in SCHEMA_JSON.values():
if sf is None:
continue
yield from dp_cases_from_json_schema(sf, jsonpointer)
UNSUPPORTED_OF_SV = frozenset([
(OutputFormat.JSON, SchemaVersion.V1_1),
(OutputFormat.JSON, SchemaVersion.V1_0),
])
NAMED_OF_SV = tuple(
(f'{of.name}-{sv.to_version()}', of, sv)
for of in OutputFormat
for sv in SchemaVersion
if (of, sv) not in UNSUPPORTED_OF_SV
)
class _EnumTestCase(TestCase, SnapshotMixin):
def _test_knows_value(self, enum: Type[Enum], value: str) -> None:
ec = enum(value) # throws valueError if value unknown
self.assertTrue(ec.name) # TODO test for an expected name
@staticmethod
def __str_rmp(s: str, p: str) -> str:
# str.removeprefix() for all py versions
pl = len(p)
return s[pl:] if s[:pl] == p else s
def _test_cases_render(self, bom: Bom, of: OutputFormat, sv: SchemaVersion) -> None:
snapshot_name = f'enum_{self.__str_rmp(type(self).__name__, "TestEnum")}-{sv.to_version()}.{of.name.lower()}'
output = make_outputter(bom, of, sv).output_as_string(indent=2)
try:
validation_errors = make_schemabased_validator(of, sv).validate_str(output)
except MissingOptionalDependencyException:
warn('!!! skipped schema validation',
category=UserWarning, stacklevel=0)
else:
self.assertIsNone(validation_errors)
self.assertEqualSnapshot(output, snapshot_name)
@ddt
class TestEnumDataFlow(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='dataFlowType']"),
dp_cases_from_json_schemas('definitions', 'dataFlowDirection'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(DataFlow, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(services=[Service(name='dummy', bom_ref='dummy', data=(
Data(flow=df, classification=df.name)
# DataClassification(flow=df, classification=df.name)
for df in DataFlow
))])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumEncoding(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='encoding']"),
dp_cases_from_json_schemas('definitions', 'attachment', 'properties', 'encoding'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(Encoding, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(components=[Component(name='dummy', type=ComponentType.LIBRARY, bom_ref='dummy', licenses=(
DisjunctiveLicense(name=f'att.encoding: {encoding.name}', text=AttachedText(
content=f'att.encoding: {encoding.name}', encoding=encoding
)) for encoding in Encoding
))])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumExternalReferenceType(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='externalReferenceType']"),
dp_cases_from_json_schemas('definitions', 'externalReference', 'properties', 'type'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ExternalReferenceType, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(components=[
Component(name='dummy', type=ComponentType.LIBRARY, bom_ref='dummy', external_references=(
ExternalReference(type=extref, url=XsUri(f'tests/{extref.name}'))
for extref in ExternalReferenceType
))
])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumHashAlgorithm(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='hashAlg']"),
dp_cases_from_json_schemas('definitions', 'hash-alg'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(HashAlgorithm, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(components=[Component(name='dummy', type=ComponentType.LIBRARY, bom_ref='dummy', hashes=(
HashType(alg=alg, content='ae2b1fca515949e5d54fb22b8ed95575')
for alg in HashAlgorithm
))])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumComponentScope(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='scope']"),
dp_cases_from_json_schemas('definitions', 'component', 'properties', 'scope'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ComponentScope, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(components=(
Component(bom_ref=f'scoped-{scope.name}', name=f'dummy-{scope.name}',
type=ComponentType.LIBRARY, scope=scope)
for scope in ComponentScope
))
super()._test_cases_render(bom, of, sv)
class _DP_ComponentType(): # noqa: N801
XML_SCHEMA_XPATH = f"./{SCHEMA_NS}simpleType[@name='classification']"
JSON_SCHEMA_POINTER = ('definitions', 'component', 'properties', 'type')
@classmethod
def unsupported_cases(cls) -> Generator[Tuple[str, OutputFormat, SchemaVersion, ComponentType], None, None]:
for name, of, sv in NAMED_OF_SV:
if OutputFormat.XML is of:
schema_cases = set(dp_cases_from_xml_schema(SCHEMA_XML[sv], cls.XML_SCHEMA_XPATH))
elif OutputFormat.JSON is of:
schema_cases = set(dp_cases_from_json_schema(SCHEMA_JSON[sv], cls.JSON_SCHEMA_POINTER))
else:
raise ValueError(f'unexpected of: {of!r}')
for ct in ComponentType:
if ct.value not in schema_cases:
yield f'{name}-{ct.name}', of, sv, ct
@ddt
class TestEnumComponentType(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(_DP_ComponentType.XML_SCHEMA_XPATH),
dp_cases_from_json_schemas(*_DP_ComponentType.JSON_SCHEMA_POINTER),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ComponentType, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
if OutputFormat.XML is of:
schema_cases = set(dp_cases_from_xml_schema(SCHEMA_XML[sv], _DP_ComponentType.XML_SCHEMA_XPATH))
elif OutputFormat.JSON is of:
schema_cases = set(dp_cases_from_json_schema(SCHEMA_JSON[sv], _DP_ComponentType.JSON_SCHEMA_POINTER))
else:
raise ValueError(f'unexpected of: {of!r}')
bom = _make_bom(components=(
Component(bom_ref=f'typed-{ct.name}', name=f'dummy {ct.name}', type=ct)
for ct in ComponentType
if ct.value in schema_cases
))
super()._test_cases_render(bom, of, sv)
@named_data(*_DP_ComponentType.unsupported_cases())
def test_cases_render_raises_on_unsupported(self, of: OutputFormat, sv: SchemaVersion,
ct: ComponentType,
*_: Any, **__: Any) -> None:
bom = _make_bom(components=[
Component(bom_ref=f'typed-{ct.name}', name=f'dummy {ct.name}', type=ct)
])
with self.assertRaises(SerializationOfUnsupportedComponentTypeException):
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumPatchClassification(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='patchClassification']"),
dp_cases_from_json_schemas('definitions', 'patch', 'properties', 'type'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(PatchClassification, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(components=[
Component(name='dummy', type=ComponentType.LIBRARY, bom_ref='dummy', pedigree=Pedigree(patches=(
Patch(type=pc)
for pc in PatchClassification
)))
])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumImpactAnalysisAffectedStatus(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='impactAnalysisAffectedStatusType']"),
dp_cases_from_json_schemas('definitions', 'affectedStatus'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ImpactAnalysisAffectedStatus, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(vulnerabilities=[Vulnerability(
bom_ref='dummy', id='dummy', affects=[BomTarget(ref='urn:cdx:bom23/1#comp42', versions=(
BomTargetVersionRange(version=f'1.33.7+{iaas.name}', status=iaas)
for iaas in ImpactAnalysisAffectedStatus
))]
)])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumImpactAnalysisJustification(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='impactAnalysisJustificationType']"),
dp_cases_from_json_schemas('definitions', 'impactAnalysisJustification'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ImpactAnalysisJustification, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(vulnerabilities=(
Vulnerability(
bom_ref=f'vuln-with-{iaj.name}', id=f'vuln-with-{iaj.name}',
analysis=VulnerabilityAnalysis(justification=iaj)
) for iaj in ImpactAnalysisJustification
))
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumImpactAnalysisResponse(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='impactAnalysisResponsesType']"),
dp_cases_from_json_schemas('definitions', 'vulnerability', 'properties', 'analysis', 'properties', 'response',
'items'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ImpactAnalysisResponse, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(vulnerabilities=[Vulnerability(
bom_ref='dummy', id='dummy',
analysis=VulnerabilityAnalysis(responses=(
iar for iar in ImpactAnalysisResponse
))
)])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumImpactAnalysisState(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='impactAnalysisStateType']"),
dp_cases_from_json_schemas('definitions', 'impactAnalysisState'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(ImpactAnalysisState, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(vulnerabilities=(
Vulnerability(
bom_ref=f'vuln-wit-state-{ias.name}', id=f'vuln-wit-state-{ias.name}',
analysis=VulnerabilityAnalysis(state=ias)
) for ias in ImpactAnalysisState
))
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumIssueClassification(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='issueClassification']"),
dp_cases_from_json_schemas('definitions', 'issue', 'properties', 'type'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(IssueClassification, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(components=[
Component(name='dummy', type=ComponentType.LIBRARY, bom_ref='dummy', pedigree=Pedigree(patches=[
Patch(type=PatchClassification.BACKPORT, resolves=(
IssueType(type=ic, id=f'issue-{ic.name}')
for ic in IssueClassification
))
]))
])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumVulnerabilityScoreSource(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='scoreSourceType']"),
dp_cases_from_json_schemas('definitions', 'scoreMethod'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(VulnerabilityScoreSource, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(vulnerabilities=[Vulnerability(bom_ref='dummy', id='dummy', ratings=(
VulnerabilityRating(method=vss)
for vss in VulnerabilityScoreSource
))])
super()._test_cases_render(bom, of, sv)
@ddt
class TestEnumVulnerabilitySeverity(_EnumTestCase):
@idata(set(chain(
dp_cases_from_xml_schemas(f"./{SCHEMA_NS}simpleType[@name='severityType']"),
dp_cases_from_json_schemas('definitions', 'severity'),
)))
def test_knows_value(self, value: str) -> None:
super()._test_knows_value(VulnerabilitySeverity, value)
@named_data(*NAMED_OF_SV)
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
def test_cases_render_valid(self, of: OutputFormat, sv: SchemaVersion, *_: Any, **__: Any) -> None:
bom = _make_bom(vulnerabilities=[Vulnerability(bom_ref='dummy', id='dummy', ratings=(
VulnerabilityRating(severity=vs)
for vs in VulnerabilitySeverity
))])
super()._test_cases_render(bom, of, sv)