217 lines
6.9 KiB
Go
217 lines
6.9 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
)
|
|
|
|
// Switching the Guest Tools plugin off must stop the bridge for real: feature
|
|
// calls get an error instead of quietly starting a new server.
|
|
func TestGuestToolsBridgeFollowsPluginSwitch(t *testing.T) {
|
|
dataRoot := t.TempDir()
|
|
mgr, err := plugin.NewManager(plugin.ManagerOptions{DataRoot: dataRoot, HostVersion: "0.0.0"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer mgr.Close()
|
|
cfg := config.Default()
|
|
cfg.DataRoot = dataRoot
|
|
cfg.Locale = i18n.EnUS
|
|
a := &App{cfg: cfg, pluginMgr: mgr}
|
|
|
|
err = mgr.Registry().Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{Schema: 1, ID: builtin.IDGuestTools, Version: "0.0.0"},
|
|
Source: plugin.SourceBuiltin,
|
|
Trust: plugin.TrustOfficial,
|
|
Desired: false,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
srv := guesttools.NewTransportServer(nil)
|
|
if _, err := srv.Start(-1); err != nil {
|
|
return err
|
|
}
|
|
sc.Provide(builtin.ServiceGuestToolsTransport, "1.0.0", srv)
|
|
sc.OnDispose(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
_ = srv.Close(ctx)
|
|
})
|
|
return nil
|
|
},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mgr.Registry().Reconcile(context.Background())
|
|
|
|
if _, _, err := a.ensureGuestToolsBridge(); err == nil || !strings.Contains(err.Error(), "turned off") {
|
|
t.Fatalf("plugin off: want a turned-off error, got %v", err)
|
|
}
|
|
if a.guestTools != nil {
|
|
t.Fatal("plugin off: a feature call started the Guest Tools server")
|
|
}
|
|
|
|
if err := mgr.Registry().SetDesired(context.Background(), builtin.IDGuestTools, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv, port, err := a.ensureGuestToolsBridge()
|
|
if err != nil || srv == nil || port <= 0 {
|
|
t.Fatalf("plugin on: want the plugin's server, got srv=%v port=%d err=%v", srv, port, err)
|
|
}
|
|
|
|
if err := mgr.Registry().SetDesired(context.Background(), builtin.IDGuestTools, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if a.guestToolsEnabled() {
|
|
t.Fatal("plugin switched back off: Guest Tools still reported as available")
|
|
}
|
|
}
|
|
|
|
func TestClipboardSettingsApplyGloballyFromPluginConfig(t *testing.T) {
|
|
srv := guesttools.NewTransportServer(nil)
|
|
cfg := config.Default()
|
|
cfg.DataRoot = t.TempDir()
|
|
a := &App{cfg: cfg, guestTools: srv}
|
|
mgr, err := plugin.NewManager(plugin.ManagerOptions{
|
|
DataRoot: cfg.DataRoot,
|
|
HostVersion: "0.0.0",
|
|
OnSettingsChanged: a.onPluginSettingsChanged,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer mgr.Close()
|
|
a.pluginMgr = mgr
|
|
|
|
err = builtin.RegisterAll(mgr.Registry(), builtin.HostCallbacks{
|
|
EnsureGuestTools: func() (*guesttools.Server, int, error) { return srv, 1, nil },
|
|
}, func(id string, _ bool) bool { return id == builtin.IDGuestTools || id == builtin.IDClipboard })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mgr.Registry().Reconcile(context.Background())
|
|
if !mgr.Registry().IsActive(builtin.IDClipboard) {
|
|
t.Fatal("clipboard plugin did not activate")
|
|
}
|
|
|
|
info, ok := mgr.Registry().Info(builtin.IDClipboard)
|
|
if !ok || info.Settings == nil {
|
|
t.Fatal("clipboard plugin missing settings schema")
|
|
}
|
|
if info.Settings.Scope != "app" {
|
|
t.Fatalf("clipboard schema scope = %q, want app", info.Settings.Scope)
|
|
}
|
|
for key, prop := range info.Settings.Properties {
|
|
if prop.Scope != "app" {
|
|
t.Fatalf("clipboard setting %q scope = %q, want app", key, prop.Scope)
|
|
}
|
|
}
|
|
|
|
vm := driver.NewVMRef("", "mac").Key()
|
|
peer := driver.NewVMRef("", "peer").Key()
|
|
set := func(vmKey string, vals map[string]any) {
|
|
t.Helper()
|
|
if err := a.SetPluginSettings(builtin.IDClipboard, vmKey, vals); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Stale per-VM entries saved before moving clipboard settings to plugin scope
|
|
// must not override the plugin-level configuration.
|
|
set(vm, map[string]any{"enabled": false, "direction": "guest-to-host", "max_file_mb": 256})
|
|
set("", map[string]any{
|
|
"enabled": true,
|
|
"direction": "host-to-guest",
|
|
"sync_images": false,
|
|
"max_image_mb": 8,
|
|
"sync_files": false,
|
|
"max_file_mb": 64,
|
|
})
|
|
for _, target := range []string{vm, peer} {
|
|
policy := srv.EffectiveClipboardPolicy(target)
|
|
if !policy.Enabled || policy.Direction != guesttools.ClipboardHostToGuest ||
|
|
policy.SyncImages || policy.MaxImageBytes != 8<<20 ||
|
|
policy.SyncFiles || policy.MaxFileBytes != 64<<20 {
|
|
t.Fatalf("VM %q policy did not match plugin config: %+v", target, policy)
|
|
}
|
|
}
|
|
|
|
set("", map[string]any{"enabled": false, "sync_files": true, "max_file_mb": 32})
|
|
if policy := srv.EffectiveClipboardPolicy(vm); policy.Enabled || !policy.SyncFiles || policy.MaxFileBytes != 32<<20 {
|
|
t.Fatalf("updated plugin config did not apply immediately: %+v", policy)
|
|
}
|
|
}
|
|
|
|
func TestClipboardStagingFollowsWritableDataRoot(t *testing.T) {
|
|
cfg := config.Default()
|
|
cfg.DataRoot = t.TempDir()
|
|
srv := guesttools.NewServer(&guesttools.OSClipboard{})
|
|
a := &App{cfg: cfg, needsSetup: true, guestTools: srv}
|
|
defer func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
_ = srv.Close(ctx)
|
|
}()
|
|
_, port, _, err := a.startGuestToolsServer()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const token = "clipboard-setup-transition-token"
|
|
srv.RegisterVM(guesttools.VMConfig{VMName: "mac", Token: token})
|
|
client := &http.Client{Timeout: time.Second}
|
|
postInvalidArchive := func(want int) {
|
|
t.Helper()
|
|
// Invalid archives reach staging validation without mutating the host clipboard.
|
|
address := fmt.Sprintf("http://127.0.0.1:%d/v1/clipboard", port)
|
|
req, err := http.NewRequest(http.MethodPost, address, strings.NewReader("invalid archive"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
req.Header.Set("Content-Type", "application/x-tar")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != want {
|
|
t.Fatalf("POST clipboard archive returned %d, want %d", resp.StatusCode, want)
|
|
}
|
|
}
|
|
updateRoot := func(dir string, readOnly bool) {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.cfg.DataRoot = dir
|
|
a.needsSetup = false
|
|
a.root.ReadOnly = readOnly
|
|
a.syncClipboardStagingLocked()
|
|
}
|
|
|
|
postInvalidArchive(http.StatusServiceUnavailable)
|
|
if entries := dirEntries(t, cfg.DataRoot); len(entries) != 0 {
|
|
t.Fatalf("staging used suggested data folder: %v", entries)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
dir := t.TempDir()
|
|
updateRoot(dir, false)
|
|
postInvalidArchive(http.StatusBadRequest)
|
|
if info, err := os.Stat(filepath.Join(dir, "tmp", "clip-files")); err != nil || !info.IsDir() {
|
|
t.Fatalf("writable root %q was not used for staging: %v", dir, err)
|
|
}
|
|
}
|
|
updateRoot(cfg.DataRoot, true)
|
|
postInvalidArchive(http.StatusServiceUnavailable)
|
|
}
|