| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "io" |
| 6 | "net" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestWindowsWindowRFBPixelWheel(t *testing.T) { |
| 12 | for _, tc := range []struct { |
| 13 | name string |
| 14 | negotiated bool |
| 15 | delta int32 |
| 16 | accepted bool |
| 17 | }{ |
| 18 | {"distance", true, 720, true}, {"reverse", true, -360, true}, |
| 19 | {"unnegotiated", false, 100, false}, {"bounded", true, 2049, false}, |
| 20 | } { |
| 21 | t.Run(tc.name, func(t *testing.T) { |
| 22 | frame := windowTestFrame(t, 2, 1) |
| 23 | frame.Target = "verified-frame" |
| 24 | frame.Wheel = true |
| 25 | server, client := net.Pipe() |
| 26 | defer client.Close() |
| 27 | client.SetDeadline(time.Now().Add(5 * time.Second)) |
| 28 | inputs := make(chan map[string]any, 1) |
| 29 | done := make(chan error, 1) |
| 30 | go func() { |
| 31 | defer server.Close() |
| 32 | done <- serveWindowsWindowRFB(server, frame, func(input map[string]any, output any) error { |
| 33 | if input["action"] == "frame" { |
| 34 | *output.(*windowsWindowFrame) = frame |
| 35 | } else { |
| 36 | inputs <- input |
| 37 | } |
| 38 | return nil |
| 39 | }) |
| 40 | }() |
| 41 | read := func(n int) []byte { |
| 42 | t.Helper() |
| 43 | b := make([]byte, n) |
| 44 | if _, e := io.ReadFull(client, b); e != nil { |
| 45 | t.Fatal(e) |
| 46 | } |
| 47 | return b |
| 48 | } |
| 49 | write := func(b []byte) { |
| 50 | t.Helper() |
| 51 | if _, e := client.Write(b); e != nil { |
| 52 | t.Fatal(e) |
| 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 | if tc.negotiated { |
| 64 | packet := []byte{2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0} |
| 65 | fence, wheel := int32(-312), int32(cageWheelEncoding) |
| 66 | binary.BigEndian.PutUint32(packet[4:], uint32(fence)) |
| 67 | binary.BigEndian.PutUint32(packet[8:], uint32(wheel)) |
| 68 | write(packet) |
| 69 | read(9) |
| 70 | header := read(9) |
| 71 | if string(read(int(header[8]))) != cageWheelCapability { |
| 72 | t.Fatal("missing capability") |
| 73 | } |
| 74 | } |
| 75 | write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) |
| 76 | read(24) |
| 77 | packet := make([]byte, 14) |
| 78 | packet[0] = cageWheelMessage |
| 79 | binary.BigEndian.PutUint16(packet[2:], 1) |
| 80 | binary.BigEndian.PutUint32(packet[10:], uint32(tc.delta)) |
| 81 | write(packet) |
| 82 | if tc.accepted { |
| 83 | select { |
| 84 | case input := <-inputs: |
| 85 | if input["deltaY"] != tc.delta || input["target"] != "verified-frame" || input["action"] != "pointer" { |
| 86 | t.Fatalf("invalid input %v", input) |
| 87 | } |
| 88 | case <-time.After(time.Second): |
| 89 | t.Fatal("wheel missing") |
| 90 | } |
| 91 | |
| 92 | write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) |
| 93 | read(24) |
| 94 | } else { |
| 95 | select { |
| 96 | case e := <-done: |
| 97 | if e == nil { |
| 98 | t.Fatal("invalid wheel accepted") |
| 99 | } |
| 100 | case <-time.After(time.Second): |
| 101 | t.Fatal("invalid wheel not rejected") |
| 102 | } |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | func TestWindowsWindowRFBKeepsWheelStepsForAGuestWithoutDistances(t *testing.T) { |
| 111 | frame := windowTestFrame(t, 2, 1) |
| 112 | server, client := net.Pipe() |
| 113 | defer client.Close() |
| 114 | client.SetDeadline(time.Now().Add(5 * time.Second)) |
| 115 | done := make(chan error, 1) |
| 116 | go func() { |
| 117 | defer server.Close() |
| 118 | done <- serveWindowsWindowRFB(server, frame, func(input map[string]any, output any) error { |
| 119 | if input["action"] == "frame" { |
| 120 | *output.(*windowsWindowFrame) = frame |
| 121 | } |
| 122 | return nil |
| 123 | }) |
| 124 | }() |
| 125 | read := func(n int) []byte { |
| 126 | t.Helper() |
| 127 | b := make([]byte, n) |
| 128 | if _, e := io.ReadFull(client, b); e != nil { |
| 129 | t.Fatal(e) |
| 130 | } |
| 131 | return b |
| 132 | } |
| 133 | read(12) |
| 134 | client.Write([]byte("RFB 003.008\n")) |
| 135 | read(2) |
| 136 | client.Write([]byte{1}) |
| 137 | read(4) |
| 138 | client.Write([]byte{1}) |
| 139 | init := read(24) |
| 140 | read(int(binary.BigEndian.Uint32(init[20:]))) |
| 141 | packet := []byte{2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0} |
| 142 | fence, wheel := int32(-312), int32(cageWheelEncoding) |
| 143 | binary.BigEndian.PutUint32(packet[4:], uint32(fence)) |
| 144 | binary.BigEndian.PutUint32(packet[8:], uint32(wheel)) |
| 145 | client.Write(packet) |
| 146 | read(9) |
| 147 | |
| 148 | client.Write([]byte{3, 0, 0, 0, 0, 0, 0, 2, 0, 1}) |
| 149 | if update := read(24); update[0] != 0 { |
| 150 | t.Fatalf("unexpected message %d", update[0]) |
| 151 | } |
| 152 | distance := make([]byte, 14) |
| 153 | distance[0] = cageWheelMessage |
| 154 | binary.BigEndian.PutUint32(distance[10:], 100) |
| 155 | client.Write(distance) |
| 156 | select { |
| 157 | case err := <-done: |
| 158 | if err == nil { |
| 159 | t.Fatal("a distance was accepted without confirmation") |
| 160 | } |
| 161 | case <-time.After(time.Second): |
| 162 | t.Fatal("a distance was not rejected") |
| 163 | } |
| 164 | } |