Skip to content

Commit

Permalink
Merge pull request #17 from simplesamlphp/feature/assert-hexbinary
Browse files Browse the repository at this point in the history
Add assertion to test xs:hexBinary
  • Loading branch information
tvdijen authored Dec 4, 2024
2 parents 4e805d7 + 2ab4df7 commit 057a9e9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
* @method static void nullOrThrows(Closure|null $expression, string $class, string $message = '', string $exception = '')
* @method static void allThrows(Closure[] $expression, string $class, string $message = '', string $exception = '')
*
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
Expand All @@ -322,6 +323,7 @@
* @method static void validURL(mixed $value, string $message = '', string $exception = '')
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
Expand All @@ -333,6 +335,7 @@
* @method static void nullOrValidURL(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
Expand Down
24 changes: 24 additions & 0 deletions src/CustomAssertionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
*/
trait CustomAssertionTrait
{
/** @var string */
private static string $hexbin_regex = '/^([0-9a-fA-F]{2})+$/D';

/** @var string */
private static string $nmtoken_regex = '/^[\w.:-]+$/Du';

Expand Down Expand Up @@ -130,6 +133,27 @@ private static function stringPlausibleBase64(string $value, string $message = '
}


/**
* @param string $value
* @param string $message
*/
private static function validHexBinary(string $value, string $message = ''): void
{
$result = true;

if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$hexbin_regex]]) === false) {
$result = false;
}

if ($result === false) {
throw new InvalidArgumentException(sprintf(
$message ?: '\'%s\' is not a valid hexBinary string',
$value,
));
}
}


/**
* @param string $value
* @param string $message
Expand Down
51 changes: 51 additions & 0 deletions tests/Assert/HexBinaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Assert;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Assert\Assert;
use SimpleSAML\Assert\AssertionFailedException;

/**
* Class \SimpleSAML\Assert\HexBinaryTest
*
* @package simplesamlphp/assert
*/
#[CoversClass(Assert::class)]
final class HexBinaryTest extends TestCase
{
/**
* @param boolean $shouldPass
* @param string $name
*/
#[DataProvider('provideHexBinary')]
public function testHexBinary(bool $shouldPass, string $name): void
{
try {
Assert::validHexBinary($name);
$this->assertTrue($shouldPass);
} catch (AssertionFailedException $e) {
$this->assertFalse($shouldPass);
}
}


/**
* @return array<string, array{0: bool, 1: string}>
*/
public static function provideHexBinary(): array
{
return [
'empty' => [false, ''],
'base64' => [false, 'U2ltcGxlU0FNTHBocA=='],
'valid' => [true, '3f3c6d78206c657673726f693d6e3122302e20226e656f636964676e223d54552d4622383e3f'],
'invalid' => [false, '3f3r'],
'bogus' => [false, '&*$(#&^@!(^%$'],
'length not dividable by 4' => [false, '3f3'],
];
}
}

0 comments on commit 057a9e9

Please sign in to comment.