You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
5.1 KiB
PHP
151 lines
5.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudIpAddress;
|
|
use App\Services\Cloud\CloudPublicIpPoolContract;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$rejects = static function (callable $callback, string $message): void {
|
|
try {
|
|
$callback();
|
|
} catch (InvalidArgumentException) {
|
|
return;
|
|
}
|
|
throw new RuntimeException($message);
|
|
};
|
|
|
|
$row = static function (int $family): object {
|
|
return (object) ($family === 4 ? [
|
|
'pool_key' => str_repeat('a', 32),
|
|
'address_family' => 4,
|
|
'cidr' => '8.8.8.0/24',
|
|
'gateway' => '8.8.8.1',
|
|
'gateway_on_link' => false,
|
|
'guest_prefix_length' => 24,
|
|
'dns_servers_json' => '["1.1.1.1","2606:4700:4700::1111"]',
|
|
'reserved_ips_json' => '["8.8.8.2"]',
|
|
'allocation_start' => null,
|
|
'allocation_end' => null,
|
|
'allocation_cursor' => null,
|
|
'max_allocations' => 0,
|
|
] : [
|
|
'pool_key' => str_repeat('b', 32),
|
|
'address_family' => 6,
|
|
'cidr' => '2404:c140:156:3a::/64',
|
|
'gateway' => '2404:c140:156::1',
|
|
'gateway_on_link' => true,
|
|
'guest_prefix_length' => 64,
|
|
'dns_servers_json' => '["1.1.1.1","2606:4700:4700::1111"]',
|
|
'reserved_ips_json' => '["2404:c140:156:3a::1002"]',
|
|
'allocation_start' => '2404:c140:156:3a::1000',
|
|
'allocation_end' => '2404:c140:156:3a::10ff',
|
|
'allocation_cursor' => '2404:c140:156:3a::1000',
|
|
'max_allocations' => 64,
|
|
]);
|
|
};
|
|
|
|
$ipv4 = CloudPublicIpPoolContract::fromRow($row(4));
|
|
$ipv6 = CloudPublicIpPoolContract::fromRow($row(6));
|
|
$assert($ipv4->family() === 4
|
|
&& $ipv4->addressAllowed(CloudIpAddress::parse('8.8.8.20'))
|
|
&& !$ipv4->addressAllowed(CloudIpAddress::parse('8.8.8.2')),
|
|
'The canonical IPv4 pool contract was not projected correctly.');
|
|
$assert($ipv6->family() === 6
|
|
&& $ipv6->gatewayOnLink()
|
|
&& $ipv6->allocationStart()?->text()
|
|
=== '2404:c140:156:3a::1000'
|
|
&& $ipv6->addressAllowed(
|
|
CloudIpAddress::parse('2404:c140:156:3a::1003')
|
|
), 'The canonical off-prefix IPv6 gateway contract was rejected.');
|
|
|
|
foreach ([
|
|
'family mismatch' => static function (object $pool): void {
|
|
$pool->address_family = 6;
|
|
},
|
|
'noncanonical cidr' => static function (object $pool): void {
|
|
$pool->cidr = '8.8.8.0/023';
|
|
},
|
|
'out-of-policy prefix' => static function (object $pool): void {
|
|
$pool->cidr = '8.8.8.0/23';
|
|
$pool->guest_prefix_length = 23;
|
|
},
|
|
'guest prefix drift' => static function (object $pool): void {
|
|
$pool->guest_prefix_length = 25;
|
|
},
|
|
'IPv4 on-link gateway' => static function (object $pool): void {
|
|
$pool->gateway_on_link = true;
|
|
},
|
|
'IPv4 invalid boolean gateway flag' => static function (object $pool): void {
|
|
$pool->gateway_on_link = 2;
|
|
},
|
|
'IPv4 off-pool gateway' => static function (object $pool): void {
|
|
$pool->gateway = '8.8.9.1';
|
|
},
|
|
'private DNS' => static function (object $pool): void {
|
|
$pool->dns_servers_json = '["10.0.0.1"]';
|
|
},
|
|
'wrong-family reserved address' => static function (object $pool): void {
|
|
$pool->reserved_ips_json = '["2606:4700:4700::1111"]';
|
|
},
|
|
'IPv4 sparse range' => static function (object $pool): void {
|
|
$pool->allocation_start = '8.8.8.10';
|
|
},
|
|
] as $label => $mutate) {
|
|
$pool = $row(4);
|
|
$mutate($pool);
|
|
$rejects(
|
|
static fn () => CloudPublicIpPoolContract::fromRow($pool),
|
|
'Invalid IPv4 pool accepted: ' . $label
|
|
);
|
|
}
|
|
|
|
foreach ([
|
|
'off-pool gateway without on-link' => static function (object $pool): void {
|
|
$pool->gateway_on_link = false;
|
|
},
|
|
'IPv6 invalid boolean gateway flag' => static function (object $pool): void {
|
|
$pool->gateway_on_link = 'false';
|
|
},
|
|
'range outside pool' => static function (object $pool): void {
|
|
$pool->allocation_end = '2404:c140:156:3b::10ff';
|
|
},
|
|
'cursor outside range' => static function (object $pool): void {
|
|
$pool->allocation_cursor = '2404:c140:156:3a::1100';
|
|
},
|
|
'maximum below one' => static function (object $pool): void {
|
|
$pool->max_allocations = 0;
|
|
},
|
|
'maximum above contract' => static function (object $pool): void {
|
|
$pool->max_allocations = 65536;
|
|
},
|
|
'wrong-family reserved address' => static function (object $pool): void {
|
|
$pool->reserved_ips_json = '["8.8.8.2"]';
|
|
},
|
|
] as $label => $mutate) {
|
|
$pool = $row(6);
|
|
$mutate($pool);
|
|
$rejects(
|
|
static fn () => CloudPublicIpPoolContract::fromRow($pool),
|
|
'Invalid IPv6 pool accepted: ' . $label
|
|
);
|
|
}
|
|
|
|
$large = $row(6);
|
|
$large->allocation_end = '2404:c140:156:3a::1:1002';
|
|
$large->max_allocations = 65535;
|
|
$started = microtime(true);
|
|
for ($iteration = 0; $iteration < 100; $iteration++) {
|
|
CloudPublicIpPoolContract::fromRow($large);
|
|
}
|
|
$assert(microtime(true) - $started < 1.0,
|
|
'Maximum sparse-capacity validation regressed to per-address scanning.');
|
|
|
|
echo "Shared public-IP pool contract tests passed.\n";
|