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.
136 lines
5.0 KiB
PHP
136 lines
5.0 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudStorageReplicaHelper;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$operationId = str_repeat('a', 32);
|
|
$request = [
|
|
'protocol' => 'object-replica-v1',
|
|
'operation_id' => $operationId,
|
|
'deadline_at' => time() + 60,
|
|
];
|
|
$helper = new CloudStorageReplicaHelper(null,
|
|
static fn (array $input): array => [
|
|
'protocol' => $input['protocol'],
|
|
'operation_id' => $input['operation_id'],
|
|
'outcome' => 'succeeded',
|
|
'effect_resolution' => 'applied',
|
|
'size_bytes' => 4096,
|
|
'sha256' => str_repeat('b', 64),
|
|
'etag' => '"etag"',
|
|
// Versionless S3 intentionally omits version_id.
|
|
]
|
|
);
|
|
$receipt = $helper->invoke($request);
|
|
$assert(($receipt['version_id'] ?? null) === '',
|
|
'Versionless S3 success was not normalized to an explicit empty version.');
|
|
|
|
$mismatch = new CloudStorageReplicaHelper(null,
|
|
static fn (): array => [
|
|
'protocol' => 'object-replica-v1',
|
|
'operation_id' => str_repeat('c', 32),
|
|
'outcome' => 'succeeded',
|
|
'effect_resolution' => 'applied',
|
|
'size_bytes' => 4096,
|
|
'sha256' => str_repeat('b', 64),
|
|
'etag' => '"etag"',
|
|
'version_id' => '',
|
|
]
|
|
);
|
|
$receipt = $mismatch->invoke($request);
|
|
$assert($receipt['effect_resolution'] === 'unknown'
|
|
&& $receipt['error_code'] === 'replicator_invalid_receipt',
|
|
'Mismatched helper receipt was accepted as authoritative.');
|
|
|
|
$verify = new CloudStorageReplicaHelper(null,
|
|
static fn (array $input): array => [
|
|
'protocol' => $input['protocol'],
|
|
'operation_id' => $input['operation_id'],
|
|
'outcome' => 'failed',
|
|
'effect_resolution' => 'unknown',
|
|
'error_code' => 'object_download_failed',
|
|
]
|
|
);
|
|
$verifyReceipt = $verify->invoke([
|
|
'protocol' => 'object-replica-verify-v1',
|
|
'operation_id' => $operationId,
|
|
'deadline_at' => time() + 60,
|
|
]);
|
|
$assert($verifyReceipt['effect_resolution'] === 'unknown',
|
|
'Read-only verification uncertainty was not preserved.');
|
|
|
|
// Exercise the real proc_open path. proc_get_status() commonly consumes the
|
|
// child exit status and makes proc_close() return -1; the remembered exit code
|
|
// must still accept a valid bounded receipt.
|
|
$script = tempnam(sys_get_temp_dir(), 'virt-storage-helper-');
|
|
if (!is_string($script)) {
|
|
throw new RuntimeException('Unable to allocate helper fixture.');
|
|
}
|
|
$program = '#!' . PHP_BINARY . "\n<?php\n"
|
|
. '$request = json_decode(stream_get_contents(STDIN), true);' . "\n"
|
|
. 'echo json_encode([' . "\n"
|
|
. "'protocol' => \$request['protocol'],\n"
|
|
. "'operation_id' => \$request['operation_id'],\n"
|
|
. "'outcome' => 'succeeded', 'effect_resolution' => 'applied',\n"
|
|
. "'size_bytes' => 4096, 'sha256' => str_repeat('d', 64),\n"
|
|
. "'etag' => '\"fixture\"', 'version_id' => ''\n"
|
|
. ']);' . "\n";
|
|
file_put_contents($script, $program);
|
|
chmod($script, 0700);
|
|
try {
|
|
$receipt = (new CloudStorageReplicaHelper($script))->invoke($request);
|
|
$assert($receipt['outcome'] === 'succeeded'
|
|
&& $receipt['effect_resolution'] === 'applied',
|
|
'Valid helper receipt was rejected after proc_get_status().');
|
|
} finally {
|
|
@unlink($script);
|
|
}
|
|
|
|
// A helper that never reads stdin must be terminated by the same absolute
|
|
// deadline used for stdout/stderr, even when the request fills the pipe.
|
|
$blockedScript = tempnam(sys_get_temp_dir(), 'virt-storage-helper-blocked-');
|
|
if (!is_string($blockedScript)) {
|
|
throw new RuntimeException('Unable to allocate blocked helper fixture.');
|
|
}
|
|
$blockedProgram = '#!' . PHP_BINARY . "\n<?php\nsleep(10);\n";
|
|
file_put_contents($blockedScript, $blockedProgram);
|
|
chmod($blockedScript, 0700);
|
|
try {
|
|
$blockedRequest = $request;
|
|
$blockedRequest['deadline_at'] = time() - 5;
|
|
$blockedRequest['padding'] = str_repeat('x', 1572864);
|
|
$started = microtime(true);
|
|
$blockedReceipt = (new CloudStorageReplicaHelper($blockedScript))
|
|
->invoke($blockedRequest);
|
|
$elapsed = microtime(true) - $started;
|
|
$assert($blockedReceipt['effect_resolution'] === 'unknown'
|
|
&& in_array($blockedReceipt['error_code'], [
|
|
'replicator_timeout', 'replicator_stdin_failed',
|
|
], true),
|
|
'A helper that ignored stdin did not return an unknown receipt.');
|
|
$assert($elapsed < 4.0,
|
|
'Blocking stdin escaped the helper deadline: ' . $elapsed);
|
|
} finally {
|
|
@unlink($blockedScript);
|
|
}
|
|
|
|
$source = file_get_contents(
|
|
dirname(__DIR__) . '/app/Services/Cloud/CloudStorageReplicaHelper.php'
|
|
);
|
|
$assert(is_string($source)
|
|
&& str_contains($source, '$stdinFailed = true;')
|
|
&& str_contains($source, 'stream_select(')
|
|
&& str_contains($source, 'stream_set_blocking($pipe, false)')
|
|
&& str_contains($source, '$closedExit >= 0 ? $closedExit : $observedExit')
|
|
&& str_contains($source, "['bypass_shell' => true]"),
|
|
'Helper runner lost its stdin, exit-status, or shell-isolation guard.');
|
|
|
|
echo "Storage replica helper tests passed.\n";
|