-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_test.go
303 lines (292 loc) · 6.88 KB
/
decode_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package protowire
import (
"reflect"
"testing"
"github.com/convto/protowire/testdata"
"github.com/golang/protobuf/proto"
)
func TestUnmarshal(t *testing.T) {
testVarintBin, _ := proto.Marshal(&testdata.TestVarint{
Int32: 12345,
Int64: 67890,
Boolean: true,
})
type testVarint struct {
Int32 int32 `protowire:"1,0,int32,optional"`
Int64 int64 `protowire:"2,0,int64,optional"`
Boolean bool `protowire:"3,0,bool,optional"`
}
testVarintZigzagBin, _ := proto.Marshal(&testdata.TestVarintZigzag{
Sint32: -12345,
Sint64: -67890,
})
type testVarintZigzag struct {
Sint32 int32 `protowire:"1,0,sint32,optional"`
Sint64 int64 `protowire:"2,0,sint64,optional"`
}
testLengthDelimitedBin, _ := proto.Marshal(&testdata.TestLengthDelimited{
Str: "これはてすとだよ",
Bytes: []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA},
})
type testLengthDelimited struct {
Str string `protowire:"1,2,string,optional"`
Bytes []byte `protowire:"2,2,bytes,optional"`
}
test64BitBin, _ := proto.Marshal(&testdata.Test64Bit{
Fixed64: 12345,
Sfixed64: -67890,
Double: 1.23456789,
})
type test64Bit struct {
Fixed64 uint64 `protowire:"1,1,fixed64,optional"`
Sfixed64 int64 `protowire:"2,1,sfixed64,optional"`
Double float64 `protowire:"3,1,double,optional"`
}
test32BitBin, _ := proto.Marshal(&testdata.Test32Bit{
Fixed32: 12345,
Sfixed32: -67890,
Float: 1.23456789,
})
type test32Bit struct {
Fixed32 uint32 `protowire:"1,5,fixed32,optional"`
Sfixed32 int32 `protowire:"2,5,sfixed32,optional"`
Float float32 `protowire:"3,5,float,optional"`
}
testEmbedBin, _ := proto.Marshal(&testdata.TestEmbed{
EmbedVarint: &testdata.TestVarint{
Int32: -12345,
Int64: -67890,
Boolean: true,
},
EmbedLengthDelimited: &testdata.TestLengthDelimited{
Str: "これはてすとだよ🐛",
Bytes: []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA},
},
Embed64Bit: &testdata.Test64Bit{
Fixed64: 12345,
Sfixed64: -67890,
Double: 1.23456789,
},
})
type testEmbed struct {
TestVarint *testVarint `protowire:"1,2,embed,optional"`
TestLengthDelimited *testLengthDelimited `protowire:"2,2,embed,optional"`
Test64Bit *test64Bit `protowire:"3,2,embed,optional"`
}
testRepeatedBin, _ := proto.Marshal(&testdata.TestRepeated{
Int64: []int64{
12345,
67890,
-12345,
-67890,
},
Fixed64: []uint64{
12345,
67890,
},
Fixed32: []uint32{
12345,
67890,
},
Str: []string{
"これはてすとです",
"this is test",
"🐛",
},
Bytes: [][]byte{
{0x00, 0x11, 0x22, 0x33},
{0x44, 0x55, 0x66, 0x77},
{0x88, 0x99, 0xAA, 0xBB},
{0xCC, 0xDD, 0xEE, 0xFF},
},
TestLengthDelimited: []*testdata.TestLengthDelimited{
{
Str: "これはてすとだよ🐛",
Bytes: []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA},
},
{
Str: "this is test",
Bytes: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
},
},
})
type testRepeated struct {
Int64 []int64 `protowire:"1,2,int64,packed,repeated"`
Fixed64 []uint64 `protowire:"2,2,fixed64,packed,repeated"`
Fixed32 []uint32 `protowire:"3,2,fixed32,packed,repeated"`
Str []string `protowire:"4,2,string,repeated"`
Bytes [][]byte `protowire:"5,2,bytes,repeated"`
TestLengthDelimited []*testLengthDelimited `protowire:"6,2,embed,repeated"`
}
testOneOfBin, _ := proto.Marshal(&testdata.TestOneOf{
Name: "test oneof",
TestIdentifier: &testdata.TestOneOf_Id{
Id: "identifier string",
},
TestMessage: &testdata.TestOneOf_TextMessage{
TextMessage: "message string",
},
})
type args struct {
b []byte
v interface{}
}
tests := []struct {
name string
args args
want interface{}
wantErr bool
}{
{
name: "Varintの検証バイナリ",
args: args{
b: testVarintBin,
v: &testVarint{},
},
want: &testVarint{
Int32: 12345,
Int64: 67890,
Boolean: true,
},
},
{
name: "Varintでzigzagの検証バイナリ",
args: args{
b: testVarintZigzagBin,
v: &testVarintZigzag{},
},
want: &testVarintZigzag{
Sint32: -12345,
Sint64: -67890,
},
},
{
name: "Length-delimitedの検証バイナリ",
args: args{
b: testLengthDelimitedBin,
v: &testLengthDelimited{},
},
want: &testLengthDelimited{
Str: "これはてすとだよ",
Bytes: []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA},
},
},
{
name: "64-bitの検証バイナリ",
args: args{
b: test64BitBin,
v: &test64Bit{},
},
want: &test64Bit{
Fixed64: 12345,
Sfixed64: -67890,
Double: 1.23456789,
},
},
{
name: "32-bitの検証バイナリ",
args: args{
b: test32BitBin,
v: &test32Bit{},
},
want: &test32Bit{
Fixed32: 12345,
Sfixed32: -67890,
Float: 1.23456789,
},
},
{
name: "Embedの検証バイナリ",
args: args{
b: testEmbedBin,
v: &testEmbed{},
},
want: &testEmbed{
TestVarint: &testVarint{
Int32: -12345,
Int64: -67890,
Boolean: true,
},
TestLengthDelimited: &testLengthDelimited{
Str: "これはてすとだよ🐛",
Bytes: []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA},
},
Test64Bit: &test64Bit{
Fixed64: 12345,
Sfixed64: -67890,
Double: 1.23456789,
},
},
},
{
name: "Repeatedの検証バイナリ",
args: args{
b: testRepeatedBin,
v: &testRepeated{},
},
want: &testRepeated{
Int64: []int64{
12345,
67890,
-12345,
-67890,
},
Fixed64: []uint64{
12345,
67890,
},
Fixed32: []uint32{
12345,
67890,
},
Str: []string{
"これはてすとです",
"this is test",
"🐛",
},
Bytes: [][]byte{
{0x00, 0x11, 0x22, 0x33},
{0x44, 0x55, 0x66, 0x77},
{0x88, 0x99, 0xAA, 0xBB},
{0xCC, 0xDD, 0xEE, 0xFF},
},
TestLengthDelimited: []*testLengthDelimited{
{
Str: "これはてすとだよ🐛",
Bytes: []byte{0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA},
},
{
Str: "this is test",
Bytes: []byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
},
},
},
},
{
name: "oneofの検証バイナリ",
args: args{
b: testOneOfBin,
v: &testOneOf{},
},
want: &testOneOf{
Name: "test oneof",
TestIdentifier: &TestOneOf_Id{
Id: "identifier string",
},
TestMessage: &TestOneOf_TextMessage{
TextMessage: "message string",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := Unmarshal(tt.args.b, tt.args.v); (err != nil) != tt.wantErr {
t.Errorf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
if !reflect.DeepEqual(tt.args.v, tt.want) {
t.Errorf("Unmarshal() got = %v, want %v", tt.args.v, tt.want)
}
})
}
}