Files
Reims-Go/internal/host/hypervisor_test.go
T
Dnyo666 ff99d8d702 feat(host,ui): add opt-in Hyper-V boot switch and hypervisor launch detection (#122)
- Distinguish hypervisor launch disabled (hypervisor_off) from BIOS/UEFI
  virtualization disabled (no_hypervisor) via unprivileged Win32 & DeviceGuard
  probes without spawning background PowerShell/BCD processes
- Add default-off `enable_hypervisor_switch` preference in Settings -> General
  with confirmation modal, active VM/task guards, HVCI notice, same-boot undo,
  and single-UAC System32 execution without auto-restarting Windows
- Update Engine page, first-run Setup, `reims-go doctor`, and VM startprep
  diagnostics for `hypervisor_off` and pending boot states
2026-12-03 13:39:08 +08:00

189 lines
7.7 KiB
Go

package host
import (
"os"
"path/filepath"
"testing"
)
func TestClassifyWHPXState(t *testing.T) {
cases := []struct {
name string
dll bool
hvPresent bool
firmVirt bool
want WHPXState
}{
{"missing dll without firmware virt", false, false, false, WHPXMissing},
{"missing dll with firmware virt", false, false, true, WHPXMissing},
{"hypervisor running (firmware virt reported false under root partition)", true, true, false, WHPXReady},
{"hypervisor running (firmware virt true)", true, true, true, WHPXReady},
{"hypervisor launch off with BIOS virtualization enabled", true, false, true, WHPXHypervisorOff},
{"BIOS virtualization off or nested guest without VMX", true, false, false, WHPXNoHypervisor},
}
for _, tc := range cases {
if got := ClassifyWHPXState(tc.dll, tc.hvPresent, tc.firmVirt); got != tc.want {
t.Errorf("%s: ClassifyWHPXState(%v, %v, %v) = %q, want %q", tc.name, tc.dll, tc.hvPresent, tc.firmVirt, got, tc.want)
}
}
}
func TestEvaluateDeviceGuard(t *testing.T) {
cases := []struct {
name string
vbs, hvci, secureKernel uint32
wantHVCI, wantVBSRunning bool
}{
{"all off", 0, 0, 0, false, false},
{"hvci scenario enabled with secure kernel running and top-level vbs 0", 0, 1, 1, true, true},
{"hvci scenario enabled pending reboot", 0, 1, 0, true, false},
{"top-level vbs enabled without hvci", 1, 0, 0, false, true},
{"secure kernel running without hvci", 0, 0, 1, false, true},
}
for _, tc := range cases {
hvci, vbs := EvaluateDeviceGuard(tc.vbs, tc.hvci, tc.secureKernel)
if hvci != tc.wantHVCI || vbs != tc.wantVBSRunning {
t.Errorf("%s: EvaluateDeviceGuard(%d, %d, %d) = (%v, %v), want (%v, %v)",
tc.name, tc.vbs, tc.hvci, tc.secureKernel, hvci, vbs, tc.wantHVCI, tc.wantVBSRunning)
}
}
}
func TestPendingHypervisorBootLifecycle(t *testing.T) {
defer func() {
pendingBootMu.Lock()
pendingBootMem = nil
pendingBootMu.Unlock()
}()
root := t.TempDir()
// Before <dataRoot>/tmp exists, ReadPendingHypervisorBoot must not create files,
// while in-memory state still tracks the current session.
const boot1 = int64(1_700_000_000)
if got := recordPendingHypervisorBootAt(root, WHPXReady, false, boot1); got != HypervisorBootOff {
t.Fatalf("record disable while ready = %q, want %q", got, HypervisorBootOff)
}
if _, err := os.Stat(filepath.Join(root, "tmp")); !os.IsNotExist(err) {
t.Fatalf("must not create <dataRoot>/tmp before setup; stat err=%v", err)
}
if got := readPendingHypervisorBootAt(root, WHPXReady, boot1+30); got != HypervisorBootOff {
t.Fatalf("read within NTP tolerance = %q, want %q", got, HypervisorBootOff)
}
// Undoing (setting enable=true while WHPX is still live Ready) clears pending state.
if got := recordPendingHypervisorBootAt(root, WHPXReady, true, boot1+35); got != "" {
t.Fatalf("undo back to live state = %q, want empty", got)
}
if got := readPendingHypervisorBootAt(root, WHPXReady, boot1+35); got != "" {
t.Fatalf("read after undo = %q, want empty", got)
}
// With <dataRoot>/tmp present, record persists across process restarts (simulated by clearing memory)
// within the same Windows boot session, and expires once Windows reboots (boot2).
if err := os.MkdirAll(filepath.Join(root, "tmp"), 0o755); err != nil {
t.Fatal(err)
}
if got := recordPendingHypervisorBootAt(root, WHPXHypervisorOff, true, boot1); got != HypervisorBootAuto {
t.Fatalf("record enable while hypervisor_off = %q, want %q", got, HypervisorBootAuto)
}
pendingBootMu.Lock()
pendingBootMem = nil
pendingBootMu.Unlock()
if got := readPendingHypervisorBootAt(root, WHPXHypervisorOff, boot1+10); got != HypervisorBootAuto {
t.Fatalf("read from disk in same boot = %q, want %q", got, HypervisorBootAuto)
}
// Simulated OS reboot (boot epoch jumps by 600s): stale record is ignored.
const boot2 = boot1 + 600
if got := readPendingHypervisorBootAt(root, WHPXHypervisorOff, boot2); got != "" {
t.Fatalf("read after simulated reboot = %q, want empty", got)
}
// Also ignored if live state already converged to target in same boot.
if got := readPendingHypervisorBootAt(root, WHPXReady, boot1+10); got != "" {
t.Fatalf("read once live state is WHPXReady = %q, want empty", got)
}
}
func TestPendingHypervisorBootUndoAcrossDataRoots(t *testing.T) {
defer func() {
pendingBootMu.Lock()
pendingBootMem = nil
pendingBootPath = ""
pendingBootMu.Unlock()
}()
rootA := t.TempDir()
rootB := t.TempDir()
if err := os.MkdirAll(filepath.Join(rootA, "tmp"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(rootB, "tmp"), 0o755); err != nil {
t.Fatal(err)
}
const boot1 = int64(1_700_000_000)
if got := recordPendingHypervisorBootAt(rootA, WHPXReady, false, boot1); got != HypervisorBootOff {
t.Fatalf("record disable in rootA = %q, want %q", got, HypervisorBootOff)
}
// Verify atomic write leaves no leftover temporary files in <rootA>/tmp.
entries, err := os.ReadDir(filepath.Join(rootA, "tmp"))
if err != nil || len(entries) != 1 || entries[0].Name() != pendingBootFileName {
t.Fatalf("unexpected files in rootA/tmp after atomic write: %v (err=%v)", entries, err)
}
// User switches DataRoot to rootB and clicks Undo before restarting Windows.
if got := recordPendingHypervisorBootAt(rootB, WHPXReady, true, boot1+5); got != "" {
t.Fatalf("undo in rootB = %q, want empty", got)
}
// Simulate restarting Reims-Go process and switching back to rootA in the same boot session:
// stale pending file in rootA/tmp must have been cleaned up on undo.
pendingBootMu.Lock()
pendingBootMem = nil
pendingBootPath = ""
pendingBootMu.Unlock()
if got := readPendingHypervisorBootAt(rootA, WHPXReady, boot1+10); got != "" {
t.Fatalf("stale pending record remained in rootA after undo in rootB: got %q, want empty", got)
}
}
func TestPendingHypervisorBootFastRebootAndSleepResilience(t *testing.T) {
defer func() {
pendingBootMu.Lock()
pendingBootMem = nil
pendingBootPath = ""
pendingBootMu.Unlock()
}()
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "tmp"), 0o755); err != nil {
t.Fatal(err)
}
// 1. S3 sleep / S4 hibernate wake: wall-clock minus GetTickCount64 drifts by 3600s
// (> 120s tolerance), while kernel volatile BootMarker stays identical and TickMs increases.
sessBeforeSleep := bootSession{Marker: "hw:1a2b3c4d", EpochSec: 1_700_000_000, TickMs: 25_000}
if got := recordPendingHypervisorBootSession(root, WHPXReady, false, sessBeforeSleep); got != HypervisorBootOff {
t.Fatalf("record before sleep = %q, want %q", got, HypervisorBootOff)
}
sessAfterSleep := bootSession{Marker: "hw:1a2b3c4d", EpochSec: 1_700_003_600, TickMs: 30_000}
if got := readPendingHypervisorBootSession(root, WHPXReady, sessAfterSleep); got != HypervisorBootOff {
t.Fatalf("pending state lost after sleep drift: got %q, want %q", got, HypervisorBootOff)
}
// 2. Fast NVMe reboot (completed in 30s, well within 120s wall-clock tolerance):
// both volatile BootMarker change AND monotonic TickMs reset expire the stale record.
sessFastRebootMarker := bootSession{Marker: "hw:9f8e7d6c", EpochSec: 1_700_000_030, TickMs: 40_000}
if got := readPendingHypervisorBootSession(root, WHPXReady, sessFastRebootMarker); got != "" {
t.Fatalf("fast reboot with new boot marker must expire pending state, got %q", got)
}
// Fallback path (no volatile marker): monotonic TickMs reset from 60s -> 8s detects fast reboot.
sessFallbackBefore := bootSession{EpochSec: 1_700_000_000, TickMs: 60_000}
_ = recordPendingHypervisorBootSession(root, WHPXReady, false, sessFallbackBefore)
sessFallbackFastReboot := bootSession{EpochSec: 1_700_000_065, TickMs: 8_000}
if got := readPendingHypervisorBootSession(root, WHPXReady, sessFallbackFastReboot); got != "" {
t.Fatalf("fallback fast reboot with reset TickMs must expire pending state, got %q", got)
}
}