- 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
132 lines
3.9 KiB
Go
132 lines
3.9 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
func TestBuildEnableMissingWHPXCmdLineExitCodes(t *testing.T) {
|
|
sysDir, err := windows.GetSystemDirectory()
|
|
if err != nil {
|
|
t.Fatalf("GetSystemDirectory: %v", err)
|
|
}
|
|
cmdExe := filepath.Join(sysDir, "cmd.exe")
|
|
|
|
cases := []struct {
|
|
name string
|
|
dismCode int
|
|
bcdCode int
|
|
wantExit int
|
|
}{
|
|
{"dism 0 and bcdedit 0 succeeds", 0, 0, 0},
|
|
{"dism 3010 reboot-required and bcdedit 0 succeeds", 3010, 0, 0},
|
|
{"dism failure 87 aborts without running bcdedit", 87, 0, 87},
|
|
{"dism 3010 followed by bcdedit failure 1 propagates 1", 3010, 1, 1},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
dismMock := `cmd.exe /c exit ` + itoa(tc.dismCode)
|
|
bcdMock := `cmd.exe /c exit ` + itoa(tc.bcdCode)
|
|
rawArgs := buildEnableMissingWHPXCmdLine(dismMock, bcdMock)
|
|
|
|
c := exec.Command(cmdExe)
|
|
c.SysProcAttr = &syscall.SysProcAttr{CmdLine: `"` + cmdExe + `" ` + rawArgs}
|
|
err := c.Run()
|
|
got := 0
|
|
if err != nil {
|
|
exitErr, ok := err.(*exec.ExitError)
|
|
if !ok {
|
|
t.Fatalf("%s: unexpected exec error: %v", tc.name, err)
|
|
}
|
|
got = exitErr.ExitCode()
|
|
}
|
|
if got != tc.wantExit {
|
|
t.Errorf("%s: exit code = %d, want %d (args=%s)", tc.name, got, tc.wantExit, rawArgs)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestWindowsBootEpochAndUnprivilegedVirtualizationProbes(t *testing.T) {
|
|
sess1 := currentBootSession()
|
|
sess2 := currentBootSession()
|
|
if sess1.EpochSec <= 0 {
|
|
t.Fatalf("expected positive Windows boot epoch, got %d", sess1.EpochSec)
|
|
}
|
|
if sess1.Marker == "" || sess1.Marker != sess2.Marker {
|
|
t.Fatalf("expected stable non-empty volatile boot marker, got %q vs %q", sess1.Marker, sess2.Marker)
|
|
}
|
|
if sess2.TickMs < sess1.TickMs {
|
|
t.Fatalf("expected monotonic TickMs, got %d -> %d", sess1.TickMs, sess2.TickMs)
|
|
}
|
|
// Canceled context must abort runElevatedWait before invoking ShellExecuteExW / UAC.
|
|
canceledCtx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, err := runElevatedWait(canceledCtx, `C:\Windows\System32\bcdedit.exe`, `/enum`); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("expected context.Canceled before UAC, got %v", err)
|
|
}
|
|
// Ensure unprivileged DeviceGuard and firmware virtualization probes never panic or fail.
|
|
_, _ = probeDeviceGuard()
|
|
_ = probeFirmwareVirtEnabled()
|
|
}
|
|
|
|
func TestBuildEnableMissingWHPXCmdLineQuotedPathsWithSpaces(t *testing.T) {
|
|
sysDir, err := windows.GetSystemDirectory()
|
|
if err != nil {
|
|
t.Fatalf("GetSystemDirectory: %v", err)
|
|
}
|
|
cmdExe := filepath.Join(sysDir, "cmd.exe")
|
|
|
|
spacedDir := filepath.Join(t.TempDir(), "System Tools With Spaces")
|
|
if err := os.MkdirAll(spacedDir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dismBat := filepath.Join(spacedDir, "mock dism.cmd")
|
|
bcdBat := filepath.Join(spacedDir, "mock bcdedit.cmd")
|
|
markerFile := filepath.Join(spacedDir, "bcd ran.txt")
|
|
if err := os.WriteFile(dismBat, []byte("@exit /b 3010\r\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(bcdBat, []byte(fmt.Sprintf("@echo ok>\"%s\" & exit /b 0\r\n", markerFile)), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
dismCmd := fmt.Sprintf(`"call "%s""`, dismBat)
|
|
// Also test realistic direct quoted executable invocation (`"C:\...\cmd.exe" /c ...`)
|
|
dismCmd = fmt.Sprintf(`"%s" /d /c ""%s""`, cmdExe, dismBat)
|
|
bcdCmd := fmt.Sprintf(`"%s" /d /c ""%s""`, cmdExe, bcdBat)
|
|
rawArgs := buildEnableMissingWHPXCmdLine(dismCmd, bcdCmd)
|
|
|
|
c := exec.Command(cmdExe)
|
|
c.SysProcAttr = &syscall.SysProcAttr{CmdLine: `"` + cmdExe + `" ` + rawArgs}
|
|
if err := c.Run(); err != nil {
|
|
t.Fatalf("quoted spaced command failed: %v (args=%s)", err, rawArgs)
|
|
}
|
|
if _, err := os.Stat(markerFile); err != nil {
|
|
t.Fatalf("expected chained bcdedit mock to run after DISM 3010, stat err=%v", err)
|
|
}
|
|
}
|
|
|
|
func itoa(v int) string {
|
|
if v == 0 {
|
|
return "0"
|
|
}
|
|
var buf [20]byte
|
|
i := len(buf)
|
|
for v > 0 {
|
|
i--
|
|
buf[i] = byte('0' + v%10)
|
|
v /= 10
|
|
}
|
|
return string(buf[i:])
|
|
}
|