Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit\Schema;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\Schema\BytesSchema;
/** * Standard padded and unpadded base64 use the Lex JSON bytes object. BytesSchema * validates that representation and exposes no binary codec. */final class BytesSchemaTest extends TestCase {
#[DataProvider('invalidShapeProvider')] public function testRejectsNonBytesObjects(mixed $value): void { $result = new BytesSchema()->safeValidate($value);
static::assertFalse($result->isValid()); static::assertSame(['invalid_bytes'], \array_column($result->violations, 'code')); }
/** @return array<string, array{mixed}> */ public static function invalidShapeProvider(): array { return [ 'null' => [NULL], 'string' => ['Hello'], 'number' => [42], 'list' => [[72, 101, 108, 108, 111]], 'plain object' => [['value' => 'SGVsbG8=']], 'non-string payload' => [['$bytes' => [72, 101]]], 'extra field' => [['$bytes' => 'SGVsbG8=', 'extra' => TRUE]], ]; }
#[DataProvider('encodingProvider')] public function testRequiresCanonicalBase64(string $encoded, bool $valid): void { $result = new BytesSchema()->safeValidate(['$bytes' => $encoded]);
static::assertSame($valid, $result->isValid()); }
/** * @return array<string, array{string, bool}> */ public static function encodingProvider(): array { return [ 'canonical padded' => ['Zg==', TRUE], 'canonical unpadded' => ['Zg', TRUE], 'empty bytes' => ['', TRUE], 'whitespace' => ['Z g==', FALSE], 'non-zero padding bits' => ['Zh==', FALSE], 'partial padding' => ['Zg=', FALSE], ]; }
/** @param list<string> $codes */ #[DataProvider('lengthProvider')] public function testEnforcesDecodedByteLength(string $encoded, bool $valid, array $codes): void { $result = new BytesSchema(minLength: 2, maxLength: 4)->safeValidate(['$bytes' => $encoded]);
static::assertSame($valid, $result->isValid()); static::assertSame($codes, \array_column($result->violations, 'code')); }
/** @return array<string, array{string, bool, list<string>}> */ public static function lengthProvider(): array { return [ 'below minimum' => [\base64_encode("\x01"), FALSE, ['min_length']], 'at minimum' => [\base64_encode("\x01\x02"), TRUE, []], 'within range' => [\base64_encode("\x01\x02\x03"), TRUE, []], 'at maximum' => [\base64_encode("\x01\x02\x03\x04"), TRUE, []], 'above maximum' => [\base64_encode("\x01\x02\x03\x04\x05"), FALSE, ['max_length']], ]; }
public function testZeroLengthBounds(): void { static::assertTrue(new BytesSchema(minLength: 0)->safeValidate(['$bytes' => ''])->isValid()); static::assertTrue(new BytesSchema(maxLength: 0)->safeValidate(['$bytes' => ''])->isValid()); static::assertFalse(new BytesSchema(maxLength: 0)->safeValidate(['$bytes' => 'AA=='])->isValid()); }
#[DataProvider('canonicalBase64VectorProvider')] public function testAcceptsCanonicalBase64Vectors(string $encoded): void { static::assertTrue(new BytesSchema()->safeValidate(['$bytes' => $encoded])->isValid()); }
/** @return iterable<string, array{string}> */ public static function canonicalBase64VectorProvider(): iterable { $values = [ '', "\0\0", "\0\0\0", "\0\0\0\0", '__', 'é', 'àç', "\0éàç", "```\x1B", 'aaa', 'Hello, World!', '😀😃😄😁😆😅😂🤣😊😇', '👩💻👨💻👩🔬👨🔬👩🚀👨🚀', '🌍🌎🌏🌐🪐🌟✨⚡🔥💧', "\xFB\xFF\xBF", "\xFB\xFF\xBF", "\x4D", "\x4D\x61", "\x4D\x61\x6E", "\x4D", "\x4D\x61", "\x00\x4D\x61\x6E\x00", ]; foreach ($values as $index => $value) { $padded = \base64_encode($value); yield 'padded ' . $index => [$padded]; $unpadded = \rtrim($padded, '='); if ($padded !== $unpadded) { yield 'unpadded ' . $index => [$unpadded]; } } }
#[DataProvider('invalidBase64VectorProvider')] public function testRejectsInvalidBase64Vectors(string $encoded): void { static::assertFalse(new BytesSchema()->safeValidate(['$bytes' => $encoded])->isValid()); }
/** @return array<string, array{string}> */ public static function invalidBase64VectorProvider(): array { return [ 'cedilla pair' => ['çç'], 'accented character' => ['é'], 'trailing dollars' => ['YWJjZGU$$$'], 'at signs' => ['@@@@'], 'punctuation suffix' => ['abcd!'], 'interior padding' => ['ab=cd'], 'valid then one accent' => ['YWFhé'], 'valid then two accents' => ['YWFhéé'], 'valid then three accents' => ['YWFhééé'], 'valid then four accents' => ['YWFhéééé'], 'unneeded one padding' => ['YWFh='], 'unneeded two padding' => ['YWFh=='], 'unneeded three padding' => ['YWFh==='], 'unneeded four padding' => ['YWFh===='], 'unneeded five padding' => ['YWFh====='], 'unneeded six padding' => ['YWFh======'], 'accent in payload' => ['TWEé'], 'emoji in payload' => ['TWE👍'], 'one excessive padding for Ma' => ['TWE=='], 'two excessive padding for Ma' => ['TWE==='], 'one excessive padding for M' => ['TQ==='], 'two excessive padding for M' => ['TQ===='], ]; }
public function testAcceptsTenMegabyteBase64Payload(): void { $bytes = \str_repeat("\xF0\x9F\x90\xA9", 2_500_000);
static::assertTrue( new BytesSchema() ->safeValidate(['$bytes' => \base64_encode($bytes)]) ->isValid(), ); }
}