201 lines
6.7 KiB
Go
201 lines
6.7 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/installguide"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
)
|
|
|
|
func TestBuiltinClipboardAndInputLifecycleZeroResidue(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
gtSrv := guesttools.NewTransportServer(nil)
|
|
if _, err := gtSrv.Start(-1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = gtSrv.Close(context.Background()) }()
|
|
|
|
cb := HostCallbacks{
|
|
GetConfig: func() config.Config { return config.Config{DataRoot: t.TempDir()} },
|
|
EnsureGuestTools: func() (*guesttools.Server, int, error) {
|
|
return gtSrv, gtSrv.Port(), nil
|
|
},
|
|
}
|
|
if err := RegisterAll(reg, cb, nil); err != nil {
|
|
t.Fatalf("RegisterAll: %v", err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
|
|
// Clipboard extension should be active and have mounted /v1/clipboard + watcher.
|
|
if !reg.IsActive(IDGuestTools) || !reg.IsActive(IDClipboard) {
|
|
t.Fatalf("expected guest-tools and clipboard active")
|
|
}
|
|
if !gtSrv.HasRoute("/v1/clipboard") || !gtSrv.IsClipboardActive() {
|
|
t.Fatal("expected clipboard route mounted while builtin.bridge.clipboard is active")
|
|
}
|
|
|
|
// Disable clipboard live -> route removed, watcher stopped, zero residue!
|
|
if err := reg.SetDesired(context.Background(), IDClipboard, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if gtSrv.HasRoute("/v1/clipboard") || gtSrv.IsClipboardActive() {
|
|
t.Fatal("expected zero clipboard route residue after disabling builtin.bridge.clipboard")
|
|
}
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for gtSrv.IsClipboardWatcherRunning() && time.Now().Before(deadline) {
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
if gtSrv.IsClipboardWatcherRunning() {
|
|
t.Fatal("expected clipboard watcher stopped after disable")
|
|
}
|
|
|
|
// Re-enable clipboard -> active again.
|
|
if err := reg.SetDesired(context.Background(), IDClipboard, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !gtSrv.HasRoute("/v1/clipboard") {
|
|
t.Fatal("expected clipboard route restored on re-enable")
|
|
}
|
|
|
|
// Disabling transport service cascades and disables clipboard cleanly.
|
|
if err := reg.SetDesired(context.Background(), IDGuestTools, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reg.IsActive(IDClipboard) || gtSrv.HasRoute("/v1/clipboard") {
|
|
t.Fatal("disabling guest-tools transport must cascade-unload clipboard")
|
|
}
|
|
|
|
// Input service disables cleanly with zero hook residue.
|
|
if raw, ok := reg.LookupService(ServiceHostInput); !ok || raw == nil {
|
|
t.Fatal("expected host.input service provided")
|
|
}
|
|
if err := reg.SetDesired(context.Background(), IDInput, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := reg.LookupService(ServiceHostInput); ok {
|
|
t.Fatal("expected host.input service removed after disable")
|
|
}
|
|
}
|
|
|
|
func TestInputServiceTypeCheck(t *testing.T) {
|
|
_ = host.NewInputService(nil)
|
|
}
|
|
|
|
func TestBuiltinInstallGuideLifecycleZeroResidue(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
syncCount := 0
|
|
cb := HostCallbacks{
|
|
GetConfig: func() config.Config { return config.Config{DataRoot: t.TempDir()} },
|
|
SyncInstallGuides: func() { syncCount++ },
|
|
}
|
|
if err := RegisterAll(reg, cb, nil); err != nil {
|
|
t.Fatalf("RegisterAll: %v", err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
|
|
if !reg.IsActive(IDInstallGuide) {
|
|
t.Fatal("expected builtin.install-guide active by default")
|
|
}
|
|
if syncCount != 1 {
|
|
t.Fatalf("SyncInstallGuides called %d times, want 1", syncCount)
|
|
}
|
|
raw, ok := reg.LookupService(ServiceInstallGuide)
|
|
if !ok {
|
|
t.Fatal("expected ui.install-guide service provided")
|
|
}
|
|
mgr, ok := raw.(*installguide.Manager)
|
|
if !ok || mgr == nil {
|
|
t.Fatalf("unexpected service type %T", raw)
|
|
}
|
|
|
|
// Disable live -> service removed and manager closed (rejects new starts).
|
|
if err := reg.SetDesired(context.Background(), IDInstallGuide, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if reg.IsActive(IDInstallGuide) {
|
|
t.Fatal("expected builtin.install-guide inactive after disable")
|
|
}
|
|
if _, ok := reg.LookupService(ServiceInstallGuide); ok {
|
|
t.Fatal("expected ui.install-guide service removed after disable")
|
|
}
|
|
if mgr.Start(context.Background(), installguide.Guide{Name: "mac", PID: 101}) {
|
|
t.Fatal("closed guide manager accepted a new Start after plugin disable")
|
|
}
|
|
|
|
// Re-enable live -> fresh manager provided and SyncInstallGuides triggered.
|
|
if err := reg.SetDesired(context.Background(), IDInstallGuide, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reg.IsActive(IDInstallGuide) || syncCount != 2 {
|
|
t.Fatalf("re-enable: active=%v syncCount=%d", reg.IsActive(IDInstallGuide), syncCount)
|
|
}
|
|
raw2, ok := reg.LookupService(ServiceInstallGuide)
|
|
if !ok || raw2 == mgr {
|
|
t.Fatal("expected a fresh ui.install-guide service instance after re-enable")
|
|
}
|
|
}
|
|
|
|
func TestGuestToolsInputBridgeEnvGating(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
gtSrv := guesttools.NewTransportServer(nil)
|
|
if _, err := gtSrv.Start(-1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = gtSrv.Close(context.Background()) }()
|
|
|
|
cb := HostCallbacks{
|
|
GetConfig: func() config.Config { return config.Config{DataRoot: t.TempDir()} },
|
|
EnsureGuestTools: func() (*guesttools.Server, int, error) {
|
|
return gtSrv, gtSrv.Port(), nil
|
|
},
|
|
}
|
|
if err := RegisterAll(reg, cb, nil); err != nil {
|
|
t.Fatalf("RegisterAll: %v", err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
|
|
vmDir := t.TempDir()
|
|
dispatch := func(caps []string) map[string]string {
|
|
intents, err := reg.Hooks().DispatchBeforeStart(context.Background(), plugin.BeforeStartPayload{
|
|
VMName: "mac",
|
|
VMDir: vmDir,
|
|
DriverID: IDDriverReimsMacOS,
|
|
RuntimeCaps: caps,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("DispatchBeforeStart: %v", err)
|
|
}
|
|
for _, in := range intents {
|
|
if in.PluginID == IDGuestTools {
|
|
return in.Env
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Runtime without safe win32-unicode-input capability (e.g. runtime-3731afd-p3)
|
|
// must NOT receive REIMS_GO_INPUT_URL / REIMS_GO_INPUT_TOKEN.
|
|
if env := dispatch(nil); env["REIMS_GO_INPUT_URL"] != "" || env["REIMS_GO_INPUT_TOKEN"] != "" {
|
|
t.Fatalf("without capability: unexpected input bridge env %v", env)
|
|
}
|
|
|
|
// Fixed runtime + active builtin.input plugin -> env vars injected.
|
|
env := dispatch([]string{"win32-unicode-input"})
|
|
if env["REIMS_GO_INPUT_URL"] == "" || env["REIMS_GO_INPUT_TOKEN"] == "" {
|
|
t.Fatalf("with capability and active input plugin: missing input bridge env %v", env)
|
|
}
|
|
|
|
// Disabling builtin.input stops injecting REIMS_GO_INPUT_URL / TOKEN even on fixed runtime.
|
|
if err := reg.SetDesired(context.Background(), IDInput, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if env := dispatch([]string{"win32-unicode-input"}); env["REIMS_GO_INPUT_URL"] != "" || env["REIMS_GO_INPUT_TOKEN"] != "" {
|
|
t.Fatalf("with input plugin disabled: unexpected input bridge env %v", env)
|
|
}
|
|
}
|