279 lines
9.2 KiB
Go
279 lines
9.2 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/net/webdav"
|
|
)
|
|
|
|
func TestClipboardSharePolicyWithdrawal(t *testing.T) {
|
|
const name = "clipboard-test"
|
|
enabled := DefaultClipboardPolicy()
|
|
disabled := enabled
|
|
disabled.Enabled = false
|
|
inbound := enabled
|
|
inbound.Direction = ClipboardGuestToHost
|
|
for _, tc := range []struct {
|
|
name string
|
|
withdraw func(*Server)
|
|
otherActive bool
|
|
}{
|
|
{"global disable", func(s *Server) { s.SetGlobalClipboardPolicy(disabled) }, true},
|
|
{"VM guest to host", func(s *Server) { s.SetVMClipboardPolicy(name, &inbound) }, true},
|
|
{"clear VM override", func(s *Server) {
|
|
s.SetVMClipboardPolicy(name, &enabled)
|
|
s.SetGlobalClipboardPolicy(inbound)
|
|
s.SetVMClipboardPolicy(name, nil)
|
|
}, true},
|
|
{"extension deactivated", func(s *Server) { s.SetClipboardActive(false) }, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
host := filepath.Join(t.TempDir(), "shared.txt")
|
|
if err := os.WriteFile(host, []byte("content"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
clip := &memClipboard{}
|
|
clip.WriteFiles([]string{host})
|
|
srv := newClipboardTestServer(clip)
|
|
srv.RegisterVM(VMConfig{VMName: "other", Token: "other-token"})
|
|
srv.SetVMClipboardPolicy("other", &enabled)
|
|
entries := []DropShareEntry{{Name: "shared.txt", HostPath: host}}
|
|
if !srv.RegisterDropShare(name, "drag", entries) || !srv.ensureClipboardShare("other", "other-clip", entries) {
|
|
t.Fatal("registering control shares failed")
|
|
}
|
|
resp := clipboardRequest(t, srv, http.MethodGet, "?accept=files", "", "", nil)
|
|
var manifest struct {
|
|
DropID string `json:"drop_id"`
|
|
}
|
|
if err := json.Unmarshal(resp.Body.Bytes(), &manifest); err != nil || resp.Code != http.StatusOK || manifest.DropID == "" {
|
|
t.Fatalf("clipboard manifest: %d %q, %v", resp.Code, resp.Body, err)
|
|
}
|
|
|
|
request := func(vm, method, path string, handler http.HandlerFunc) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, path, nil)
|
|
r.SetBasicAuth(vm, vm+"-token")
|
|
r.Header.Set("Depth", "1")
|
|
w := httptest.NewRecorder()
|
|
handler(w, r)
|
|
return w
|
|
}
|
|
checkShare := func(vm, id string, visible bool) {
|
|
t.Helper()
|
|
prefix := "/dav-drop/" + vm + "/"
|
|
list := request(vm, "PROPFIND", prefix, srv.handleDropWebDAV)
|
|
if list.Code != http.StatusMultiStatus || strings.Contains(list.Body.String(), prefix+id+"/") != visible {
|
|
t.Fatalf("root listing, want %s visible=%t: %d %s", id, visible, list.Code, list.Body)
|
|
}
|
|
for _, op := range []struct {
|
|
method, path string
|
|
handler http.HandlerFunc
|
|
status int
|
|
}{
|
|
{"PROPFIND", prefix + id + "/", srv.handleDropWebDAV, http.StatusMultiStatus},
|
|
{"GET", prefix + id + "/shared.txt", srv.handleDropWebDAV, http.StatusOK},
|
|
{"GET", "/v1/drop-file?drop_id=" + id + "&path=shared.txt", srv.handleDropFile, http.StatusOK},
|
|
} {
|
|
w := request(vm, op.method, op.path, op.handler)
|
|
wantStatus := op.status
|
|
if !visible {
|
|
wantStatus = http.StatusNotFound
|
|
}
|
|
if w.Code != wantStatus {
|
|
t.Fatalf("%s %s: %d %s, want %d", op.method, op.path, w.Code, w.Body, wantStatus)
|
|
}
|
|
if visible && op.method == "GET" && w.Body.String() != "content" {
|
|
t.Fatalf("GET %s: unexpected content %q", op.path, w.Body)
|
|
}
|
|
}
|
|
}
|
|
checkShare(name, manifest.DropID, true)
|
|
tc.withdraw(srv)
|
|
checkShare(name, manifest.DropID, false)
|
|
checkShare(name, "drag", true)
|
|
checkShare("other", "other-clip", tc.otherActive)
|
|
store := srv.vms[vmRegistryKey("", name)].dropShares
|
|
if store.touch(manifest.DropID, true) != nil {
|
|
t.Fatal("withdrawn share still registered")
|
|
}
|
|
if srv.ensureClipboardShare(name, manifest.DropID, entries) {
|
|
t.Fatal("in-flight clipboard poll recreated a withdrawn share")
|
|
}
|
|
|
|
// Exercise serving policy independently of revocation by injecting a stale record.
|
|
if !store.ensure("stale", entries) {
|
|
t.Fatal("creating stale record failed")
|
|
}
|
|
checkShare(name, "stale", false)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDropShareEnsureConcurrentFirstPoll(t *testing.T) {
|
|
store := newDropShareStore()
|
|
start := time.Unix(1700000000, 0)
|
|
store.modTime = start.Add(-time.Second)
|
|
now := start
|
|
store.now = func() time.Time {
|
|
value := now
|
|
now = now.Add(time.Nanosecond)
|
|
return value
|
|
}
|
|
entries := []DropShareEntry{{Name: "file", HostPath: filepath.Join(t.TempDir(), "file")}}
|
|
var wg sync.WaitGroup
|
|
ready := make(chan struct{})
|
|
for range 64 {
|
|
wg.Go(func() {
|
|
<-ready
|
|
if !store.ensure("clip", entries) {
|
|
t.Error("ensure failed")
|
|
}
|
|
})
|
|
}
|
|
close(ready)
|
|
wg.Wait()
|
|
rec := store.records["clip"]
|
|
if rec == nil || !rec.createdAt.Equal(start) || !store.modTime.Equal(start) {
|
|
t.Fatalf("first poll should register exactly once: record %+v, root modtime %v", rec, store.modTime)
|
|
}
|
|
}
|
|
|
|
func TestDropWebDAV(t *testing.T) {
|
|
f := &dropWebDAVFS{store: newDropShareStore()}
|
|
now := time.Unix(1700000000, 0)
|
|
start := now
|
|
f.store.now = func() time.Time { return now }
|
|
dir := t.TempDir()
|
|
host := filepath.Join(dir, "host.txt")
|
|
if err := os.WriteFile(host, []byte("host content"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entries := []DropShareEntry{{"alias.txt", host, false}, {"folder", dir, true}}
|
|
rootTime := func() time.Time {
|
|
t.Helper()
|
|
info, err := f.Stat(t.Context(), "/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return info.ModTime().Truncate(time.Second)
|
|
}
|
|
last := rootTime()
|
|
checkAdvance := func() {
|
|
t.Helper()
|
|
next := rootTime()
|
|
if !next.After(last) {
|
|
t.Fatalf("root modtime %v did not advance past %v", next, last)
|
|
}
|
|
last = next
|
|
}
|
|
if !f.store.register("drop", entries) {
|
|
t.Fatal("registration failed")
|
|
}
|
|
checkAdvance()
|
|
root, err := f.OpenFile(t.Context(), ".", os.O_RDONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer root.Close()
|
|
children, err := root.Readdir(-1)
|
|
if err != nil || len(children) != 1 {
|
|
t.Fatalf("root children %v, err %v", children, err)
|
|
}
|
|
child, err := f.OpenFile(t.Context(), "/drop", os.O_RDONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer child.Close()
|
|
info, err := child.Stat()
|
|
if err != nil || children[0].Name() != "drop" || !children[0].ModTime().Equal(info.ModTime()) {
|
|
t.Fatalf("listed child %v, opened child %v, err %v", children[0], info, err)
|
|
}
|
|
const prefix = "/dav-drop/mac"
|
|
h := &webdav.Handler{Prefix: prefix, FileSystem: f, LockSystem: webdav.NewMemLS()}
|
|
request := func(method, name string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, prefix+name, nil)
|
|
r.Header.Set("Depth", "1")
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
direct := func(id, rel string) *httptest.ResponseRecorder {
|
|
w := httptest.NewRecorder()
|
|
serveDropFile(f, w, httptest.NewRequest("GET", "/", nil), id, rel)
|
|
return w
|
|
}
|
|
for name, child := range map[string]string{"/": "/drop", "/drop": "/alias.txt", "/drop/folder": "/host.txt"} {
|
|
if w := request("OPTIONS", name); w.Code != 200 || w.Header().Get("DAV") == "" {
|
|
t.Errorf("OPTIONS %s: %d %v", name, w.Code, w.Header())
|
|
}
|
|
if w := request("PROPFIND", name); w.Code != 207 || !strings.Contains(w.Body.String(), prefix+strings.TrimSuffix(name, "/")+child) {
|
|
t.Errorf("PROPFIND %s: %d %s", name, w.Code, w.Body)
|
|
}
|
|
}
|
|
for _, rel := range []string{"alias.txt", "folder/host.txt"} {
|
|
for _, w := range []*httptest.ResponseRecorder{request("GET", "/drop/"+rel), direct("drop", rel)} {
|
|
if w.Code != 200 || w.Body.String() != "host content" {
|
|
t.Errorf("GET %s: %d %s", rel, w.Code, w.Body)
|
|
}
|
|
}
|
|
}
|
|
for method, name := range map[string]string{"PUT": "/drop/alias.txt", "DELETE": "/drop/alias.txt", "MKCOL": "/drop/folder/new"} {
|
|
if w := request(method, name); w.Code < 400 {
|
|
t.Errorf("%s succeeded: %d", method, w.Code)
|
|
}
|
|
}
|
|
if w := direct("unknown", "alias.txt"); w.Code != 404 {
|
|
t.Errorf("unknown drop: %d", w.Code)
|
|
}
|
|
for _, rel := range []string{"folder/../alias.txt", "/alias.txt", ".", "", "folder"} {
|
|
if w := direct("drop", rel); w.Code != 400 {
|
|
t.Errorf("direct path %q: %d", rel, w.Code)
|
|
}
|
|
}
|
|
if _, err := f.Stat(t.Context(), "/drop/folder/../alias.txt"); !errors.Is(err, os.ErrInvalid) {
|
|
t.Errorf("traversal: %v", err)
|
|
}
|
|
t.Run("symlink", func(t *testing.T) {
|
|
link := filepath.Join(dir, "link")
|
|
if err := os.Symlink(dir, link); err != nil {
|
|
t.Skipf("cannot create symlink: %v", err)
|
|
}
|
|
if w := request("PROPFIND", "/drop/folder"); w.Code != 207 || strings.Contains(w.Body.String(), "/folder/link") {
|
|
t.Errorf("symlink listing: %d %s", w.Code, w.Body)
|
|
}
|
|
for _, rel := range []string{"folder/link", "folder/link/host.txt"} {
|
|
if w := direct("drop", rel); w.Code != 403 {
|
|
t.Errorf("symlink %s: %d", rel, w.Code)
|
|
}
|
|
}
|
|
})
|
|
for elapsed := DropWebDAVTTL - time.Minute; elapsed < DropWebDAVMaxLifetime; elapsed += DropWebDAVTTL - time.Minute {
|
|
now = start.Add(elapsed)
|
|
if w := direct("drop", "alias.txt"); w.Code != 200 {
|
|
t.Fatalf("read at %v: %d", elapsed, w.Code)
|
|
}
|
|
}
|
|
now = start.Add(DropWebDAVMaxLifetime)
|
|
if w := direct("drop", "alias.txt"); w.Code != 404 {
|
|
t.Errorf("maximum lifetime: %d", w.Code)
|
|
}
|
|
checkAdvance()
|
|
if !f.store.register("drop", entries) {
|
|
t.Fatal("re-registration failed")
|
|
}
|
|
checkAdvance()
|
|
f.store.revoke("drop")
|
|
checkAdvance()
|
|
if w := direct("drop", "alias.txt"); w.Code != 404 {
|
|
t.Errorf("revoked drop: %d", w.Code)
|
|
}
|
|
}
|