159 lines
6.0 KiB
Go
159 lines
6.0 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileSyncRequiresConfirmedRevisionAndContent(t *testing.T) {
|
|
root := t.TempDir()
|
|
shared := filepath.Join(root, "share")
|
|
srv := NewTransportServer(&memClipboard{})
|
|
port, err := srv.Start(-1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = srv.Close(context.Background()) })
|
|
cfg, err := LoadOrCreateVMConfig(root, "sync-test", shared, port)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv.RegisterVM(cfg)
|
|
|
|
request := func(method, path, body string, headers http.Header, want int) (http.Header, string) {
|
|
t.Helper()
|
|
req, err := http.NewRequest(method, fmt.Sprintf("http://127.0.0.1:%d%s", port, path), strings.NewReader(body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if headers != nil {
|
|
req.Header = headers.Clone()
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+cfg.Token)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil || resp.StatusCode != want {
|
|
t.Fatalf("%s %s: status=%d, want=%d, body=%q, read error=%v", method, path, resp.StatusCode, want, b, err)
|
|
}
|
|
return resp.Header, string(b)
|
|
}
|
|
|
|
const fileURL = "/v1/file?path=entry.txt"
|
|
const contents = "original"
|
|
digest := fmt.Sprintf("%x", sha256.Sum256([]byte(contents)))
|
|
list, _ := request(http.MethodGet, "/v1/files?sync=1", "", nil, http.StatusOK)
|
|
scope := list.Get(syncScopeHeader)
|
|
if scope == "" {
|
|
t.Fatal("missing sync scope")
|
|
}
|
|
conditions := make(http.Header)
|
|
conditions.Set(syncScopeHeader, scope)
|
|
conditions.Set("If-None-Match", "*")
|
|
created, _ := request(http.MethodPut, fileURL+"&mtime=1700000000", contents, conditions, http.StatusOK)
|
|
tag := created.Get("ETag")
|
|
if tag == "" || created.Get(syncDigestHeader) != digest {
|
|
t.Fatalf("upload revision or digest missing: %v", created)
|
|
}
|
|
_, legacyList := request(http.MethodGet, "/v1/files", "", nil, http.StatusOK)
|
|
_, versionedList := request(http.MethodGet, "/v1/files?sync=1", "", nil, http.StatusOK)
|
|
if legacyList != "1700000000\t8\tentry.txt\n" ||
|
|
versionedList != "1700000000\t8\t"+tag+"\tentry.txt\n" {
|
|
t.Fatalf("unexpected file listings: legacy=%q, versioned=%q", legacyList, versionedList)
|
|
}
|
|
|
|
request(http.MethodPut, fileURL, "overwritten", conditions, http.StatusPreconditionFailed)
|
|
conditions.Del("If-None-Match")
|
|
conditions.Set("If-Match", tag)
|
|
probed, _ := request(http.MethodHead, fileURL+"&sha256=1", "", conditions, http.StatusOK)
|
|
if probed.Get(syncDigestHeader) != digest {
|
|
t.Fatalf("unexpected digest: %v", probed)
|
|
}
|
|
request(http.MethodDelete, fileURL, "", conditions, http.StatusPreconditionRequired)
|
|
conditions.Set(syncDigestHeader, digest)
|
|
conditions.Set("If-Match", "\"stale-revision\"")
|
|
request(http.MethodDelete, fileURL, "", conditions, http.StatusPreconditionFailed)
|
|
conditions.Set("If-Match", tag)
|
|
request(http.MethodPut, fileURL+"&mtime=1700000000", "modified", nil, http.StatusOK)
|
|
request(http.MethodDelete, fileURL, "", conditions, http.StatusPreconditionFailed)
|
|
_, got := request(http.MethodGet, fileURL, "", nil, http.StatusOK)
|
|
if got != "modified" {
|
|
t.Fatalf("modified copy was lost: %q", got)
|
|
}
|
|
conditions.Set(syncDigestHeader, fmt.Sprintf("%x", sha256.Sum256([]byte(got))))
|
|
request(http.MethodDelete, fileURL, "", conditions, http.StatusNoContent)
|
|
request(http.MethodHead, fileURL, "", conditions, http.StatusNotFound)
|
|
if err := os.Rename(shared, filepath.Join(root, "old-share")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request(http.MethodGet, "/v1/files?sync=1", "", conditions, http.StatusServiceUnavailable)
|
|
request(http.MethodHead, fileURL, "", conditions, http.StatusServiceUnavailable)
|
|
srv.RegisterVM(cfg)
|
|
list, _ = request(http.MethodGet, "/v1/files?sync=1", "", nil, http.StatusOK)
|
|
newScope := list.Get(syncScopeHeader)
|
|
if newScope == "" || newScope == scope {
|
|
t.Fatalf("recreated share must have a new scope: %q", newScope)
|
|
}
|
|
request(http.MethodHead, fileURL, "", conditions, http.StatusPreconditionFailed)
|
|
request(http.MethodPut, fileURL, "stale upload", conditions, http.StatusPreconditionFailed)
|
|
conditions = http.Header{syncScopeHeader: {newScope}}
|
|
request(http.MethodHead, fileURL, "", conditions, http.StatusNotFound)
|
|
request(http.MethodPut, "/v1/file?path=nested/file.txt", contents, conditions, http.StatusOK)
|
|
_, got = request(http.MethodGet, "/v1/file?path=nested/file.txt", "", conditions, http.StatusOK)
|
|
if got != contents {
|
|
t.Fatalf("nested file contents: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestFileSyncConfinesUploadToShare(t *testing.T) {
|
|
shared, outside := t.TempDir(), t.TempDir()
|
|
link := filepath.Join(shared, "linked")
|
|
if err := os.Symlink(outside, link); err != nil {
|
|
if runtime.GOOS != "windows" {
|
|
t.Skipf("directory symlink unavailable: %v", err)
|
|
}
|
|
output, err := exec.Command("cmd.exe", "/c", "mklink", "/J", link, outside).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("create test junction: %v: %s", err, output)
|
|
}
|
|
}
|
|
srv := NewTransportServer(&memClipboard{})
|
|
token := strings.Repeat("a", 64)
|
|
srv.RegisterVM(VMConfig{VMName: "sync-test", Token: token, SharedDir: shared})
|
|
list := httptest.NewRequest(http.MethodGet, "/v1/files?sync=1", nil)
|
|
list.Header.Set("Authorization", "Bearer "+token)
|
|
listed := httptest.NewRecorder()
|
|
srv.handleListFiles(listed, list)
|
|
scope := listed.Header().Get(syncScopeHeader)
|
|
if listed.Code != http.StatusOK || scope == "" {
|
|
t.Fatalf("listing failed: %d: %s", listed.Code, listed.Body)
|
|
}
|
|
for _, version := range []string{"", scope} {
|
|
req := httptest.NewRequest(http.MethodPut, "/v1/file?path=linked/upload.txt", strings.NewReader("test upload"))
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Set(syncScopeHeader, version)
|
|
res := httptest.NewRecorder()
|
|
srv.handleFile(res, req)
|
|
if res.Code < http.StatusBadRequest {
|
|
t.Fatalf("upload through an out-of-share link succeeded: %d", res.Code)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(outside, "upload.txt")); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("upload escaped share: %v", err)
|
|
}
|
|
}
|
|
}
|