Files
Reims-Go/internal/guesttools/drop_agent_test.go
T
ShroapandShroap 7f4cbbe911 feat(guesttools): support Windows-to-macOS window drag-and-drop (zero-copy WebDAV + stealth paste) (#82)
* fix(installer): use 8 KiB HFS+ catalog B-tree nodes for REIMSTOOLS image

* feat(guesttools): support Windows-to-macOS window drag-and-drop (Agent v13)

---------

Co-authored-by: Shroap <184123002+YinLiaoCo@users.noreply.github.com>
2026-12-02 21:35:59 +08:00

188 lines
7.2 KiB
Go

package guesttools
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestToolsDiskImageIncludesDropResolveJS(t *testing.T) {
t.Run("TestDropScriptsUseLF", func(t *testing.T) {
if bytes.Contains(GuestAgentScript(), []byte("\r")) || strings.Contains(guestDropResolveJS, "\r") {
t.Fatal("embedded drop scripts contain a carriage return")
}
})
resolver := guestDropResolveJS
if !strings.Contains(resolver, "com.apple.finder") || strings.Contains(resolver, "'Finder'") {
t.Fatal("drop resolver must identify Finder by bundle ID")
}
path := filepath.Join(t.TempDir(), "tools.img")
if err := BuildToolsDiskImage(path, VMConfig{VMName: "mac"}); err != nil {
t.Fatal(err)
}
image, err := os.ReadFile(path)
if err != nil || !bytes.Contains(image, []byte(resolver)) {
t.Fatalf("missing resolver in image: %v", err)
}
install := string(GuestInstallScript())
agent := string(GuestAgentScript())
if !strings.Contains(install, `cp "$SRC/drop-resolve.js" "$APP_DIR/drop-resolve.js"`) ||
!strings.Contains(install, `chmod 755 "$APP_DIR/drop-resolve.js"`) ||
!strings.Contains(agent, "ensure_drop_webdav_mount") ||
!strings.Contains(agent, "drop_events_loop &\nDROP_EVENTS_PID=$!") {
t.Fatal("missing resolver installation or drop agent startup")
}
}
func TestDropAgentRoutes(t *testing.T) {
lockShellFixture(t)
sh, err := exec.LookPath("sh")
if runtime.GOOS == "windows" {
const gitSH = "C:/Program Files/Git/bin/sh.exe"
if _, statErr := os.Stat(gitSH); statErr == nil {
sh, err = gitSH, nil
}
}
if err != nil {
t.Fatal(err)
}
const osascript = `#!/bin/sh
set -eu
[ "$1" = -l ] && [ "$2" = JavaScript ] && [ "$3" = "./drop-resolve.js" ] || exit 1
shift 3
echo "$1" >> osascript-calls
case "$1" in
run-drop)
[ "$#" = 4 ] && [ "$2" = event ] && [ "$3" = "$PWD/mount" ] &&
[ "$4" = http://host/v1/drop-events/paste ] && [ -f "$DROP_INJECT_LOCK" ] &&
[ "$REIMS_TOKEN" = fixture-secret ] || exit 1
for arg do case "$arg" in *fixture-secret*) exit 1 ;; esac; done
case "$CASE" in
fast-window|fast-text|fast-photos|fast-cancelled) printf 'done\t%s\n' "$EXPECT"; exit 0 ;;
esac
printf 'fallback\nid\t%s\t%s\t0\t0.4\t0.6\ttrue\n' "$MODE" "$DAV"
case "$TARGET" in
desktop) printf 'desktop\tDesktop\t%s/Desktop\t0\n' "$PWD" ;;
photos) printf 'photos\tPhotos\tcom.apple.Photos\t31\n' ;;
window) printf 'window\tNotes\tcom.apple.Notes\t42\n' ;;
esac
printf 'file.txt\tfile.txt\n'
if [ "$CASE" = desktop ]; then printf 'Folder\tFolder\n'; fi ;;
check-mount)
[ "$#" = 3 ] && [ "$2" = "$PWD/mount" ] && [ "$3" = "$PWD/mount/id" ] || exit 1
echo check >> mount-checks
case "$CASE" in
remount) [ -f remounted ] || exit 1 ;;
unhealthy-cache) exit 1 ;;
esac
echo ok ;;
import-photos)
[ "$#" = 2 ] && [ "$2" = "$PWD/mount/id/file.txt" ] || exit 1
if [ "$CASE" = photos-denied ]; then
echo 'Error -1743: Not authorized to send Apple events to Photos.' >&2
exit 1
fi
touch imported
echo ok ;;
paste-drop)
[ "$#" = 10 ] && [ -f "$DROP_INJECT_LOCK" ] && [ "$REIMS_TOKEN" = fixture-secret ] || exit 1
for arg do case "$arg" in *fixture-secret*) exit 1 ;; esac; done
[ "$2 $3 $4 $5 $6 $7 $8 $9" = "0 0.4 0.6 42 com.apple.Notes $MODE http://host/v1/drop-events/paste id" ]
touch attempted
if [ "$CASE" = cache ] || [ "$CASE" = unhealthy-cache ] || [ "$CASE" = legacy ]; then
[ "${10}" = "$PWD/cache/id/file.txt" ] && [ "$(cat "${10}")" = downloaded ] || exit 1
else [ "${10}" = "$PWD/mount/id/file.txt" ]; fi
if [ "$CASE" = closed ]; then echo cancelled
else touch requested; echo pasted; fi ;;
encode-ack) printf '%s|%s|%s|%s|%s' "$2" "$3" "$4" "$5" "$6" ;;
*) exit 1 ;;
esac
`
for _, tc := range []struct{ name, mode, target, ack string }{
{"desktop", "files", "desktop", "placed|desktop|Desktop|"},
{"photos", "files", "photos", "placed|photos|Photos|"},
{"photos-denied", "files", "photos", "failed|photos|Photos|Photos import blocked: allow Automation access to Photos in macOS Privacy & Security"},
{"window", "files", "window", "pasted|window|Notes|"},
{"closed", "files", "window", "cancelled|cancelled||"},
{"fast-window", "files", "window", "pasted|window|Notes|"},
{"fast-photos", "files", "photos", "placed|photos|Photos|"},
{"fast-text", "text", "window", "pasted|window|Notes|"},
{"fast-cancelled", "text", "desktop", "cancelled|cancelled||"},
{"cache", "files", "window", "pasted|window|Notes|"},
{"remount", "files", "window", "pasted|window|Notes|"},
{"unhealthy-cache", "files", "window", "pasted|window|Notes|"},
{"legacy", "files", "window", "pasted|window|Notes|"},
} {
t.Run(tc.name, func(t *testing.T) {
work := t.TempDir()
if err := os.WriteFile(filepath.Join(work, "osascript"), []byte(osascript), 0o755); err != nil {
t.Fatal(err)
}
cmd := exec.Command(sh, "-s")
cmd.Dir = work
cmd.Env = append(os.Environ(), "CASE="+tc.name, "MODE="+tc.mode, "TARGET="+tc.target, "EXPECT=id|"+tc.ack)
cmd.Stdin = strings.NewReader(guestDropAgent + `
set -eu
PATH="$PWD:$PATH"
APP_DIR=. TMP_DIR=. DROP_CACHE="$PWD/cache" DROP_MOUNT="$PWD/mount"
unset REIMS_TOKEN
REIMS_TOKEN=fixture-secret REIMS_HOST_URL=http://host
export DROP_INJECT_LOCK="$PWD/custom-lock" DAV=true
mkdir Desktop
case "$CASE" in
cache) ;;
legacy) DAV=false ;;
*) mkdir -p mount/id/Folder; echo file > mount/id/file.txt; echo nested > mount/id/Folder/child ;;
esac
ensure_drop_webdav_mount() {
touch mount-checked
if [ -f unmounted ]; then touch remounted; fi
}
unmount_shared_folder() {
[ "$1" = "$DROP_MOUNT" ] || return 1
touch unmounted
}
urlencode() { printf '%s' "$1"; }
curl() {
case "$*" in
*'/v1/drop-events/ack') [ ! -e "$DROP_INJECT_LOCK" ] && [ "$8" = --data-binary ] && printf '%s' "$9" > ack ;;
*) case "$CASE:$*" in
cache:*'/v1/drop-file?drop_id=id&path=file.txt'|unhealthy-cache:*'/v1/drop-file?drop_id=id&path=file.txt'|legacy:*'/v1/file?path=file.txt') ;;
*) return 1 ;;
esac
[ "$6" = -o ] && echo downloaded > "$7" ;;
esac
}
process_drop_event event
[ "$(cat ack)" = "$EXPECT" ]
[ ! -e "$DROP_INJECT_LOCK" ]
[ ! -e "$TMP_DIR/drop_text.$$" ]
case "$CASE" in
fast-*) [ "$(wc -l < osascript-calls)" -eq 1 ] && [ "$(cat osascript-calls)" = run-drop ]; exit $? ;;
esac
if [ "$TARGET" = window ]; then [ -f attempted ]; else [ ! -e attempted ]; fi
if [ "$TARGET" = window ] && [ "$CASE" != closed ]; then [ -f requested ]; else [ ! -e requested ]; fi
if [ "$CASE" = photos ]; then [ -f imported ]; else [ ! -e imported ]; fi
if [ "$MODE" = files ] && [ "$DAV" = true ]; then [ -f mount-checked ]; fi
if [ "$CASE" = remount ] || [ "$CASE" = unhealthy-cache ]; then
[ -f unmounted ] && [ -f remounted ] || exit 1
[ "$(wc -l < mount-checks)" -eq 2 ]
fi
if [ "$CASE" = desktop ]; then
[ "$(cat Desktop/file.txt)" = file ]
[ "$(cat Desktop/Folder/child)" = nested ]
[ ! -e "$DROP_CACHE" ]
fi
`)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("sh: %v\n%s", err, out)
}
})
}
}