Skip to content

Commit

Permalink
Merge pull request #30 from ChristianRiesen/add-tests-and-make-phpsta…
Browse files Browse the repository at this point in the history
…n-happy

Add tests and make phpstan happy
  • Loading branch information
ChristianRiesen authored Feb 26, 2021
2 parents bc84d99 + 1bf951f commit 2e82dab
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Base32.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ class Base32
/**
* Alphabet for encoding and decoding base32.
*
* @var array
* @var string
*/
protected const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=';

protected const BASE32_PATTERN = '/[^A-Z2-7]/';
protected const BASE32HEX_PATTERN = '/[^A-Z2-7]/';

/**
* Maps the Base32 pentet to its corresponding bit value.
* Maps the Base32 character to its corresponding bit value.
*/
protected const MAPPING = [
'=' => 0b00000,
Expand Down Expand Up @@ -126,7 +126,7 @@ public static function decode(string $base32String): string
$base32String = \strtoupper($base32String);

// Remove anything that is not base32 alphabet
$base32String = \preg_replace(static::BASE32_PATTERN, '', $base32String);
$base32String = \preg_replace(static::BASE32HEX_PATTERN, '', $base32String);

// Empty string results in empty string
if ('' === $base32String || null === $base32String) {
Expand Down
6 changes: 3 additions & 3 deletions src/Base32Hex.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class Base32Hex extends Base32
/**
* Alphabet for encoding and decoding base32 extended hex.
*
* @var array
* @var string
*/
protected const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUV=';

protected const BASE32_PATTERN = '/[^0-9A-V]/';
protected const BASE32HEX_PATTERN = '/[^0-9A-V]/';

/**
* Maps the Base32 pentet to its corresponding bit value.
* Maps the Base32 character to its corresponding bit value.
*/
protected const MAPPING = [
'=' => 0b00000,
Expand Down
21 changes: 21 additions & 0 deletions tests/Base32HexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function encodeDataProvider(): array
return \array_merge($encodeData, self::RFC_VECTORS);
}

/**
* Back and forth encoding must return the same result.
*
* @return array<string, array>
*/
public function backAndForthDataProvider(): array
{
return Base32Test::BASE_CLEAR_STRINGS;
}

/**
* @dataProvider decodeDataProvider
* @covers ::decode
Expand All @@ -70,4 +80,15 @@ public function testEncode(string $clear, string $base32): void
{
$this->assertEquals($base32, Base32Hex::encode($clear));
}

/**
* @dataProvider backAndForthDataProvider
* @covers ::encode
* @covers ::decode
*/
public function testEncodeAndDecode(string $clear): void
{
// Encoding then decoding again, to ensure that the back and forth works as intended
$this->assertEquals($clear, Base32Hex::decode(Base32Hex::encode($clear)));
}
}
41 changes: 39 additions & 2 deletions tests/Base32Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@
*/
class Base32Test extends TestCase
{
/**
* Strings to test back and forth encoding/decoding to make sure results are the same.
*
* @var array<string,string>
*/
public const BASE_CLEAR_STRINGS = [
'Empty String' => [''],
'Ten' => ['10'],
'Test130' => ['test130'],
'test' => ['test'],
'Eight' => ['8'],
'Zero' => ['0'],
'Equals' => ['='],
'Foobar' => ['foobar'],
];

/**
* Vectors from RFC with cleartext => base32 pairs.
*
Expand All @@ -35,7 +51,7 @@ public function decodeDataProvider(): array
'Empty String' => ['', ''],
'All Invalid Characters' => ['', '8908908908908908'],
'Random Integers' => [\base64_decode('HgxBl1kJ4souh+ELRIHm/x8yTc/cgjDmiCNyJR/NJfs='), 'DYGEDF2ZBHRMULUH4EFUJAPG74PTETOP3SBDBZUIENZCKH6NEX5Q===='],
'Partial zero edge case' => ["8", "HA======"],
'Partial zero edge case' => ['8', 'HA======'],
];

return \array_merge($encodeData, self::RFC_VECTORS);
Expand All @@ -49,12 +65,22 @@ public function encodeDataProvider(): array
$encodeData = [
'Empty String' => ['', ''],
'Random Integers' => [\base64_decode('HgxBl1kJ4souh+ELRIHm/x8yTc/cgjDmiCNyJR/NJfs='), 'DYGEDF2ZBHRMULUH4EFUJAPG74PTETOP3SBDBZUIENZCKH6NEX5Q===='],
'Partial zero edge case' => ["8", "HA======"],
'Partial zero edge case' => ['8', 'HA======'],
];

return \array_merge($encodeData, self::RFC_VECTORS);
}

/**
* Back and forth encoding must return the same result.
*
* @return array<string, array>
*/
public function backAndForthDataProvider(): array
{
return self::BASE_CLEAR_STRINGS;
}

/**
* @dataProvider decodeDataProvider
* @covers ::decode
Expand All @@ -72,4 +98,15 @@ public function testEncode(string $clear, string $base32): void
{
$this->assertEquals($base32, Base32::encode($clear));
}

/**
* @dataProvider backAndForthDataProvider
* @covers ::encode
* @covers ::decode
*/
public function testEncodeAndDecode(string $clear): void
{
// Encoding then decoding again, to ensure that the back and forth works as intended
$this->assertEquals($clear, Base32::decode(Base32::encode($clear)));
}
}

0 comments on commit 2e82dab

Please sign in to comment.