mirror of
https://github.com/danog/endtoend-test-psl.git
synced 2024-12-02 17:56:09 +01:00
49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Psl\Tests\Encoding;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psl\Encoding\Exception;
|
|
use Psl\Encoding\Hex;
|
|
use Psl\SecureRandom;
|
|
|
|
use function bin2hex;
|
|
|
|
final class HexTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider provideRandomBytes
|
|
*/
|
|
public function testRandom(string $random): void
|
|
{
|
|
$enc = Hex\encode($random);
|
|
static::assertSame($random, Hex\decode($enc));
|
|
static::assertSame(bin2hex($random), $enc);
|
|
$enc = Hex\encode($random);
|
|
static::assertSame($random, Hex\decode($enc));
|
|
}
|
|
|
|
public function testDecodeThrowsForCharactersOutsideTheHexRange(): void
|
|
{
|
|
$this->expectException(Exception\RangeException::class);
|
|
|
|
Hex\decode('gf');
|
|
}
|
|
|
|
public function testDecodeThrowsForAnOddNumberOfCharacters(): void
|
|
{
|
|
$this->expectException(Exception\RangeException::class);
|
|
|
|
Hex\decode('f');
|
|
}
|
|
|
|
public function provideRandomBytes(): iterable
|
|
{
|
|
for ($i = 1; $i < 128; ++$i) {
|
|
yield [SecureRandom\bytes($i)];
|
|
}
|
|
}
|
|
}
|