|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the ATProto Core package. |
| 5 | + * |
| 6 | + * (c) Core Branch |
| 7 | + * |
| 8 | + * This source code is licensed under the MIT license found in the |
| 9 | + * LICENSE file in the root directory of this source tree. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Tests\Unit\CBOR\MajorTypes; |
| 13 | + |
| 14 | +use ATProto\Core\CBOR\MajorTypes\TextString; |
| 15 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +class TextStringTest extends TestCase |
| 19 | +{ |
| 20 | + #[DataProvider('provideValidCases')] |
| 21 | + public function testEncodeProducesCorrectCBORRepresentation(string $input, string $expectedHeader): void |
| 22 | + { |
| 23 | + $actual = bin2hex(TextString::encode($input)); |
| 24 | + $expected = bin2hex($expectedHeader . $input); |
| 25 | + |
| 26 | + $this->assertSame($expected, $actual); |
| 27 | + } |
| 28 | + |
| 29 | + #[DataProvider('provideValidCases')] |
| 30 | + public function testDecodeExtractsOriginalStringFromCBORRepresentation(string $input, string $header): void |
| 31 | + { |
| 32 | + $encoded = $header . $input; |
| 33 | + |
| 34 | + $actual = TextString::decode($encoded); |
| 35 | + $expected = $input; |
| 36 | + |
| 37 | + $this->assertSame($expected, $actual); |
| 38 | + } |
| 39 | + |
| 40 | + public static function provideValidCases(): array |
| 41 | + { |
| 42 | + return [ |
| 43 | + ["f", "\x61"], |
| 44 | + ["fo", "\x62"], |
| 45 | + ["foo", "\x63"], |
| 46 | + ["foob", "\x64"], |
| 47 | + ["fooba", "\x65"], |
| 48 | + ["foobar", "\x66"], |
| 49 | + [ |
| 50 | + "This is a longer string. This is a longer string. This is a longer string. This is a longer string.", |
| 51 | + "\x78\x63" |
| 52 | + ], |
| 53 | + [ |
| 54 | + "This is a longer string. This is a longer string. This is a longer string. This is a longer string. |
| 55 | + This is a longer string. This is a longer string. This is a longer string. This is a longer string. |
| 56 | + This is a longer string. This is a longer string. This is a longer string. This is a longer string.", |
| 57 | + "\x79\x01\x4D" |
| 58 | + ] |
| 59 | + ]; |
| 60 | + } |
| 61 | + |
| 62 | + public function testDecodeThrowsExceptionForInvalidMajorType(): void |
| 63 | + { |
| 64 | + $this->expectException(\ValueError::class); |
| 65 | + $this->expectExceptionMessage("Invalid CBOR TextString major type."); |
| 66 | + |
| 67 | + TextString::decode("\x0C"); |
| 68 | + } |
| 69 | + |
| 70 | + public function testDecodeThrowsExceptionForLengthMismatch(): void |
| 71 | + { |
| 72 | + $this->expectException(\ValueError::class); |
| 73 | + $this->expectExceptionMessage("Invalid CBOR TextString length mismatch."); |
| 74 | + |
| 75 | + // Encoded length is 5, but actual length is 4 |
| 76 | + TextString::decode("\x65\x66\x6F\x6F\x62"); |
| 77 | + } |
| 78 | + |
| 79 | + public function testDecodeThrowsExceptionForInvalidAdditionalInformation(): void |
| 80 | + { |
| 81 | + $this->expectException(\ValueError::class); |
| 82 | + $this->expectExceptionMessage("Invalid CBOR TextString length information."); |
| 83 | + |
| 84 | + // Additional info 28 is invalid for text strings |
| 85 | + TextString::decode("\x7C\x01"); |
| 86 | + } |
| 87 | +} |
0 commit comments