Skip to content

Commit

Permalink
unknownproto: recognize packed and repeated primitive types (#7225)
Browse files Browse the repository at this point in the history
* Fix canEncodeType for repeated fields

* Fix tests

* Remove duplicate tests

* Don't remove tests

Co-authored-by: Jack Zampolin <jack.zampolin@gmail.com>
  • Loading branch information
amaury1093 and jackzampolin authored Sep 2, 2020
1 parent da7a8e6 commit cf394ed
Show file tree
Hide file tree
Showing 4 changed files with 403 additions and 159 deletions.
14 changes: 14 additions & 0 deletions codec/unknownproto/unknown_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,20 @@ var checks = [...]map[descriptor.FieldDescriptorProto_Type]bool{
descriptor.FieldDescriptorProto_TYPE_STRING: true,
descriptor.FieldDescriptorProto_TYPE_BYTES: true,
descriptor.FieldDescriptorProto_TYPE_MESSAGE: true,
// The following types can be packed repeated.
// ref: "Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire types) can be declared "packed"."
// ref: https://developers.google.com/protocol-buffers/docs/encoding#packed
descriptor.FieldDescriptorProto_TYPE_INT32: true,
descriptor.FieldDescriptorProto_TYPE_INT64: true,
descriptor.FieldDescriptorProto_TYPE_UINT32: true,
descriptor.FieldDescriptorProto_TYPE_UINT64: true,
descriptor.FieldDescriptorProto_TYPE_SINT32: true,
descriptor.FieldDescriptorProto_TYPE_SINT64: true,
descriptor.FieldDescriptorProto_TYPE_BOOL: true,
descriptor.FieldDescriptorProto_TYPE_ENUM: true,
descriptor.FieldDescriptorProto_TYPE_FIXED64: true,
descriptor.FieldDescriptorProto_TYPE_SFIXED64: true,
descriptor.FieldDescriptorProto_TYPE_DOUBLE: true,
},

// "3 Start group: groups (deprecated)"
Expand Down
61 changes: 27 additions & 34 deletions codec/unknownproto/unknown_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,9 @@ func TestRejectUnknownFieldsNested(t *testing.T) {
recv: new(testdata.TestVersion1),
wantErr: &errMismatchedWireType{
Type: "*testdata.TestVersion3",
TagNum: 1,
GotWireType: 2,
WantWireType: 0,
TagNum: 8,
GotWireType: 7,
WantWireType: 2,
},
},
{
Expand All @@ -463,13 +463,8 @@ func TestRejectUnknownFieldsNested(t *testing.T) {
},
},
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: &errMismatchedWireType{
Type: "*testdata.TestVersion4LoneNesting_Inner1_InnerInner",
TagNum: 1,
GotWireType: 2,
WantWireType: 0,
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: nil,
},
{
name: "From nested proto message, message index 1",
Expand All @@ -483,13 +478,8 @@ func TestRejectUnknownFieldsNested(t *testing.T) {
},
},
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: &errMismatchedWireType{
Type: "*testdata.TestVersion4LoneNesting_Inner2_InnerInner",
TagNum: 2,
GotWireType: 2,
WantWireType: 0,
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: nil,
},
}

Expand Down Expand Up @@ -684,9 +674,9 @@ func TestMismatchedTypes_Nested(t *testing.T) {
recv: new(testdata.TestVersion1),
wantErr: &errMismatchedWireType{
Type: "*testdata.TestVersion3",
TagNum: 1,
GotWireType: 2,
WantWireType: 0,
TagNum: 8,
GotWireType: 7,
WantWireType: 2,
},
},
{
Expand All @@ -701,13 +691,8 @@ func TestMismatchedTypes_Nested(t *testing.T) {
},
},
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: &errMismatchedWireType{
Type: "*testdata.TestVersion4LoneNesting_Inner1_InnerInner",
TagNum: 1,
GotWireType: 2,
WantWireType: 0,
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: nil,
},
{
name: "From nested proto message, message index 1",
Expand All @@ -721,13 +706,8 @@ func TestMismatchedTypes_Nested(t *testing.T) {
},
},
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: &errMismatchedWireType{
Type: "*testdata.TestVersion4LoneNesting_Inner2_InnerInner",
TagNum: 2,
GotWireType: 2,
WantWireType: 0,
},
recv: new(testdata.TestVersion4LoneNesting),
wantErr: nil,
},
}

Expand All @@ -746,6 +726,19 @@ func TestMismatchedTypes_Nested(t *testing.T) {
}
}

// Issue https://github.com/cosmos/cosmos-sdk/issues/7222, we need to ensure that repeated
// uint64 are recognized as packed.
func TestPackedEncoding(t *testing.T) {
data := testdata.TestRepeatedUints{Nums: []uint64{12, 13}}

marshalled, err := data.Marshal()
require.NoError(t, err)

unmarshalled := &testdata.TestRepeatedUints{}
_, err = RejectUnknownFields(marshalled, unmarshalled, false)
require.NoError(t, err)
}

func mustMarshal(msg proto.Message) []byte {
blob, err := proto.Marshal(msg)
if err != nil {
Expand Down
Loading

0 comments on commit cf394ed

Please sign in to comment.