github.com/cosmos/cosmos-sdk@v0.50.10/codec/any_test.go (about)

     1  package codec_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/cosmos/cosmos-sdk/codec"
     9  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    10  	"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
    11  	cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
    12  	"github.com/cosmos/cosmos-sdk/testutil/testdata"
    13  	"github.com/cosmos/cosmos-sdk/types/module/testutil"
    14  )
    15  
    16  func NewTestInterfaceRegistry() codectypes.InterfaceRegistry {
    17  	registry := codectypes.NewInterfaceRegistry()
    18  	registry.RegisterInterface("Animal", (*testdata.Animal)(nil))
    19  	registry.RegisterImplementations(
    20  		(*testdata.Animal)(nil),
    21  		&testdata.Dog{},
    22  		&testdata.Cat{},
    23  	)
    24  	return registry
    25  }
    26  
    27  func TestMarshalAny(t *testing.T) {
    28  	catRegistry := codectypes.NewInterfaceRegistry()
    29  	catRegistry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Cat{})
    30  
    31  	registry := codectypes.NewInterfaceRegistry()
    32  
    33  	cdc := codec.NewProtoCodec(registry)
    34  
    35  	kitty := &testdata.Cat{Moniker: "Kitty"}
    36  	emptyBz, err := cdc.MarshalInterface(kitty)
    37  	require.ErrorContains(t, err, "does not have a registered interface")
    38  
    39  	catBz, err := codec.NewProtoCodec(catRegistry).MarshalInterface(kitty)
    40  	require.NoError(t, err)
    41  	require.NotEmpty(t, catBz)
    42  
    43  	var animal testdata.Animal
    44  
    45  	// deserializing cat bytes should error in an empty registry
    46  	err = cdc.UnmarshalInterface(catBz, &animal)
    47  	require.ErrorContains(t, err, "no registered implementations of type testdata.Animal")
    48  
    49  	// deserializing an empty byte array will return nil, but no error
    50  	err = cdc.UnmarshalInterface(emptyBz, &animal)
    51  	require.Nil(t, animal)
    52  	require.NoError(t, err)
    53  
    54  	// wrong type registration should fail
    55  	registry.RegisterImplementations((*testdata.Animal)(nil), &testdata.Dog{})
    56  	err = cdc.UnmarshalInterface(catBz, &animal)
    57  	require.Error(t, err)
    58  
    59  	// should pass
    60  	registry = NewTestInterfaceRegistry()
    61  	cdc = codec.NewProtoCodec(registry)
    62  	err = cdc.UnmarshalInterface(catBz, &animal)
    63  	require.NoError(t, err)
    64  	require.Equal(t, kitty, animal)
    65  
    66  	// nil should fail
    67  	_ = NewTestInterfaceRegistry()
    68  	err = cdc.UnmarshalInterface(catBz, nil)
    69  	require.Error(t, err)
    70  }
    71  
    72  func TestMarshalProtoPubKey(t *testing.T) {
    73  	require := require.New(t)
    74  	ccfg := testutil.MakeTestEncodingConfig()
    75  	privKey := ed25519.GenPrivKey()
    76  	pk := privKey.PubKey()
    77  
    78  	// **** test JSON serialization ****
    79  
    80  	pkAny, err := codectypes.NewAnyWithValue(pk)
    81  	require.NoError(err)
    82  	bz, err := ccfg.Codec.MarshalJSON(pkAny)
    83  	require.NoError(err)
    84  
    85  	var pkAny2 codectypes.Any
    86  	err = ccfg.Codec.UnmarshalJSON(bz, &pkAny2)
    87  	require.NoError(err)
    88  	// Before getting a cached value we need to unpack it.
    89  	// Normally this happens in types which implement UnpackInterfaces
    90  	var pkI cryptotypes.PubKey
    91  	err = ccfg.InterfaceRegistry.UnpackAny(&pkAny2, &pkI)
    92  	require.NoError(err)
    93  	pk2 := pkAny2.GetCachedValue().(cryptotypes.PubKey)
    94  	require.True(pk2.Equals(pk))
    95  
    96  	// **** test binary serialization ****
    97  
    98  	bz, err = ccfg.Codec.Marshal(pkAny)
    99  	require.NoError(err)
   100  
   101  	var pkAny3 codectypes.Any
   102  	err = ccfg.Codec.Unmarshal(bz, &pkAny3)
   103  	require.NoError(err)
   104  	err = ccfg.InterfaceRegistry.UnpackAny(&pkAny3, &pkI)
   105  	require.NoError(err)
   106  	pk3 := pkAny3.GetCachedValue().(cryptotypes.PubKey)
   107  	require.True(pk3.Equals(pk))
   108  }
   109  
   110  // TestMarshalProtoInterfacePubKey tests PubKey marshaling using (Un)marshalInterface
   111  // helper functions
   112  func TestMarshalProtoInterfacePubKey(t *testing.T) {
   113  	require := require.New(t)
   114  	ccfg := testutil.MakeTestEncodingConfig()
   115  	privKey := ed25519.GenPrivKey()
   116  	pk := privKey.PubKey()
   117  
   118  	// **** test JSON serialization ****
   119  
   120  	bz, err := ccfg.Codec.MarshalInterfaceJSON(pk)
   121  	require.NoError(err)
   122  
   123  	var pk3 cryptotypes.PubKey
   124  	err = ccfg.Codec.UnmarshalInterfaceJSON(bz, &pk3)
   125  	require.NoError(err)
   126  	require.True(pk3.Equals(pk))
   127  
   128  	// ** Check unmarshal using JSONCodec **
   129  	// Unpacking won't work straightforward s Any type
   130  	// Any can't implement UnpackInterfacesMessage interface. So Any is not
   131  	// automatically unpacked and we won't get a value.
   132  	var pkAny codectypes.Any
   133  	err = ccfg.Codec.UnmarshalJSON(bz, &pkAny)
   134  	require.NoError(err)
   135  	ifc := pkAny.GetCachedValue()
   136  	require.Nil(ifc)
   137  
   138  	// **** test binary serialization ****
   139  
   140  	bz, err = ccfg.Codec.MarshalInterface(pk)
   141  	require.NoError(err)
   142  
   143  	var pk2 cryptotypes.PubKey
   144  	err = ccfg.Codec.UnmarshalInterface(bz, &pk2)
   145  	require.NoError(err)
   146  	require.True(pk2.Equals(pk))
   147  }