forked from mitchellh/go-vnc
-
Notifications
You must be signed in to change notification settings - Fork 16
/
server_test.go
224 lines (203 loc) · 7.28 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package vnc
import (
"testing"
"github.com/kward/go-vnc/encodings"
"github.com/kward/go-vnc/go/operators"
)
func TestRectangle_Marshal(t *testing.T) {
var (
bytes []byte
rect *Rectangle
)
mockConn := &MockConn{}
conn := NewClientConn(mockConn, &ClientConfig{})
rect = &Rectangle{1, 2, 3, 4, &RawEncoding{}, conn.Encodable}
bytes, err := rect.Marshal()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if got, want := bytes, []byte{0, 1, 0, 2, 0, 3, 0, 4, 0, 0, 0, 0}; !operators.EqualSlicesOfByte(got, want) {
t.Errorf("incorrect result; got = %v, want = %v", got, want)
}
}
func TestRectangle_Unmarshal(t *testing.T) {
var rect *Rectangle
rect = &Rectangle{}
if err := rect.Unmarshal([]byte{0, 2, 0, 3, 0, 4, 0, 5, 0, 0, 0, 0}); err != nil {
t.Errorf("unexpected error: %v", err)
}
if got, want := rect.X, uint16(2); got != want {
t.Errorf("incorrect x-position; got = %v, want = %v", got, want)
}
if got, want := rect.Y, uint16(3); got != want {
t.Errorf("incorrect y-position; got = %v, want = %v", got, want)
}
if got, want := rect.Width, uint16(4); got != want {
t.Errorf("incorrect width; got = %v, want = %v", got, want)
}
if got, want := rect.Height, uint16(5); got != want {
t.Errorf("incorrect height; got = %v, want = %v", got, want)
}
if rect.Enc == nil {
t.Errorf("empty encoding")
return
}
if got, want := rect.Enc.Type(), encodings.Raw; got != want {
t.Errorf("incorrect encoding-type; got = %v, want = %v", got, want)
}
}
// TODO(kward): need to read encodings in addition to rectangles.
func TestFramebufferUpdate(t *testing.T) {
mockConn := &MockConn{}
conn := NewClientConn(mockConn, &ClientConfig{})
// Use empty PixelFormat so that the BPP is zero, and rects won't be read.
// TODO(kward): give some real rectangles so this hack isn't necessary.
conn.pixelFormat = PixelFormat{}
for _, tt := range []struct {
desc string
rects []Rectangle
ok bool
}{
{"single raw encoded rect",
[]Rectangle{{1, 2, 3, 4, &RawEncoding{}, conn.Encodable}}, true},
} {
mockConn.Reset()
// Send the message.
msg := newFramebufferUpdate(tt.rects)
bytes, err := msg.Marshal()
if err != nil {
t.Errorf("%s: failed to marshal; %s", tt.desc, err)
continue
}
if err := conn.send(bytes); err != nil {
t.Errorf("%s: failed to send; %s", tt.desc, err)
continue
}
// Validate message handling.
var messageType uint8
if err := conn.receive(&messageType); err != nil {
t.Fatal(err)
}
fu := &FramebufferUpdate{}
parsedFU, err := fu.Read(conn)
if err != nil {
t.Fatalf("%s: failed to read; %s", tt.desc, err)
}
rects := parsedFU.(*FramebufferUpdate).Rects
// Validate the message.
if got, want := len(rects), len(tt.rects); got != want {
t.Errorf("%s: incorrect number-of-rectangles; got %d, want %d", tt.desc, got, want)
continue
}
for i, r := range tt.rects {
if got, want := rects[i].X, r.X; got != want {
t.Errorf("%s: rect[%d] incorrect x-position; got %d, want %d", tt.desc, i, got, want)
}
if got, want := rects[i].Y, r.Y; got != want {
t.Errorf("%s: rect[%d] incorrect y-position; got %d, want %d", tt.desc, i, got, want)
}
if got, want := rects[i].Width, r.Width; got != want {
t.Errorf("%s: rect[%d] incorrect width; got %d, want %d", tt.desc, i, got, want)
}
if got, want := rects[i].Height, r.Height; got != want {
t.Errorf("%s: rect[%d] incorrect height; got %d, want %d", tt.desc, i, got, want)
}
if rects[i].Enc == nil {
t.Errorf("%s: rect[%d] has empty encoding", tt.desc, i)
continue
}
if got, want := rects[i].Enc.Type(), r.Enc.Type(); got != want {
t.Errorf("%s: rect[%d] incorrect encoding-type; got %d, want %d", tt.desc, i, got, want)
}
}
}
}
func TestColor_Marshal(t *testing.T) {
cm := ColorMap{}
for i := 0; i < len(cm); i++ {
cm[i] = Color{R: uint16(i), G: uint16(i << 4), B: uint16(i << 8)}
}
tests := []struct {
c *Color
data []byte
}{
// 8 BPP, with ColorMap
{&Color{&PixelFormat8bit, &cm, 0, 0, 0, 0}, []byte{0}},
{&Color{&PixelFormat8bit, &cm, 127, 127, 2032, 32512}, []byte{127}},
{&Color{&PixelFormat8bit, &cm, 255, 255, 4080, 65280}, []byte{255}},
// 16 BPP
{&Color{&PixelFormat16bit, &ColorMap{}, 0, 0, 0, 0}, []byte{0, 0}},
{&Color{&PixelFormat16bit, &ColorMap{}, 0, 127, 7, 0}, []byte{0, 127}},
{&Color{&PixelFormat16bit, &ColorMap{}, 0, 32767, 2047, 127}, []byte{127, 255}},
{&Color{&PixelFormat16bit, &ColorMap{}, 0, 65535, 4095, 255}, []byte{255, 255}},
// 32 BPP
{&Color{&PixelFormat32bit, &ColorMap{}, 0, 0, 0, 0}, []byte{0, 0, 0, 0}},
{&Color{&PixelFormat32bit, &ColorMap{}, 0, 127, 0, 0}, []byte{0, 0, 0, 127}},
{&Color{&PixelFormat32bit, &ColorMap{}, 0, 32767, 127, 0}, []byte{0, 0, 127, 255}},
{&Color{&PixelFormat32bit, &ColorMap{}, 0, 65535, 32767, 127}, []byte{0, 127, 255, 255}},
{&Color{&PixelFormat32bit, &ColorMap{}, 0, 65535, 65535, 32767}, []byte{127, 255, 255, 255}},
{&Color{&PixelFormat32bit, &ColorMap{}, 0, 65535, 65535, 65535}, []byte{255, 255, 255, 255}},
}
for i, tt := range tests {
data, err := tt.c.Marshal()
if err != nil {
t.Errorf("%v: unexpected error: %v", i, err)
continue
}
if got, want := data, tt.data; !operators.EqualSlicesOfByte(got, want) {
t.Errorf("%v: incorrect result; got = %v, want = %v", i, got, want)
}
}
}
func TestColor_Unmarshal(t *testing.T) {
var cm ColorMap
for i := 0; i < len(cm); i++ {
cm[i] = Color{R: uint16(i), G: uint16(i << 4), B: uint16(i << 8)}
}
tests := []struct {
data []byte
pf *PixelFormat
cm *ColorMap
cmIndex uint32
R, G, B uint16
}{
// 8 BPP, with ColorMap
{[]byte{0}, &PixelFormat8bit, &cm, 0, 0, 0, 0},
{[]byte{127}, &PixelFormat8bit, &cm, 127, 127, 2032, 32512},
{[]byte{255}, &PixelFormat8bit, &cm, 255, 255, 4080, 65280},
// 16 BPP
{[]byte{0, 0}, &PixelFormat16bit, &ColorMap{}, 0, 0, 0, 0},
{[]byte{0, 127}, &PixelFormat16bit, &ColorMap{}, 0, 127, 7, 0},
{[]byte{127, 255}, &PixelFormat16bit, &ColorMap{}, 0, 32767, 2047, 127},
{[]byte{255, 255}, &PixelFormat16bit, &ColorMap{}, 0, 65535, 4095, 255},
// 32 BPP
{[]byte{0, 0, 0, 0}, &PixelFormat32bit, &ColorMap{}, 0, 0, 0, 0},
{[]byte{0, 0, 0, 127}, &PixelFormat32bit, &ColorMap{}, 0, 127, 0, 0},
{[]byte{0, 0, 127, 255}, &PixelFormat32bit, &ColorMap{}, 0, 32767, 127, 0},
{[]byte{0, 127, 255, 255}, &PixelFormat32bit, &ColorMap{}, 0, 65535, 32767, 127},
{[]byte{127, 255, 255, 255}, &PixelFormat32bit, &ColorMap{}, 0, 65535, 65535, 32767},
{[]byte{255, 255, 255, 255}, &PixelFormat32bit, &ColorMap{}, 0, 65535, 65535, 65535},
}
for i, tt := range tests {
color := NewColor(tt.pf, tt.cm)
if err := color.Unmarshal(tt.data); err != nil {
t.Errorf("%v: unexpected error: %v", i, err)
continue
}
if got, want := color.cmIndex, tt.cmIndex; got != want {
t.Errorf("%v: incorrect cmIndex value; got = %v, want = %v", i, got, want)
}
if got, want := color.R, tt.R; got != want {
t.Errorf("%v: incorrect R value; got = %v, want = %v", i, got, want)
}
if got, want := color.G, tt.G; got != want {
t.Errorf("%v: incorrect G value; got = %v, want = %v", i, got, want)
}
if got, want := color.B, tt.B; got != want {
t.Errorf("%v: incorrect B value; got = %v, want = %v", i, got, want)
}
}
}
func TestSetColorMapEntries(t *testing.T) {}
func TestBell(t *testing.T) {}
func TestServerCutText(t *testing.T) {}