* 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>
145 lines
4.5 KiB
Go
145 lines
4.5 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"errors"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"golang.org/x/net/webdav"
|
|
)
|
|
|
|
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()
|
|
f.serveDropFile(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)
|
|
}
|
|
}
|