This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
binary_test.go
245 lines (203 loc) · 5.23 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
package amino_test
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"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, _ := time.Parse("2006-01-02", "1934-11-09")
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
cdc.UnmarshalBinaryBare(b, &inner)
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
cdc.UnmarshalBinaryBare(b, &outer)
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.NotZero(t, len(b), "amino:\"write_empty\" did not work")
b, err = cdc.MarshalBinaryBare(InnerWriteEmpty{})
assert.NoError(t, err)
t.Log(b)
// TODO(ismail): this alone won't be encoded:
//assert.NotZero(t, len(b), "amino:\"write_empty\" did not work")
}
func TestStructSlice(t *testing.T) {
type Foo struct {
A int
B int
}
type Foos []Foo
f := Foos{Foo{100, 101}, Foo{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
cdc.UnmarshalBinaryBare(bz, &f2)
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{&Foo{}, &Foo{}, &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",
}
bz, 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{&Foo{}, &Foo{}, &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])
}