package driver import ( "strings" "testing" "time" ) func TestValidateEndpoint(t *testing.T) { good := []VMEndpoint{ {Kind: EndpointSSH, Host: "127.0.0.1", Port: 22022, User: "dev", Ready: true}, {Kind: EndpointSSH, Port: 2222}, {Kind: EndpointWSL, Distro: "dev-ubuntu"}, {Kind: EndpointRDP, Host: "172.20.0.5"}, {Kind: EndpointRDP, Host: "win11.local", Port: 3390}, {Kind: EndpointRDP, Host: "fe80::1"}, {Kind: EndpointVMConnect, VM: "win11-dev"}, {Kind: EndpointVMConnect, VM: "Windows 11 (dev)"}, {Kind: EndpointADB, Serial: "emulator-5554"}, {Kind: EndpointADB, Serial: "192.168.1.5:5555"}, } for _, ep := range good { if err := ValidateEndpoint(ep); err != nil { t.Errorf("ValidateEndpoint(%+v) = %v", ep, err) } } bad := map[string]VMEndpoint{ "unknown kind": {Kind: "telnet", Host: "127.0.0.1", Port: 23}, "empty kind": {}, "ssh port zero": {Kind: EndpointSSH}, "ssh port high": {Kind: EndpointSSH, Port: 70000}, "ssh host flag": {Kind: EndpointSSH, Host: "-oProxyCommand=calc", Port: 22}, "ssh host space": {Kind: EndpointSSH, Host: "a b", Port: 22}, "ssh user injection": {Kind: EndpointSSH, Port: 22, User: "dev; calc"}, "ssh user flag": {Kind: EndpointSSH, Port: 22, User: "-oProxyCommand"}, "rdp host zone": {Kind: EndpointRDP, Host: "fe80::1%x & calc"}, "wsl distro flag": {Kind: EndpointWSL, Distro: "-u"}, "wsl distro space": {Kind: EndpointWSL, Distro: "a b"}, "wsl distro empty": {Kind: EndpointWSL}, "rdp host empty": {Kind: EndpointRDP}, "rdp host injection": {Kind: EndpointRDP, Host: "h /admin"}, "rdp port negative": {Kind: EndpointRDP, Host: "h", Port: -1}, "vmconnect empty": {Kind: EndpointVMConnect}, "vmconnect switch": {Kind: EndpointVMConnect, VM: "/edit"}, "vmconnect dash": {Kind: EndpointVMConnect, VM: "-x"}, "vmconnect quote": {Kind: EndpointVMConnect, VM: `a"b`}, "vmconnect ampersand": {Kind: EndpointVMConnect, VM: "a&calc"}, "vmconnect percent": {Kind: EndpointVMConnect, VM: "%PATH%"}, "vmconnect control": {Kind: EndpointVMConnect, VM: "a\nb"}, "vmconnect padded": {Kind: EndpointVMConnect, VM: " a"}, "vmconnect too long": {Kind: EndpointVMConnect, VM: strings.Repeat("a", 129)}, "adb serial flag": {Kind: EndpointADB, Serial: "-s"}, "adb serial injection": {Kind: EndpointADB, Serial: "x;calc"}, "adb serial empty": {Kind: EndpointADB}, } for name, ep := range bad { if err := ValidateEndpoint(ep); err == nil { t.Errorf("%s: ValidateEndpoint(%+v) accepted", name, ep) } } if err := ValidateEndpoints([]VMEndpoint{good[0], bad["unknown kind"]}); err == nil { t.Error("ValidateEndpoints accepted a list with a bad endpoint") } } func TestSyncSSHEndpoint(t *testing.T) { vm := VMInfo{State: "running", SSHPort: 2222} SyncSSHEndpoint(&vm) if len(vm.Endpoints) != 1 || vm.Endpoints[0].Kind != EndpointSSH || vm.Endpoints[0].Port != 2222 || !vm.Endpoints[0].Ready || vm.Endpoints[0].Host != "127.0.0.1" { t.Fatalf("SSH port did not produce an ssh endpoint: %+v", vm.Endpoints) } SyncSSHEndpoint(&vm) if len(vm.Endpoints) != 1 { t.Fatalf("SyncSSHEndpoint is not idempotent: %+v", vm.Endpoints) } stopped := VMInfo{State: "stopped", SSHPort: 2222} SyncSSHEndpoint(&stopped) if stopped.Endpoints[0].Ready { t.Fatal("stopped VM reports a ready ssh endpoint") } fromEndpoint := VMInfo{Endpoints: []VMEndpoint{{Kind: EndpointSSH, Port: 22022, Ready: true}}} SyncSSHEndpoint(&fromEndpoint) if fromEndpoint.SSHPort != 22022 { t.Fatalf("ssh endpoint did not fill SSHPort: %d", fromEndpoint.SSHPort) } keep := VMInfo{SSHPort: 2200, Endpoints: []VMEndpoint{{Kind: EndpointSSH, Port: 22022}}} SyncSSHEndpoint(&keep) if keep.SSHPort != 2200 { t.Fatalf("an existing SSHPort was overwritten: %d", keep.SSHPort) } } func TestPrimaryHostedEndpointPriority(t *testing.T) { all := []VMEndpoint{ {Kind: EndpointADB, Serial: "e", Ready: true}, {Kind: EndpointSSH, Port: 22, Ready: true}, {Kind: EndpointWSL, Distro: "d", Ready: true}, {Kind: EndpointRDP, Host: "h", Ready: true}, {Kind: EndpointVMConnect, VM: "v", Ready: true}, } want := []string{EndpointVMConnect, EndpointRDP, EndpointWSL, EndpointSSH, EndpointADB} for i := 0; i < len(want); i++ { got, ok := PrimaryHostedEndpoint(all) if !ok || got.Kind != want[i] { t.Fatalf("step %d: primary = %+v (%v), want %s", i, got, ok, want[i]) } // Drop the winner. next := all[:0:0] for _, ep := range all { if ep.Kind != got.Kind { next = append(next, ep) } } all = next } if _, ok := PrimaryHostedEndpoint(all); ok { t.Fatal("empty list produced a primary endpoint") } notReady := []VMEndpoint{{Kind: EndpointVMConnect, VM: "v"}, {Kind: EndpointSSH, Port: 22, Ready: true}} if got, ok := PrimaryHostedEndpoint(notReady); !ok || got.Kind != EndpointSSH { t.Fatalf("a not-ready endpoint was promoted: %+v", got) } } func fixedTable(now *time.Time) *StateTable { t := NewStateTable() t.now = func() time.Time { return *now } return t } func TestStateTableReportValidation(t *testing.T) { now := time.Unix(1000, 0) tbl := fixedTable(&now) for _, rep := range []ReportedState{ {Ref: VMRef{DriverID: "d", Name: ""}, State: "running"}, {Ref: VMRef{DriverID: "d", Name: "vm"}, State: "exploded"}, {Ref: VMRef{DriverID: "d", Name: "vm"}, State: "running", Phase: "weird"}, {Ref: VMRef{DriverID: "d", Name: "vm"}, State: "running", Endpoints: []VMEndpoint{{Kind: "telnet"}}}, } { if err := tbl.Report(rep); err == nil { t.Errorf("Report(%+v) accepted", rep) } } var nilTable *StateTable if err := nilTable.Report(ReportedState{Ref: VMRef{DriverID: "d", Name: "vm"}, State: "running"}); err == nil { t.Error("nil table accepted a report") } if _, ok := nilTable.Get(VMRef{DriverID: "d", Name: "vm"}); ok { t.Error("nil table returned a report") } } func TestStateTableMergeOrdersByFreshness(t *testing.T) { now := time.Unix(1000, 0) tbl := fixedTable(&now) ready := true ref := VMRef{DriverID: "test.driver.a", Name: "dev"} if err := tbl.Report(ReportedState{Ref: ref, State: "running", Ready: &ready, Phase: PhaseInstaller, Endpoints: []VMEndpoint{{Kind: EndpointWSL, Distro: "dev", Ready: true}}, GuestOS: &GuestOSInfo{Family: GuestFamilyLinux}}); err != nil { t.Fatal(err) } // The driver answered before the report: the report wins. vm := VMInfo{Name: "dev", DriverID: ref.DriverID, State: "starting"} tbl.Merge(&vm, now.Add(-time.Second)) if vm.State != "running" || vm.Ready == nil || !*vm.Ready || vm.Phase != PhaseInstaller || len(vm.Endpoints) != 1 || vm.GuestOS == nil || vm.GuestOS.Family != GuestFamilyLinux { t.Fatalf("fresh report not applied: %+v", vm) } // The driver answered after the report and disagrees on state: the driver wins. vm = VMInfo{Name: "dev", DriverID: ref.DriverID, State: "stopped"} tbl.Merge(&vm, now.Add(time.Second)) if vm.State != "stopped" || vm.Ready != nil || len(vm.Endpoints) != 0 || vm.Phase != "" { t.Fatalf("stale report overrode the driver: %+v", vm) } // Same state, driver left optional fields empty: the report fills them. vm = VMInfo{Name: "dev", DriverID: ref.DriverID, State: "running"} tbl.Merge(&vm, now.Add(time.Second)) if vm.Ready == nil || vm.Phase != PhaseInstaller || len(vm.Endpoints) != 1 { t.Fatalf("matching report did not fill empty fields: %+v", vm) } // A same-named VM of another driver is untouched. other := VMInfo{Name: "dev", DriverID: "test.driver.b", State: "stopped"} tbl.Merge(&other, time.Time{}) if other.State != "stopped" || len(other.Endpoints) != 0 { t.Fatalf("report leaked across drivers: %+v", other) } // A nil table still keeps the ssh endpoint consistent. var nilTable *StateTable ssh := VMInfo{State: "running", SSHPort: 22} nilTable.Merge(&ssh, time.Now()) if len(ssh.Endpoints) != 1 { t.Fatalf("nil table skipped SyncSSHEndpoint: %+v", ssh) } } func TestStateTableDeleteAndFallback(t *testing.T) { now := time.Unix(1000, 0) tbl := fixedTable(&now) a, b := VMRef{DriverID: "test.driver.a", Name: "dev"}, VMRef{DriverID: "test.driver.b", Name: "dev"} for _, ref := range []VMRef{a, b} { if err := tbl.Report(ReportedState{Ref: ref, State: "running"}); err != nil { t.Fatal(err) } } if tbl.NeedsFallback(a, 10*time.Second, now.Add(5*time.Second)) { t.Error("a recent report needs a fallback") } if !tbl.NeedsFallback(a, 10*time.Second, now.Add(11*time.Second)) { t.Error("an old report needs no fallback") } if !tbl.NeedsFallback(VMRef{DriverID: "test.driver.a", Name: "missing"}, time.Hour, now) { t.Error("an unknown VM needs no fallback") } tbl.Delete(a) if _, ok := tbl.Get(a); ok { t.Error("Delete kept the report") } if _, ok := tbl.Get(b); !ok { t.Error("Delete removed another driver's report") } if err := tbl.Report(ReportedState{Ref: a, State: "running"}); err != nil { t.Fatal(err) } tbl.DeleteDriver("test.driver.a") if _, ok := tbl.Get(a); ok { t.Error("DeleteDriver kept the driver's report") } if _, ok := tbl.Get(b); !ok { t.Error("DeleteDriver removed another driver's report") } tbl.PruneDrivers(map[string]bool{"test.driver.c": true}) if _, ok := tbl.Get(b); ok { t.Error("PruneDrivers kept an inactive driver's report") } }