Skip to content

Commit

Permalink
Merge pull request #351 from CycloneDX/ttest_validation_functional
Browse files Browse the repository at this point in the history
tests: add functional tests for all validators
  • Loading branch information
jkowalleck committed Sep 13, 2023
2 parents 92184c1 + 4b39ce4 commit 05ea93e
Show file tree
Hide file tree
Showing 451 changed files with 18,050 additions and 0 deletions.
47 changes: 47 additions & 0 deletions tests/Core/Validation/Validators/JsonStrictValidatorTest.php
Expand Up @@ -23,16 +23,20 @@

namespace CycloneDX\Tests\Core\Validation\Validators;

use CycloneDX\Core\Spec\_Spec;
use CycloneDX\Core\Spec\_SpecProtocol;
use CycloneDX\Core\Spec\SpecFactory;
use CycloneDX\Core\Spec\Version;
use CycloneDX\Core\Validation\BaseValidator;
use CycloneDX\Core\Validation\Errors\JsonValidationError;
use CycloneDX\Core\Validation\Exceptions\FailedLoadingSchemaException;
use CycloneDX\Core\Validation\ValidationError;
use CycloneDX\Core\Validation\Validators\JsonStrictValidator;
use CycloneDX\Core\Validation\Validators\JsonValidator;
use Generator;
use JsonException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\Constraint\IsInstanceOf;
use PHPUnit\Framework\TestCase;
Expand All @@ -43,6 +47,7 @@
#[CoversClass(BaseValidator::class)]
#[UsesClass(JsonValidationError::class)]
#[UsesClass(ValidationError::class)]
#[UsesClass(_Spec::class)]
class JsonStrictValidatorTest extends TestCase
{
public function testConstructor(): JsonStrictValidator
Expand Down Expand Up @@ -164,4 +169,46 @@ public function testValidateDataThrowsOnSchemaFileUnknown(): void

$validator->validateData(new stdClass());
}

#[DataProvider('dpSchemaTestDataValid')]
public function testFunctionalValid(_Spec $spec, string $file): void
{
$validator = new JsonStrictValidator($spec);
$errors = $validator->validateString(file_get_contents($file));
$this->assertNull($errors);
}

#[DataProvider('dpSchemaTestDataInvalid')]
public function testFunctionalInvalid(_Spec $spec, string $file): void
{
$validator = new JsonStrictValidator($spec);
$errors = $validator->validateString(file_get_contents($file));
$this->assertInstanceOf(JsonValidationError::class, $errors);
}

public static function dpSchemaTestDataValid(): Generator
{
yield from self::dpSchemaTestData('valid');
}

public static function dpSchemaTestDataInvalid(): Generator
{
yield from self::dpSchemaTestData('invalid');
}

private static function dpSchemaTestData(string $filePrefix): Generator
{
/** @var _SpecProtocol $spec */
foreach ([
SpecFactory::make1dot5(),
SpecFactory::make1dot4(),
SpecFactory::make1dot3(),
SpecFactory::make1dot2(),
] as $spec) {
$specVersion = $spec->getVersion()->value;
foreach (glob(__DIR__."/../../../_data/schemaTestData/$specVersion/$filePrefix-*.json") as $file) {
yield "$specVersion ".basename($file, '.json') => [$spec, $file];
}
}
}
}
47 changes: 47 additions & 0 deletions tests/Core/Validation/Validators/JsonValidatorTest.php
Expand Up @@ -23,15 +23,19 @@

namespace CycloneDX\Tests\Core\Validation\Validators;

use CycloneDX\Core\Spec\_Spec;
use CycloneDX\Core\Spec\_SpecProtocol;
use CycloneDX\Core\Spec\SpecFactory;
use CycloneDX\Core\Spec\Version;
use CycloneDX\Core\Validation\BaseValidator;
use CycloneDX\Core\Validation\Errors\JsonValidationError;
use CycloneDX\Core\Validation\Exceptions\FailedLoadingSchemaException;
use CycloneDX\Core\Validation\ValidationError;
use CycloneDX\Core\Validation\Validators\JsonValidator;
use Generator;
use JsonException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\Constraint\IsInstanceOf;
use PHPUnit\Framework\TestCase;
Expand All @@ -41,6 +45,7 @@
#[CoversClass(BaseValidator::class)]
#[UsesClass(JsonValidationError::class)]
#[UsesClass(ValidationError::class)]
#[UsesClass(_Spec::class)]
class JsonValidatorTest extends TestCase
{
public function testConstructor(): JsonValidator
Expand Down Expand Up @@ -169,4 +174,46 @@ public function testValidateDataThrowsOnSchemaFileUnknown(): void

$validator->validateData(new stdClass());
}

