| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "strings" |
| 12 | "sync/atomic" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/gorilla/websocket" |
| 17 | "golang.org/x/crypto/ssh" |
| 18 | ) |
| 19 | |
| 20 | func TestMacOSAudioUsesBoundTransportAndClosesCapture(t *testing.T) { |
| 21 | var allowed atomic.Bool |
| 22 | allowed.Store(true) |
| 23 | closed := make(chan struct{}, 1) |
| 24 | pcm := bytes.Repeat([]byte{1, 2, 3, 4}, 960) |
| 25 | guest := newMacWorkspaceGuest(t, func(channel ssh.Channel, command string) (uint32, bool) { |
| 26 | if command != macOSWindowSocketCommand { |
| 27 | return 1, false |
| 28 | } |
| 29 | line, err := bufio.NewReader(channel).ReadString('\n') |
| 30 | if err != nil { |
| 31 | return 1, true |
| 32 | } |
| 33 | switch strings.TrimSpace(line) { |
| 34 | case `{"action":"audio-status"}`: |
| 35 | json.NewEncoder(channel).Encode(map[string]any{"supported": true, "available": allowed.Load(), "reason": "screen_recording_permission"}) |
| 36 | case `{"action":"audio-capture"}`: |
| 37 | fmt.Fprintln(channel, `{"ready":true,"format":"s16le","sample_rate":48000,"channels":2}`) |
| 38 | channel.Write(pcm) |
| 39 | io.Copy(io.Discard, channel) |
| 40 | closed <- struct{}{} |
| 41 | default: |
| 42 | return 1, true |
| 43 | } |
| 44 | return 0, true |
| 45 | }) |
| 46 | s := guest.s |
| 47 | server := httptest.NewServer(s.handler()) |
| 48 | defer server.Close() |
| 49 | s.origin = server.URL |
| 50 | headers := http.Header{ |
| 51 | "Origin": {s.origin}, |
| 52 | "Cookie": {s.cookieName() + "=" + s.session + "; " + s.cookieName() + "-desktop=" + trustedViewProof(s.token)}, |
| 53 | } |
| 54 | statusRequest, _ := http.NewRequest(http.MethodGet, s.origin+"/api/audio", nil) |
| 55 | statusRequest.Header = headers.Clone() |
| 56 | response, err := server.Client().Do(statusRequest) |
| 57 | if err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | var format map[string]any |
| 61 | decodeErr := json.NewDecoder(response.Body).Decode(&format) |
| 62 | response.Body.Close() |
| 63 | if response.StatusCode != http.StatusOK || decodeErr != nil || format["sample_rate"] != float64(48000) || format["channels"] != float64(2) { |
| 64 | t.Fatalf("native audio status: %d %v", response.StatusCode, decodeErr) |
| 65 | } |
| 66 | ws, _, err := websocket.DefaultDialer.Dial("ws"+strings.TrimPrefix(s.origin, "http")+"/api/audio", headers) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | defer ws.Close() |
| 71 | ws.SetReadDeadline(time.Now().Add(5 * time.Second)) |
| 72 | if err := ws.ReadJSON(&format); err != nil || format["format"] != "s16le" { |
| 73 | t.Fatalf("native audio websocket format: %v", err) |
| 74 | } |
| 75 | kind, data, err := ws.ReadMessage() |
| 76 | if err != nil || kind != websocket.BinaryMessage || !bytes.Equal(data, pcm) { |
| 77 | t.Fatalf("native PCM changed: %v", err) |
| 78 | } |
| 79 | ws.Close() |
| 80 | select { |
| 81 | case <-closed: |
| 82 | case <-time.After(3 * time.Second): |
| 83 | t.Fatal("closed workbench retained its SSH capture") |
| 84 | } |
| 85 | allowed.Store(false) |
| 86 | response, err = server.Client().Do(statusRequest) |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | message, _ := io.ReadAll(response.Body) |
| 91 | response.Body.Close() |
| 92 | if response.StatusCode != http.StatusConflict || !strings.Contains(string(message), "Cage Guest Window") || !strings.Contains(string(message), "系统声音") { |
| 93 | t.Fatal("missing guest permission did not return an actionable error") |
| 94 | } |
| 95 | } |