refactor: use proto.Message instead of ProtoMarshler (#14208)

This commit is contained in:
Marko
2022-12-08 19:38:38 +00:00
committed by GitHub
parent 09bff2f444
commit 755c99ac44
18 changed files with 131 additions and 175 deletions
-55
View File
@@ -1,8 +1,6 @@
package codec_test
import (
"errors"
"fmt"
"math"
"reflect"
"testing"
@@ -43,15 +41,6 @@ func TestProtoCodec(t *testing.T) {
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"}
@@ -138,50 +127,6 @@ func grpcServerEncode(c encoding.Codec, msg interface{}) ([]byte, error) {
return b, nil
}
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 {