#[DataProvider('dpSchemaTestDataValid')]
public function testFunctionalValid(_Spec $spec, string $file): void
{
$validator = new JsonValidator($spec);
$errors = $validator->validateString(file_get_contents($file));
$this->assertNull($errors);
}

#[DataProvider('dpSchemaTestDataInvalid')]
public function testFunctionalInvalid(_Spec $spec, string $file): void
{
$validator = new JsonValidator($spec);
$errors = $validator->validateString(file_get_contents($file));
$this->assertInstanceOf(JsonValidationError::class, $errors);
}

public static function dpSchemaTestDataValid(): Generator
{
yield from self::dpSchemaTestData('valid');
}

public static function dpSchemaTestDataInvalid(): Generator
{
yield from self::dpSchemaTestData('invalid');
}

private static function dpSchemaTestData(string $filePrefix): Generator
{
/** @var _SpecProtocol $spec */
foreach ([
SpecFactory::make1dot5(),
SpecFactory::make1dot4(),
SpecFactory::make1dot3(),
SpecFactory::make1dot2(),
] as $spec) {
$specVersion = $spec->getVersion()->value;
foreach (glob(__DIR__."/../../../_data/schemaTestData/$specVersion/$filePrefix-*.json") as $file) {
yield "$specVersion ".basename($file, '.json') => [$spec, $file];
}
}
}
}
48 changes: 48 additions & 0 deletions tests/Core/Validation/Validators/XmlValidatorTest.php
Expand Up @@ -23,7 +23,9 @@

namespace CycloneDX\Tests\Core\Validation\Validators;

use CycloneDX\Core\Spec\_Spec;
use CycloneDX\Core\Spec\_SpecProtocol;
use CycloneDX\Core\Spec\SpecFactory;
use CycloneDX\Core\Spec\Version;
use CycloneDX\Core\Validation\BaseValidator;
use CycloneDX\Core\Validation\Errors\XmlValidationError;
Expand All @@ -32,7 +34,9 @@
use CycloneDX\Core\Validation\Validators\XmlValidator;
use DOMDocument;
use DOMException;
use Generator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\Constraint\IsInstanceOf;
use PHPUnit\Framework\TestCase;
Expand All @@ -41,6 +45,7 @@
#[CoversClass(BaseValidator::class)]
#[UsesClass(XmlValidationError::class)]
#[UsesClass(ValidationError::class)]
#[UsesClass(_Spec::class)]
class XmlValidatorTest extends TestCase
{
public function testConstructor(): XmlValidator
Expand Down Expand Up @@ -210,4 +215,47 @@ public function testValidateDomThrowsOnSchemaFileUnknown(): void

$validator->validateDom($doc);
}

#[DataProvider('dpSchemaTestDataValid')]
public function testFunctionalValid(_Spec $spec, string $file): void
{
$validator = new XmlValidator($spec);
$errors = $validator->validateString(file_get_contents($file));
$this->assertNull($errors);
}

#[DataProvider('dpSchemaTestDataInvalid')]
public function testFunctionalInvalid(_Spec $spec, string $file): void
{
$validator = new XmlValidator($spec);
$errors = $validator->validateString(file_get_contents($file));
$this->assertInstanceOf(XmlValidationError::class, $errors);
}

public static function dpSchemaTestDataValid(): Generator
{
yield from self::dpSchemaTestData('valid');
}

public static function dpSchemaTestDataInvalid(): Generator
{
yield from self::dpSchemaTestData('invalid');
}

