forked from tendermint/go-amino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_test.go
316 lines (261 loc) · 7 KB
/
binary_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
304
305
306
307
308
309
310
311
312
313
314
315
316
package amino_test
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
amino "github.com/tendermint/go-amino"
)
func TestNilSliceEmptySlice(t *testing.T) {
var cdc = amino.NewCodec()
type TestStruct struct {
A []byte
B []int
C [][]byte
D [][]int
E []*[]byte
F []*[]int
}
nnb, nni := []byte(nil), []int(nil)
eeb, eei := []byte{}, []int{}
a := TestStruct{
A: nnb,
B: nni,
C: [][]byte{nnb},
D: [][]int{nni},
E: []*[]byte{nil},
F: []*[]int{nil},
}
b := TestStruct{
A: eeb,
B: eei,
C: [][]byte{eeb},
D: [][]int{eei},
E: []*[]byte{&nnb},
F: []*[]int{&nni},
}
c := TestStruct{
A: eeb,
B: eei,
C: [][]byte{eeb},
D: [][]int{eei},
E: []*[]byte{&eeb},
F: []*[]int{&eei},
}
abz := cdc.MustMarshalBinaryLengthPrefixed(a)
bbz := cdc.MustMarshalBinaryLengthPrefixed(b)
cbz := cdc.MustMarshalBinaryLengthPrefixed(c)
assert.Equal(t, abz, bbz, "a != b")
assert.Equal(t, abz, cbz, "a != c")
}
func TestNewFieldBackwardsCompatibility(t *testing.T) {
type V1 struct {
String string
String2 string
}
type V2 struct {
String string
String2 string
// new fields in V2:
Time time.Time
Int int
}
type SomeStruct struct {
Sth int
}
type V3 struct {
String string
// different from V1 starting here:
Int int
Some SomeStruct
}
cdc := amino.NewCodec()
notNow, err := time.Parse("2006-01-02", "1934-11-09")
assert.NoError(t, err)
v2 := V2{String: "hi", String2: "cosmos", Time: notNow, Int: 4}
bz, err := cdc.MarshalBinaryBare(v2)
assert.Nil(t, err, "unexpected error while encoding V2: %v", err)
var v1 V1
err = cdc.UnmarshalBinaryBare(bz, &v1)
assert.Nil(t, err, "unexpected error %v", err)
assert.Equal(t, v1, V1{"hi", "cosmos"},
"backwards compatibility failed: didn't yield expected result ...")
v3 := V3{String: "tender", Int: 2014, Some: SomeStruct{Sth: 84}}
bz2, err := cdc.MarshalBinaryBare(v3)
assert.Nil(t, err, "unexpected error")
err = cdc.UnmarshalBinaryBare(bz2, &v1)
// this might change later but we include this case to document the current behaviour:
assert.NotNil(t, err, "expected an error here because of changed order of fields")
// we still expect that decoding worked to some extend (until above error occurred):
assert.Equal(t, v1, V1{"tender", "cosmos"})
}
func TestWriteEmpty(t *testing.T) {
type Inner struct {
Val int
}
type SomeStruct struct {
Inner Inner
}
cdc := amino.NewCodec()
b, err := cdc.MarshalBinaryBare(Inner{})
assert.NoError(t, err)
assert.Equal(t, b, []byte(nil), "empty struct should be encoded as empty bytes")
var inner Inner
err = cdc.UnmarshalBinaryBare(b, &inner)
require.NoError(t, err)
assert.Equal(t, Inner{}, inner, "")
b, err = cdc.MarshalBinaryBare(SomeStruct{})
assert.NoError(t, err)
assert.Equal(t, b, []byte(nil), "empty structs should be encoded as empty bytes")
var outer SomeStruct
err = cdc.UnmarshalBinaryBare(b, &outer)
require.NoError(t, err)
assert.Equal(t, SomeStruct{}, outer, "")
}
func TestForceWriteEmpty(t *testing.T) {
type InnerWriteEmpty struct {
// sth. that isn't zero-len if default, e.g. fixed32:
ValIn int32 `amino:"write_empty" binary:"fixed32"`
}
type OuterWriteEmpty struct {
In InnerWriteEmpty `amino:"write_empty"`
Val int `amino:"write_empty" binary:"fixed32"`
}
cdc := amino.NewCodec()
b, err := cdc.MarshalBinaryBare(OuterWriteEmpty{})
assert.NoError(t, err)
assert.Equal(t, []byte{10, 5, 13, 0, 0, 0, 0, 16, 0}, b)
b, err = cdc.MarshalBinaryBare(InnerWriteEmpty{})
assert.NoError(t, err)
assert.Equal(t, []byte{13, 0, 0, 0, 0}, b)
}
func TestStructSlice(t *testing.T) {
type Foo struct {
A int
B int
}
type Foos struct {
Fs []Foo
}
f := Foos{Fs: []Foo{{100, 101}, {102, 103}}}
cdc := amino.NewCodec()
bz, err := cdc.MarshalBinaryBare(f)
assert.NoError(t, err)
assert.Equal(t, "0A04086410650A0408661067", fmt.Sprintf("%X", bz))
t.Log(bz)
var f2 Foos
err = cdc.UnmarshalBinaryBare(bz, &f2)
require.NoError(t, err)
assert.Equal(t, f, f2)
}
func TestStructPointerSlice1(t *testing.T) {
cdc := amino.NewCodec()
type Foo struct {
A string
B int
C []*Foo
D string // exposed
}
var f = Foo{
A: "k",
B: 2,
C: []*Foo{nil, nil, nil},
D: "j",
}
bz, err := cdc.MarshalBinaryLengthPrefixed(f)
assert.NoError(t, err)
var f2 Foo
err = cdc.UnmarshalBinaryLengthPrefixed(bz, &f2)
assert.Nil(t, err)
assert.Equal(t, f, f2)
assert.Nil(t, f2.C[0])
var f3 = Foo{
A: "k",
B: 2,
C: []*Foo{{}, {}, {}},
D: "j",
}
bz2, err := cdc.MarshalBinaryLengthPrefixed(f3)
assert.NoError(t, err)
assert.Equal(t, bz, bz2, "empty slices should be decoded to nil unless empty_elements")
}
// Like TestStructPointerSlice2, but with EmptyElements.
func TestStructPointerSlice2(t *testing.T) {
cdc := amino.NewCodec()
type Foo struct {
A string
B int
C []*Foo `amino:"empty_elements"`
D string // exposed
}
var f = Foo{
A: "k",
B: 2,
C: []*Foo{nil, nil, nil},
D: "j",
}
_, err := cdc.MarshalBinaryLengthPrefixed(f)
assert.Error(t, err, "nil elements of a slice/array not supported when empty_elements field tag set.")
f.C = []*Foo{{}, {}, {}}
bz, err := cdc.MarshalBinaryLengthPrefixed(f)
assert.NoError(t, err)
var f2 Foo
err = cdc.UnmarshalBinaryLengthPrefixed(bz, &f2)
assert.Nil(t, err)
assert.Equal(t, f, f2)
assert.NotNil(t, f2.C[0])
}
func TestBasicTypes(t *testing.T) {
// we explicitly disallow type definitions like the following:
type byteAlias []byte
cdc := amino.NewCodec()
ba := byteAlias([]byte("this should work because it gets wrapped by a struct"))
bz, err := cdc.MarshalBinaryLengthPrefixed(ba)
assert.NotZero(t, bz)
require.NoError(t, err)
res := &byteAlias{}
err = cdc.UnmarshalBinaryLengthPrefixed(bz, res)
require.NoError(t, err)
assert.Equal(t, ba, *res)
}
func TestUnmarshalMapBinary(t *testing.T) {
obj := new(map[string]int)
cdc := amino.NewCodec()
// Binary doesn't support decoding to a map...
binBytes := []byte(`dontcare`)
assert.Panics(t, func() {
err := cdc.UnmarshalBinaryBare(binBytes, &obj)
assert.Fail(t, "should have paniced but got err: %v", err)
})
assert.Panics(t, func() {
err := cdc.UnmarshalBinaryBare(binBytes, obj)
require.Error(t, err)
})
// ... nor encoding it.
assert.Panics(t, func() {
bz, err := cdc.MarshalBinaryBare(obj)
assert.Fail(t, "should have paniced but got bz: %X err: %v", bz, err)
})
}
func TestUnmarshalFuncBinary(t *testing.T) {
obj := func() {}
cdc := amino.NewCodec()
// Binary doesn't support decoding to a func...
binBytes := []byte(`dontcare`)
err := cdc.UnmarshalBinaryLengthPrefixed(binBytes, &obj)
// on length prefixed we return an error:
assert.Error(t, err)
assert.Panics(t, func() {
err = cdc.UnmarshalBinaryBare(binBytes, &obj)
require.Error(t, err)
})
err = cdc.UnmarshalBinaryBare(binBytes, obj)
require.Error(t, err)
require.Equal(t, err, amino.ErrNoPointer)
// ... nor encoding it.
assert.Panics(t, func() {
bz, err := cdc.MarshalBinaryLengthPrefixed(obj)
assert.Fail(t, "should have paniced but got bz: %X err: %v", bz, err)
})
}