-
Notifications
You must be signed in to change notification settings - Fork 15
/
protobuf_test.go
302 lines (267 loc) · 6.47 KB
/
protobuf_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
package protobuf
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type emb struct {
I32 int32
S string
}
// test custom type-aliases
type mybool bool
type myint int
type myint32 int32
type myint64 int64
type myuint32 uint32
type myuint64 uint64
type myfloat32 float32
type myfloat64 float64
type mybytes []byte
type mystring string
type test struct {
Bool bool `protobuf:"boolean,opt"`
I int
I32 int32
I64 int64
U32 uint32
U64 uint64
SX32 Sfixed32
SX64 Sfixed64
UX32 Ufixed32
UX64 Ufixed64
F32 float32
F64 float64
Bytes []byte
Array [2]byte
String string
Struct emb
OBool *mybool `protobuf:"50"`
OI32 *myint32
OI64 *myint64
OU32 *myuint32
OU64 *myuint64
OF32 *myfloat32
OF64 *myfloat64
OBytes *mybytes
OString *mystring
OStruct *test
SBool []mybool `protobuf:"100"`
SI32 []myint32
SI64 []myint64
SU32 []myuint32
SU64 []myuint64
SSX32 []Sfixed32
SSX64 []Sfixed64
SUX32 []Ufixed32
SUX64 []Ufixed64
SF32 []myfloat32
SF64 []myfloat64
SBytes []mybytes
SString []mystring
SStruct []emb
}
func eqrep(i1, i2 interface{}) bool {
return fmt.Sprintf("%v", i1) == fmt.Sprintf("%v", i2)
}
func (e1 *emb) equal(e2 *emb) bool {
return e1.I32 == e2.I32 &&
e1.S == e2.S
}
func (t1 *test) equal(t2 *test) bool {
return t1.Bool == t2.Bool && // required fields
t1.I == t2.I &&
t1.I32 == t2.I32 &&
t1.I64 == t2.I64 &&
t1.U32 == t2.U32 &&
t1.U64 == t2.U64 &&
t1.SX32 == t2.SX32 &&
t1.SX64 == t2.SX64 &&
t1.UX32 == t2.UX32 &&
t1.UX64 == t2.UX64 &&
t1.F32 == t2.F32 &&
t1.F64 == t2.F64 &&
bytes.Equal(t1.Bytes, t2.Bytes) &&
t1.String == t2.String &&
t1.Struct.equal(&t2.Struct) &&
((t1.OBool == nil && t2.OBool == nil) || // optional
(*t1.OBool == *t2.OBool)) &&
((t1.OI32 == nil && t2.OI32 == nil) ||
(*t1.OI32 == *t2.OI32)) &&
((t1.OI64 == nil && t2.OI64 == nil) ||
(*t1.OI64 == *t2.OI64)) &&
((t1.OU32 == nil && t2.OU32 == nil) ||
(*t1.OU32 == *t2.OU32)) &&
((t1.OU64 == nil && t2.OU64 == nil) ||
(*t1.OU64 == *t2.OU64)) &&
((t1.OF32 == nil && t2.OF32 == nil) ||
(*t1.OF32 == *t2.OF32)) &&
((t1.OF64 == nil && t2.OF64 == nil) ||
(*t1.OF64 == *t2.OF64)) &&
((t1.OBytes == nil && t2.OBytes == nil) ||
bytes.Equal(*t1.OBytes, *t2.OBytes)) &&
((t1.OString == nil && t2.OString == nil) ||
(*t1.OString == *t2.OString)) &&
((t1.OStruct == nil && t2.OStruct == nil) ||
(*t1.OStruct).equal(t2.OStruct)) &&
eqrep(t1.SBool, t2.SBool) && // repeated
eqrep(t1.SI32, t2.SI32) &&
eqrep(t1.SI64, t2.SI64) &&
eqrep(t1.SU32, t2.SU32) &&
eqrep(t1.SU64, t2.SU64) &&
eqrep(t1.SSX32, t2.SSX32) &&
eqrep(t1.SSX64, t2.SSX64) &&
eqrep(t1.SUX32, t2.SUX32) &&
eqrep(t1.SUX64, t2.SUX64) &&
eqrep(t1.SF32, t2.SF32) &&
eqrep(t1.SF64, t2.SF64) &&
eqrep(t1.SBytes, t2.SBytes) &&
eqrep(t1.SString, t2.SString) &&
eqrep(t1.SStruct, t2.SStruct)
}
func TestProtobuf(t *testing.T) {
b0 := mybool(true)
i1 := myint32(-1)
i2 := myint64(-2)
i3 := myuint32(3)
i4 := myuint64(4)
f5 := myfloat32(5.5)
f6 := myfloat64(6.6)
b7 := mybytes("789")
s8 := mystring("ABC")
e9 := test{Bytes: []byte{}}
t1 := test{true, 0, -1, -2, 3, 4, -11, -22, 33, 44, 5.0, 6.0,
[]byte("789"), [2]byte{1, 2}, "abc", emb{123, "def"},
&b0, &i1, &i2, &i3, &i4, &f5, &f6, &b7, &s8, &e9,
[]mybool{true, false, true},
[]myint32{1, -2, 3}, []myint64{2, -3, 4},
[]myuint32{3, 4, 5}, []myuint64{4, 5, 6},
[]Sfixed32{11, -22, 33}, []Sfixed64{22, -33, 44},
[]Ufixed32{33, 44, 55}, []Ufixed64{44, 55, 66},
[]myfloat32{5.5, 6.6, 7.7}, []myfloat64{6.6, 7.7, 8.8},
[]mybytes{[]byte("the"), []byte("quick"), []byte("brown"), []byte("fox")},
[]mystring{"the", "quick", "brown", "fox"},
[]emb{emb{-1, "a"}, emb{-2, "b"}, emb{-3, "c"}},
}
buf, err := Encode(&t1)
assert.NoError(t, err)
t2 := test{}
err = Decode(buf, &t2)
assert.NoError(t, err)
assert.Equal(t, t1, t2)
}
type simpleFilledInput struct {
Bytes []mybytes
I string
Ptr *mybool
}
func TestProtobuf_FilledInput(t *testing.T) {
b0 := mybool(true)
b1 := mybool(false)
t1 := simpleFilledInput{
[]mybytes{[]byte("the"), []byte("quick"), []byte("brown"), []byte("fox")},
"intermediate value",
&b0,
}
buf, err := Encode(&t1)
assert.NoError(t, err)
t2 := simpleFilledInput{
[]mybytes{[]byte("the"), []byte("quick"), []byte("brown"), []byte("fox")},
"intermediate value",
&b1,
}
err = Decode(buf, &t2)
assert.NoError(t, err)
assert.Equal(t, t1, t2)
t1 = simpleFilledInput{}
buf, err = Encode(&t1)
err = Decode(buf, &t2)
assert.NoError(t, err)
assert.Equal(t, t1, t2)
}
type padded struct {
Field1 int32 // = 1
_ struct{} // = 2
Field3 int32 // = 3
_ int // = 4
Field5 int32 // = 5
}
func TestPadded(t *testing.T) {
t1 := padded{}
t1.Field1 = 10
t1.Field3 = 30
t1.Field5 = 50
buf, err := Encode(&t1)
assert.NoError(t, err)
t2 := padded{}
err = Decode(buf, &t2)
if err != nil {
panic(err.Error())
}
if t1 != t2 {
panic("decode didn't reproduce identical struct")
}
}
type TimeTypes struct {
Time time.Time
Duration time.Duration
}
const shortForm = "2006-Jan-02"
func TestTimeTypesEncodeDecode(t *testing.T) {
tt, _ := time.Parse(shortForm, "2013-Feb-03")
in := &TimeTypes{
Time: tt,
Duration: time.Second * 30,
}
buf, err := Encode(in)
assert.NoError(t, err)
out := &TimeTypes{}
err = Decode(buf, out)
assert.NoError(t, err)
assert.Equal(t, in.Time.UnixNano(), out.Time.UnixNano())
assert.Equal(t, in.Duration, out.Duration)
}
/*encoding of testMsg is equivalent to the encoding to the following in*/
//a .proto file:
// message cipherText {
// int32 a = 1;
// int32 b = 2;
// }
// message MapFieldEntry {
// uint32 key = 1;
// cipherText value = 2;
// }
// message testMsg {
// repeated MapFieldEntry map_field = 1;
// }
//for details see:
/*https://developers.google.com/protocol-buffers/docs/proto#backwards-compatibility*/
type wrongTestMsg struct {
M map[uint32][]cipherText
}
type rightTestMsg struct {
M map[uint32]*cipherText
}
type cipherText struct {
A, B int32
}
func TestMapSliceStruct(t *testing.T) {
cv := []cipherText{{}, {}}
msg := &wrongTestMsg{
M: map[uint32][]cipherText{1: cv},
}
_, err := Encode(msg)
assert.Error(t, err)
msg2 := &rightTestMsg{
M: map[uint32]*cipherText{1: {4, 5}},
}
buff, err := Encode(msg2)
assert.NoError(t, err)
dec := &rightTestMsg{}
err = Decode(buff, dec)
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(dec, msg2))
}