Skip to content

Commit 305b08f

Browse files
committed
Create assertion and type-class for xs:hexBinary
1 parent 5bd52ba commit 305b08f

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

src/Assert/HexBinaryTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* @package simplesamlphp/xml-common
11+
*/
12+
trait HexBinaryTrait
13+
{
14+
/** @var string */
15+
private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D';
16+
17+
18+
/**
19+
* @param string $value
20+
* @param string $message
21+
*/
22+
protected static function validHexBinary(string $value, string $message = ''): void
23+
{
24+
parent::regex(
25+
$value,
26+
self::$hexbin_regex,
27+
$message ?: '%s is not a valid xs:hexBinary',
28+
InvalidArgumentException::class,
29+
);
30+
}
31+
}

src/Type/HexBinaryValue.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\XML\Type;
6+
7+
use SimpleSAML\XML\Assert\Assert;
8+
use SimpleSAML\XML\Exception\SchemaViolationException;
9+
10+
use function str_replace;
11+
12+
/**
13+
* @package simplesaml/xml-common
14+
*/
15+
class HexBinaryValue extends AbstractValueType
16+
{
17+
/**
18+
* Sanitize the value.
19+
*
20+
* Note: There are no processing rules for xs:hexBinary regarding whitespace. General consensus is to strip them
21+
*
22+
* @param string $value The unsanitized value
23+
* @return string
24+
*/
25+
protected function sanitizeValue(string $value): string
26+
{
27+
return str_replace(["\f", "\r", "\n", "\t", "\v", ' '], '', $value);
28+
}
29+
30+
31+
/**
32+
* Validate the value.
33+
*
34+
* @param string $value
35+
* @throws \SimpleSAML\XML\Exception\SchemaViolationException on failure
36+
* @return void
37+
*/
38+
protected function validateValue(string $value): void
39+
{
40+
// Note: value must already be sanitized before validating
41+
Assert::regex(
42+
$this->sanitizeValue($value),
43+
'/([0-9A-F]{2})*/i',
44+
SchemaViolationException::class,
45+
);
46+
}
47+
}

tests/Assert/HexBinaryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Assert;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\Assert\AssertionFailedException;
11+
use SimpleSAML\XML\Assert\Assert;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Assert\HexBinaryTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class HexBinaryTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $name
24+
*/
25+
#[DataProvider('provideHexBinary')]
26+
public function testHexBinary(bool $shouldPass, string $name): void
27+
{
28+
try {
29+
Assert::validHexBinary($name);
30+
$this->assertTrue($shouldPass);
31+
} catch (AssertionFailedException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideHexBinary(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'base64' => [false, 'U2ltcGxlU0FNTHBocA=='],
45+
'valid' => [true, '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f'],
46+
'invalid' => [false, '3f3r'],
47+
'bogus' => [false, '&*$(#&^@!(^%$'],
48+
'length not dividable by 4' => [false, '3f3'],
49+
];
50+
}
51+
}

tests/Type/HexBinaryValueTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Test\XML\Type;
6+
7+
use PHPUnit\Framework\Attributes\CoversClass;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use PHPUnit\Framework\TestCase;
10+
use SimpleSAML\XML\Exception\SchemaViolationException;
11+
use SimpleSAML\XML\Type\HexBinaryValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\HexBinaryValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(HexBinaryValue::class)]
19+
final class HexBinaryValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $hexbin
24+
*/
25+
#[DataProvider('provideHexBinary')]
26+
public function testHexBinary(bool $shouldPass, string $hexbin): void
27+
{
28+
try {
29+
HexBinaryValue::fromString($hexbin);
30+
$this->assertTrue($shouldPass);
31+
} catch (SchemaViolationException $e) {
32+
$this->assertFalse($shouldPass);
33+
}
34+
}
35+
36+
37+
/**
38+
* @return array<string, array{0: bool, 1: string}>
39+
*/
40+
public static function provideHexBinary(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'base64' => [false, 'U2ltcGxlU0FNTHBocA=='],
45+
'valid' => [true, '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f'],
46+
'invalid' => [false, '3f3r'],
47+
'bogus' => [false, '&*$(#&^@!(^%$'],
48+
'length not dividable by 4' => [false, '3f3'],
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)