* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
268 lines
10 KiB
Go
268 lines
10 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"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")
|
|
}
|
|
var foundResolver bool
|
|
for _, f := range ToolsDiskFiles(VMConfig{VMName: "mac"}) {
|
|
if f.Name != "drop-resolve.js" {
|
|
continue
|
|
}
|
|
rc, err := f.Open()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := io.ReadAll(rc)
|
|
_ = rc.Close()
|
|
if err != nil || !bytes.Contains(data, []byte(resolver)) {
|
|
t.Fatalf("missing resolver in tools disk files: %v", err)
|
|
}
|
|
foundResolver = true
|
|
}
|
|
if !foundResolver {
|
|
t.Fatal("missing drop-resolve.js in tools disk files")
|
|
}
|
|
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" >> drop-calls
|
|
window=Notes bundle=com.apple.Notes
|
|
if [ "$CASE" = stale-reference ]; then window=Finder bundle=com.apple.finder; fi
|
|
case "$1" in
|
|
run-drop)
|
|
[ "$#" = 3 ] && [ "$2" = event ] &&
|
|
[ "$3" = 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-text|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\t%s\t%s\t42\n' "$window" "$bundle" ;;
|
|
esac
|
|
case "$CASE" in
|
|
denied-directory) printf 'Folder\tFolder\n' ;;
|
|
desktop) printf 'file.txt\tfile.txt\nFolder\tFolder\n' ;;
|
|
*) printf 'file.txt\tfile.txt\n' ;;
|
|
esac ;;
|
|
probe-mount|probe-mount-ref)
|
|
[ "$#" = 3 ] && [ "$2" = "$PWD/mount" ] && [ "$3" = "$PWD/mount/id" ] &&
|
|
[ ! -e "$DROP_INJECT_LOCK" ] || exit 1
|
|
case "$TARGET:$1" in
|
|
desktop:probe-mount|window:probe-mount-ref|photos:probe-mount-ref) ;;
|
|
*) exit 1 ;;
|
|
esac
|
|
echo check >> mount-checks
|
|
case "$CASE" in
|
|
remount) [ -f remounted ] || exit 1 ;;
|
|
stale-reference)
|
|
[ ! -e drop_mount_denied ] || exit 1
|
|
if [ ! -f remounted ]; then echo unhealthy; exit 0; fi ;;
|
|
unhealthy-cache) echo unhealthy; exit 0 ;;
|
|
denied-*) echo denied; exit 0 ;;
|
|
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 $bundle $MODE http://host/v1/drop-events/paste id" ]
|
|
touch attempted
|
|
case "$CASE" in
|
|
cache|unhealthy-cache|legacy)
|
|
[ "${10}" = "$PWD/cache/id/file.txt" ] && [ "$(cat "${10}")" = downloaded ] || exit 1 ;;
|
|
*) [ "${10}" = "$PWD/mount/id/file.txt" ] || exit 1 ;;
|
|
esac
|
|
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-text", "text", "window", "pasted|window|Notes|"},
|
|
{"fast-cancelled", "text", "desktop", "cancelled|cancelled||"},
|
|
{"cache", "files", "window", "pasted|window|Notes|"},
|
|
{"remount", "files", "window", "pasted|window|Notes|"},
|
|
{"stale-reference", "files", "window", "pasted|window|Finder|"},
|
|
{"unhealthy-cache", "files", "window", "pasted|window|Notes|"},
|
|
{"legacy", "files", "window", "pasted|window|Notes|"},
|
|
{"allowed-after-denial", "files", "window", "pasted|window|Notes|"},
|
|
{"denied-file-desktop", "files", "desktop", "failed|desktop|Desktop|Network Volumes permission denied"},
|
|
{"denied-file-window", "files", "window", "failed|window|Notes|Network Volumes permission denied"},
|
|
{"denied-cached", "files", "desktop", "failed|desktop|Desktop|Network Volumes permission denied"},
|
|
{"denied-directory", "files", "desktop", "failed|desktop|Desktop|Network Volumes permission denied"},
|
|
} {
|
|
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 REIMS_VM=guest
|
|
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
|
|
case "$CASE" in
|
|
denied-cached|allowed-after-denial|stale-reference) : > "$TMP_DIR/drop_mount_denied" ;;
|
|
esac
|
|
drop_mount_readable() {
|
|
[ ! -e "$DROP_INJECT_LOCK" ] || return 1
|
|
touch fast-mount-check
|
|
case "$CASE" in
|
|
denied-cached) return 2 ;;
|
|
allowed-after-denial|stale-reference) return 0 ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
ensure_drop_webdav_mount() {
|
|
if [ -f unmounted ]; then
|
|
echo remount >> drop-calls
|
|
touch remounted
|
|
fi
|
|
}
|
|
is_reims_dav_mounted() {
|
|
[ "$1" = "$DROP_MOUNT" ] && [ "$2" = /dav-drop/guest/ ] && [ -d "$DROP_MOUNT" ] &&
|
|
{ [ ! -e unmounted ] || [ -e remounted ]; }
|
|
}
|
|
unmount_shared_folder() {
|
|
[ "$1" = "$DROP_MOUNT" ] || return 1
|
|
echo unmount >> drop-calls
|
|
touch unmounted
|
|
}
|
|
urlencode() { printf '%s' "$1"; }
|
|
curl() {
|
|
case "$*" in
|
|
*'/v1/drop-events/ack') [ ! -e "$DROP_INJECT_LOCK" ] && [ "$8" = --data-binary ] && printf '%s' "$9" > ack ;;
|
|
*)
|
|
printf '%s\n' "$*" >> downloads
|
|
[ ! -e "$DROP_INJECT_LOCK" ] && [ "$6" = -o ] || return 1
|
|
case "$CASE:$8" 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')
|
|
echo downloaded > "$7" ;;
|
|
*) return 1 ;;
|
|
esac ;;
|
|
esac
|
|
}
|
|
process_drop_event event
|
|
[ "$(cat ack)" = "$EXPECT" ]
|
|
[ ! -e "$DROP_INJECT_LOCK" ]
|
|
[ ! -e "$TMP_DIR/drop_text.$$" ]
|
|
case "$CASE" in
|
|
fast-*) [ "$(wc -l < drop-calls)" -eq 1 ] && [ "$(cat drop-calls)" = run-drop ]; exit $? ;;
|
|
esac
|
|
case "$CASE:$TARGET" in
|
|
denied-*:*) [ ! -e attempted ] ;;
|
|
*:window) [ -f attempted ] ;;
|
|
*) [ ! -e attempted ] ;;
|
|
esac
|
|
if [ -f attempted ] && [ "$CASE" != closed ]; then [ -f requested ]; else [ ! -e requested ]; fi
|
|
if [ "$CASE" = photos ]; then [ -f imported ]; else [ ! -e imported ]; fi
|
|
case "$CASE" in
|
|
remount|stale-reference|unhealthy-cache)
|
|
[ -f unmounted ] && [ -f remounted ] || exit 1
|
|
[ "$(wc -l < mount-checks)" -eq 2 ] ;;
|
|
legacy|cache|denied-cached) [ ! -e mount-checks ] ;;
|
|
*) [ "$(wc -l < mount-checks)" -eq 1 ] ;;
|
|
esac
|
|
case "$CASE" in
|
|
denied-*)
|
|
[ -f "$TMP_DIR/drop_mount_denied" ]
|
|
[ ! -e unmounted ] && [ ! -e remounted ] || exit 1
|
|
[ ! -e downloads ] && [ ! -e "$DROP_CACHE" ] || exit 1
|
|
[ ! -e Desktop/file.txt ] && [ ! -e Desktop/Folder ] || exit 1 ;;
|
|
*) [ ! -e "$TMP_DIR/drop_mount_denied" ] ;;
|
|
esac
|
|
case "$CASE" in
|
|
desktop)
|
|
[ "$(cat Desktop/file.txt)" = file ]
|
|
[ "$(cat Desktop/Folder/child)" = nested ]
|
|
[ ! -e "$DROP_CACHE" ] && [ ! -e downloads ] ;;
|
|
window|allowed-after-denial) [ ! -e downloads ] ;;
|
|
stale-reference)
|
|
[ ! -e downloads ] && [ ! -e "$DROP_CACHE" ] || exit 1
|
|
expected_calls=$(printf '%s\n' run-drop probe-mount-ref unmount remount probe-mount-ref paste-drop encode-ack)
|
|
[ "$(cat drop-calls)" = "$expected_calls" ] ;;
|
|
esac
|
|
case "$CASE" in
|
|
denied-cached)
|
|
[ -f fast-mount-check ]
|
|
! grep -Eq '^probe-mount(-ref)?$' drop-calls ;;
|
|
allowed-after-denial|stale-reference) [ -f fast-mount-check ] ;;
|
|
esac
|
|
`)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("sh: %v\n%s", err, out)
|
|
}
|
|
})
|
|
}
|
|
}
|