Skip to content

Commit 83e7d99

Browse files
committed
Create assertion and type-class for xs:base64Binary
1 parent 67a90fc commit 83e7d99

File tree

5 files changed

+171
-0
lines changed

5 files changed

+171
-0
lines changed

src/Assert/Assert.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @package simplesamlphp/xml-common
1111
*
1212
* @method static void validAnyURI(mixed $value, string $message = '', string $exception = '')
13+
* @method static void validBase64Binary(mixed $value, string $message = '', string $exception = '')
1314
* @method static void validDate(mixed $value, string $message = '', string $exception = '')
1415
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
1516
* @method static void validDay(mixed $value, string $message = '', string $exception = '')
@@ -33,6 +34,7 @@
3334
* @method static void validToken(mixed $value, string $message = '', string $exception = '')
3435
* @method static void validYearMonth(mixed $value, string $message = '', string $exception = '')
3536
* @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '')
37+
* @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '')
3638
* @method static void nullOrValidDate(mixed $value, string $message = '', string $exception = '')
3739
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
3840
* @method static void nullOrValidDay(mixed $value, string $message = '', string $exception = '')
@@ -56,6 +58,7 @@
5658
* @method static void nullOrValidToken(mixed $value, string $message = '', string $exception = '')
5759
* @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '')
5860
* @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '')
61+
* @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '')
5962
* @method static void allValidDate(mixed $value, string $message = '', string $exception = '')
6063
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
6164
* @method static void allValidDay(mixed $value, string $message = '', string $exception = '')
@@ -82,6 +85,7 @@
8285
class Assert extends BaseAssert
8386
{
8487
use AnyURITrait;
88+
use Base64BinaryTrait;
8589
use DateTrait;
8690
use DateTimeTrait;
8791
use DayTrait;

src/Assert/Base64BinaryTrait.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 Base64BinaryTrait
13+
{
14+
/**
15+
* @param string $value
16+
* @param string $message
17+
*/
18+
protected static function validBase64Binary(string $value, string $message = ''): void
19+
{
20+
parent::validBase64(
21+
$value,
22+
$message ?: '%s is not a valid xs:base64Binary',
23+
InvalidArgumentException::class,
24+
);
25+
}
26+
}

src/Type/Base64BinaryValue.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Base64BinaryValue extends AbstractValueType
16+
{
17+
/**
18+
* Sanitize the value.
19+
*
20+
* Note: There are no processing rules for xs:base64 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::validBase64Binary($this->sanitizeValue($value), SchemaViolationException::class);
42+
}
43+
}

tests/Assert/Base64Test.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\XML\Assert\Base64BinaryTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Assert::class)]
19+
final class Base64BinaryTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $uri
24+
*/
25+
#[DataProvider('provideBase64')]
26+
public function testValidBase64Binary(bool $shouldPass, string $base64): void
27+
{
28+
try {
29+
Assert::validBase64Binary($base64);
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 provideBase64(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid' => [true, 'U2ltcGxlU0FNTHBocA=='],
45+
'illegal characters' => [false, '&*$(#&^@!(^%$'],
46+
'length not dividable by 4' => [false, 'U2ltcGxlU0FTHBocA=='],
47+
];
48+
}
49+
}

tests/Type/Base64BinaryTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Base64BinaryValue;
12+
13+
/**
14+
* Class \SimpleSAML\Test\Type\Base64BinaryValueTest
15+
*
16+
* @package simplesamlphp/xml-common
17+
*/
18+
#[CoversClass(Base64BinaryValue::class)]
19+
final class Base64BinaryValueTest extends TestCase
20+
{
21+
/**
22+
* @param boolean $shouldPass
23+
* @param string $base64
24+
*/
25+
#[DataProvider('provideBase64')]
26+
public function testBase64Binary(bool $shouldPass, string $base64): void
27+
{
28+
try {
29+
Base64BinaryValue::fromString($base64);
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 provideBase64(): array
41+
{
42+
return [
43+
'empty' => [false, ''],
44+
'valid' => [true, 'U2ltcGxlU0FNTHBocA=='],
45+
'illegal characters' => [false, '&*$(#&^@!(^%$'],
46+
'length not dividable by 4' => [false, 'U2ltcGxlU0FTHBocA=='],
47+
];
48+
}
49+
}

0 commit comments

Comments
 (0)