- Pure-Go Win32 clipboard + localhost HTTP bridge (127.0.0.1:19847) with per-VM 256-bit random Bearer token and jailed shared directory (<DataRoot>/shared/<vm>) - Pure-Go HFS+ REIMSTOOLS virtual USB disk with stock-macOS POSIX LaunchAgent installer (no Xcode or Python needed) for two-way UTF-8 clipboard and ~/Desktop/ReimsShare sync - VMPage Guest Tools card with live connection badge, Open shared folder, Attach/Eject tools disk, and one-click QMP install command - Update docs/roadmap.md with v0.3/v0.4 competitor-aligned follow-up tasks
184 lines
5.2 KiB
Go
184 lines
5.2 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/installer"
|
|
)
|
|
|
|
type memClipboard struct {
|
|
mu sync.Mutex
|
|
text string
|
|
}
|
|
|
|
func (m *memClipboard) ReadText() (string, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
return m.text, nil
|
|
}
|
|
|
|
func (m *memClipboard) WriteText(s string) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.text = s
|
|
return nil
|
|
}
|
|
|
|
func TestServerAuthClipboardAndSharedFiles(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
vmDir := filepath.Join(tmp, "vm")
|
|
sharedDir := filepath.Join(tmp, "shared")
|
|
clip := &memClipboard{text: "hello from windows"}
|
|
|
|
srv := NewServer(clip)
|
|
port, err := srv.Start(-1)
|
|
if err != nil {
|
|
t.Fatalf("Start: %v", err)
|
|
}
|
|
defer srv.Close(context.Background())
|
|
|
|
cfg, err := LoadOrCreateVMConfig(vmDir, "macos-tahoe", sharedDir, port)
|
|
if err != nil {
|
|
t.Fatalf("LoadOrCreateVMConfig: %v", err)
|
|
}
|
|
srv.RegisterVM(cfg)
|
|
base := fmt.Sprintf("http://127.0.0.1:%d", port)
|
|
|
|
// 1. Rejects missing or wrong Bearer token.
|
|
resp, err := http.Get(base + "/v1/ping")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
resp.Body.Close()
|
|
if resp.StatusCode != http.StatusUnauthorized {
|
|
t.Fatalf("unauthenticated ping: got %d, want 401", resp.StatusCode)
|
|
}
|
|
|
|
doReq := func(method, path string, body io.Reader) *http.Response {
|
|
req, err := http.NewRequest(method, base+path, body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+cfg.Token)
|
|
r, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// 2. Authenticated ping marks VM connected.
|
|
r := doReq("GET", "/v1/ping", nil)
|
|
r.Body.Close()
|
|
if r.StatusCode != http.StatusOK {
|
|
t.Fatalf("ping status=%d", r.StatusCode)
|
|
}
|
|
if !srv.IsConnected("macos-tahoe", 5*time.Second) {
|
|
t.Fatalf("expected VM to be marked connected after ping")
|
|
}
|
|
|
|
// 3. Clipboard GET -> 200, then GET with since=<hash> -> 304, then POST updates host clipboard.
|
|
r = doReq("GET", "/v1/clipboard", nil)
|
|
b, _ := io.ReadAll(r.Body)
|
|
h := r.Header.Get("X-Clip-Hash")
|
|
r.Body.Close()
|
|
if r.StatusCode != 200 || string(b) != "hello from windows" || h == "" {
|
|
t.Fatalf("clipboard GET: status=%d body=%q hash=%q", r.StatusCode, string(b), h)
|
|
}
|
|
r = doReq("GET", "/v1/clipboard?since="+h, nil)
|
|
r.Body.Close()
|
|
if r.StatusCode != http.StatusNotModified {
|
|
t.Fatalf("clipboard unchanged: status=%d, want 304", r.StatusCode)
|
|
}
|
|
r = doReq("POST", "/v1/clipboard", strings.NewReader("你好 macOS"))
|
|
r.Body.Close()
|
|
if r.StatusCode != 200 {
|
|
t.Fatalf("clipboard POST: status=%d", r.StatusCode)
|
|
}
|
|
if got, _ := clip.ReadText(); got != "你好 macOS" {
|
|
t.Fatalf("host clipboard = %q, want %q", got, "你好 macOS")
|
|
}
|
|
|
|
// 4. Shared files: reject path traversal, allow upload & download with UTF-8/spaces.
|
|
r = doReq("GET", "/v1/file?path="+url.QueryEscape("../secret.txt"), nil)
|
|
r.Body.Close()
|
|
if r.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("traversal must be 400, got %d", r.StatusCode)
|
|
}
|
|
r = doReq("PUT", "/v1/file?path="+url.QueryEscape("notes/测试 file.txt")+"&mtime=1700000100", bytes.NewBufferString("shared payload"))
|
|
r.Body.Close()
|
|
if r.StatusCode != http.StatusOK {
|
|
t.Fatalf("file PUT status=%d", r.StatusCode)
|
|
}
|
|
r = doReq("GET", "/v1/files", nil)
|
|
listBytes, _ := io.ReadAll(r.Body)
|
|
r.Body.Close()
|
|
if !strings.Contains(string(listBytes), "1700000100\t14\tnotes/测试 file.txt") {
|
|
t.Fatalf("unexpected /v1/files listing:\n%s", string(listBytes))
|
|
}
|
|
r = doReq("GET", "/v1/file?path="+url.QueryEscape("notes/测试 file.txt"), nil)
|
|
dlBytes, _ := io.ReadAll(r.Body)
|
|
r.Body.Close()
|
|
if r.StatusCode != 200 || string(dlBytes) != "shared payload" {
|
|
t.Fatalf("file GET: status=%d body=%q", r.StatusCode, string(dlBytes))
|
|
}
|
|
}
|
|
|
|
func TestBuildToolsDiskImageAndScriptSyntax(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
imgPath := filepath.Join(tmp, "reims-tools.img")
|
|
cfg := VMConfig{VMName: "macos-tahoe", Token: strings.Repeat("ab", 32), Port: 19847}
|
|
if err := BuildToolsDiskImage(imgPath, cfg); err != nil {
|
|
t.Fatalf("BuildToolsDiskImage: %v", err)
|
|
}
|
|
f, err := os.Open(imgPath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
for _, name := range []string{"config", "guest-tools.sh", "Install-Reims-Tools.command", "Uninstall-Reims-Tools.command"} {
|
|
if _, sz, err := installer.FindFileInHFSPlusImage(f, name); err != nil || sz == 0 {
|
|
t.Fatalf("missing %s in REIMSTOOLS image: err=%v size=%d", name, err, sz)
|
|
}
|
|
}
|
|
|
|
// Syntax check every generated POSIX script if sh is available.
|
|
shBin, err := exec.LookPath("sh")
|
|
if err != nil {
|
|
for _, cand := range []string{`C:\Program Files\Git\bin\sh.exe`, `D:\DevTools\msys64\usr\bin\sh.exe`} {
|
|
if _, serr := os.Stat(cand); serr == nil {
|
|
shBin = cand
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if shBin == "" {
|
|
return
|
|
}
|
|
for name, content := range map[string][]byte{
|
|
"guest-tools.sh": GuestAgentScript(),
|
|
"Install-Reims-Tools.command": GuestInstallScript(),
|
|
"Uninstall-Reims-Tools.command": GuestUninstallScript(),
|
|
} {
|
|
p := filepath.Join(tmp, name)
|
|
if err := os.WriteFile(p, content, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out, err := exec.Command(shBin, "-n", p).CombinedOutput(); err != nil {
|
|
t.Fatalf("sh -n %s failed: %v\n%s", name, err, out)
|
|
}
|
|
}
|
|
}
|