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/CloudProtocolTest.php

48 lines
3.6 KiB
PHP

<?php
namespace App\Services {
final class Config {
public static function get($name, $default = null) {
return $name === 'cloud_engine_hmac_key' ? base64_encode(str_repeat('k', 32)) : $default;
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudEngineClient;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudMoney;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudWorkerService;
use App\Controllers\CloudInternalController;
$assert = function ($condition, $message) { if (!$condition) { throw new RuntimeException($message); } };
$assert(CloudMoney::cents('9.99') + CloudMoney::cents('0.01') === 1000, 'Money arithmetic must be exact.');
$assert(CloudMoney::decimal(999999999999) === '9999999999.99', 'Maximum decimal value changed.');
foreach ([1.1, '-1', '1e3', '1.001', '01.00', '10000000000.00'] as $bad) {
try { CloudMoney::cents($bad); throw new RuntimeException('Invalid amount accepted.'); } catch (CloudError $expected) {}
}
$actor = ['user_id' => 9, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => true, 'session_id' => str_repeat('a', 64)];
$body = '{"session_id":"example"}';
$path = '/api/internal/cloud/session-introspection?a=1%2F2';
$headers = CloudEngineClient::headers('POST', $path, $body, $actor, 1800000000, 'PythonUrlSafeNonce_ab-CD_123456789');
$named = ['timestamp' => $headers['X-Cloud-Timestamp'], 'nonce' => $headers['X-Cloud-Nonce'],
'actor' => $headers['X-Cloud-Actor'], 'signature' => $headers['X-Cloud-Signature']];
$assert(CloudInternalController::validSignature('POST', $path, $body, $named, str_repeat('k', 32), 1800000000), 'Signed request rejected.');
foreach ([['POST', $path . '0', $body, 1800000000], ['GET', $path, $body, 1800000000],
['POST', $path, $body . ' ', 1800000000], ['POST', $path, $body, 1800000061]] as [$method, $target, $raw, $now]) {
$assert(!CloudInternalController::validSignature($method, $target, $raw, $named, str_repeat('k', 32), $now), 'Tampered or stale signature accepted.');
}
$digest = hash('sha256', $body);
$assert(CloudEngineClient::canonicalDigest('POST', $path, $digest, '1800000000', $named['nonce'], $named['actor'])
=== CloudEngineClient::canonical('POST', $path, $body, '1800000000', $named['nonce'], $named['actor']), 'Upload digest contract differs.');
$assert(CloudWorkerService::disposition(['status' => 'failed'], true) === 'uncertain', 'Unknown creation failure must not refund.');
$assert(CloudWorkerService::disposition(['status' => 'failed', 'safe_to_refund' => false], true) === 'uncertain', 'Explicitly unsafe creation failure must not refund.');
$assert(CloudWorkerService::disposition(['status' => 'failed', 'safe_to_refund' => true], true) === 'failed', 'Confirmed absent creation must refund.');
foreach (['shortA1!', 'longwithoutupper1!', 'LongWithoutSymbol123', "StrongPassword!1\n", str_repeat('a', 254) . 'A1!'] as $bad) {
try { CloudInput::password($bad); throw new RuntimeException('Weak guest password accepted.'); } catch (CloudError $expected) {}
}
$assert(strlen(CloudInput::password(null)) >= 12 && strlen(CloudInput::password('Aa1!' . str_repeat('x', 252))) === 256, 'Guest password bounds wrong.');
$safe = CloudInput::safeResult(['uuid' => 'test', 'generated_password' => 'never-save', 'nested' => ['private_key' => 'secret', 'name' => 'safe']]);
$assert($safe === ['uuid' => 'test', 'nested' => ['name' => 'safe']], 'Secret entered durable job output.');
echo "Cloud money, password, HMAC and ambiguity checks passed.\n";
}