304 lines
11 KiB
Go
304 lines
11 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"maps"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGuestAgentProbesDropMountAccess(t *testing.T) {
|
|
node, err := exec.LookPath("node")
|
|
if err != nil {
|
|
t.Skip("Node.js is unavailable")
|
|
}
|
|
fixture, err := filepath.Abs("testdata/pasteboard.cjs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const mount, scope = "mount", "mount/selection"
|
|
const referencePaths = mount + "\n" + scope + "\n"
|
|
type nativeError struct {
|
|
Domain string `json:"domain"`
|
|
Code int `json:"code"`
|
|
Underlying *nativeError `json:"underlying,omitempty"`
|
|
}
|
|
withPOSIX := func(code int) *nativeError {
|
|
return &nativeError{"NSCocoaErrorDomain", 256, &nativeError{Domain: "NSPOSIXErrorDomain", Code: code}}
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
errorAt string
|
|
nativeError *nativeError
|
|
missingScope bool
|
|
missingRef string
|
|
perlErrno string
|
|
requireRef bool
|
|
want string
|
|
wantReferences string
|
|
}{
|
|
{name: "readable root and scope", want: "ok"},
|
|
{name: "desktop accepts a readable scope with a nil file reference", missingRef: scope, want: "ok"},
|
|
{name: "non-desktop accepts readable paths with valid file references", requireRef: true, want: "ok", wantReferences: referencePaths},
|
|
{name: "non-desktop rejects a readable scope with a nil file reference", missingRef: scope, requireRef: true, want: "unhealthy", wantReferences: referencePaths},
|
|
{name: "non-desktop handles EPERM at scope before checking any file reference", errorAt: scope, nativeError: withPOSIX(1), missingRef: mount, requireRef: true, want: "denied"},
|
|
{name: "Cocoa permission denial at root", errorAt: mount, nativeError: &nativeError{Domain: "NSCocoaErrorDomain", Code: 257}, want: "denied"},
|
|
{name: "underlying POSIX EPERM at scope", errorAt: scope, nativeError: withPOSIX(1), want: "denied"},
|
|
{name: "underlying POSIX EACCES at root", errorAt: mount, nativeError: withPOSIX(13), want: "denied"},
|
|
{name: "missing drop scope", missingScope: true, want: "unhealthy"},
|
|
{name: "underlying POSIX EIO", errorAt: scope, nativeError: withPOSIX(5), want: "unhealthy"},
|
|
{name: "errno fallback without a bridged NSError", errorAt: mount, perlErrno: "EPERM", want: "denied"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write := func(name string, contents []byte) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), contents, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
path := scope
|
|
if tc.missingScope {
|
|
path = mount
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(dir, path), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("drop-resolve.js", []byte(guestDropResolveJS))
|
|
if tc.errorAt != "" {
|
|
data, err := json.Marshal(map[string]*nativeError{tc.errorAt: tc.nativeError})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("test-mount-directory-errors.json", data)
|
|
}
|
|
if tc.perlErrno != "" {
|
|
data, err := json.Marshal(map[string]string{tc.errorAt: tc.perlErrno})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("test-hosted-probe-errors.json", data)
|
|
}
|
|
if tc.missingRef != "" {
|
|
write("test-missing-file-reference", []byte(tc.missingRef))
|
|
}
|
|
mode := "probe-mount"
|
|
if tc.requireRef {
|
|
mode = "probe-mount-ref"
|
|
}
|
|
cmd := exec.CommandContext(t.Context(), node, fixture, "drop-resolve.js", mode, mount, scope)
|
|
cmd.Dir = dir
|
|
output, err := cmd.CombinedOutput()
|
|
if got := strings.TrimSpace(string(output)); err != nil || got != tc.want {
|
|
t.Fatalf("probe mount = %q, want %q, err=%v", got, tc.want, err)
|
|
}
|
|
wantProbe := mount + "\t$()\n"
|
|
if tc.errorAt != mount {
|
|
wantProbe += scope + "\t$()\n"
|
|
}
|
|
probe, err := os.ReadFile(filepath.Join(dir, "test-mount-directory-probe.log"))
|
|
if err != nil || string(probe) != wantProbe {
|
|
t.Fatalf("Foundation calls = %q, want %q, err=%v", probe, wantProbe, err)
|
|
}
|
|
references, err := os.ReadFile(filepath.Join(dir, "test-mount-reference-probe.log"))
|
|
if tc.wantReferences == "" {
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("unexpected file reference calls = %q, err=%v", references, err)
|
|
}
|
|
} else if err != nil || string(references) != tc.wantReferences {
|
|
t.Fatalf("file reference calls = %q, want %q, err=%v", references, tc.wantReferences, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGuestAgentChecksHostedClipboardReadabilityBeforePublishing(t *testing.T) {
|
|
node, err := exec.LookPath("node")
|
|
if err != nil {
|
|
t.Skip("Node.js is unavailable")
|
|
}
|
|
fixture, err := filepath.Abs("testdata/pasteboard.cjs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const dropID = "clip-0123456789abcdef"
|
|
const previous = `{"changeCount":7,"items":[{"public.utf8-plain-text":"guest copy"}]}`
|
|
paths := []string{"mount/" + dropID + "/empty.txt", "mount/" + dropID + "/empty-dir", "mount/" + dropID + "/folder"}
|
|
for _, tc := range []struct {
|
|
name string
|
|
denied string
|
|
errno string
|
|
mode string
|
|
want string
|
|
probe bool
|
|
missingScope bool
|
|
}{
|
|
{name: "files and folders readable", want: "ok", probe: true},
|
|
{name: "file stat passes but cannot read", denied: paths[0], want: "unreadable", probe: true},
|
|
{name: "directory stat passes but cannot list", denied: paths[1], want: "unreadable", probe: true},
|
|
{name: "file open returns EPERM", denied: paths[0], errno: "EPERM", want: "denied", probe: true},
|
|
{name: "directory read returns EACCES", denied: paths[1], errno: "EACCES", want: "denied", probe: true},
|
|
{name: "missing scope with inaccessible mount", denied: "mount", errno: "EPERM", want: "denied", probe: true, missingScope: true},
|
|
{name: "missing selection", want: "remount", probe: true},
|
|
{name: "local archive cache skips the network probe", denied: paths[0], mode: "cache", want: "ok"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write := func(name, contents string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
probePaths, dirs := paths, paths[1:]
|
|
if tc.missingScope {
|
|
probePaths = []string{"mount"}
|
|
dirs = probePaths
|
|
}
|
|
for _, path := range dirs {
|
|
if err := os.MkdirAll(filepath.Join(dir, path), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if !tc.missingScope {
|
|
if tc.want != "remount" {
|
|
write(paths[0], "")
|
|
}
|
|
write(paths[2]+"/first.txt", "first")
|
|
}
|
|
write("manifest.json", `{"drop_id":"`+dropID+`","items":["empty.txt","empty-dir","folder"]}`)
|
|
write("drop-resolve.js", guestDropResolveJS)
|
|
write("test-pasteboard.json", previous)
|
|
if tc.errno != "" {
|
|
data, err := json.Marshal(map[string]string{tc.denied: tc.errno})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("test-hosted-probe-errors.json", string(data))
|
|
} else if tc.denied != "" {
|
|
data, err := json.Marshal([]string{tc.denied})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
write("test-hosted-probe-denied.json", string(data))
|
|
}
|
|
cmd := exec.CommandContext(t.Context(), node, fixture, "drop-resolve.js", "write-clip-files",
|
|
"manifest.json", "mount", "7", tc.mode)
|
|
cmd.Dir = dir
|
|
output, err := cmd.CombinedOutput()
|
|
if got := strings.TrimSpace(string(output)); err != nil || got != tc.want {
|
|
t.Fatalf("write clipboard = %q, want %q, err=%v", got, tc.want, err)
|
|
}
|
|
probed, err := os.ReadFile(filepath.Join(dir, "test-hosted-probe.json"))
|
|
if tc.probe {
|
|
var got []string
|
|
if err != nil || json.Unmarshal(probed, &got) != nil || !slices.Equal(got, probePaths) {
|
|
t.Fatalf("probe paths = %q, want %v, err=%v", probed, probePaths, err)
|
|
}
|
|
} else if !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("unexpected probe for %s, err=%v", tc.want, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "test-mount-directory-probe.log")); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("clipboard write invoked the prompting probe, err=%v", err)
|
|
}
|
|
if tc.want != "ok" {
|
|
got, err := os.ReadFile(filepath.Join(dir, "test-pasteboard.json"))
|
|
if err != nil || string(got) != previous {
|
|
t.Fatalf("guest clipboard changed after failed readiness check: %q, err=%v", got, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGuestAgentWritesClipboardFilesWithDeferredFlavors(t *testing.T) {
|
|
node, err := exec.LookPath("node")
|
|
if err != nil {
|
|
t.Skip("Node.js is unavailable")
|
|
}
|
|
fixture, err := filepath.Abs("testdata/pasteboard.cjs")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const dropID = "clip-0123456789abcdef"
|
|
const mixed = `{"changeCount":7,"items":[{"com.example.deferred":null,"public.utf8-plain-text":"guest copy"}]}`
|
|
const deferredOnly = `{"changeCount":7,"items":[{"com.example.deferred":null}]}`
|
|
for _, tc := range []struct {
|
|
name string
|
|
previous string
|
|
failFile string
|
|
wantError string
|
|
}{
|
|
{name: "readable and deferred flavors", previous: mixed},
|
|
{name: "deferred flavor only", previous: deferredOnly},
|
|
{name: "write failure restores readable flavor", previous: mixed, failFile: "test-fail-file-write", wantError: "Clipboard write failed"},
|
|
{name: "partial write restores readable flavor", previous: mixed, failFile: "test-partial-file-write", wantError: "Clipboard file readback did not match"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
write := func(name, contents string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(contents), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
mountDir := filepath.Join("mount", dropID)
|
|
if err := os.MkdirAll(filepath.Join(dir, mountDir), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
paths := []string{filepath.Join(mountDir, "first.txt"), filepath.Join(mountDir, "second.txt")}
|
|
for _, path := range paths {
|
|
write(path, "")
|
|
}
|
|
const manifest = `{"drop_id":"` + dropID + `","items":["first.txt","second.txt"]}`
|
|
write("manifest.json", manifest)
|
|
write("drop-resolve.js", guestDropResolveJS)
|
|
write("test-pasteboard.json", tc.previous)
|
|
if tc.failFile != "" {
|
|
write(tc.failFile, "")
|
|
}
|
|
cmd := exec.CommandContext(t.Context(), node, fixture, "drop-resolve.js", "write-clip-files", "manifest.json", "mount", "7")
|
|
cmd.Dir = dir
|
|
output, err := cmd.CombinedOutput()
|
|
if tc.wantError == "" {
|
|
if got := strings.TrimSpace(string(output)); err != nil || got != "ok" {
|
|
t.Fatalf("write clipboard = %q, want ok, err=%v", got, err)
|
|
}
|
|
} else if err == nil || !strings.Contains(string(output), "Error: "+tc.wantError) {
|
|
t.Fatalf("write clipboard = %q, want error %q, err=%v", output, tc.wantError, err)
|
|
}
|
|
data, err := os.ReadFile(filepath.Join(dir, "test-pasteboard.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got struct{ Items []map[string]string }
|
|
if err := json.Unmarshal(data, &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.wantError != "" {
|
|
wantItems := []map[string]string{{"public.utf8-plain-text": "guest copy"}}
|
|
if !slices.EqualFunc(got.Items, wantItems, maps.Equal) {
|
|
t.Fatalf("guest clipboard items = %v, want %v", got.Items, wantItems)
|
|
}
|
|
return
|
|
}
|
|
if len(got.Items) != len(paths) {
|
|
t.Fatalf("guest clipboard items = %v, want %d file URLs", got.Items, len(paths))
|
|
}
|
|
for i, item := range got.Items {
|
|
fileURL, err := url.Parse(item["public.file-url"])
|
|
wantPath := "/" + strings.TrimPrefix(filepath.ToSlash(filepath.Join(dir, paths[i])), "/")
|
|
if err != nil || len(item) != 1 || fileURL.Scheme != "file" || fileURL.Host != "" || fileURL.Path != wantPath {
|
|
t.Fatalf("guest clipboard item %d = %v, want file URL for %q", i, item, wantPath)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|