From dbf475d55c8cb2a7b0f951e4bc6f8c8fc5902069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=83=E5=B7=9D=E7=99=BD=E6=B5=AA?= <1679659@qq.com> Date: Tue, 1 Dec 2026 09:19:26 -0700 Subject: [PATCH] test: serialize Windows POSIX shell fixtures across packages --- internal/guesttools/clipboard_agent_test.go | 1 + internal/guesttools/drive_mount_test.go | 1 + internal/guesttools/folder_agent_test.go | 11 +++-- internal/guesttools/guesttools_test.go | 1 + internal/guesttools/image.go | 25 ++++------ internal/guesttools/keymap_agent_test.go | 2 + internal/guesttools/shell_lock_test.go | 16 ++++++ .../macos/installer/offline_helper_test.go | 1 + .../macos/installer/shell_lock_other_test.go | 9 ++++ .../installer/shell_lock_windows_test.go | 49 +++++++++++++++++++ .../testutil/shelllock/shelllock_other.go | 7 +++ .../testutil/shelllock/shelllock_windows.go | 43 ++++++++++++++++ 12 files changed, 148 insertions(+), 18 deletions(-) create mode 100644 internal/guesttools/shell_lock_test.go create mode 100644 internal/macos/installer/shell_lock_other_test.go create mode 100644 internal/macos/installer/shell_lock_windows_test.go create mode 100644 internal/testutil/shelllock/shelllock_other.go create mode 100644 internal/testutil/shelllock/shelllock_windows.go diff --git a/internal/guesttools/clipboard_agent_test.go b/internal/guesttools/clipboard_agent_test.go index 1a0a382..678fa1f 100644 --- a/internal/guesttools/clipboard_agent_test.go +++ b/internal/guesttools/clipboard_agent_test.go @@ -37,6 +37,7 @@ func TestGuestAgentClipboardScript(t *testing.T) { } { t.Run(name, func(t *testing.T) { t.Parallel() + lockShellFixture(t) tmp := t.TempDir() script := filepath.Join(tmp, "clipboard-test.sh") code := "set -eu\n" + agent[start:end] + clipboardShellFixture diff --git a/internal/guesttools/drive_mount_test.go b/internal/guesttools/drive_mount_test.go index cbe4264..a71d667 100644 --- a/internal/guesttools/drive_mount_test.go +++ b/internal/guesttools/drive_mount_test.go @@ -18,6 +18,7 @@ func TestGuestAgentDriveMount(t *testing.T) { t.Skip("POSIX shell is unavailable") } } + lockShellFixture(t) dir := t.TempDir() helper := `#!/usr/bin/perl diff --git a/internal/guesttools/folder_agent_test.go b/internal/guesttools/folder_agent_test.go index 33ec082..2129aae 100644 --- a/internal/guesttools/folder_agent_test.go +++ b/internal/guesttools/folder_agent_test.go @@ -5,6 +5,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strconv" "strings" "testing" ) @@ -23,6 +24,7 @@ func TestGuestAgentFolderMounts(t *testing.T) { run := func(t *testing.T, script string, args ...string) { t.Helper() t.Parallel() + lockShellFixture(t) cmd := exec.Command(sh, args...) cmd.Dir = t.TempDir() if err := os.WriteFile(filepath.Join(cmd.Dir, "mount_webdav"), []byte(folderMountWebDAV), 0o755); err != nil { @@ -35,9 +37,12 @@ func TestGuestAgentFolderMounts(t *testing.T) { } } agent, uninstall, install := string(GuestAgentScript()), string(GuestUninstallScript()), string(GuestInstallScript()) - if CurrentAgentVersion != 10 || !strings.Contains(agent, `"agent_version\":10,`) || - !strings.Contains(install, "Reims Guest Tools 8 installed and connected!") { - t.Fatal("missing Guest Tools version 10") + if CurrentAgentVersion != KeymapAgentVersion { + t.Fatalf("CurrentAgentVersion = %d, want keymap version %d", CurrentAgentVersion, KeymapAgentVersion) + } + versionMarker := `"agent_version\":` + strconv.Itoa(CurrentAgentVersion) + `,` + if !strings.Contains(agent, versionMarker) || !strings.Contains(install, "Reims Guest Tools installed") { + t.Fatal("Guest Tools scripts do not advertise the current agent version") } mounts := scriptSection(t, agent, "urlencode() {", "\nhash_file() {") + scriptSection(t, agent, "\ndrive_mount_failures=", "\nreport_status() {") diff --git a/internal/guesttools/guesttools_test.go b/internal/guesttools/guesttools_test.go index 77e8c00..8ee7263 100644 --- a/internal/guesttools/guesttools_test.go +++ b/internal/guesttools/guesttools_test.go @@ -640,6 +640,7 @@ func TestBuildToolsDiskImageAndScriptSyntax(t *testing.T) { if shBin == "" { return } + lockShellFixture(t) for name, content := range map[string][]byte{ "guest-tools.sh": GuestAgentScript(), "Install-Reims-Tools.command": GuestInstallScript(), diff --git a/internal/guesttools/image.go b/internal/guesttools/image.go index 7da79d8..56ab1e4 100644 --- a/internal/guesttools/image.go +++ b/internal/guesttools/image.go @@ -24,10 +24,9 @@ const ( //go:embed file_sync.sh var guestFileSync string -// BuildToolsDiskImage creates a small read-only HFS+ disk image ("REIMSTOOLS") -// containing the per-VM Bearer token config, Guest Tools 8 POSIX agent script -// (text + PNG image clipboard with long-poll, WebDAV live mount with fallback -// sync, and caffeinate prevent-sleep daemon), plus install/uninstall/pmset scripts. +// BuildToolsDiskImage creates a read-only HFS+ disk image ("REIMSTOOLS") +// containing the per-VM Bearer token config, the POSIX guest agent script, and +// install/uninstall/pmset helper scripts. func BuildToolsDiskImage(outPath string, cfg VMConfig) error { if err := os.MkdirAll(filepath.Dir(outPath), 0o755); err != nil { return err @@ -74,16 +73,12 @@ func BuildToolsDiskImage(outPath string, cfg VMConfig) error { return installer.BuildHFSPlusDiskImage(outPath, ToolsVolumeName, files) } -// GuestAgentScript returns the Guest Tools 8 stock-macOS POSIX shell daemon -// (no Xcode or Python required): -// - Two-way UTF-8 text + PNG screenshot clipboard with server long-polling -// - Zero-duplication WebDAV shared folder mount (~/Desktop/ReimsShare) with automatic sync fallback -// - Host-controlled read-only Windows fixed-drive mounts with automatic unmount -// - Host-controlled custom shared folder mounts -// - Live host-controlled prevent-sleep guard via `caffeinate -dims` (no sudo required) +// GuestAgentScript returns the stock-macOS POSIX shell daemon handling +// clipboard sync, WebDAV and fallback folder shares, read-only host drive +// mounts, and caffeinate prevent-sleep. func GuestAgentScript() []byte { return []byte(`#!/bin/sh -# Reims Guest Tools 8: clipboard, WebDAV shares, read-only Windows drives, prevent-sleep. +# Reims Guest Tools agent: clipboard, WebDAV shares, read-only Windows drives, prevent-sleep. set -u # Force UTF-8 for macOS pbpaste/pbcopy under launchd (otherwise Simplified @@ -609,7 +604,7 @@ sync_clipboard() { fi ;; 503) - # Feature disabled on host: back off instead of busy-looping osascript + curl (M11). + # Feature disabled on host: back off instead of busy-looping osascript + curl. sleep 5 ;; 000) @@ -769,12 +764,12 @@ launchctl bootstrap "gui/$uid" "$PLIST" 2>/dev/null || launchctl load -w "$PLIST # Verify host bridge connectivity immediately. . "$APP_DIR/config" if curl -fsS -m 5 -H "Authorization: Bearer $REIMS_TOKEN" "$REIMS_HOST_URL/v1/ping" >/dev/null 2>&1; then - echo "Reims Guest Tools 8 installed and connected!" + echo "Reims Guest Tools installed and connected!" echo "Clipboard: UTF-8 text + PNG screenshots (two-way)" echo "Shared folder: ~/Desktop/ReimsShare (WebDAV live mount, auto-fallback to sync)" echo "Prevent-sleep: controlled live from Reims-Go host UI" else - echo "Reims Guest Tools 8 installed (waiting for Windows host bridge at $REIMS_HOST_URL)..." + echo "Reims Guest Tools installed (waiting for Windows host bridge at $REIMS_HOST_URL)..." fi `) } diff --git a/internal/guesttools/keymap_agent_test.go b/internal/guesttools/keymap_agent_test.go index eed06eb..569ae06 100644 --- a/internal/guesttools/keymap_agent_test.go +++ b/internal/guesttools/keymap_agent_test.go @@ -28,6 +28,7 @@ func TestGuestAgentKeymapScript(t *testing.T) { for _, input := range []string{"(null)", "(\n)", "({ HIDKeyboardModifierMappingSrc = 30064771184; HIDKeyboardModifierMappingDst = 30064771299; })"} { t.Run(input, func(t *testing.T) { t.Parallel() + lockShellFixture(t) dir := t.TempDir() fixture := keymapShellFixture + strings.ReplaceAll(agent[start:end], "/usr/bin/hidutil", "hidutil") fixture += "\nprintf '%s\\n' \"$1\" > state\nsync_keymap_carrier '{}'\n" @@ -54,6 +55,7 @@ func TestGuestAgentKeymapScript(t *testing.T) { for _, teardown := range []string{"disable", "exit", "uninstall"} { t.Run(teardown, func(t *testing.T) { t.Parallel() + lockShellFixture(t) dir := t.TempDir() fixture := keymapShellFixture + strings.ReplaceAll(agent[start:end], "/usr/bin/hidutil", "hidutil") + keymapShellExercise if teardown == "uninstall" { diff --git a/internal/guesttools/shell_lock_test.go b/internal/guesttools/shell_lock_test.go new file mode 100644 index 0000000..d5abc8a --- /dev/null +++ b/internal/guesttools/shell_lock_test.go @@ -0,0 +1,16 @@ +package guesttools + +import ( + "testing" + + "github.com/Nwflower/reims-go/internal/testutil/shelllock" +) + +func lockShellFixture(t *testing.T) { + t.Helper() + release, err := shelllock.Acquire() + if err != nil { + t.Fatal(err) + } + t.Cleanup(release) +} diff --git a/internal/macos/installer/offline_helper_test.go b/internal/macos/installer/offline_helper_test.go index d27b8f7..5d871a8 100644 --- a/internal/macos/installer/offline_helper_test.go +++ b/internal/macos/installer/offline_helper_test.go @@ -210,6 +210,7 @@ func TestHelperInstallScriptSyntaxAndDiskInspection(t *testing.T) { if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil { t.Fatal(err) } + lockPOSIXShell(t) // 1. POSIX sh syntax check. if out, err := exec.Command(shBin, "-n", scriptPath).CombinedOutput(); err != nil { t.Fatalf("sh -n install.sh failed: %v\n%s", err, out) diff --git a/internal/macos/installer/shell_lock_other_test.go b/internal/macos/installer/shell_lock_other_test.go new file mode 100644 index 0000000..1db5967 --- /dev/null +++ b/internal/macos/installer/shell_lock_other_test.go @@ -0,0 +1,9 @@ +//go:build !windows + +package installer + +import "testing" + +func lockPOSIXShell(t *testing.T) { + t.Helper() +} diff --git a/internal/macos/installer/shell_lock_windows_test.go b/internal/macos/installer/shell_lock_windows_test.go new file mode 100644 index 0000000..60c24cf --- /dev/null +++ b/internal/macos/installer/shell_lock_windows_test.go @@ -0,0 +1,49 @@ +//go:build windows + +package installer + +import ( + "errors" + "runtime" + "testing" + + "golang.org/x/sys/windows" +) + +const shellFixtureMutexName = `Local\ReimsGo-Test-MSYS` + +func lockPOSIXShell(t *testing.T) { + t.Helper() + + runtime.LockOSThread() + name, err := windows.UTF16PtrFromString(shellFixtureMutexName) + if err != nil { + runtime.UnlockOSThread() + t.Fatal(err) + } + handle, err := windows.CreateMutex(nil, false, name) + if err != nil && !errors.Is(err, windows.ERROR_ALREADY_EXISTS) { + runtime.UnlockOSThread() + t.Fatalf("create MSYS test mutex: %v", err) + } + event, err := windows.WaitForSingleObject(handle, windows.INFINITE) + if err != nil { + _ = windows.CloseHandle(handle) + runtime.UnlockOSThread() + t.Fatalf("wait for MSYS test mutex: %v", err) + } + if event != windows.WAIT_OBJECT_0 && event != windows.WAIT_ABANDONED { + _ = windows.CloseHandle(handle) + runtime.UnlockOSThread() + t.Fatalf("unexpected MSYS test mutex result: %#x", event) + } + t.Cleanup(func() { + if err := windows.ReleaseMutex(handle); err != nil { + t.Errorf("release MSYS test mutex: %v", err) + } + if err := windows.CloseHandle(handle); err != nil { + t.Errorf("close MSYS test mutex: %v", err) + } + runtime.UnlockOSThread() + }) +} diff --git a/internal/testutil/shelllock/shelllock_other.go b/internal/testutil/shelllock/shelllock_other.go new file mode 100644 index 0000000..a430487 --- /dev/null +++ b/internal/testutil/shelllock/shelllock_other.go @@ -0,0 +1,7 @@ +//go:build !windows + +package shelllock + +func Acquire() (func(), error) { + return func() {}, nil +} diff --git a/internal/testutil/shelllock/shelllock_windows.go b/internal/testutil/shelllock/shelllock_windows.go new file mode 100644 index 0000000..9f2cbfb --- /dev/null +++ b/internal/testutil/shelllock/shelllock_windows.go @@ -0,0 +1,43 @@ +//go:build windows + +package shelllock + +import ( + "errors" + "fmt" + "runtime" + + "golang.org/x/sys/windows" +) + +const mutexName = `Local\ReimsGo-Test-MSYS` + +func Acquire() (func(), error) { + runtime.LockOSThread() + name, err := windows.UTF16PtrFromString(mutexName) + if err != nil { + runtime.UnlockOSThread() + return nil, err + } + handle, err := windows.CreateMutex(nil, false, name) + if err != nil && !errors.Is(err, windows.ERROR_ALREADY_EXISTS) { + runtime.UnlockOSThread() + return nil, fmt.Errorf("create MSYS test mutex: %w", err) + } + event, err := windows.WaitForSingleObject(handle, windows.INFINITE) + if err != nil { + _ = windows.CloseHandle(handle) + runtime.UnlockOSThread() + return nil, fmt.Errorf("wait for MSYS test mutex: %w", err) + } + if event != windows.WAIT_OBJECT_0 && event != windows.WAIT_ABANDONED { + _ = windows.CloseHandle(handle) + runtime.UnlockOSThread() + return nil, fmt.Errorf("unexpected MSYS test mutex result: %#x", event) + } + return func() { + _ = windows.ReleaseMutex(handle) + _ = windows.CloseHandle(handle) + runtime.UnlockOSThread() + }, nil +}