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.
126 lines
4.3 KiB
PHTML
126 lines
4.3 KiB
PHTML
|
2 weeks ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudIpAddress;
|
||
|
|
use App\Services\Cloud\CloudIpPrefix;
|
||
|
|
use App\Services\Cloud\CloudPublicNetworkSnapshot;
|
||
|
|
|
||
|
|
$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);
|
||
|
|
};
|
||
|
|
|
||
|
|
$ipv4 = CloudIpAddress::parse('198.51.100.10');
|
||
|
|
$assert($ipv4->family() === 4 && $ipv4->text() === '198.51.100.10'
|
||
|
|
&& strlen($ipv4->binary()) === 4,
|
||
|
|
'IPv4 canonicalization changed.');
|
||
|
|
$ipv6 = CloudIpAddress::parse('2404:0c140:0156:003a:0:0:0:1000');
|
||
|
|
$assert($ipv6->family() === 6
|
||
|
|
&& $ipv6->text() === '2404:c140:156:3a::1000'
|
||
|
|
&& strlen($ipv6->binary()) === 16,
|
||
|
|
'IPv6 RFC 5952 canonicalization changed.');
|
||
|
|
$rejects(static fn () => CloudIpAddress::parse('fe80::1%br0'),
|
||
|
|
'A scoped IPv6 address crossed the public-network contract.');
|
||
|
|
|
||
|
|
$pool6 = CloudIpPrefix::network('2404:c140:156:3a::/64');
|
||
|
|
$assert($pool6->family() === 6 && $pool6->prefixLength() === 64
|
||
|
|
&& $pool6->canonical() === '2404:c140:156:3a::/64'
|
||
|
|
&& $pool6->contains($ipv6),
|
||
|
|
'IPv6 pool containment changed.');
|
||
|
|
$assert($pool6->overlaps(CloudIpPrefix::network(
|
||
|
|
'2404:c140:156:3a:8000::/65'
|
||
|
|
)) && !$pool6->overlaps(CloudIpPrefix::network(
|
||
|
|
'2404:c140:156:3b::/64'
|
||
|
|
)),
|
||
|
|
'IPv6 prefix-overlap detection changed.');
|
||
|
|
$rejects(static fn () => CloudIpPrefix::network(
|
||
|
|
'2404:c140:156:3a::1/64'
|
||
|
|
), 'A non-canonical network prefix was accepted.');
|
||
|
|
|
||
|
|
$snapshot = CloudPublicNetworkSnapshot::fromArray([
|
||
|
|
'contract' => 1,
|
||
|
|
'network_selector' => 'bridge:br0',
|
||
|
|
'ipv4' => [
|
||
|
|
'pool_key' => str_repeat('a', 32),
|
||
|
|
'pool_cidr' => '8.8.8.0/24',
|
||
|
|
'address_cidr' => '8.8.8.10/24',
|
||
|
|
'gateway' => '8.8.8.1',
|
||
|
|
'gateway_on_link' => false,
|
||
|
|
],
|
||
|
|
'ipv6' => [
|
||
|
|
'pool_key' => str_repeat('b', 32),
|
||
|
|
'pool_cidr' => '2404:c140:156:3a::/64',
|
||
|
|
'address_cidr' => '2404:0c140:0156:003a::1000/64',
|
||
|
|
'gateway' => '2404:c140:156::1',
|
||
|
|
'gateway_on_link' => true,
|
||
|
|
],
|
||
|
|
'dns_servers' => [
|
||
|
|
'1.1.1.1',
|
||
|
|
'2606:4700:4700:0000:0000:0000:0000:1111',
|
||
|
|
],
|
||
|
|
]);
|
||
|
|
$expected = [
|
||
|
|
'contract' => 1,
|
||
|
|
'network_selector' => 'bridge:br0',
|
||
|
|
'ipv4' => [
|
||
|
|
'pool_key' => str_repeat('a', 32),
|
||
|
|
'pool_cidr' => '8.8.8.0/24',
|
||
|
|
'address_cidr' => '8.8.8.10/24',
|
||
|
|
'gateway' => '8.8.8.1',
|
||
|
|
'gateway_on_link' => false,
|
||
|
|
],
|
||
|
|
'ipv6' => [
|
||
|
|
'pool_key' => str_repeat('b', 32),
|
||
|
|
'pool_cidr' => '2404:c140:156:3a::/64',
|
||
|
|
'address_cidr' => '2404:c140:156:3a::1000/64',
|
||
|
|
'gateway' => '2404:c140:156::1',
|
||
|
|
'gateway_on_link' => true,
|
||
|
|
],
|
||
|
|
'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'],
|
||
|
|
];
|
||
|
|
$assert($snapshot->toArray() === $expected,
|
||
|
|
'The frozen dual-stack network snapshot is not canonical.');
|
||
|
|
$assert($snapshot->fingerprint() === hash('sha256', json_encode(
|
||
|
|
$expected,
|
||
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
|
||
|
|
)), 'The network fingerprint is not derived from the canonical contract.');
|
||
|
|
|
||
|
|
$rejects(static fn () => CloudPublicNetworkSnapshot::fromArray([
|
||
|
|
'contract' => 1,
|
||
|
|
'network_selector' => 'bridge:br0',
|
||
|
|
'ipv4' => $expected['ipv4'],
|
||
|
|
'ipv6' => array_merge($expected['ipv6'], [
|
||
|
|
'gateway_on_link' => false,
|
||
|
|
]),
|
||
|
|
'dns_servers' => $expected['dns_servers'],
|
||
|
|
]), 'An off-prefix IPv6 gateway was accepted without on-link intent.');
|
||
|
|
$rejects(static fn () => CloudPublicNetworkSnapshot::fromArray([
|
||
|
|
'contract' => 1,
|
||
|
|
'network_selector' => 'bridge:br0',
|
||
|
|
'ipv4' => $expected['ipv4'],
|
||
|
|
'dns_servers' => $expected['dns_servers'],
|
||
|
|
]), 'An IPv4-only network crossed the new-instance contract.');
|
||
|
|
$rejects(static fn () => CloudPublicNetworkSnapshot::fromArray([
|
||
|
|
'contract' => 1,
|
||
|
|
'network_selector' => 'bridge:br0',
|
||
|
|
'ipv4' => array_merge($expected['ipv4'], [
|
||
|
|
'pool_cidr' => '198.51.100.0/24',
|
||
|
|
'address_cidr' => '198.51.100.10/24',
|
||
|
|
'gateway' => '198.51.100.1',
|
||
|
|
]),
|
||
|
|
'ipv6' => $expected['ipv6'],
|
||
|
|
'dns_servers' => $expected['dns_servers'],
|
||
|
|
]), 'A special-use IPv4 range crossed the public-network contract.');
|
||
|
|
|
||
|
|
echo "Dual-stack public network contract tests passed.\n";
|