<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9499 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### 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 - [x] 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/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] 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)
137 lines
3.7 KiB
Go
137 lines
3.7 KiB
Go
package codec_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
"github.com/cosmos/cosmos-sdk/codec/types"
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
|
)
|
|
|
|
func NewTestInterfaceRegistry() types.InterfaceRegistry {
|
|
registry := types.NewInterfaceRegistry()
|
|
registry.RegisterInterface("Animal", (*testdata.Animal)(nil))
|
|
registry.RegisterImplementations(
|
|
(*testdata.Animal)(nil),
|
|
&testdata.Dog{},
|
|
&testdata.Cat{},
|
|
)
|
|
return registry
|
|
}
|
|
|
|
func TestMarshalAny(t *testing.T) {
|
|
registry := types.NewInterfaceRegistry()
|
|
|
|
cdc := codec.NewProtoCodec(registry)
|
|
|
|
kitty := &testdata.Cat{Moniker: "Kitty"}
|
|
bz, err := cdc.MarshalInterface(kitty)
|
|
require.NoError(t, err)
|
|
|
|
var animal testdata.Animal
|
|
|
|
// empty registry should fail
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.Error(t, err)
|
|
|
|
// wrong type registration should fail
|
|
registry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Dog{})
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.Error(t, err)
|
|
|
|
// should pass
|
|
registry = NewTestInterfaceRegistry()
|
|
cdc = codec.NewProtoCodec(registry)
|
|
err = cdc.UnmarshalInterface(bz, &animal)
|
|
require.NoError(t, err)
|
|
require.Equal(t, kitty, animal)
|
|
|
|
// nil should fail
|
|
registry = NewTestInterfaceRegistry()
|
|
err = cdc.UnmarshalInterface(bz, nil)
|
|
require.Error(t, err)
|
|
}
|
|
|
|
func TestMarshalProtoPubKey(t *testing.T) {
|
|
require := require.New(t)
|
|
ccfg := simapp.MakeTestEncodingConfig()
|
|
privKey := ed25519.GenPrivKey()
|
|
pk := privKey.PubKey()
|
|
|
|
// **** test JSON serialization ****
|
|
|
|
pkAny, err := codectypes.NewAnyWithValue(pk)
|
|
require.NoError(err)
|
|
bz, err := ccfg.Codec.MarshalJSON(pkAny)
|
|
require.NoError(err)
|
|
|
|
var pkAny2 codectypes.Any
|
|
err = ccfg.Codec.UnmarshalJSON(bz, &pkAny2)
|
|
require.NoError(err)
|
|
// Before getting a cached value we need to unpack it.
|
|
// Normally this happens in types which implement UnpackInterfaces
|
|
var pkI cryptotypes.PubKey
|
|
err = ccfg.InterfaceRegistry.UnpackAny(&pkAny2, &pkI)
|
|
require.NoError(err)
|
|
var pk2 = pkAny2.GetCachedValue().(cryptotypes.PubKey)
|
|
require.True(pk2.Equals(pk))
|
|
|
|
// **** test binary serialization ****
|
|
|
|
bz, err = ccfg.Codec.Marshal(pkAny)
|
|
require.NoError(err)
|
|
|
|
var pkAny3 codectypes.Any
|
|
err = ccfg.Codec.Unmarshal(bz, &pkAny3)
|
|
require.NoError(err)
|
|
err = ccfg.InterfaceRegistry.UnpackAny(&pkAny3, &pkI)
|
|
require.NoError(err)
|
|
var pk3 = pkAny3.GetCachedValue().(cryptotypes.PubKey)
|
|
require.True(pk3.Equals(pk))
|
|
}
|
|
|
|
// TestMarshalProtoInterfacePubKey tests PubKey marshaling using (Un)marshalInterface
|
|
// helper functions
|
|
func TestMarshalProtoInterfacePubKey(t *testing.T) {
|
|
require := require.New(t)
|
|
ccfg := simapp.MakeTestEncodingConfig()
|
|
privKey := ed25519.GenPrivKey()
|
|
pk := privKey.PubKey()
|
|
|
|
// **** test JSON serialization ****
|
|
|
|
bz, err := ccfg.Codec.MarshalInterfaceJSON(pk)
|
|
require.NoError(err)
|
|
|
|
var pk3 cryptotypes.PubKey
|
|
err = ccfg.Codec.UnmarshalInterfaceJSON(bz, &pk3)
|
|
require.NoError(err)
|
|
require.True(pk3.Equals(pk))
|
|
|
|
// ** Check unmarshal using JSONCodec **
|
|
// Unpacking won't work straightforward s Any type
|
|
// Any can't implement UnpackInterfacesMessage interface. So Any is not
|
|
// automatically unpacked and we won't get a value.
|
|
var pkAny codectypes.Any
|
|
err = ccfg.Codec.UnmarshalJSON(bz, &pkAny)
|
|
require.NoError(err)
|
|
ifc := pkAny.GetCachedValue()
|
|
require.Nil(ifc)
|
|
|
|
// **** test binary serialization ****
|
|
|
|
bz, err = ccfg.Codec.MarshalInterface(pk)
|
|
require.NoError(err)
|
|
|
|
var pk2 cryptotypes.PubKey
|
|
err = ccfg.Codec.UnmarshalInterface(bz, &pk2)
|
|
require.NoError(err)
|
|
require.True(pk2.Equals(pk))
|
|
}
|