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/CloudPortableRestoreCompati...

86 lines
2.6 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudPortableRestoreCompatibility;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$canonical = [
'contract' => 1,
'architecture' => 'x86_64',
'machine' => 'q35',
'firmware' => 'uefi',
'secure_boot' => false,
'disk_format' => 'qcow2',
'system_disk_only' => true,
'public_network_contract' => 1,
'vpc_inherited' => false,
];
$contract = CloudPortableRestoreCompatibility::fromPayload($canonical);
$assert($contract->toArray() === $canonical,
'Portable restore contract did not canonicalize exactly.');
$assert(strlen($contract->sha256()) === 64,
'Portable restore contract digest is invalid.');
$assert(CloudPortableRestoreCompatibility::fromStored(
$contract->json(), $contract->sha256()
)->matches($contract), 'Stored portable restore evidence did not round trip.');
foreach (array_keys($canonical) as $field) {
$broken = $canonical;
unset($broken[$field]);
try {
CloudPortableRestoreCompatibility::fromPayload($broken);
throw new RuntimeException("Missing {$field} was accepted.");
} catch (InvalidArgumentException) {
}
}
foreach ([
'contract' => 2,
'architecture' => 'aarch64',
'machine' => 'pc-i440fx',
'firmware' => 'bios',
'secure_boot' => true,
'disk_format' => 'raw',
'system_disk_only' => false,
'public_network_contract' => 2,
'vpc_inherited' => true,
] as $field => $wrong) {
$broken = $canonical;
$broken[$field] = $wrong;
try {
CloudPortableRestoreCompatibility::fromPayload($broken);
throw new RuntimeException("Wrong {$field} was accepted.");
} catch (InvalidArgumentException) {
}
}
$extra = $canonical + ['path' => '/private/root.qcow2'];
try {
CloudPortableRestoreCompatibility::fromPayload($extra);
throw new RuntimeException('An unbounded contract field was accepted.');
} catch (InvalidArgumentException) {
}
$reordered = array_reverse($canonical, true);
try {
CloudPortableRestoreCompatibility::fromStored(
json_encode($reordered, JSON_THROW_ON_ERROR),
$contract->sha256()
);
throw new RuntimeException('Non-canonical stored JSON was accepted.');
} catch (InvalidArgumentException) {
}
try {
CloudPortableRestoreCompatibility::fromStored(
$contract->json(), str_repeat('f', 64)
);
throw new RuntimeException('A tampered contract digest was accepted.');
} catch (InvalidArgumentException) {
}
echo "Portable restore compatibility contract passed.\n";