| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/binary" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "net" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestBrowserFocusRecoveryKeepsRFBDisplayAlive(t *testing.T) { |
| 15 | frame := windowTestFrame(t, 2, 1) |
| 16 | frame.ID, frame.Document = "tab", "doc" |
| 17 | server, client := net.Pipe() |
| 18 | defer client.Close() |
| 19 | client.SetDeadline(time.Now().Add(5 * time.Second)) |
| 20 | |
| 21 | go func() { |
| 22 | defer server.Close() |
| 23 | serveWindowsWindowRFB(server, frame, func(input map[string]any, output any) error { |
| 24 | if input["action"] == "frame" { |
| 25 | *output.(*windowsWindowFrame) = frame |
| 26 | return nil |
| 27 | } |
| 28 | _, err := recoverBrowserInputFocus(context.Background(), func(_ context.Context, _, path string, _ any) ([]byte, error) { |
| 29 | switch path { |
| 30 | case "/input": |
| 31 | return nil, windowsBrowserFailure{"browser_focus_required", "focus"} |
| 32 | case "/tabs": |
| 33 | return []byte(`{"active":"tab","tabs":[{"id":"tab","document":"doc"}]}`), nil |
| 34 | default: |
| 35 | return []byte(`{"ok":true}`), nil |
| 36 | } |
| 37 | }, input, func() {}) |
| 38 | return err |
| 39 | }) |
| 40 | }() |
| 41 | read := func(n int) []byte { |
| 42 | t.Helper() |
| 43 | b := make([]byte, n) |
| 44 | if _, err := io.ReadFull(client, b); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | return b |
| 48 | } |
| 49 | write := func(b []byte) { |
| 50 | t.Helper() |
| 51 | if _, err := client.Write(b); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | } |
| 55 | read(12) |
| 56 | write([]byte("RFB 003.008\n")) |
| 57 | read(2) |
| 58 | write([]byte{1}) |
| 59 | read(4) |
| 60 | write([]byte{1}) |
| 61 | init := read(24) |
| 62 | read(int(binary.BigEndian.Uint32(init[20:]))) |
| 63 | |
| 64 | write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) |
| 65 | read(24) |
| 66 | |
| 67 | |
| 68 | write([]byte{5, 1, 0, 1, 0, 0}) |
| 69 | write([]byte{4, 1, 0, 0, 0, 0, 0, 97}) |
| 70 | write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) |
| 71 | update := read(16) |
| 72 | if binary.BigEndian.Uint16(update[2:]) != 1 { |
| 73 | t.Fatal("display frame missing after focus rejection") |
| 74 | } |
| 75 | read(8) |
| 76 | } |
| 77 | |
| 78 | func TestBrowserFrameTransitionRetry(t *testing.T) { |
| 79 | for _, tc := range []struct { |
| 80 | name, code string |
| 81 | failures, calls int |
| 82 | fatal bool |
| 83 | }{ |
| 84 | {"transient", "browser_target_changed", 1, 2, false}, |
| 85 | {"document transition", "target_changed", 1, 2, false}, |
| 86 | {"popup capture transition", "desktop_target_changed", 2, 3, false}, |
| 87 | {"popup capture busy", "desktop_capture_busy", 1, 2, false}, |
| 88 | {"persistent", "browser_target_changed", 8, 6, true}, |
| 89 | {"permission", "desktop_capture_permission_required", 1, 1, true}, |
| 90 | {"locked", "requires_gui_login", 1, 1, true}, |
| 91 | {"identity", "browser_process_unavailable", 1, 1, true}, |
| 92 | } { |
| 93 | t.Run(tc.name, func(t *testing.T) { |
| 94 | calls := 0 |
| 95 | data, err := requestBrowserFrame(context.Background(), func(_ context.Context, method, path string, _ any) ([]byte, error) { |
| 96 | if method != "GET" || path != "/frame" { |
| 97 | t.Fatal("retried a mutation") |
| 98 | } |
| 99 | calls++ |
| 100 | if calls <= tc.failures { |
| 101 | return nil, windowsBrowserFailure{tc.code, "rejected"} |
| 102 | } |
| 103 | return []byte("fresh validated frame"), nil |
| 104 | }, map[string]any{"action": "frame"}) |
| 105 | if calls != tc.calls || (err != nil) != tc.fatal { |
| 106 | t.Fatalf("calls=%d error=%v", calls, err) |
| 107 | } |
| 108 | if !tc.fatal && string(data) != "fresh validated frame" { |
| 109 | t.Fatal("missing fresh frame") |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestBrowserFrameTransitionCancellation(t *testing.T) { |
| 116 | ctx, cancel := context.WithCancel(context.Background()) |
| 117 | calls := 0 |
| 118 | _, err := requestBrowserFrame(ctx, func(context.Context, string, string, any) ([]byte, error) { |
| 119 | calls++ |
| 120 | cancel() |
| 121 | return nil, windowsBrowserFailure{"browser_target_changed", "changed"} |
| 122 | }, nil) |
| 123 | if !errors.Is(err, context.Canceled) || calls != 1 { |
| 124 | t.Fatalf("calls=%d err=%v", calls, err) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestBrowserFocusRecoveryDoesNotReplayInput(t *testing.T) { |
| 129 | |
| 130 | var calls []string |
| 131 | attempts := 0 |
| 132 | released := 0 |
| 133 | request := func(_ context.Context, method, path string, input any) ([]byte, error) { |
| 134 | calls = append(calls, method+" "+path) |
| 135 | switch path { |
| 136 | case "/input": |
| 137 | attempts++ |
| 138 | if attempts == 1 { |
| 139 | return nil, windowsBrowserFailure{"browser_focus_required", "focus rejected"} |
| 140 | } |
| 141 | return []byte(`{"ok":true}`), nil |
| 142 | case "/tabs": |
| 143 | return []byte(`{"active":"tab","tabs":[{"id":"tab","document":"doc"}]}`), nil |
| 144 | case "/activate": |
| 145 | if input.(map[string]any)["id"] != "tab" { |
| 146 | t.Fatal("activated wrong tab") |
| 147 | } |
| 148 | return []byte(`{"ok":true}`), nil |
| 149 | } |
| 150 | t.Fatalf("unexpected request %s", path) |
| 151 | return nil, nil |
| 152 | } |
| 153 | input := map[string]any{"kind": "key", "target": "geometry", "down": true} |
| 154 | if _, err := recoverBrowserInputFocus(context.Background(), request, input, func() { released++ }); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | if attempts != 1 || released != 1 { |
| 158 | t.Fatal("input was replayed or its lease was not released") |
| 159 | } |
| 160 | if !reflect.DeepEqual(calls, []string{"POST /input"}) { |
| 161 | t.Fatal(calls) |
| 162 | } |
| 163 | if _, err := recoverBrowserInputFocus(context.Background(), request, input, func() { released++ }); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if attempts != 2 || released != 1 { |
| 167 | t.Fatal("next user input was not accepted") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func TestBrowserFocusRecoveryPreservesBoundaries(t *testing.T) { |
| 172 | for _, tc := range []struct { |
| 173 | name, code, tabs string |
| 174 | fatal bool |
| 175 | }{ |
| 176 | {"other tab", "browser_focus_required", `{"active":"other","tabs":[{"id":"tab","document":"doc"}]}`, false}, |
| 177 | {"new document", "browser_focus_required", `{"active":"tab","tabs":[{"id":"tab","document":"new"}]}`, false}, |
| 178 | {"target changed", "browser_target_changed", "", false}, |
| 179 | {"document changed", "target_changed", "", false}, |
| 180 | {"locked session", "requires_gui_login", "", true}, |
| 181 | {"transport failure", "", "", true}, |
| 182 | } { |
| 183 | t.Run(tc.name, func(t *testing.T) { |
| 184 | |
| 185 | failure := error(windowsBrowserFailure{tc.code, "rejected"}) |
| 186 | if tc.code == "" { |
| 187 | failure = errors.New("EOF") |
| 188 | } |
| 189 | request := func(_ context.Context, method, path string, _ any) ([]byte, error) { |
| 190 | switch path { |
| 191 | case "/input": |
| 192 | return nil, failure |
| 193 | case "/tabs": |
| 194 | return []byte(tc.tabs), nil |
| 195 | default: |
| 196 | t.Fatalf("unexpected recovery %s %s", method, path) |
| 197 | return nil, nil |
| 198 | } |
| 199 | } |
| 200 | _, err := recoverBrowserInputFocus(context.Background(), request, map[string]any{}, func() {}) |
| 201 | if (err != nil) != tc.fatal { |
| 202 | t.Fatalf("fatal=%v err=%v", tc.fatal, err) |
| 203 | } |
| 204 | }) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func TestBrowserFocusRecoveryNeverStealsFocus(t *testing.T) { |
| 209 | |
| 210 | request := func(_ context.Context, _, path string, _ any) ([]byte, error) { |
| 211 | if path != "/input" { |
| 212 | t.Fatal("focus recovery flooded") |
| 213 | } |
| 214 | return nil, windowsBrowserFailure{"browser_focus_required", "focus"} |
| 215 | } |
| 216 | _, err := recoverBrowserInputFocus(context.Background(), request, map[string]any{}, func() {}) |
| 217 | if err != nil { |
| 218 | t.Fatal(err) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | func TestBrowserFocusRejectionStaysAnErrorWithoutAReleasableConnection(t *testing.T) { |
| 226 | failure := windowsBrowserFailure{"target_changed", "changed"} |
| 227 | _, err := recoverBrowserInputFocus(context.Background(), func(context.Context, string, string, any) ([]byte, error) { |
| 228 | return nil, failure |
| 229 | }, map[string]any{}, nil) |
| 230 | if !errors.Is(err, failure) { |
| 231 | t.Fatalf("rejection hidden: %v", err) |
| 232 | } |
| 233 | } |