## Description Closes: #12894 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [x] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [x] included comments for [documenting Go code](https://blog.golang.org/godoc) - [x] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
199 lines
5.2 KiB
Go
199 lines
5.2 KiB
Go
package codec_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"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 TestEnsureRegistered(t *testing.T) {
|
|
interfaceRegistry := types.NewInterfaceRegistry()
|
|
cat := &testdata.Cat{Moniker: "Garfield"}
|
|
|
|
err := interfaceRegistry.EnsureRegistered(*cat)
|
|
require.ErrorContains(t, err, "testdata.Cat is not a pointer")
|
|
|
|
err = interfaceRegistry.EnsureRegistered(cat)
|
|
require.ErrorContains(t, err, "testdata.Cat does not have a registered interface")
|
|
|
|
interfaceRegistry.RegisterInterface("testdata.Animal",
|
|
(*testdata.Animal)(nil),
|
|
&testdata.Cat{},
|
|
)
|
|
|
|
require.NoError(t, interfaceRegistry.EnsureRegistered(cat))
|
|
}
|
|
|
|
func TestProtoCodecMarshal(t *testing.T) {
|
|
interfaceRegistry := types.NewInterfaceRegistry()
|
|
interfaceRegistry.RegisterInterface("testdata.Animal",
|
|
(*testdata.Animal)(nil),
|
|
&testdata.Cat{},
|
|
)
|
|
cdc := codec.NewProtoCodec(interfaceRegistry)
|
|
|
|
cartonRegistry := types.NewInterfaceRegistry()
|
|
cartonRegistry.RegisterInterface("testdata.Cartoon",
|
|
(*testdata.Cartoon)(nil),
|
|
&testdata.Bird{},
|
|
)
|
|
cartoonCdc := codec.NewProtoCodec(cartonRegistry)
|
|
|
|
cat := &testdata.Cat{Moniker: "Garfield", Lives: 6}
|
|
bird := &testdata.Bird{Species: "Passerina ciris"}
|
|
require.NoError(t, interfaceRegistry.EnsureRegistered(cat))
|
|
|
|
var (
|
|
animal testdata.Animal
|
|
cartoon testdata.Cartoon
|
|
)
|
|
|
|
// sanity check
|
|
require.True(t, reflect.TypeOf(cat).Implements(reflect.TypeOf((*testdata.Animal)(nil)).Elem()))
|
|
|
|
bz, err := cdc.MarshalInterface(cat)
|
|
require.NoError(t, err)
|
|
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.NoError(t, err)
|
|
|
|
bz, err = cdc.MarshalInterface(bird)
|
|
require.ErrorContains(t, err, "does not have a registered interface")
|
|
|
|
bz, err = cartoonCdc.MarshalInterface(bird)
|
|
require.NoError(t, err)
|
|
|
|
err = cdc.UnmarshalInterface(bz, &cartoon)
|
|
require.ErrorContains(t, err, "no registered implementations")
|
|
|
|
err = cartoonCdc.UnmarshalInterface(bz, &cartoon)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
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) {
|
|
pCdc := codec.NewProtoCodec(types.NewInterfaceRegistry())
|
|
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)))
|
|
}
|
|
}
|