| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/gorilla/websocket" |
| 19 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 20 | ) |
| 21 | |
| 22 | func TestWindowsGuestTerminalRPC(t *testing.T) { |
| 23 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 24 | defer cancel() |
| 25 | cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestWindowsTerminalRelayHelper$") |
| 26 | cmd.Env = append(os.Environ(), "CAGE_WINDOWS_TERMINAL_RPC_TEST=echo") |
| 27 | p, err := startWindowsGuestTerminal(cmd, 100, 30) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | defer p.Close() |
| 32 | expected := []byte("中文🙂\x03") |
| 33 | if _, err = p.Write(expected); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | actual := make([]byte, len(expected)) |
| 37 | if _, err = io.ReadFull(p, actual); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if string(actual) != string(expected) { |
| 41 | t.Fatalf("output changed: %q", actual) |
| 42 | } |
| 43 | if err = p.Resize(160, 50); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | marker := make([]byte, len("160x50")) |
| 47 | if _, err = io.ReadFull(p, marker); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if string(marker) != "160x50" { |
| 51 | t.Fatalf("resize did not reach guest: %q", marker) |
| 52 | } |
| 53 | if err = p.Resize(0, 50); err == nil { |
| 54 | t.Fatal("invalid resize admitted") |
| 55 | } |
| 56 | if _, err = p.Write([]byte("exit")); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | if _, err = p.Read(make([]byte, 1)); err != io.EOF { |
| 60 | t.Fatalf("clean remote exit got %v", err) |
| 61 | } |
| 62 | if err = p.Wait(); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | } |
| 66 | func TestWindowsGuestTerminalRejectsMissingReady(t *testing.T) { |
| 67 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 68 | defer cancel() |
| 69 | cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestWindowsTerminalRelayHelper$") |
| 70 | cmd.Env = append(os.Environ(), "CAGE_WINDOWS_TERMINAL_RPC_TEST=unavailable") |
| 71 | if p, err := startWindowsGuestTerminal(cmd, 100, 30); err == nil { |
| 72 | p.Close() |
| 73 | t.Fatal("unavailable GUI became terminal") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func TestWindowsGuestTerminalLargePasteThroughWorkbench(t *testing.T) { |
| 78 | s := testServer(t) |
| 79 | s.windowsGuest = &guestos.WindowsProfile{} |
| 80 | ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 81 | defer cancel() |
| 82 | command := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestWindowsTerminalRelayHelper$") |
| 83 | command.Env = append(os.Environ(), "CAGE_WINDOWS_TERMINAL_RPC_TEST=echo") |
| 84 | session, err := s.terminals.startSessionWithPTY(command, t.TempDir(), "shell", claudeSelection{}, nil, func() (terminalPTY, error) { |
| 85 | return startWindowsGuestTerminal(command, 100, 30) |
| 86 | }) |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | defer s.terminals.close() |
| 91 | server := httptest.NewServer(s.handler()) |
| 92 | defer server.Close() |
| 93 | s.origin = server.URL |
| 94 | headers := http.Header{ |
| 95 | "Origin": {s.origin}, |
| 96 | "Cookie": {s.cookieName() + "=" + s.session + "; " + s.cookieName() + "-desktop=" + trustedViewProof(s.token)}, |
| 97 | } |
| 98 | ws, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(s.origin, "http")+"/api/terminals/"+session.id+"/stream", headers) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | defer ws.Close() |
| 103 | read := func(expected string) { |
| 104 | t.Helper() |
| 105 | if err := ws.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | var actual []byte |
| 109 | for len(actual) < len(expected) { |
| 110 | _, data, err := ws.ReadMessage() |
| 111 | if err != nil { |
| 112 | t.Fatalf("terminal disconnected after %d of %d bytes: %v", len(actual), len(expected), err) |
| 113 | } |
| 114 | actual = append(actual, data...) |
| 115 | } |
| 116 | if string(actual) != expected { |
| 117 | t.Fatalf("terminal changed input bytes: received=%d expected=%d", len(actual), len(expected)) |
| 118 | } |
| 119 | } |
| 120 | for _, payload := range []string{"small", strings.Repeat("x", 64<<10), strings.Repeat("x", (64<<10)+1), strings.Repeat("中文🙂\n", 20000), strings.Repeat("x", (1<<20)-64)} { |
| 121 | if err := ws.WriteJSON(map[string]string{"type": "input", "data": payload}); err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | read(payload) |
| 125 | } |
| 126 | if err := ws.WriteJSON(map[string]any{"type": "resize", "cols": 599, "rows": 399}); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | read("599x399") |
| 130 | if session.summary()["closed"] != false { |
| 131 | t.Fatal("large paste closed its terminal") |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestWindowsTerminalRelayHelper(t *testing.T) { |
| 136 | mode := os.Getenv("CAGE_WINDOWS_TERMINAL_RPC_TEST") |
| 137 | if mode == "" { |
| 138 | return |
| 139 | } |
| 140 | reader := bufio.NewScanner(os.Stdin) |
| 141 | reader.Buffer(make([]byte, 4096), 100<<10) |
| 142 | if !reader.Scan() { |
| 143 | os.Exit(2) |
| 144 | } |
| 145 | var open map[string]any |
| 146 | if json.Unmarshal(reader.Bytes(), &open) != nil || open["action"] != "open" || open["workspace"] != "home" || open["shell"] != "powershell" { |
| 147 | os.Exit(2) |
| 148 | } |
| 149 | write := func(value any) { |
| 150 | if json.NewEncoder(os.Stdout).Encode(value) != nil { |
| 151 | os.Exit(2) |
| 152 | } |
| 153 | } |
| 154 | if mode == "unavailable" { |
| 155 | write(map[string]any{"error": "requires_gui_login"}) |
| 156 | os.Exit(1) |
| 157 | } |
| 158 | write(map[string]any{"ready": true}) |
| 159 | for reader.Scan() { |
| 160 | var value map[string]any |
| 161 | if json.Unmarshal(reader.Bytes(), &value) != nil { |
| 162 | os.Exit(2) |
| 163 | } |
| 164 | switch value["action"] { |
| 165 | case "input": |
| 166 | data, err := base64.StdEncoding.DecodeString(value["data"].(string)) |
| 167 | if err != nil || len(data) > 64<<10 { |
| 168 | os.Exit(2) |
| 169 | } |
| 170 | if string(data) == "exit" { |
| 171 | write(map[string]any{"exit": 0}) |
| 172 | os.Exit(0) |
| 173 | } |
| 174 | write(map[string]any{"data": base64.StdEncoding.EncodeToString(data)}) |
| 175 | case "resize": |
| 176 | cols, rows := value["cols"].(float64), value["rows"].(float64) |
| 177 | if cols < 1 || cols >= 600 || rows < 1 || rows > 500 { |
| 178 | os.Exit(2) |
| 179 | } |
| 180 | write(map[string]any{"data": base64.StdEncoding.EncodeToString(fmt.Appendf(nil, "%dx%d", int(cols), int(rows)))}) |
| 181 | default: |
| 182 | os.Exit(2) |
| 183 | } |
| 184 | } |
| 185 | os.Exit(0) |
| 186 | } |