-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproto_test.go
134 lines (115 loc) · 2.29 KB
/
proto_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
package gobroke
import (
"testing"
"github.com/RoanBrand/gobroke/internal/model"
)
func TestVariableLengthEncoding(t *testing.T) {
t.Parallel()
l := 0
ve := model.VariableLengthEncode([]byte{}, l)
if len(ve) != 1 || ve[0] != 0 {
t.Fatal(l)
}
l = 127
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 1 || ve[0] != 127 {
t.Fatal(l)
}
l = 128
e := []byte{0x80, 0x01, 0, 0}
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 2 {
t.Fatal(l)
}
for i, b := range ve {
if b != e[i] {
t.Fatal(l)
}
}
l = 16383
e[0], e[1] = 0xFF, 0x7F
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 2 {
t.Fatal(l)
}
for i, b := range ve {
if b != e[i] {
t.Fatal(l)
}
}
l = 16384
e[0], e[1], e[2] = 0x80, 0x80, 0x01
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 3 {
t.Fatal(l)
}
for i, b := range ve {
if b != e[i] {
t.Fatal(l)
}
}
l = 2097151
e[0], e[1], e[2] = 0xFF, 0xFF, 0x7F
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 3 {
t.Fatal(l)
}
for i, b := range ve {
if b != e[i] {
t.Fatal(l)
}
}
l = 2097152
e[0], e[1], e[2], e[3] = 0x80, 0x80, 0x80, 0x01
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 4 {
t.Fatal(l)
}
for i, b := range ve {
if b != e[i] {
t.Fatal(l)
}
}
l = 268435455
e[0], e[1], e[2], e[3] = 0xFF, 0xFF, 0xFF, 0x7F
ve = model.VariableLengthEncode(ve[:0], l)
if len(ve) != 4 {
t.Fatal(l)
}
for i, b := range ve {
if b != e[i] {
t.Fatal(l)
}
}
}
func TestUTF8(t *testing.T) {
t.Parallel()
// U+0000 invalid
if err := checkUTF8([]byte{0x00}, false); err == nil {
t.Fatal(0)
}
// U+D7FF valid
if err := checkUTF8([]byte{0xED, 0x9F, 0xBF, 0x31}, false); err != nil {
t.Fatal(1, err)
}
// U+D800 invalid
if err := checkUTF8([]byte{0xED, 0xA0, 0x80}, false); err == nil {
t.Fatal(3)
}
// U+DFFF invalid
if err := checkUTF8([]byte{0xED, 0xBF, 0xBF}, false); err == nil {
t.Fatal(4)
}
// U+E000 valid
if err := checkUTF8([]byte{0xEE, 0x80, 0x80}, false); err != nil {
t.Fatal(5, err)
}
// U+0001, U+FEFF valid
if err := checkUTF8([]byte{0x01, 0xEF, 0xBB, 0xBF, 0x59}, false); err != nil {
t.Fatal(6, err)
}
// U+0001, U+FEFF, U+0000 invalid
if err := checkUTF8([]byte{0x01, 0xEF, 0xBB, 0xBF, 0x59, 0}, false); err == nil {
t.Fatal(6)
}
}