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

138 lines
4.5 KiB
PHTML

<?php
define('BASE_PATH', sys_get_temp_dir() . '/virt-cloud-local-upload-'
. bin2hex(random_bytes(8)));
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudLocalUploadService;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$expect = static function (callable $callable, int $status,
string $message) use ($assert): void {
try {
$callable();
} catch (CloudError $error) {
$assert($error->httpStatus === $status,
$message . ' Unexpected status: ' . $error->httpStatus);
return;
}
throw new RuntimeException($message);
};
$request = static function (string $body, string $contentType,
?string $declaredLength = null): object {
return new class($body, $contentType, $declaredLength) {
private string $body;
private string $contentType;
private ?string $declaredLength;
public function __construct(string $body, string $contentType,
?string $declaredLength)
{
$this->body = $body;
$this->contentType = $contentType;
$this->declaredLength = $declaredLength;
}
public function getHeaderLine(string $name): string
{
return match (strtolower($name)) {
'content-type' => $this->contentType,
'content-length' => $this->declaredLength ?? '',
default => '',
};
}
public function getBody(): object
{
return new class($this->body) {
private string $body;
private int $offset = 0;
public function __construct(string $body)
{
$this->body = $body;
}
public function isSeekable(): bool
{
return true;
}
public function rewind(): void
{
$this->offset = 0;
}
public function eof(): bool
{
return $this->offset >= strlen($this->body);
}
public function read(int $length): string
{
$chunk = substr($this->body, $this->offset, $length);
$this->offset += strlen($chunk);
return $chunk;
}
};
}
};
};
$payload = "qcow2\0test-image";
$capture = CloudLocalUploadService::capture(
$request($payload, 'application/octet-stream; charset=binary',
(string) strlen($payload)),
'qcow2', 1, 1024
);
$assert(is_file($capture['path'])
&& (int) $capture['size_bytes'] === strlen($payload)
&& hash_file('sha256', $capture['path']) === $capture['sha256']
&& preg_match('/^[a-f0-9]{64}\.qcow2$/D',
$capture['file_key']) === 1,
'Local upload did not create a bounded private capture.');
$expect(static fn () => CloudLocalUploadService::capture(
$request('invalid', 'text/plain', '7'), 'qcow2', 1, 1024
), 415, 'Local upload accepted a non-binary content type.');
$expect(static fn () => CloudLocalUploadService::capture(
$request('mismatch', 'application/octet-stream', '7'),
'qcow2', 1, 1024
), 400, 'Local upload accepted a false Content-Length.');
try {
CloudLocalUploadService::capture(
$request('iso', 'application/octet-stream', '3'),
'iso', 1, 1024
);
throw new RuntimeException('Retired rescue ISO upload was accepted.');
} catch (InvalidArgumentException $expected) {
// Rescue ISO imports are HTTPS-only and never browser-uploaded.
}
CloudLocalUploadService::discard($capture);
$assert(!is_file($capture['path']),
'Local upload capture survived explicit discard.');
$routes = file_get_contents(dirname(__DIR__) . '/config/routes_cloud.php');
$controller = file_get_contents(
dirname(__DIR__) . '/app/Controllers/CloudAdminController.php'
);
$assert(is_string($routes) && is_string($controller)
&& !str_contains($routes, '/api/internal/cloud/upload-staging/')
&& !str_contains($routes, '/admin/rescue-templates/upload')
&& !str_contains($controller, 'rescueUpload(')
&& !str_contains($controller, "'rescue-template'"),
'Retired rescue upload transport is still reachable.');
@rmdir(BASE_PATH . '/storage/cloud-local-upload');
@rmdir(BASE_PATH . '/storage');
@rmdir(BASE_PATH);
echo "Cloud local upload boundary passed.\n";