| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/base64" |
| 6 | "encoding/binary" |
| 7 | "image" |
| 8 | "image/color" |
| 9 | "image/jpeg" |
| 10 | "io" |
| 11 | "net" |
| 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| 16 | func TestNativeTightPreservesJPEGAndInputAfterUnchangedFrame(t *testing.T) { |
| 17 | img := image.NewRGBA(image.Rect(0, 0, 64, 48)) |
| 18 | for y := 0; y < 48; y++ { |
| 19 | for x := 0; x < 64; x++ { |
| 20 | img.SetRGBA(x, y, color.RGBA{R: 200, G: 90, A: 255}) |
| 21 | } |
| 22 | } |
| 23 | var encoded bytes.Buffer |
| 24 | if err := jpeg.Encode(&encoded, img, &jpeg.Options{Quality: 85}); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | frame := windowsWindowFrame{JPEG: base64.StdEncoding.EncodeToString(encoded.Bytes()), Width: 64, Height: 48, Target: "window", Sequence: "first"} |
| 28 | server, client := net.Pipe() |
| 29 | defer client.Close() |
| 30 | _ = client.SetDeadline(time.Now().Add(5 * time.Second)) |
| 31 | keys := make(chan map[string]any, 1) |
| 32 | go func() { |
| 33 | defer server.Close() |
| 34 | _ = serveWindowsWindowRFB(server, frame, func(input map[string]any, output any) error { |
| 35 | if input["action"] == "key" { |
| 36 | keys <- input |
| 37 | return nil |
| 38 | } |
| 39 | if input["after"] == "first" { |
| 40 | *output.(*windowsWindowFrame) = windowsWindowFrame{Sequence: "first", Unchanged: true} |
| 41 | } else { |
| 42 | *output.(*windowsWindowFrame) = frame |
| 43 | } |
| 44 | return nil |
| 45 | }) |
| 46 | }() |
| 47 | read := func(n int) []byte { |
| 48 | t.Helper() |
| 49 | data := make([]byte, n) |
| 50 | if _, err := io.ReadFull(client, data); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | return data |
| 54 | } |
| 55 | write := func(data []byte) { |
| 56 | t.Helper() |
| 57 | if _, err := client.Write(data); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | } |
| 61 | read(12) |
| 62 | write([]byte("RFB 003.008\n")) |
| 63 | read(2) |
| 64 | write([]byte{1}) |
| 65 | read(4) |
| 66 | write([]byte{1}) |
| 67 | init := read(24) |
| 68 | read(int(binary.BigEndian.Uint32(init[20:]))) |
| 69 | write([]byte{2, 0, 0, 1, 0, 0, 0, 7}) |
| 70 | write([]byte{3, 0, 0, 0, 0, 0, 0, 64, 0, 48}) |
| 71 | header := read(16) |
| 72 | if binary.BigEndian.Uint32(header[12:]) != 7 || read(1)[0] != 0x90 { |
| 73 | t.Fatal("JPEG did not use Tight passthrough") |
| 74 | } |
| 75 | length := 0 |
| 76 | for shift := 0; shift <= 14; shift += 7 { |
| 77 | value := read(1)[0] |
| 78 | if shift == 14 { |
| 79 | length |= int(value) << shift |
| 80 | break |
| 81 | } |
| 82 | length |= int(value&127) << shift |
| 83 | if value&128 == 0 { |
| 84 | break |
| 85 | } |
| 86 | } |
| 87 | if !bytes.Equal(read(length), encoded.Bytes()) { |
| 88 | t.Fatal("JPEG was re-encoded") |
| 89 | } |
| 90 | write([]byte{3, 1, 0, 0, 0, 0, 0, 64, 0, 48}) |
| 91 | if !bytes.Equal(read(4), []byte{0, 0, 0, 0}) { |
| 92 | t.Fatal("unchanged frame resent pixels") |
| 93 | } |
| 94 | write([]byte{4, 1, 0, 0, 0, 0, 0, 'a'}) |
| 95 | select { |
| 96 | case input := <-keys: |
| 97 | if input["target"] != "window" { |
| 98 | t.Fatal("unchanged frame lost the input target") |
| 99 | } |
| 100 | case <-time.After(time.Second): |
| 101 | t.Fatal("input stalled after Tight frame") |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestNativeTightPNGPreservesEncodedWindow(t *testing.T) { |
| 106 | frame := windowTestFrame(t, 64, 48) |
| 107 | encoded, err := base64.StdEncoding.DecodeString(frame.PNG) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | rectangles, count, supported, err := nativeTightRectangles(frame, -260) |
| 112 | if err != nil || !supported || count != 1 || int32(binary.BigEndian.Uint32(rectangles[8:12])) != -260 || rectangles[12] != 0xa0 { |
| 113 | t.Fatalf("PNG passthrough failed: supported=%v count=%d error=%v", supported, count, err) |
| 114 | } |
| 115 | if !bytes.HasSuffix(rectangles, encoded) { |
| 116 | t.Fatal("PNG changed during transport") |
| 117 | } |
| 118 | _, _, supported, err = nativeTightRectangles(frame, 7) |
| 119 | if err != nil || supported { |
| 120 | t.Fatal("PNG sent to a JPEG-only Tight client") |
| 121 | } |
| 122 | } |