package app import ( "errors" "slices" "strings" "testing" "github.com/Nwflower/reims-go/internal/driver" ) func lookPathOnly(found ...string) func(string) (string, error) { return func(name string) (string, error) { if slices.Contains(found, name) { return `C:\tools\` + name, nil } return "", errors.New("not found") } } func TestBuildEndpointLaunchTemplates(t *testing.T) { wt := lookPathOnly("wt.exe", "adb", "code.cmd") noTools := lookPathOnly() cases := []struct { name string ep driver.VMEndpoint action string look func(string) (string, error) exe string args []string }{ {"wsl terminal", driver.VMEndpoint{Kind: driver.EndpointWSL, Distro: "Ubuntu-24.04", Ready: true}, "", wt, `C:\tools\wt.exe`, []string{"new-tab", "--title", "dev", "wsl.exe", "-d", "Ubuntu-24.04"}}, {"wsl terminal without Windows Terminal", driver.VMEndpoint{Kind: driver.EndpointWSL, Distro: "Debian", Ready: true}, endpointActionTerminal, noTools, "cmd.exe", []string{"/k", "wsl.exe", "-d", "Debian"}}, {"wsl vscode", driver.VMEndpoint{Kind: driver.EndpointWSL, Distro: "Debian", Ready: true}, endpointActionVSCode, wt, `C:\tools\code.cmd`, []string{"--remote", "wsl+Debian"}}, {"rdp default port", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "10.0.0.5", Ready: true}, "", wt, "mstsc.exe", []string{"/v:10.0.0.5:3389"}}, {"rdp ipv6", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "fe80::1", Port: 3390, Ready: true}, endpointActionOpen, wt, "mstsc.exe", []string{"/v:[fe80::1]:3390"}}, {"vmconnect", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "Win 11", Ready: true}, endpointActionOpen, wt, "vmconnect.exe", []string{"localhost", "Win 11"}}, {"vmconnect edit", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "Win 11", Ready: true}, endpointActionEdit, wt, "vmconnect.exe", []string{"localhost", "Win 11", "/edit"}}, {"adb shell", driver.VMEndpoint{Kind: driver.EndpointADB, Serial: "emulator-5554", Ready: true}, endpointActionShell, wt, `C:\tools\wt.exe`, []string{"new-tab", "--title", "dev", "adb", "-s", "emulator-5554", "shell"}}, } for _, tc := range cases { spec, err := buildEndpointLaunch("dev", tc.ep, tc.action, tc.look) if err != nil { t.Errorf("%s: %v", tc.name, err) continue } if spec.exe != tc.exe || !slices.Equal(spec.args, tc.args) { t.Errorf("%s: got %s %q, want %s %q", tc.name, spec.exe, spec.args, tc.exe, tc.args) } } } func TestBuildEndpointLaunchRejects(t *testing.T) { look := lookPathOnly("wt.exe") cases := []struct { name string ep driver.VMEndpoint action string }{ {"not ready", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "10.0.0.5"}, ""}, {"ssh goes through the SSH shortcuts", driver.VMEndpoint{Kind: driver.EndpointSSH, Port: 22, Ready: true}, ""}, {"unknown action", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "10.0.0.5", Ready: true}, "run"}, {"vscode needs wsl", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "vm", Ready: true}, endpointActionVSCode}, {"vscode not installed", driver.VMEndpoint{Kind: driver.EndpointWSL, Distro: "Debian", Ready: true}, endpointActionVSCode}, {"adb not installed", driver.VMEndpoint{Kind: driver.EndpointADB, Serial: "emulator-5554", Ready: true}, ""}, {"unknown kind", driver.VMEndpoint{Kind: "telnet", Ready: true}, ""}, {"shell metacharacters in host", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "a;calc", Ready: true}, ""}, {"command chaining in host", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "a&&calc", Ready: true}, ""}, {"newline in distro", driver.VMEndpoint{Kind: driver.EndpointWSL, Distro: "a\nb", Ready: true}, ""}, {"vmconnect option injection", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "/edit", Ready: true}, ""}, {"vmconnect quote", driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: `a"b`, Ready: true}, ""}, {"rdp port out of range", driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "10.0.0.5", Port: 70000, Ready: true}, ""}, {"adb option injection", driver.VMEndpoint{Kind: driver.EndpointADB, Serial: "-s", Ready: true}, ""}, } for _, tc := range cases { if spec, err := buildEndpointLaunch("dev", tc.ep, tc.action, look); err == nil { t.Errorf("%s: accepted, launching %s %q", tc.name, spec.exe, spec.args) } } } func TestEndpointCommandLine(t *testing.T) { cases := []struct { ep driver.VMEndpoint want string }{ {driver.VMEndpoint{Kind: driver.EndpointADB, Serial: "emulator-5554"}, "adb -s emulator-5554"}, {driver.VMEndpoint{Kind: driver.EndpointWSL, Distro: "Debian"}, "wsl.exe -d Debian"}, {driver.VMEndpoint{Kind: driver.EndpointRDP, Host: "10.0.0.5"}, "mstsc.exe /v:10.0.0.5:3389"}, {driver.VMEndpoint{Kind: driver.EndpointVMConnect, VM: "Win 11"}, `vmconnect.exe localhost "Win 11"`}, {driver.VMEndpoint{Kind: driver.EndpointSSH, Port: 2222}, "ssh -p 2222 127.0.0.1"}, {driver.VMEndpoint{Kind: driver.EndpointSSH, Host: "10.0.0.7", Port: 22, User: "root"}, "ssh -p 22 root@10.0.0.7"}, } for _, tc := range cases { got, err := endpointCommandLine(tc.ep) if err != nil || got != tc.want { t.Errorf("endpointCommandLine(%+v) = %q, %v; want %q", tc.ep, got, err, tc.want) } } for _, ep := range []driver.VMEndpoint{ {Kind: driver.EndpointWSL, Distro: "a b"}, {Kind: driver.EndpointSSH, Port: 0}, {Kind: "x"}, } { if got, err := endpointCommandLine(ep); err == nil { t.Errorf("endpointCommandLine(%+v) = %q, want an error", ep, got) } } } func TestTerminalLaunchSanitizesTheTitle(t *testing.T) { spec := terminalLaunch(lookPathOnly("wt.exe"), "a;b\r\nc", "x") if spec.exe != `C:\tools\wt.exe` || len(spec.args) != 4 || spec.args[2] != "a b c" { t.Fatalf("title launch = %+v", spec) } long := terminalLaunch(lookPathOnly("wt.exe"), strings.Repeat("长", 100), "x") if got := []rune(long.args[2]); len(got) != 64 { t.Fatalf("title has %d runes, want 64", len(got)) } plain := terminalLaunch(lookPathOnly(), "dev", "wsl.exe", "-d", "x") if plain.exe != "cmd.exe" || !slices.Equal(plain.args, []string{"/k", "wsl.exe", "-d", "x"}) { t.Fatalf("fallback launch = %+v", plain) } } func TestSSHTargetPrefersTheEndpoint(t *testing.T) { vm := driver.VMInfo{Network: true, SSHPort: 2222, Endpoints: []driver.VMEndpoint{ {Kind: driver.EndpointSSH, Host: "10.1.2.3", Port: 22, User: "dev", Ready: true}, }} if host, port, user, ok := sshTarget(vm); !ok || host != "10.1.2.3" || port != 22 || user != "dev" { t.Fatalf("sshTarget = %q %d %q %v", host, port, user, ok) } vm.Endpoints[0].Host = "" if host, _, _, ok := sshTarget(vm); !ok || host != "127.0.0.1" { t.Fatalf("empty endpoint host = %q, %v; want loopback", host, ok) } legacy := driver.VMInfo{Network: true, SSHPort: 2222} if host, port, _, ok := sshTarget(legacy); !ok || host != "127.0.0.1" || port != 2222 { t.Fatalf("legacy sshTarget = %q %d %v", host, port, ok) } bad := driver.VMInfo{Endpoints: []driver.VMEndpoint{{Kind: driver.EndpointSSH, Host: "a;b", Port: 22}}} if _, _, _, ok := sshTarget(bad); ok { t.Fatal("an invalid ssh endpoint was used") } } func TestSSHHostAliasSeparatesDrivers(t *testing.T) { mac := sshHostAlias("", "dev") other := sshHostAlias("vendor.driver.linux", "dev") another := sshHostAlias("vendor.driver.windows", "dev") if mac == other || other == another || mac == another { t.Fatalf("aliases collide: %q %q %q", mac, other, another) } if sshConfigKey("vendor.driver.linux", "dev") == sshConfigKey("vendor.driver.windows", "dev") { t.Fatal("ssh config block keys collide across drivers") } }