- run elevated actions as fixed System32 tools instead of a hidden subcommand - require a SHA-256 for plugin downloads - keep records of secret files that could not be deleted, and never overwrite files the host did not create - stop two concurrent requests from starting the same VM - drop invalid endpoints instead of failing the whole VM list, and reject ssh users and IPv6 zones that could inject command-line text - accept driver screenshots only at the requested path - do not treat an unreadable guest phase as normal Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
3.4 KiB
Go
102 lines
3.4 KiB
Go
//go:build windows
|
|
|
|
package host
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// stubElevation records elevated runs instead of raising a UAC prompt.
|
|
func stubElevation(t *testing.T, code uint32, runErr error, members ...bool) *[]string {
|
|
t.Helper()
|
|
prevRun, prevMember := elevatedRun, inHyperVAdminGroup
|
|
t.Cleanup(func() { elevatedRun, inHyperVAdminGroup = prevRun, prevMember })
|
|
runs := &[]string{}
|
|
elevatedRun = func(_ context.Context, exe, params string) (uint32, error) {
|
|
*runs = append(*runs, exe+" "+params)
|
|
return code, runErr
|
|
}
|
|
inHyperVAdminGroup = func(string) bool {
|
|
if len(members) == 0 {
|
|
return false
|
|
}
|
|
m := members[0]
|
|
members = members[1:]
|
|
return m
|
|
}
|
|
return runs
|
|
}
|
|
|
|
func TestRunElevatedActionEnablesFeaturesThroughSystem32(t *testing.T) {
|
|
sysDir, err := windows.GetSystemDirectory()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for action, feature := range map[string]string{
|
|
ElevateHyperVEnable: "Microsoft-Hyper-V-All",
|
|
ElevateSandboxEnable: "Containers-DisposableClientVM",
|
|
} {
|
|
runs := stubElevation(t, errorSuccessRebootRequired, nil)
|
|
res, err := RunElevatedAction(t.Context(), action, "")
|
|
if err != nil || !res.OK || !res.RebootRequired {
|
|
t.Fatalf("%s: %+v, %v", action, res, err)
|
|
}
|
|
want := filepath.Join(sysDir, "dism.exe") + " /Online /Enable-Feature /FeatureName:" + feature + " /All /Quiet /NoRestart"
|
|
if len(*runs) != 1 || (*runs)[0] != want {
|
|
t.Fatalf("%s ran %q, want %q", action, *runs, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunElevatedActionReportsFailures(t *testing.T) {
|
|
stubElevation(t, 50, nil)
|
|
if res, err := RunElevatedAction(t.Context(), ElevateHyperVEnable, ""); err == nil || res.OK || res.ExitCode != 50 {
|
|
t.Fatalf("a failed dism run = %+v, %v", res, err)
|
|
}
|
|
stubElevation(t, 0, ErrElevationDeclined)
|
|
if _, err := RunElevatedAction(t.Context(), ElevateHyperVEnable, ""); !errors.Is(err, ErrElevationDeclined) {
|
|
t.Fatalf("err = %v, want ErrElevationDeclined", err)
|
|
}
|
|
}
|
|
|
|
func TestRunElevatedActionAddsTheAccountToTheHyperVGroup(t *testing.T) {
|
|
if _, err := hyperVAdminsGroupName(); err != nil {
|
|
t.Skipf("this Windows has no Hyper-V Administrators group: %v", err)
|
|
}
|
|
const account = `DESKTOP-1\Alice Smith`
|
|
|
|
runs := stubElevation(t, 0, nil)
|
|
res, err := RunElevatedAction(t.Context(), ElevateHyperVAddAdminGroup, account)
|
|
if err != nil || !res.OK || !res.LogoffRequired {
|
|
t.Fatalf("result = %+v, %v", res, err)
|
|
}
|
|
if len(*runs) != 1 || !strings.Contains((*runs)[0], `"`+account+`" /add`) {
|
|
t.Fatalf("runs = %q", *runs)
|
|
}
|
|
|
|
runs = stubElevation(t, 0, nil, true)
|
|
if res, err := RunElevatedAction(t.Context(), ElevateHyperVAddAdminGroup, account); err != nil || !res.OK || len(*runs) != 0 {
|
|
t.Fatalf("an existing member still raised UAC: %+v, %v, %q", res, err, *runs)
|
|
}
|
|
|
|
stubElevation(t, 2, nil, false, true)
|
|
if res, err := RunElevatedAction(t.Context(), ElevateHyperVAddAdminGroup, account); err != nil || !res.OK {
|
|
t.Fatalf("an account added meanwhile = %+v, %v", res, err)
|
|
}
|
|
stubElevation(t, 2, nil)
|
|
if res, err := RunElevatedAction(t.Context(), ElevateHyperVAddAdminGroup, account); err == nil || res.OK {
|
|
t.Fatalf("a failed net run = %+v, %v", res, err)
|
|
}
|
|
|
|
runs = stubElevation(t, 0, nil)
|
|
if res, err := RunElevatedAction(t.Context(), ElevateHyperVAddAdminGroup, "alice & calc"); err == nil || res.OK || len(*runs) != 0 {
|
|
t.Fatalf("an unsafe account name ran: %+v, %v, %q", res, err, *runs)
|
|
}
|
|
}
|