Files
Reims-Go/internal/host/elevate_windows_test.go
T
NwflowerandClaude Opus 5.5 a32cd8afad fix: track the whole VM process tree and open hosted endpoints
- start VM processes suspended so children cannot escape the VM's job
- read Hyper-V Administrators from the member list, not the sign-in token
- accept the "open" action the app sends for Remote Desktop and VMConnect

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 09:15:38 -07:00

115 lines
3.9 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 TestAccountInLocalGroupReadsTheMemberList(t *testing.T) {
const administrators, guests = "S-1-5-32-544", "S-1-5-32-546"
if _, _, _, err := windows.LookupSID("", "Administrator"); err != nil {
t.Skipf("the built-in administrator is renamed here: %v", err)
}
if !accountInLocalGroup(administrators, "Administrator") {
t.Error("the built-in administrator is not seen in Administrators")
}
if accountInLocalGroup(guests, "Administrator") {
t.Error("the built-in administrator is seen in Guests")
}
}
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 := localGroupName(hyperVAdminsSID); 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)
}
}