forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_codec_test.go
133 lines (112 loc) · 3.35 KB
/
proto_codec_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
package codec_test
import (
"errors"
"fmt"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
func createTestInterfaceRegistry() types.InterfaceRegistry {
interfaceRegistry := types.NewInterfaceRegistry()
interfaceRegistry.RegisterInterface("testdata.Animal",
(*testdata.Animal)(nil),
&testdata.Dog{},
&testdata.Cat{},
)
return interfaceRegistry
}
func TestProtoMarsharlInterface(t *testing.T) {
cdc := codec.NewProtoCodec(createTestInterfaceRegistry())
m := interfaceMarshaler{cdc.MarshalInterface, cdc.UnmarshalInterface}
testInterfaceMarshaling(require.New(t), m, false)
m = interfaceMarshaler{cdc.MarshalInterfaceJSON, cdc.UnmarshalInterfaceJSON}
testInterfaceMarshaling(require.New(t), m, false)
}
func TestProtoCodec(t *testing.T) {
cdc := codec.NewProtoCodec(createTestInterfaceRegistry())
testMarshaling(t, cdc)
}
type lyingProtoMarshaler struct {
codec.ProtoMarshaler
falseSize int
}
func (lpm *lyingProtoMarshaler) Size() int {
return lpm.falseSize
}
func TestProtoCodecUnmarshalLengthPrefixedChecks(t *testing.T) {
cdc := codec.NewProtoCodec(createTestInterfaceRegistry())
truth := &testdata.Cat{Lives: 9, Moniker: "glowing"}
realSize := len(cdc.MustMarshal(truth))
falseSizes := []int{
100,
5,
}
for _, falseSize := range falseSizes {
falseSize := falseSize
t.Run(fmt.Sprintf("ByMarshaling falseSize=%d", falseSize), func(t *testing.T) {
lpm := &lyingProtoMarshaler{
ProtoMarshaler: &testdata.Cat{Lives: 9, Moniker: "glowing"},
falseSize: falseSize,
}
var serialized []byte
require.NotPanics(t, func() { serialized = cdc.MustMarshalLengthPrefixed(lpm) })
recv := new(testdata.Cat)
gotErr := cdc.UnmarshalLengthPrefixed(serialized, recv)
var wantErr error
if falseSize > realSize {
wantErr = fmt.Errorf("not enough bytes to read; want: %d, got: %d", falseSize, realSize)
} else {
wantErr = fmt.Errorf("too many bytes to read; want: %d, got: %d", falseSize, realSize)
}
require.Equal(t, gotErr, wantErr)
})
}
t.Run("Crafted bad uvarint size", func(t *testing.T) {
crafted := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}
recv := new(testdata.Cat)
gotErr := cdc.UnmarshalLengthPrefixed(crafted, recv)
require.Equal(t, gotErr, errors.New("invalid number of bytes read from length-prefixed encoding: -10"))
require.Panics(t, func() { cdc.MustUnmarshalLengthPrefixed(crafted, recv) })
})
}
func mustAny(msg proto.Message) *types.Any {
any, err := types.NewAnyWithValue(msg)
if err != nil {
panic(err)
}
return any
}
func BenchmarkProtoCodecMarshalLengthPrefixed(b *testing.B) {
var pCdc = codec.NewProtoCodec(types.NewInterfaceRegistry())
var msg = &testdata.HasAnimal{
X: 1000,
Animal: mustAny(&testdata.HasAnimal{
X: 2000,
Animal: mustAny(&testdata.HasAnimal{
X: 3000,
Animal: mustAny(&testdata.HasAnimal{
X: 4000,
Animal: mustAny(&testdata.HasAnimal{
X: 5000,
Animal: mustAny(&testdata.Cat{
Moniker: "Garfield",
Lives: 6,
}),
}),
}),
}),
}),
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
blob, err := pCdc.MarshalLengthPrefixed(msg)
if err != nil {
b.Fatal(err)
}
b.SetBytes(int64(len(blob)))
}
}