178 lines
6.3 KiB
Go
178 lines
6.3 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"maps"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
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
|
|
mode string
|
|
want string
|
|
probe 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: "missing selection", want: "remount"},
|
|
{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)
|
|
}
|
|
}
|
|
for _, path := range paths[1:] {
|
|
if err := os.MkdirAll(filepath.Join(dir, path), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
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.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, paths) {
|
|
t.Fatalf("probe paths = %q, want %v, err=%v", probed, paths, err)
|
|
}
|
|
} else if !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("unexpected probe for %s, err=%v", tc.want, 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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|