Skip to content
This repository has been archived by the owner on Dec 27, 2023. It is now read-only.

Commit

Permalink
Fix minor bug in fromBase64UrlString & increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
castarco committed Feb 25, 2017
1 parent 29a8afd commit f9423d3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/GUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public static function fromBase64String(string $b64Str, int $expectedLength = se

public static function fromBase64UrlString(string $b64Str, int $expectedLength = self::DEFAULT_GUID_SIZE): GUID
{
if (0 === \preg_match('#^(?:[A-Za-z0-9\-_]{4})*(?:[A-Za-z0-9\-_]{2}\.\.|[A-Za-z0-9\-_]{3}\.)?$#', $b64Str)) {
throw new UnserializationError('Invalid base64 string');
}

return self::fromBase64String(
\strtr($b64Str, '-_.', '+/='),
$expectedLength
Expand Down
80 changes: 80 additions & 0 deletions tests/GUID/fromBase64UrlStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);

namespace Unicity\Tests\GUID;

use PHPUnit\Framework\TestCase;
use Unicity\Interfaces\GUID as GUIDInterface;
use Unicity\GUID;

/**
* @covers \Unicity\GUID::fromBase64UrlString
* @covers \Unicity\GUID::fromBase64String
*/
class fromBase64UrlStringTest extends TestCase
{
/**
* @expectedException \Unicity\Errors\GUIDInvariantsViolationError
* @expectedExceptionMessage IDs must have at least 6 bytes of entropy
*/
public function test_too_small_expected_length()
{
GUID::fromBase64UrlString('YWJjZA..', 4);
}

/**
* @expectedException \Unicity\Errors\UnserializationError
* @expectedExceptionMessage The passed string has an unexpected length {"expected":6,"given":8}
*/
public function test_unexpected_length()
{
GUID::fromBase64UrlString('YWJjZGVmMDE.', 6);
}

/**
* @param string $b64Str
* @param int $expectedLength
*
* @dataProvider validParamsProvider
*/
public function test_valid_params(string $b64Str, int $expectedLength)
{
$guid = GUID::fromBase64UrlString($b64Str, $expectedLength);
$this->assertInstanceOf(GUIDInterface::class, $guid);
$this->assertInstanceOf(GUID::class, $guid);
}

public function validParamsProvider()
{
return [
['EjRWEjRW', 6],
['EjRWeBI0Vng.', 8],
['EjRWeJASNFZ4kA..', 10],
['EjRWeJCrEjRWeJCr', 12],
['EjRWeJCrzRI0VniQq80.', 14],
['EjRWeJCrze8SNFZ4kKvN7w..', 16]
];
}

/**
* @param string $invalidBase64Str
*
* @dataProvider invalidBase64UrlStringsProvider
* @expectedException \Unicity\Errors\UnserializationError
* @expectedExceptionMessage Invalid base64 string
*/
public function test_invalid_base64_strings(string $invalidBase64Str)
{
GUID::fromBase64UrlString($invalidBase64Str, 8);
}

public function invalidBase64UrlStringsProvider()
{
return [
['YWJjZGVmMDE'],
['YWJjZGVmMDE..'],
['YWJj*GVmMDE.'],
['YWJjZGVmMDE=']
];
}
}

0 comments on commit f9423d3

Please sign in to comment.