github.com/cosmos/cosmos-sdk@v0.50.10/codec/codec_common_test.go (about) 1 package codec_test 2 3 import ( 4 "testing" 5 6 "github.com/cosmos/gogoproto/proto" 7 "github.com/stretchr/testify/require" 8 9 "github.com/cosmos/cosmos-sdk/codec" 10 "github.com/cosmos/cosmos-sdk/codec/types" 11 "github.com/cosmos/cosmos-sdk/testutil/testdata" 12 ) 13 14 type interfaceMarshaler struct { 15 marshal func(i proto.Message) ([]byte, error) 16 unmarshal func(bz []byte, ptr interface{}) error 17 } 18 19 func testInterfaceMarshaling(require *require.Assertions, cdc interfaceMarshaler, isAminoBin bool) { 20 _, err := cdc.marshal(nil) 21 require.Error(err, "can't marshal a nil value") 22 23 dog := &testdata.Dog{Name: "rufus"} 24 var dogI testdata.Animal = dog 25 bz, err := cdc.marshal(dogI) 26 require.NoError(err) 27 28 var animal testdata.Animal 29 if isAminoBin { 30 require.PanicsWithValue("Unmarshal expects a pointer", func() { 31 _ = cdc.unmarshal(bz, animal) 32 }) 33 } else { 34 err = cdc.unmarshal(bz, animal) 35 require.Error(err) 36 require.Contains(err.Error(), "expects a pointer") 37 } 38 require.NoError(cdc.unmarshal(bz, &animal)) 39 require.Equal(dog, animal) 40 41 // Amino doesn't wrap into Any, so it doesn't need to register self type 42 if isAminoBin { 43 var dog2 testdata.Dog 44 require.NoError(cdc.unmarshal(bz, &dog2)) 45 require.Equal(*dog, dog2) 46 } 47 48 var cat testdata.Cat 49 require.Error(cdc.unmarshal(bz, &cat)) 50 } 51 52 type mustMarshaler struct { 53 marshal func(i proto.Message) ([]byte, error) 54 mustMarshal func(i proto.Message) []byte 55 unmarshal func(bz []byte, ptr proto.Message) error 56 mustUnmarshal func(bz []byte, ptr proto.Message) 57 } 58 59 type testCase struct { 60 name string 61 input proto.Message 62 recv proto.Message 63 marshalErr bool 64 unmarshalErr bool 65 } 66 67 func testMarshalingTestCase(require *require.Assertions, tc testCase, m mustMarshaler) { 68 bz, err := m.marshal(tc.input) 69 if tc.marshalErr { 70 require.Error(err) 71 require.Panics(func() { m.mustMarshal(tc.input) }) 72 } else { 73 var bz2 []byte 74 require.NoError(err) 75 require.NotPanics(func() { bz2 = m.mustMarshal(tc.input) }) 76 require.Equal(bz, bz2) 77 78 err := m.unmarshal(bz, tc.recv) 79 if tc.unmarshalErr { 80 require.Error(err) 81 require.Panics(func() { m.mustUnmarshal(bz, tc.recv) }) 82 } else { 83 require.NoError(err) 84 require.NotPanics(func() { m.mustUnmarshal(bz, tc.recv) }) 85 require.Equal(tc.input, tc.recv) 86 } 87 } 88 } 89 90 func testMarshaling(t *testing.T, cdc interface { 91 codec.BinaryCodec 92 codec.JSONCodec 93 }, 94 ) { 95 any, err := types.NewAnyWithValue(&testdata.Dog{Name: "rufus"}) 96 require.NoError(t, err) 97 98 testCases := []testCase{ 99 { 100 "valid encoding and decoding", 101 &testdata.Dog{Name: "rufus"}, 102 &testdata.Dog{}, 103 false, 104 false, 105 }, { 106 "invalid decode type", 107 &testdata.Dog{Name: "rufus"}, 108 &testdata.Cat{}, 109 false, 110 true, 111 }, 112 } 113 if _, ok := cdc.(*codec.AminoCodec); ok { 114 testCases = append(testCases, testCase{ 115 "any marshaling", 116 &testdata.HasAnimal{Animal: any}, 117 &testdata.HasAnimal{Animal: any}, 118 false, 119 false, 120 }) 121 } 122 123 for _, tc := range testCases { 124 tc := tc 125 m1 := mustMarshaler{cdc.Marshal, cdc.MustMarshal, cdc.Unmarshal, cdc.MustUnmarshal} 126 m2 := mustMarshaler{cdc.MarshalLengthPrefixed, cdc.MustMarshalLengthPrefixed, cdc.UnmarshalLengthPrefixed, cdc.MustUnmarshalLengthPrefixed} 127 m3 := mustMarshaler{ 128 func(i proto.Message) ([]byte, error) { return cdc.MarshalJSON(i) }, 129 func(i proto.Message) []byte { return cdc.MustMarshalJSON(i) }, 130 func(bz []byte, ptr proto.Message) error { return cdc.UnmarshalJSON(bz, ptr) }, 131 func(bz []byte, ptr proto.Message) { cdc.MustUnmarshalJSON(bz, ptr) }, 132 } 133 134 t.Run(tc.name+"_BinaryBare", 135 func(t *testing.T) { testMarshalingTestCase(require.New(t), tc, m1) }) 136 t.Run(tc.name+"_BinaryLengthPrefixed", 137 func(t *testing.T) { testMarshalingTestCase(require.New(t), tc, m2) }) 138 t.Run(tc.name+"_JSON", 139 func(t *testing.T) { testMarshalingTestCase(require.New(t), tc, m3) }) 140 } 141 }