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

113 lines
6.4 KiB
PHP

<?php
// Static UI contract only. The PHP control-plane and host Agent have their
// own behavioral tests; this protects the tenant-facing firewall editor from
// silently drifting away from their versioned whole-policy API.
$root = dirname(__DIR__);
$template = file_get_contents($root . '/resources/views/venix/cloud/pages/detail.tpl');
$assets = file_get_contents($root . '/resources/views/venix/layout_assets.tpl');
$script = file_get_contents($root . '/public/cloud/detail.js');
$styles = file_get_contents($root . '/public/cloud/styles.css');
$assert = static function ($condition, $message): void {
if (!$condition) throw new RuntimeException($message);
};
$assert(str_contains($template, 'data-instance-setting-tab="firewall"')
&& str_contains($template, 'data-instance-setting-panel="firewall"')
&& str_contains($template, 'id="vm-firewall-form"')
&& str_contains($template, 'id="vm-firewall-rules"'),
'Instance settings must expose the integrated firewall tab and whole-policy editor.');
$assert(preg_match("/venix_asset_version value='([0-9]+)'/", $assets, $assetVersion) === 1
&& (int) $assetVersion[1] >= 2026100505,
'Shared Venix asset cache key must publish the firewall JavaScript and CSS.');
$assert(str_contains($template, '反欺骗保护始终开启')
&& str_contains($template, '只允许规则表中列出的新 IPv4 入站连接')
&& str_contains($template, '用户防火墙仅管理公网网卡的 IPv4 入站连接')
&& str_contains($template, 'IPv6 不作为可用网络并由底层过滤器阻断')
&& str_contains($template, '已建立连接及其关联流量保持可用')
&& str_contains($template, '拒绝所有新的公网入站连接')
&& str_contains($template, 'data-firewall-preset="ssh"')
&& str_contains($template, 'data-firewall-preset="http"')
&& str_contains($template, 'data-firewall-preset="https"')
&& str_contains($template, '快捷填充只生成尚未应用的草稿'),
'Firewall UI must explain default-deny behavior and offer opt-in draft presets.');
$assert(!str_contains($template, '平台默认策略')
&& !str_contains($template, 'TCP 25'),
'Tenant firewall UI must not expose platform restriction notices.');
$assert(str_contains($template, 'id="vm-firewall-state"')
&& str_contains($template, 'id="vm-firewall-revisions"')
&& str_contains($template, 'id="vm-firewall-last-success"')
&& str_contains($template, 'id="vm-firewall-last-failure"'),
'Firewall UI must expose desired/applied evidence and last success/failure timestamps.');
$assert(str_contains($script,
"const INSTANCE_SETTING_TABS = Object.freeze(['hostname', 'password', 'ssh_key', 'ip_change', 'firewall', 'snapshots'])")
&& str_contains($script, "if (tab === 'firewall')")
&& str_contains($script, 'function loadVmFirewall(')
&& str_contains($script, 'function saveVmFirewall('),
'Firewall must participate in accessible settings navigation and lazy loading.');
$assert(str_contains($script,
'api(`/api/v1/vms/${encodeURIComponent(vmId)}/firewall`)')
&& str_contains($script, "method: 'PUT'")
&& str_contains($script, 'expected_revision:')
&& str_contains($script, 'idempotency_key: firewallIdempotencyKey()')
&& str_contains($script, 'enabled,')
&& str_contains($script, 'rules,'),
'Firewall editor must use the database-only GET and idempotent whole-policy PUT contract.');
$assert(str_contains($script, 'const FIREWALL_RULE_LIMIT = 50;')
&& str_contains($script, "['TCP', 'UDP', 'ICMP']")
&& str_contains($script, 'function validFirewallCidr(')
&& str_contains($script, 'function firewallPortRange(')
&& str_contains($script, 'port_from: portFrom')
&& str_contains($script, 'port_to: portTo'),
'Firewall drafts must validate the bounded IPv4 inbound rule shape before submission.');
$assert(str_contains($script,
"const FIREWALL_FAILURE_STATES = new Set(['failed', 'error'])")
&& str_contains($script, '编辑已冻结')
&& str_contains($script, '请勿再次提交')
&& str_contains($script,
'policy?.effect_may_have_applied === true')
&& str_contains($script, 'FIREWALL_PENDING_STATES.has(policy.sync_state)')
&& str_contains($script, 'function pollVmFirewall(vmId, receipt)')
&& str_contains($script,
"watchTask(receipt, '应用防火墙'"),
'Active tasks and failed effect-unknown receipts must freeze firewall mutation without resubmission.');
$assert(str_contains($styles, '.vm-firewall-rule{display:grid;grid-template-columns:')
&& str_contains($styles, '.vm-firewall-state.failed{')
&& str_contains($styles, '@media(max-width:740px){.vm-firewall-facts{grid-template-columns:1fr}')
&& str_contains($styles, '.vm-firewall-table{overflow-x:auto;overflow-y:hidden')
&& str_contains($styles, '.vm-firewall-rule{min-width:650px}')
&& !str_contains($styles, '.vm-firewall-rule-header{display:none}')
&& !str_contains($styles, '.vm-firewall-cell-label{display:block'),
'Firewall rules must remain one horizontal table row per rule on narrow screens.');
$assert(str_contains($styles,
'.vm-firewall-toggle input:focus,.vm-firewall-toggle input:focus-visible{outline:none;box-shadow:none}')
&& str_contains($styles,
'.vm-firewall-cell input:focus,.vm-firewall-cell select:focus{border-color:#35b9c0;outline:none;box-shadow:none}'),
'Firewall controls must not show a surrounding focus shadow on desktop or mobile.');
$assert(str_contains($styles,
'.vm-firewall-cell input,.vm-firewall-cell select{width:100%;min-width:0;height:36px;padding:7px 9px;border:1px solid #dce5e9;border-radius:7px;background:#fff;box-shadow:none;color:#15243a;font-size:10px}')
&& str_contains($styles,
'.vm-firewall-rule-header>[role=columnheader]:last-child{text-align:left}')
&& str_contains($styles,
'.vm-firewall-cell.action{display:flex;justify-content:flex-start}'),
'Firewall rule inputs must match the protocol select and the action column must stay left aligned.');
$saveStart = strpos($script, 'async function saveVmFirewall(');
$bindStart = strpos($script, 'function bindFirewallControls(', $saveStart ?: 0);
$saveSource = $saveStart !== false && $bindStart !== false
? substr($script, $saveStart, $bindStart - $saveStart) : '';
$assert($saveSource !== '' && !str_contains($saveSource, 'window.confirm'),
'Applying a firewall policy must not add a confirmation popup.');
echo "Cloud firewall settings UI contract passed.\n";