*/ 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}> */ 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 */ 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 */ 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}> */ 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}> */ 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}> */ 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|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 : '')]; } }