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.
virt/web/tests/CloudPublicNetworkAddressPo...

178 lines
5.0 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudIpAddress;
use App\Services\Cloud\CloudPublicNetworkAddressPolicy;
$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 (object $pool, string $address): object {
return (object) [
'host_id' => (int) $pool->host_id,
'public_ip_pool_id' => (int) $pool->id,
'address_family' => (int) $pool->address_family,
'address' => $address,
'address_binary' => inet_pton($address),
'prefix_length' => (int) $pool->guest_prefix_length,
];
};
$ipv4 = (object) [
'id' => 10,
'pool_key' => str_repeat('a', 32),
'host_id' => 1,
'address_family' => 4,
'cidr' => '8.8.8.0/24',
'gateway' => '8.8.8.1',
'gateway_on_link' => 0,
'guest_prefix_length' => 24,
'dns_servers_json' => '["1.1.1.1","8.8.8.8"]',
'reserved_ips_json' => '["8.8.8.9"]',
'allocation_start' => null,
'allocation_end' => null,
'allocation_cursor' => null,
'max_allocations' => 0,
];
$ipv6 = (object) [
'id' => 20,
'pool_key' => str_repeat('b', 32),
'host_id' => 1,
'address_family' => 6,
'cidr' => '2404:c140:156:3a::/64',
'gateway' => '2404:c140:156:3a::1',
'gateway_on_link' => 0,
'guest_prefix_length' => 64,
'dns_servers_json' => '["2606:4700:4700::1111"]',
'reserved_ips_json' => '["2404:c140:156:3a::5"]',
'allocation_start' => '2404:c140:156:3a::10',
'allocation_end' => '2404:c140:156:3a::ff',
'allocation_cursor' => null,
'max_allocations' => 8,
];
$v4Contract = CloudPublicNetworkAddressPolicy::contract($ipv4, 4);
$v6Contract = CloudPublicNetworkAddressPolicy::contract($ipv6, 6);
$v4 = $row($ipv4, '8.8.8.10');
$v6 = $row($ipv6, '2404:c140:156:3a::10');
$assert(
CloudPublicNetworkAddressPolicy::assertSelectable(
$v4,
$ipv4,
$v4Contract,
null
)->text() === '8.8.8.10'
&& CloudPublicNetworkAddressPolicy::assertSelectable(
$v6,
$ipv6,
$v6Contract,
'103.138.75.126'
)->text() === '2404:c140:156:3a::10',
'Canonical IPv4/IPv6 candidates were not accepted.'
);
foreach (['8.8.8.1', '8.8.8.8', '8.8.8.9'] as $excluded) {
$excludedRow = $row($ipv4, $excluded);
$assert(
CloudPublicNetworkAddressPolicy::assertIdentity(
$excludedRow,
$ipv4,
$v4Contract
)->text() === $excluded,
'Immutable identity rejected a historical excluded address.'
);
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
$excludedRow,
$ipv4,
$v4Contract,
null
),
'A gateway or reserved IPv4 was selectable.'
);
}
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
$v4,
$ipv4,
$v4Contract,
'8.8.8.10'
),
'The host management IPv4 was selectable.'
);
$historicalV6 = $row($ipv6, '2404:c140:156:3a::6');
$assert(
CloudPublicNetworkAddressPolicy::assertIdentity(
$historicalV6,
$ipv6,
$v6Contract
)->text() === '2404:c140:156:3a::6',
'Immutable identity rejected a historical out-of-range IPv6.'
);
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
$historicalV6,
$ipv6,
$v6Contract,
null
),
'An IPv6 outside the sparse allocation range was selectable.'
);
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::assertSelectable(
$row($ipv4, '1.1.1.1'),
$ipv4,
$v4Contract,
null
),
'An out-of-pool DNS address passed row identity.'
);
foreach (['host_id', 'public_ip_pool_id', 'address_family', 'prefix_length'] as $field) {
$broken = clone $v4;
$broken->{$field} = (int) $broken->{$field} + 1;
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::assertIdentity(
$broken,
$ipv4,
$v4Contract
),
'A mismatched ' . $field . ' passed immutable identity.'
);
}
$brokenBinary = clone $v4;
$brokenBinary->address_binary = inet_pton('8.8.8.11');
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::assertIdentity(
$brokenBinary,
$ipv4,
$v4Contract
),
'A mismatched address binary passed immutable identity.'
);
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::contract($ipv6, 4),
'A pool contract was accepted for the wrong family.'
);
$rejects(
static fn () => CloudPublicNetworkAddressPolicy::addressSelectable(
CloudIpAddress::parse('8.8.8.10'),
$v4Contract,
'not-an-address'
),
'An invalid management address was silently ignored.'
);
echo "Public-network address policy tests passed.\n";