forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
any_test.go
148 lines (118 loc) · 4.32 KB
/
any_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
package codec_test
import (
"testing"
"github.com/cosmos/gogoproto/types/any/test"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
codectestutil "github.com/cosmos/cosmos-sdk/codec/testutil"
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/types/module/testutil"
)
func NewTestInterfaceRegistry() codectypes.InterfaceRegistry {
registry := codectypes.NewInterfaceRegistry()
registry.RegisterInterface("Animal", (*test.Animal)(nil))
registry.RegisterImplementations(
(*test.Animal)(nil),
&test.Dog{},
&test.Cat{},
)
return registry
}
func TestMarshalAny(t *testing.T) {
catRegistry := codectypes.NewInterfaceRegistry()
catRegistry.RegisterImplementations((*test.Animal)(nil), &test.Cat{})
registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)
kitty := &test.Cat{Moniker: "Kitty"}
emptyBz, err := cdc.MarshalInterface(kitty)
require.ErrorContains(t, err, "does not have a registered interface")
catBz, err := codec.NewProtoCodec(catRegistry).MarshalInterface(kitty)
require.NoError(t, err)
require.NotEmpty(t, catBz)
var animal test.Animal
// deserializing cat bytes should error in an empty registry
err = cdc.UnmarshalInterface(catBz, &animal)
require.ErrorContains(t, err, "no registered implementations of type test.Animal")
// deserializing an empty byte array will return nil, but no error
err = cdc.UnmarshalInterface(emptyBz, &animal)
require.Nil(t, animal)
require.NoError(t, err)
// wrong type registration should fail
registry.RegisterImplementations((*test.Animal)(nil), &test.Dog{})
err = cdc.UnmarshalInterface(catBz, &animal)
require.Error(t, err)
// should pass
registry = NewTestInterfaceRegistry()
cdc = codec.NewProtoCodec(registry)
err = cdc.UnmarshalInterface(catBz, &animal)
require.NoError(t, err)
require.Equal(t, kitty, animal)
// nil should fail
_ = NewTestInterfaceRegistry()
err = cdc.UnmarshalInterface(catBz, nil)
require.Error(t, err)
}
func TestMarshalProtoPubKey(t *testing.T) {
require := require.New(t)
ccfg := testutil.MakeTestEncodingConfig(codectestutil.CodecOptions{})
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)
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)
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 := testutil.MakeTestEncodingConfig(codectestutil.CodecOptions{})
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))
}