123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- // Copyright 2019 The Go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package proto_test
- import (
- "google.golang.org/protobuf/encoding/protowire"
- "google.golang.org/protobuf/internal/flags"
- "google.golang.org/protobuf/proto"
- "google.golang.org/protobuf/testing/protopack"
- messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb"
- msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb"
- )
- func init() {
- if flags.ProtoLegacy {
- testValidMessages = append(testValidMessages, messageSetTestProtos...)
- testInvalidMessages = append(testInvalidMessages, messageSetInvalidTestProtos...)
- }
- }
- var messageSetTestProtos = []testProto{
- {
- desc: "MessageSet type_id before message content",
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
- Ext1Field1: proto.Int32(10),
- })
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet type_id after message content",
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
- Ext1Field1: proto.Int32(10),
- })
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet does not preserve unknown field",
- decodeTo: []proto.Message{build(
- &messagesetpb.MessageSet{},
- extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
- Ext1Field1: proto.Int32(10),
- }),
- )},
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- // Unknown field
- protopack.Tag{4, protopack.VarintType}, protopack.Varint(30),
- }.Marshal(),
- },
- {
- desc: "MessageSet with unknown type_id",
- decodeTo: []proto.Message{build(
- &messagesetpb.MessageSet{},
- unknown(protopack.Message{
- protopack.Tag{999, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- }.Marshal()),
- )},
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(999),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "MessageSet merges repeated message fields in item",
- decodeTo: []proto.Message{build(
- &messagesetpb.MessageSet{},
- extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
- Ext1Field1: proto.Int32(10),
- Ext1Field2: proto.Int32(20),
- }),
- )},
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "MessageSet merges message fields in repeated items",
- decodeTo: []proto.Message{build(
- &messagesetpb.MessageSet{},
- extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
- Ext1Field1: proto.Int32(10),
- Ext1Field2: proto.Int32(20),
- }),
- extend(msetextpb.E_Ext2_MessageSetExtension, &msetextpb.Ext2{
- Ext2Field1: proto.Int32(30),
- }),
- )},
- wire: protopack.Message{
- // Ext1, field1
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- // Ext2, field1
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1001),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(30),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- // Ext2, field2
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "MessageSet with missing type_id",
- decodeTo: []proto.Message{build(
- &messagesetpb.MessageSet{},
- )},
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "MessageSet with missing message",
- decodeTo: []proto.Message{build(
- &messagesetpb.MessageSet{},
- extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}),
- )},
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "MessageSet with type id out of valid field number range",
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- proto.SetExtension(m.MessageSet, msetextpb.E_ExtLargeNumber_MessageSetExtension, &msetextpb.ExtLargeNumber{})
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 1),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet with unknown type id out of valid field number range",
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- m.MessageSet.ProtoReflect().SetUnknown(
- protopack.Message{
- protopack.Tag{protowire.MaxValidNumber + 2, protopack.BytesType}, protopack.LengthPrefix{},
- }.Marshal(),
- )
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 2),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet with unknown field",
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
- Ext1Field1: proto.Int32(10),
- })
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
- }),
- protopack.Tag{4, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet with required field set",
- checkFastInit: true,
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{
- RequiredField1: proto.Int32(1),
- })
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet with required field unset",
- checkFastInit: true,
- partial: true,
- decodeTo: []proto.Message{func() proto.Message {
- m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
- proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{})
- return m
- }()},
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- }
- var messageSetInvalidTestProtos = []testProto{
- {
- desc: "MessageSet with type id 0",
- decodeTo: []proto.Message{
- (*messagesetpb.MessageSetContainer)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- {
- desc: "MessageSet with type id overflowing int32",
- decodeTo: []proto.Message{
- (*messagesetpb.MessageSetContainer)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0x80000000),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.EndGroupType},
- }),
- }.Marshal(),
- },
- }
|