private static function dpSchemaTestData(string $filePrefix): Generator
{
/** @var _SpecProtocol $spec */
foreach ([
SpecFactory::make1dot5(),
SpecFactory::make1dot4(),
SpecFactory::make1dot3(),
SpecFactory::make1dot2(),
SpecFactory::make1dot1(),
] as $spec) {
$specVersion = $spec->getVersion()->value;
foreach (glob(__DIR__."/../../../_data/schemaTestData/$specVersion/$filePrefix-*.xml") as $file) {
yield "$specVersion ".basename($file, '.xml') => [$spec, $file];
}
}
}
}
2 changes: 2 additions & 0 deletions tests/_data/schemaTestData/.gitattributes
@@ -0,0 +1,2 @@
*/*.json linguist-vendored
*/*.xml linguist-vendored
15 changes: 15 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-component-ref-1.1.xml
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="library" bom-ref="123">
<name>acme-library</name>
<version>1.0.0</version>
<components>
<component type="library" bom-ref="123">
<name>acme-library</name>
<version>1.0.0</version>
</component>
</components>
</component>
</components>
</bom>
9 changes: 9 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-component-type-1.1.xml
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="foo">
<name>acme-library</name>
<version>1.0.0</version>
</component>
</components>
</bom>
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="application">
</component>
</components>
</bom>
16 changes: 16 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-hash-alg-1.1.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="library">
<name>acme-library</name>
<version>1.0.0</version>
<scope>required</scope>
<hashes>
<hash alg="FOO">3942447fac867ae5cdb3229b658f4d48</hash>
<hash alg="SHA-1">e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a</hash>
<hash alg="SHA-256">f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b</hash>
<hash alg="SHA-512">e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282</hash>
</hashes>
</component>
</components>
</bom>
16 changes: 16 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-hash-md5-1.1.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="library">
<name>acme-library</name>
<version>1.0.0</version>
<scope>required</scope>
<hashes>
<hash alg="MD5">foo</hash>
<hash alg="SHA-1">e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a</hash>
<hash alg="SHA-256">f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b</hash>
<hash alg="SHA-512">e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282</hash>
</hashes>
</component>
</components>
</bom>
16 changes: 16 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-hash-sha1-1.1.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="library">
<name>acme-library</name>
<version>1.0.0</version>
<scope>required</scope>
<hashes>
<hash alg="MD5">3942447fac867ae5cdb3229b658f4d48</hash>
<hash alg="SHA-1">foo</hash>
<hash alg="SHA-256">f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b</hash>
<hash alg="SHA-512">e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282</hash>
</hashes>
</component>
</components>
</bom>
16 changes: 16 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-hash-sha256-1.1.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="library">
<name>acme-library</name>
<version>1.0.0</version>
<scope>required</scope>
<hashes>
<hash alg="MD5">3942447fac867ae5cdb3229b658f4d48</hash>
<hash alg="SHA-1">e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a</hash>
<hash alg="SHA-256">foo</hash>
<hash alg="SHA-512">e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282</hash>
</hashes>
</component>
</components>
</bom>
16 changes: 16 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-hash-sha512-1.1.xml
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="library">
<name>acme-library</name>
<version>1.0.0</version>
<scope>required</scope>
<hashes>
<hash alg="MD5">3942447fac867ae5cdb3229b658f4d48</hash>
<hash alg="SHA-1">e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a</hash>
<hash alg="SHA-256">f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b</hash>
<hash alg="SHA-512">foo</hash>
</hashes>
</component>
</components>
</bom>
26 changes: 26 additions & 0 deletions tests/_data/schemaTestData/1.1/invalid-license-choice-1.1.xml
@@ -0,0 +1,26 @@
<?xml version="1.0"?>
<bom serialNumber="urn:uuid:3e671687-395b-41f5-a30f-a58921a69b79" version="1" xmlns="http://cyclonedx.org/schema/bom/1.1">
<components>
<component type="application">
<publisher>Acme Inc</publisher>
<group>com.acme</group>
<name>tomcat-catalina</name>
<version>9.0.14</version>
<description>Modified version of Apache Catalina</description>
<scope>required</scope>
<hashes>
<hash alg="MD5">3942447fac867ae5cdb3229b658f4d48</hash>
<hash alg="SHA-1">e6b1000b94e835ffd37f4c6dcbdad43f4b48a02a</hash>
<hash alg="SHA-256">f498a8ff2dd007e29c2074f5e4b01a9a01775c3ff3aeaf6906ea503bc5791b7b</hash>
<hash alg="SHA-512">e8f33e424f3f4ed6db76a482fde1a5298970e442c531729119e37991884bdffab4f9426b7ee11fccd074eeda0634d71697d6f88a460dce0ac8d627a29f7d1282</hash>
</hashes>
<licenses>
<license>
<id>Apache-2.0</id>
</license>
<expression>EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0</expression>
</licenses>
<purl>pkg:maven/com.acme/tomcat-catalina@9.0.14?packaging=jar</purl>
</component>
</components>
</bom>

0 comments on commit 05ea93e

Please sign in to comment.