Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\DataModelValidator;use Wotnak\Atproto\Lexicon\ValidationContext;
/** * Byte arrays and CIDs use Lex JSON bytes and link objects. PHP has no undefined * value or separate scalar, list, and map predicates. The recursive stress case * uses 100,000 levels because PHP arrays require more memory while retaining * the stack-safety check. */final class DataModelValidatorTest extends TestCase {
private const CID = 'bafyreidfayvfuwqa7qlnopdjiqrxzs6blmoeu4rujcjtnci5beludirz2a';
#[DataProvider('validScalarProvider')] public function testAcceptsPortableLexScalarValues(mixed $value): void { static::assertTrue(self::isValidValue($value)); }
/** @return array<string, array{mixed}> */ public static function validScalarProvider(): array { return [ 'string' => ['hello'], 'boolean' => [TRUE], 'null' => [NULL], 'bytes' => [['$bytes' => 'AQID']], 'CID' => [['$link' => self::CID]], 'integer' => [42], 'empty object' => [new \stdClass()], ]; }
public function testRejectsFloatAsLexScalar(): void { static::assertFalse(self::isValidValue(3.14)); }
public function testAcceptsValidLexArray(): void { static::assertTrue(self::isValidValue([ 123, 'blah', TRUE, NULL, ['$bytes' => 'AQID'], ['a' => 1], ])); }
#[DataProvider('invalidArrayValueProvider')] public function testRejectsArraysContainingNonLexValues(array $value): void { static::assertFalse(self::isValidValue($value)); }
/** @return array<string, array{array<mixed>}> */ public static function invalidArrayValueProvider(): array { return [ 'function' => [[123, 'blah', static fn (): bool => TRUE]], 'nested function' => [[123, ['invalid' => static fn (): bool => TRUE]]], ]; }
#[DataProvider('validLexValueProvider')] public function testAcceptsPortableLexValues(mixed $value): void { static::assertTrue(self::isValidValue($value)); }
/** @return array<string, array{mixed}> */ public static function validLexValueProvider(): array { return [ 'string' => ['hello'], 'boolean' => [TRUE], 'null' => [NULL], 'bytes' => [['$bytes' => 'AQID']], 'CID' => [['$link' => self::CID]], 'record' => [[ 'a' => 123, 'b' => 'blah', 'c' => TRUE, 'd' => NULL, 'e' => ['$bytes' => 'AQID'], 'f' => ['nested' => 'value'], 'g' => [1, 2, 3], ]], 'list' => [[ 123, 'blah', TRUE, NULL, ['$bytes' => 'AQID'], ['nested' => 'value'], [1, 2, 3], ]], ]; }
#[DataProvider('invalidLexValueProvider')] public function testRejectsPortableInvalidLexValues(mixed $value): void { static::assertFalse(self::isValidValue($value)); }
/** @return array<string, array{mixed}> */ public static function invalidLexValueProvider(): array { return [ 'float' => [123.456], 'function' => [static fn (): bool => TRUE], 'object with function' => [['a' => 123, 'b' => static fn (): bool => TRUE]], 'list with function' => [[123, 'blah', static fn (): bool => TRUE]], 'date object' => [new \DateTimeImmutable()], 'map object' => [new \ArrayObject()], 'set object' => [new \SplObjectStorage()], ]; }
public function testRejectsSelfReferentialRecordWithoutHanging(): void { $script = <<<'PHP' require $argv[1]; $value = ['a' => 123, 'b' => 'blah']; $value['c'] = &$value; $context = new Wotnak\Atproto\Lexicon\ValidationContext(); (new Wotnak\Atproto\Lexicon\DataModelValidator())->validate($value, $context); exit($context->violations() === [] ? 1 : 0); PHP;
[$exitCode, $error] = self::runIsolated($script, '48M');
static::assertSame(0, $exitCode, $error); }
public function testRejectsSelfReferentialListWithoutHanging(): void { $script = <<<'PHP' require $argv[1]; $value = [123, 'blah']; $value[] = &$value; $context = new Wotnak\Atproto\Lexicon\ValidationContext(); (new Wotnak\Atproto\Lexicon\DataModelValidator())->validate($value, $context); exit($context->violations() === [] ? 1 : 0); PHP;
[$exitCode, $error] = self::runIsolated($script, '48M');
static::assertSame(0, $exitCode, $error); }
public function testRejectsIndirectCycleWithoutHanging(): void { $script = <<<'PHP' require $argv[1]; $value = ['a' => ['b' => [1, 2, 3]]]; $value['a']['b'][] = &$value; $context = new Wotnak\Atproto\Lexicon\ValidationContext(); (new Wotnak\Atproto\Lexicon\DataModelValidator())->validate($value, $context); exit($context->violations() === [] ? 1 : 0); PHP;
[$exitCode, $error] = self::runIsolated($script, '48M');
static::assertSame(0, $exitCode, $error); }
public function testHandlesDeeplyNestedStructureWithoutCallStackFailure(): void { $script = <<<'PHP' require $argv[1]; $value = null; for ($index = 0; $index < 100_000; $index++) { $value = ['nested' => $value]; } $context = new Wotnak\Atproto\Lexicon\ValidationContext(); (new Wotnak\Atproto\Lexicon\DataModelValidator())->validate($value, $context); exit($context->violations() === [] ? 0 : 1); PHP;
[$exitCode, $error] = self::runIsolated($script, '256M');
static::assertSame(0, $exitCode, $error); }
public function testAcceptsUntypedLexMapAsDataModelValue(): void { static::assertTrue(self::isValidValue([ 'a' => 123, 'b' => 'blah', 'c' => TRUE, 'd' => NULL, 'e' => ['$bytes' => 'AQID'], 'f' => ['nested' => 'value'], 'g' => [1, 2, 3], ])); }
#[DataProvider('invalidMapValueProvider')] public function testRejectsMapContainingNonLexValues(array $value): void { static::assertFalse(self::isValidValue($value)); }
/** @return array<string, array{array<string, mixed>}> */ public static function invalidMapValueProvider(): array { return [ 'function' => [['a' => 123, 'b' => static fn (): bool => TRUE]], ]; }
#[DataProvider('validTypedMapProvider')] public function testAcceptsValidTypedLexMap(array $value): void { static::assertTrue( new DataModelValidator() ->validateJsonDocument($value) ->isValid(), ); }
/** @return array<string, array{array<string, mixed>}> */ public static function validTypedMapProvider(): array { return [ 'trivial record' => [[ '$type' => 'com.example.blah', 'a' => 123, 'b' => 'blah', ]], 'integer-like float' => [[ '$type' => 'com.example.blah', 'a' => 123.0, 'b' => 'blah', ]], 'empty list and object' => [[ '$type' => 'com.example.blah', 'a' => [], 'b' => [], ]], ]; }
#[DataProvider('invalidTypedMapProvider')] public function testRejectsInvalidTypedLexMap(array $value): void { static::assertFalse( new DataModelValidator() ->validateJsonDocument($value) ->isValid(), ); }
/** @return array<string, array{array<string, mixed>}> */ public static function invalidTypedMapProvider(): array { return [ 'float' => [[ '$type' => 'com.example.blah', 'a' => 123.456, 'b' => 'blah', ]], 'null type' => [[ '$type' => NULL, 'a' => 123, 'b' => 'blah', ]], 'integer type' => [[ '$type' => 123, 'a' => 123, 'b' => 'blah', ]], 'empty type' => [[ '$type' => '', 'a' => 123, 'b' => 'blah', ]], ]; }
private static function isValidValue(mixed $value): bool { $context = new ValidationContext(); new DataModelValidator()->validate($value, $context); return $context->violations() === []; }
/** * @return array{int, string} */ private static function runIsolated(string $script, string $memoryLimit): array { $descriptorSpec = [ 0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w'], ]; /** @var array<int, resource>|null $pipes */ $pipes = NULL; $process = \proc_open( [ PHP_BINARY, '-d', 'memory_limit=' . $memoryLimit, '-d', 'max_execution_time=10', '-r', $script, \dirname(__DIR__, 2) . '/vendor/autoload.php', ], $descriptorSpec, $pipes, ); if ( !\is_resource($process) || !isset($pipes[0], $pipes[1], $pipes[2]) ) { throw new \RuntimeException('Unable to start the isolated validation process.'); } \fclose($pipes[0]); $output = \stream_get_contents($pipes[1]); $error = \stream_get_contents($pipes[2]); \fclose($pipes[1]); \fclose($pipes[2]); $exitCode = \proc_close($process); return [$exitCode, ($output !== FALSE ? $output : '') . ($error !== FALSE ? $error : '')]; }
}