|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace KDuma\BinaryTools\Tests; |
| 4 | + |
| 5 | +use KDuma\BinaryTools\Base32; |
| 6 | +use KDuma\BinaryTools\BinaryString; |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | + |
| 11 | +#[CoversClass(Base32::class)] |
| 12 | +class Base32Test extends TestCase |
| 13 | +{ |
| 14 | + public function testEmpty(): void |
| 15 | + { |
| 16 | + $this->assertSame('', Base32::toBase32('')); |
| 17 | + $this->assertSame('', Base32::fromBase32('')); |
| 18 | + |
| 19 | + $binaryString = BinaryString::fromString(''); |
| 20 | + $this->assertSame('', $binaryString->toBase32()); |
| 21 | + $this->assertTrue($binaryString->equals(BinaryString::fromBase32(''))); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * RFC 4648 test vectors (uppercase, unpadded form). |
| 26 | + * |
| 27 | + * @return array<string, array{plain: string, base32: string}> |
| 28 | + */ |
| 29 | + public static function vectors(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + 'f' => ['plain' => 'f', 'base32' => 'MY'], |
| 33 | + 'fo' => ['plain' => 'fo', 'base32' => 'MZXQ'], |
| 34 | + 'foo' => ['plain' => 'foo', 'base32' => 'MZXW6'], |
| 35 | + 'foob' => ['plain' => 'foob', 'base32' => 'MZXW6YQ'], |
| 36 | + 'fooba' => ['plain' => 'fooba', 'base32' => 'MZXW6YTB'], |
| 37 | + 'foobar' => ['plain' => 'foobar', 'base32' => 'MZXW6YTBOI'], |
| 38 | + 'A' => ['plain' => 'A', 'base32' => 'IE'], |
| 39 | + 'AB' => ['plain' => 'AB', 'base32' => 'IFBA'], |
| 40 | + 'ABC' => ['plain' => 'ABC', 'base32' => 'IFBEG'], |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + #[DataProvider('vectors')] |
| 45 | + public function testToBase32MatchesKnownVectors(string $plain, string $base32): void |
| 46 | + { |
| 47 | + $this->assertSame($base32, Base32::toBase32($plain)); |
| 48 | + $this->assertSame($base32, BinaryString::fromString($plain)->toBase32()); |
| 49 | + } |
| 50 | + |
| 51 | + #[DataProvider('vectors')] |
| 52 | + public function testFromBase32MatchesKnownVectors(string $plain, string $base32): void |
| 53 | + { |
| 54 | + $this->assertSame($plain, Base32::fromBase32($base32)); |
| 55 | + $this->assertTrue(BinaryString::fromString($plain)->equals(BinaryString::fromBase32($base32))); |
| 56 | + } |
| 57 | + |
| 58 | + public function testRoundTripRandomBinary(): void |
| 59 | + { |
| 60 | + $lengths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 31, 32, 33, 64, 100, 256, 1024]; |
| 61 | + |
| 62 | + foreach ($lengths as $length) { |
| 63 | + $binary = ($length === 0) ? '' : random_bytes($length); |
| 64 | + $encoded = Base32::toBase32($binary); |
| 65 | + $decoded = Base32::fromBase32($encoded); |
| 66 | + |
| 67 | + $this->assertSame($binary, $decoded, "Failed round-trip at length {$length}"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public function testDecodeThenEncodeIsIdempotent(): void |
| 72 | + { |
| 73 | + $original = 'MZXW6YTBOI'; // "foobar" |
| 74 | + $decoded = Base32::fromBase32($original); |
| 75 | + $reEncoded = Base32::toBase32($decoded); |
| 76 | + |
| 77 | + $this->assertSame($original, $reEncoded); |
| 78 | + } |
| 79 | + |
| 80 | + public function testLowercaseInputThrowsException(): void |
| 81 | + { |
| 82 | + $lower = 'mzxw6ytboi'; |
| 83 | + |
| 84 | + $this->expectException(\InvalidArgumentException::class); |
| 85 | + $this->expectExceptionMessage("Invalid character 'm' at position 0 in Base32 input."); |
| 86 | + |
| 87 | + Base32::fromBase32($lower); |
| 88 | + } |
| 89 | + |
| 90 | + public function testCustomAlphabet(): void |
| 91 | + { |
| 92 | + $customAlphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUV'; |
| 93 | + $data = 'Hello World'; |
| 94 | + |
| 95 | + $encoded = Base32::toBase32($data, $customAlphabet); |
| 96 | + $decoded = Base32::fromBase32($encoded, $customAlphabet); |
| 97 | + |
| 98 | + $this->assertSame($data, $decoded); |
| 99 | + } |
| 100 | + |
| 101 | + public function testInvalidAlphabetLengthThrowsException(): void |
| 102 | + { |
| 103 | + $shortAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ12345'; // 31 chars instead of 32 |
| 104 | + |
| 105 | + $this->expectException(\InvalidArgumentException::class); |
| 106 | + $this->expectExceptionMessage('Base32 alphabet must contain exactly 32 characters.'); |
| 107 | + |
| 108 | + Base32::toBase32('test', $shortAlphabet); |
| 109 | + } |
| 110 | + |
| 111 | + public function testDuplicateCharactersInAlphabetThrowsException(): void |
| 112 | + { |
| 113 | + $duplicateAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ23456A'; // 'A' appears twice |
| 114 | + |
| 115 | + $this->expectException(\InvalidArgumentException::class); |
| 116 | + $this->expectExceptionMessage('Base32 alphabet must contain unique characters.'); |
| 117 | + |
| 118 | + Base32::toBase32('test', $duplicateAlphabet); |
| 119 | + } |
| 120 | + |
| 121 | + public function testPaddingIsIgnoredDuringDecoding(): void |
| 122 | + { |
| 123 | + $paddedBase32 = 'MZXW6YTBOI======'; // "foobar" with padding |
| 124 | + $decoded = Base32::fromBase32($paddedBase32); |
| 125 | + |
| 126 | + $this->assertSame('foobar', $decoded); |
| 127 | + } |
| 128 | + |
| 129 | + public function testInvalidCharacterInMiddleThrowsException(): void |
| 130 | + { |
| 131 | + $invalidBase32 = 'MZXW@YTBOI'; // '@' is invalid |
| 132 | + |
| 133 | + $this->expectException(\InvalidArgumentException::class); |
| 134 | + $this->expectExceptionMessage("Invalid character '@' at position 4 in Base32 input."); |
| 135 | + |
| 136 | + Base32::fromBase32($invalidBase32); |
| 137 | + } |
| 138 | +} |
0 commit comments