fix(windows): launch commands on the interactive desktop
This commit is contained in:
1 parent
30e7bb8545
commit
8d28925e44
20 files changed
+1036
-28
No files matched your search
@@ -0,0 +1,253 @@
|
||||
//go:build windows
|
||||
|
||||
package windows
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1"
|
||||
"github.com/rvbox/rvbox/internal/client/supervisor"
|
||||
"github.com/rvbox/rvbox/internal/domain"
|
||||
winapi "golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// Native fixture children execute the production launcher/helper entrypoints.
|
||||
// This dispatcher exists only in the test executable, never the release image.
|
||||
func TestMain(m *testing.M) {
|
||||
if len(os.Args) == 4 && os.Args[2] == "--channel" {
|
||||
var err error
|
||||
switch os.Args[1] {
|
||||
case "--launcher":
|
||||
err = RunLauncher(context.Background(), os.Args[3])
|
||||
case "--signal-helper":
|
||||
err = RunSignalHelper(context.Background(), os.Args[3])
|
||||
default:
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestNativeDesktopAccessOnActualThread_HP_DESKTOP_02(t *testing.T) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
var sessionID uint32
|
||||
if err := winapi.ProcessIdToSessionId(winapi.GetCurrentProcessId(), &sessionID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sessionID == 0 {
|
||||
t.Skip("requires an interactive desktop; Session 0 is a separate route")
|
||||
}
|
||||
path, err := verifyCurrentInteractiveDesktop(sessionID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := validateInteractiveDesktop(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// This fixture lane requires LocalSystem (SCM/service route), one usable active
|
||||
// desktop, and the bundled PowerShell probe readable by that user. Run it in the
|
||||
// disposable native fixture, not by changing an ordinary user's service state.
|
||||
func desktopFixture(t *testing.T, elevated bool) (*execSupervisor, string, string) {
|
||||
t.Helper()
|
||||
var token winapi.Token
|
||||
if err := winapi.OpenProcessToken(winapi.CurrentProcess(), winapi.TOKEN_QUERY, &token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user, err := token.GetTokenUser()
|
||||
_ = token.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if user.User.Sid.String() != securitySystemRID {
|
||||
t.Skip("requires LocalSystem native fixture process")
|
||||
}
|
||||
probe := os.Getenv("RVBOX_NATIVE_GUI_PROBE")
|
||||
if probe == "" {
|
||||
t.Skip("set RVBOX_NATIVE_GUI_PROBE to the bundled windows_gui_probe.ps1")
|
||||
}
|
||||
if !filepath.IsAbs(probe) {
|
||||
t.Fatal("GUI probe path must be absolute")
|
||||
}
|
||||
if info, err := os.Lstat(probe); err != nil || !info.Mode().IsRegular() {
|
||||
t.Fatalf("GUI probe must be a readable regular file: %v", err)
|
||||
}
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
root := os.Getenv("SystemRoot")
|
||||
if root == "" {
|
||||
root = `C:\Windows`
|
||||
}
|
||||
manager := newExecSupervisor(NativeOptions{ExecutablePath: executable, Shells: ShellPaths{CMD: filepath.Join(root, "System32", "cmd.exe"), PowerShell: filepath.Join(root, "System32", "WindowsPowerShell", "v1.0", "powershell.exe")}, WindowsTermGrace: 3 * time.Second})
|
||||
selected, identity, err := manager.selectToken(elevated)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = selected.Close()
|
||||
if identity.SessionID == 0 {
|
||||
t.Skip("requires one usable active session, not a Session 0 fallback")
|
||||
}
|
||||
directory := t.TempDir()
|
||||
// Both levels are owned by this t.TempDir fixture. Only SYSTEM and the
|
||||
// selected effective SID receive access, including wrapper-file traversal.
|
||||
for _, path := range []string{filepath.Dir(directory), directory} {
|
||||
sd, err := winapi.SecurityDescriptorFromString(fmt.Sprintf("D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;%s)", identity.UserSID))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dacl, _, err := sd.DACL()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := winapi.SetNamedSecurityInfo(path, winapi.SE_FILE_OBJECT, winapi.DACL_SECURITY_INFORMATION|winapi.PROTECTED_DACL_SECURITY_INFORMATION, nil, nil, dacl, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
t.Cleanup(func() { _ = manager.StopAll(context.Background()) })
|
||||
return manager, directory, probe
|
||||
}
|
||||
|
||||
func desktopProbeProcess(t *testing.T, manager *execSupervisor, directory, probe string, elevated, consoleOnly bool) (supervisor.Process, context.Context) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Second)
|
||||
t.Cleanup(cancel)
|
||||
args := []string{manager.options.Shells.PowerShell, "-NoLogo", "-NoProfile", "-STA", "-ExecutionPolicy", "Bypass", "-File", probe, "-Seconds", "2"}
|
||||
if consoleOnly {
|
||||
args[len(args)-1] = "25"
|
||||
args = append(args, "-ConsoleOnly")
|
||||
}
|
||||
command, err := BuildCommandLine(args)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
issue, err := domain.NewUUIDv7()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
spec := supervisor.StartSpec{IssueUUID: issue, CommandRevision: 1, WorkingDirectory: directory, Execution: &rvboxv1.ExecutionSpec{ShellType: rvboxv1.ShellType_SHELL_CMD, Elevated: elevated, Source: &rvboxv1.ExecutionSpec_CommandText{CommandText: "@" + command}}}
|
||||
var process supervisor.Process
|
||||
if consoleOnly {
|
||||
// Isolate console-event delivery from cmd's own batch-abort behavior.
|
||||
// The actual launcher still prepares this root shell suspended, puts it
|
||||
// in the Job, and uses the production authenticated TERM helper.
|
||||
token, identity, tokenErr := manager.selectToken(elevated)
|
||||
if tokenErr != nil {
|
||||
t.Fatal(tokenErr)
|
||||
}
|
||||
defer token.Close()
|
||||
base, envErr := token.Environ(false)
|
||||
if envErr != nil {
|
||||
t.Fatal(envErr)
|
||||
}
|
||||
environment, envErr := BuildEnvironmentBlock(parseEnvironment(base), nil)
|
||||
if envErr != nil {
|
||||
t.Fatal(envErr)
|
||||
}
|
||||
process, err = manager.startViaLauncher(ctx, spec, token, identity, LaunchPlan{ApplicationName: args[0], CommandLine: command, WorkingDirectory: directory, Environment: environment}, nil)
|
||||
} else {
|
||||
process, err = manager.Start(ctx, spec)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if process.Identity().SessionID == 0 {
|
||||
t.Fatal("native GUI route unexpectedly fell back to Session 0")
|
||||
}
|
||||
if err := process.Release(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return process, ctx
|
||||
}
|
||||
|
||||
func TestNativeLauncherShowsGUIOnSelectedDesktop_HP_DESKTOP_03(t *testing.T) {
|
||||
for _, elevated := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("elevated=%v", elevated), func(t *testing.T) {
|
||||
manager, directory, probe := desktopFixture(t, elevated)
|
||||
process, ctx := desktopProbeProcess(t, manager, directory, probe, elevated, false)
|
||||
var output strings.Builder
|
||||
for {
|
||||
chunk, err := process.ReadOutput(ctx)
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output.Write(chunk.Data)
|
||||
}
|
||||
status, err := process.Wait(ctx)
|
||||
if err != nil || status.Code != 0 {
|
||||
t.Fatalf("probe exit=%v err=%v output=%s", status, err, output.String())
|
||||
}
|
||||
var report struct {
|
||||
SessionID uint32 `json:"session_id"`
|
||||
Station string `json:"window_station"`
|
||||
Desktop string `json:"desktop"`
|
||||
Input string `json:"input_desktop"`
|
||||
Visible bool `json:"window_observed"`
|
||||
ConsoleVisible bool `json:"console_window_visible"`
|
||||
}
|
||||
found := false
|
||||
for _, line := range strings.Split(output.String(), "\n") {
|
||||
if strings.HasPrefix(strings.TrimSpace(line), "{") {
|
||||
if err := json.Unmarshal([]byte(line), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found || !report.Visible || report.ConsoleVisible || report.SessionID != process.Identity().SessionID || !strings.EqualFold(report.Station+`\`+report.Desktop, interactiveDesktop) || !strings.EqualFold(report.Input, "default") {
|
||||
t.Fatalf("GUI probe did not observe an interactive window: %+v output=%s", report, output.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeHeadlessConsoleStillReceivesTERM_HP_DESKTOP_04(t *testing.T) {
|
||||
manager, directory, probe := desktopFixture(t, false)
|
||||
process, ctx := desktopProbeProcess(t, manager, directory, probe, false, true)
|
||||
var output strings.Builder
|
||||
for !strings.Contains(output.String(), "RVBOX_CONSOLE_READY") {
|
||||
chunk, err := process.ReadOutput(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("console never became ready: %v %s", err, output.String())
|
||||
}
|
||||
output.Write(chunk.Data)
|
||||
}
|
||||
outcome, err := manager.Signal(ctx, process, supervisor.SignalTerm)
|
||||
if err != nil || !outcome.Delivered || outcome.Escalated {
|
||||
t.Fatalf("TERM lost native console delivery: %+v err=%v", outcome, err)
|
||||
}
|
||||
for {
|
||||
chunk, err := process.ReadOutput(ctx)
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output.Write(chunk.Data)
|
||||
}
|
||||
if !strings.Contains(output.String(), "RVBOX_CTRL_BREAK_OBSERVED") {
|
||||
t.Fatalf("TERM never reached the registered handler: %s", output.String())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user