Skip to content

Commit

Permalink
Add assertion to test xs:hexBinary
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdijen committed Dec 4, 2024
1 parent 4e805d7 commit 064ee1e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
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);

Check failure on line 29 in tests/Assert/HexBinaryTest.php

View workflow job for this annotation

GitHub Actions / Quality control

Call to private static method validHexBinary() of class SimpleSAML\Assert\Assert.

Check failure on line 29 in tests/Assert/HexBinaryTest.php

View workflow job for this annotation

GitHub Actions / Quality control

Call to private static method validHexBinary() of class SimpleSAML\Assert\Assert.
$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 064ee1e

Please sign in to comment.