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
|
# 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 glob import iglob
from itertools import chain
from os.path import join
from typing import Generator
from unittest import TestCase
from ddt import data, ddt, idata, unpack
from cyclonedx.exception import MissingOptionalDependencyException
from cyclonedx.schema import OutputFormat, SchemaVersion
from cyclonedx.validation.json import JsonStrictValidator, JsonValidator
from tests import OWN_DATA_DIRECTORY, SCHEMA_TESTDATA_DIRECTORY, DpTuple
UNSUPPORTED_SCHEMA_VERSIONS = {SchemaVersion.V1_0, SchemaVersion.V1_1, }
def _dp_sv_tf(valid: bool) -> Generator:
prefix = 'valid-' if valid else 'invalid-'
return (
DpTuple((sv, tf))
for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS
for tf in iglob(join(SCHEMA_TESTDATA_DIRECTORY, sv.to_version(), f'{prefix}*.json'))
)
def _dp_sv_own(valid: bool) -> Generator:
return (
DpTuple((sv, tf))
for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS
for tf in iglob(join(OWN_DATA_DIRECTORY, 'json', sv.to_version(), '*.json')) if ('invalid-' in tf) != valid
)
@ddt
class TestJsonValidator(TestCase):
@idata(sv for sv in SchemaVersion if sv not in UNSUPPORTED_SCHEMA_VERSIONS)
def test_validator_as_expected(self, schema_version: SchemaVersion) -> None:
validator = JsonValidator(schema_version)
self.assertIs(validator.schema_version, schema_version)
self.assertIs(validator.output_format, OutputFormat.JSON)
@idata(UNSUPPORTED_SCHEMA_VERSIONS)
def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVersion) -> None:
with self.assertRaisesRegex(ValueError, 'Unsupported schema_version'):
JsonValidator(schema_version)
@idata(chain(
_dp_sv_tf(True),
_dp_sv_own(True)
))
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNone(validation_error)
@idata(chain(
_dp_sv_tf(False),
_dp_sv_own(False)
))
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNotNone(validation_error)
self.assertIsNotNone(validation_error.data)
@ddt
class TestJsonStrictValidator(TestCase):
@data(*UNSUPPORTED_SCHEMA_VERSIONS)
def test_throws_with_unsupported_schema_version(self, schema_version: SchemaVersion) -> None:
with self.assertRaisesRegex(ValueError, 'Unsupported schema_version'):
JsonStrictValidator(schema_version)
@idata(chain(
_dp_sv_tf(True),
_dp_sv_own(True)
))
@unpack
def test_validate_no_none(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonStrictValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNone(validation_error)
@idata(chain(
_dp_sv_tf(False),
_dp_sv_own(False)
))
@unpack
def test_validate_expected_error(self, schema_version: SchemaVersion, test_data_file: str) -> None:
validator = JsonStrictValidator(schema_version)
with open(join(test_data_file), 'r') as tdfh:
test_data = tdfh.read()
try:
validation_error = validator.validate_str(test_data)
except MissingOptionalDependencyException:
self.skipTest('MissingOptionalDependencyException')
self.assertIsNotNone(validation_error)
self.assertIsNotNone(validation_error.data)
|