forked from mitchellh/go-vnc
-
Notifications
You must be signed in to change notification settings - Fork 16
/
pixel_format_test.go
145 lines (135 loc) · 3.57 KB
/
pixel_format_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
package vnc
import (
"bytes"
"reflect"
"testing"
"github.com/kward/go-vnc/go/operators"
"github.com/kward/go-vnc/rfbflags"
)
const (
// Shadow the RFBFlag constants.
RFBFalse = rfbflags.RFBFalse
RFBTrue = rfbflags.RFBTrue
)
func TestPixelFormat_Marshal(t *testing.T) {
tests := []struct {
pf PixelFormat
b []byte
ok bool
}{
//
// Valid PixelFormats.
//
{PixelFormat{BPP: 8, Depth: 8, BigEndian: RFBTrue, TrueColor: RFBFalse},
[]uint8{8, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
{PixelFormat{BPP: 8, Depth: 16, BigEndian: RFBTrue, TrueColor: RFBFalse},
[]uint8{8, 16, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, true},
{NewPixelFormat(16),
[]uint8{16, 16, 1, 1, 255, 255, 255, 255, 255, 255, 0, 4, 8, 0, 0, 0}, true},
//
// Invalid PixelFormats.
//
// BPP invalid
{PixelFormat{BPP: 1, Depth: 1, BigEndian: RFBTrue, TrueColor: RFBFalse},
[]uint8{}, false},
// Depth invalid
{PixelFormat{BPP: 8, Depth: 1, BigEndian: RFBTrue, TrueColor: RFBFalse},
[]uint8{}, false},
// BPP > Depth
{PixelFormat{BPP: 16, Depth: 8, BigEndian: RFBTrue, TrueColor: RFBFalse},
[]uint8{}, false},
}
for _, tt := range tests {
pf := tt.pf
b, err := pf.Marshal()
if err == nil && !tt.ok {
t.Error("expected error")
}
if err != nil {
if verr, ok := err.(*VNCError); !ok {
t.Errorf("unexpected %v error: %v", reflect.TypeOf(err), verr)
}
}
if !tt.ok {
continue
}
if got, want := b, tt.b; !operators.EqualSlicesOfByte(got, want) {
t.Errorf("invalid pixel-format; got = %v, want = %v", got, want)
}
}
}
func TestPixelFormat_Unmarshal(t *testing.T) {
tests := []struct {
b []byte
pf PixelFormat
ok bool
}{
//
// Valid PixelFormats.
//
{[]uint8{8, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
PixelFormat{BPP: 8, Depth: 8, BigEndian: RFBTrue, TrueColor: RFBFalse},
true},
{[]uint8{8, 16, 1, 1, 255, 255, 255, 255, 255, 255, 0, 4, 8, 0, 0, 0},
PixelFormat{
BPP: 8, Depth: 16,
BigEndian: RFBTrue, TrueColor: RFBTrue,
RedMax: 65535, GreenMax: 65535, BlueMax: 65535,
RedShift: 0, GreenShift: 4, BlueShift: 8},
true},
{[]uint8{16, 16, 1, 1, 255, 255, 255, 255, 255, 255, 0, 4, 8, 0, 0, 0},
NewPixelFormat(16), true},
{[]uint8{32, 32, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
PixelFormat{BPP: 32, Depth: 32, BigEndian: RFBTrue, TrueColor: RFBFalse},
true},
//
// Invalid PixelFormats.
//
}
for _, tt := range tests {
var buf bytes.Buffer
buf.Write(tt.b)
var pf PixelFormat
err := pf.Unmarshal(buf.Bytes())
if err == nil && !tt.ok {
t.Error("expected error")
}
if err != nil {
if verr, ok := err.(*VNCError); !ok {
t.Errorf("unexpected %v error: %v", reflect.TypeOf(err), verr)
}
}
if !tt.ok {
continue
}
if got, want := pf, tt.pf; !equalPixelFormat(got, want) {
t.Errorf("invalid pixel-format; got = %v, want = %v", pf, tt.pf)
}
}
}
func TestPixelFormat_String(t *testing.T) {
for _, tt := range []struct {
desc string
pf PixelFormat
str string
}{
{"8bpp-8depth",
PixelFormat{BPP: 8, Depth: 8, BigEndian: RFBTrue, TrueColor: RFBFalse},
"{ bpp: 8 depth: 8 big-endian: RFBTrue true-color: RFBFalse red-max: 0 green-max: 0 blue-max: 0 red-shift: 0 green-shift: 0 blue-shift: 0 }"},
} {
if got, want := tt.pf.String(), tt.str; got != want {
t.Errorf("%s: string() = %q, want = %q", tt.desc, got, want)
}
}
}
func equalPixelFormat(g, w PixelFormat) bool {
got, err := g.Marshal()
if err != nil {
return false
}
want, err := w.Marshal()
if err != nil {
return false
}
return operators.EqualSlicesOfByte(got, want)
}