-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
236 lines (206 loc) · 5.04 KB
/
encode_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
225
226
227
228
229
230
231
232
233
234
235
236
package gomsgpack
import (
"encoding/hex"
"fmt"
"reflect"
"testing"
)
type opacity uint8
func (o *opacity) Type() int8 {
return 1
}
func (*opacity) MarshalMsgPack() ([]byte, error) {
return []byte{byte(0x3d)}, nil
}
func (o *opacity) UnmarshalMsgPack(data []byte) error {
*o = opacity(data[0])
return nil
}
type position struct {
X int8
Y int8
}
func (o *position) Type() int8 {
return 2
}
func (p *position) MarshalMsgPack() ([]byte, error) {
return []byte{byte(0x12), byte(0x34)}, nil
}
func (o *position) UnmarshalMsgPack(data []byte) error {
*o = position{
X: int8(data[0]),
Y: int8(data[1]),
}
return nil
}
type marshalerErr struct {
X int8
Y int8
}
func (o *marshalerErr) Type() int8 {
return 3
}
func (p *marshalerErr) MarshalMsgPack() ([]byte, error) {
return nil, fmt.Errorf("marshaler error")
}
func (p *marshalerErr) UnmarshalMsgPack(data []byte) error {
return fmt.Errorf("unmarshaler error")
}
type xy struct {
X int8
Y int8
}
func (o xy) Type() int8 {
return 4
}
func (p xy) MarshalMsgPack() ([]byte, error) {
return []byte{0xff}, nil
}
type ext16 [16]uint8
func (e *ext16) Type() int8 {
return 5
}
func (e *ext16) MarshalMsgPack() ([]byte, error) {
return []byte(e[:]), nil
}
func (e *ext16) UnmarshalMsgPack(data []byte) error {
var v [16]uint8
copy(v[:], data)
*e = v
return nil
}
type Optionals struct {
Str Pair `msgpack:"pair"`
Sta Pair `msgpack:",array"`
Stw Pair `msgpack:"-"`
}
func TestEncodeOptionals(t *testing.T) {
o := Optionals{
Str: Pair{X: 0x12, Y: 0x34},
Sta: Pair{X: 0x56, Y: 0x78},
Stw: Pair{X: 0x9a, Y: 0xbc},
}
got, err := Marshal(o, false)
if err != nil {
t.Fatal(err)
}
actual := hex.EncodeToString(got)
expect := "82a47061697282a15812a15934a3537461925678"
if actual != expect {
t.Errorf("got : %s\nwant: %s\n", actual, expect)
}
var p, expect2 Optionals
err = Unmarshal(got, &p)
expect2.Str = o.Str
expect2.Sta = o.Sta
if reflect.DeepEqual(o, expect2) {
t.Errorf("got : %v\nwant: %v %T\n", p, expect2, p.Stw.X)
}
}
type SamePointerNoCycle struct {
Ptr1, Ptr2 *SamePointerNoCycle
}
type PointerCycle struct {
Ptr *PointerCycle
}
var pointerCycle = &PointerCycle{}
type PointerCycleIndirect struct {
Ptrs []any
}
type RecursiveSlice []RecursiveSlice
func TestSamePointerNoCycle(t *testing.T) {
var samePointerNoCycle = &SamePointerNoCycle{}
ptr := &SamePointerNoCycle{}
samePointerNoCycle.Ptr1 = ptr
samePointerNoCycle.Ptr2 = ptr
if _, err := Marshal(samePointerNoCycle, false); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestSliceNoCycle(t *testing.T) {
var sliceNoCycle = []any{nil, nil}
sliceNoCycle[1] = sliceNoCycle[:1]
for i := startDetectingCyclesAfter; i > 0; i-- {
sliceNoCycle = []any{sliceNoCycle}
}
if _, err := Marshal(sliceNoCycle, false); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestUnsupportedValues(t *testing.T) {
pointerCycleIndirect := &PointerCycleIndirect{}
mapCycle := make(map[string]any)
sliceCycle := []any{nil}
recursiveSliceCycle := []RecursiveSlice{nil}
pointerCycle.Ptr = pointerCycle
pointerCycleIndirect.Ptrs = []any{pointerCycleIndirect}
sliceCycle[0] = sliceCycle
recursiveSliceCycle[0] = recursiveSliceCycle
mapCycle["x"] = mapCycle
unsupportedValues := []any{
pointerCycle,
pointerCycleIndirect,
mapCycle,
sliceCycle,
recursiveSliceCycle,
}
for i, v := range unsupportedValues {
if _, err := Marshal(v, false); err != nil {
if _, ok := err.(*UnsupportedValueError); !ok {
t.Errorf("for %v, got %T want UnsupportedValueError", v, err)
}
} else {
t.Errorf("#%d:for %v, expected error", i, v)
}
}
}
type marshalPanic struct{}
func (marshalPanic) MarshalMsgPack() ([]byte, error) { panic(0xdead) }
func (marshalPanic) Type() int8 { return 0x44 }
func TestMarshalPanic(t *testing.T) {
defer func() {
if got := recover(); !reflect.DeepEqual(got, 0xdead) {
t.Errorf("panic() = (%T)(%v), want 0xdead", got, got)
}
}()
Marshal(&marshalPanic{}, false)
t.Error("Marshal should have panicked")
}
func TestEncodeMarshalerError(t *testing.T) {
v := new(marshalerErr)
_, err := Marshal(v, false)
rv := reflect.ValueOf(v)
expect := &MarshalerError{rv.Type(), fmt.Errorf("marshaler error")}
if !equalError(err, expect) {
t.Errorf("expect '%v' but, got '%v'", expect, err)
}
}
func TestEncodeFixExt(t *testing.T) {
{ // fixext 1
v := new(opacity)
data, _ := Marshal(v, false)
expect := "d4013d"
actual := hex.EncodeToString(data)
if actual != expect {
t.Errorf("expect '%s' but, got '%s'", expect, actual)
}
}
{ // fixext 2
v := new(position)
data, _ := Marshal(v, false)
expect := "d5021234"
actual := hex.EncodeToString(data)
if actual != expect {
t.Errorf("expect '%s' but, got '%s'", expect, actual)
}
}
{ // fixext 16
v := &ext16{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef}
data, _ := Marshal(v, false)
expect := "d8050123456789abcdef0123456789abcdef"
actual := hex.EncodeToString(data)
if actual != expect {
t.Errorf("expect '%s' but, got '%s'", expect, actual)
}
}
}