123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025 |
- // 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/impl"
- "google.golang.org/protobuf/internal/protobuild"
- "google.golang.org/protobuf/proto"
- "google.golang.org/protobuf/reflect/protoreflect"
- "google.golang.org/protobuf/reflect/protoregistry"
- "google.golang.org/protobuf/testing/protopack"
- legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
- requiredpb "google.golang.org/protobuf/internal/testprotos/required"
- testpb "google.golang.org/protobuf/internal/testprotos/test"
- test3pb "google.golang.org/protobuf/internal/testprotos/test3"
- )
- type testProto struct {
- desc string
- decodeTo []proto.Message
- wire []byte
- partial bool
- noEncode bool
- checkFastInit bool
- unmarshalOptions proto.UnmarshalOptions
- validationStatus impl.ValidationStatus
- nocheckValidInit bool
- }
- func makeMessages(in protobuild.Message, messages ...proto.Message) []proto.Message {
- if len(messages) == 0 {
- messages = []proto.Message{
- &testpb.TestAllTypes{},
- &test3pb.TestAllTypes{},
- &testpb.TestAllExtensions{},
- }
- }
- for _, m := range messages {
- in.Build(m.ProtoReflect())
- }
- return messages
- }
- func templateMessages(messages ...proto.Message) []protoreflect.MessageType {
- if len(messages) == 0 {
- messages = []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*test3pb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- }
- }
- var out []protoreflect.MessageType
- for _, m := range messages {
- out = append(out, m.ProtoReflect().Type())
- }
- return out
- }
- var testValidMessages = []testProto{
- {
- desc: "basic scalar types",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "optional_int32": 1001,
- "optional_int64": 1002,
- "optional_uint32": 1003,
- "optional_uint64": 1004,
- "optional_sint32": 1005,
- "optional_sint64": 1006,
- "optional_fixed32": 1007,
- "optional_fixed64": 1008,
- "optional_sfixed32": 1009,
- "optional_sfixed64": 1010,
- "optional_float": 1011.5,
- "optional_double": 1012.5,
- "optional_bool": true,
- "optional_string": "string",
- "optional_bytes": []byte("bytes"),
- "optional_nested_enum": "BAR",
- }),
- wire: protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1001),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
- protopack.Tag{3, protopack.VarintType}, protopack.Uvarint(1003),
- protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(1004),
- protopack.Tag{5, protopack.VarintType}, protopack.Svarint(1005),
- protopack.Tag{6, protopack.VarintType}, protopack.Svarint(1006),
- protopack.Tag{7, protopack.Fixed32Type}, protopack.Uint32(1007),
- protopack.Tag{8, protopack.Fixed64Type}, protopack.Uint64(1008),
- protopack.Tag{9, protopack.Fixed32Type}, protopack.Int32(1009),
- protopack.Tag{10, protopack.Fixed64Type}, protopack.Int64(1010),
- protopack.Tag{11, protopack.Fixed32Type}, protopack.Float32(1011.5),
- protopack.Tag{12, protopack.Fixed64Type}, protopack.Float64(1012.5),
- protopack.Tag{13, protopack.VarintType}, protopack.Bool(true),
- protopack.Tag{14, protopack.BytesType}, protopack.String("string"),
- protopack.Tag{15, protopack.BytesType}, protopack.Bytes([]byte("bytes")),
- protopack.Tag{21, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
- }.Marshal(),
- },
- {
- desc: "zero values",
- decodeTo: makeMessages(protobuild.Message{
- "optional_int32": 0,
- "optional_int64": 0,
- "optional_uint32": 0,
- "optional_uint64": 0,
- "optional_sint32": 0,
- "optional_sint64": 0,
- "optional_fixed32": 0,
- "optional_fixed64": 0,
- "optional_sfixed32": 0,
- "optional_sfixed64": 0,
- "optional_float": 0,
- "optional_double": 0,
- "optional_bool": false,
- "optional_string": "",
- "optional_bytes": []byte{},
- }),
- wire: protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{3, protopack.VarintType}, protopack.Uvarint(0),
- protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(0),
- protopack.Tag{5, protopack.VarintType}, protopack.Svarint(0),
- protopack.Tag{6, protopack.VarintType}, protopack.Svarint(0),
- protopack.Tag{7, protopack.Fixed32Type}, protopack.Uint32(0),
- protopack.Tag{8, protopack.Fixed64Type}, protopack.Uint64(0),
- protopack.Tag{9, protopack.Fixed32Type}, protopack.Int32(0),
- protopack.Tag{10, protopack.Fixed64Type}, protopack.Int64(0),
- protopack.Tag{11, protopack.Fixed32Type}, protopack.Float32(0),
- protopack.Tag{12, protopack.Fixed64Type}, protopack.Float64(0),
- protopack.Tag{13, protopack.VarintType}, protopack.Bool(false),
- protopack.Tag{14, protopack.BytesType}, protopack.String(""),
- protopack.Tag{15, protopack.BytesType}, protopack.Bytes(nil),
- }.Marshal(),
- },
- {
- desc: "proto3 zero values",
- decodeTo: makeMessages(protobuild.Message{
- "singular_int32": 0,
- "singular_int64": 0,
- "singular_uint32": 0,
- "singular_uint64": 0,
- "singular_sint32": 0,
- "singular_sint64": 0,
- "singular_fixed32": 0,
- "singular_fixed64": 0,
- "singular_sfixed32": 0,
- "singular_sfixed64": 0,
- "singular_float": 0,
- "singular_double": 0,
- "singular_bool": false,
- "singular_string": "",
- "singular_bytes": []byte{},
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{81, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{82, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{83, protopack.VarintType}, protopack.Uvarint(0),
- protopack.Tag{84, protopack.VarintType}, protopack.Uvarint(0),
- protopack.Tag{85, protopack.VarintType}, protopack.Svarint(0),
- protopack.Tag{86, protopack.VarintType}, protopack.Svarint(0),
- protopack.Tag{87, protopack.Fixed32Type}, protopack.Uint32(0),
- protopack.Tag{88, protopack.Fixed64Type}, protopack.Uint64(0),
- protopack.Tag{89, protopack.Fixed32Type}, protopack.Int32(0),
- protopack.Tag{90, protopack.Fixed64Type}, protopack.Int64(0),
- protopack.Tag{91, protopack.Fixed32Type}, protopack.Float32(0),
- protopack.Tag{92, protopack.Fixed64Type}, protopack.Float64(0),
- protopack.Tag{93, protopack.VarintType}, protopack.Bool(false),
- protopack.Tag{94, protopack.BytesType}, protopack.String(""),
- protopack.Tag{95, protopack.BytesType}, protopack.Bytes(nil),
- }.Marshal(),
- },
- {
- desc: "groups",
- decodeTo: makeMessages(protobuild.Message{
- "optionalgroup": protobuild.Message{
- "a": 1017,
- "same_field_number": 1016,
- },
- }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{16, protopack.StartGroupType},
- protopack.Tag{17, protopack.VarintType}, protopack.Varint(1017),
- protopack.Tag{16, protopack.VarintType}, protopack.Varint(1016),
- protopack.Tag{16, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "groups (field overridden)",
- decodeTo: makeMessages(protobuild.Message{
- "optionalgroup": protobuild.Message{
- "a": 2,
- },
- }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{16, protopack.StartGroupType},
- protopack.Tag{17, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{16, protopack.EndGroupType},
- protopack.Tag{16, protopack.StartGroupType},
- protopack.Tag{17, protopack.VarintType}, protopack.Varint(2),
- protopack.Tag{16, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "messages",
- decodeTo: makeMessages(protobuild.Message{
- "optional_nested_message": protobuild.Message{
- "a": 42,
- "corecursive": protobuild.Message{
- "optional_int32": 43,
- },
- },
- }),
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "messages (split across multiple tags)",
- decodeTo: makeMessages(protobuild.Message{
- "optional_nested_message": protobuild.Message{
- "a": 42,
- "corecursive": protobuild.Message{
- "optional_int32": 43,
- },
- },
- }),
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
- }),
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "messages (field overridden)",
- decodeTo: makeMessages(protobuild.Message{
- "optional_nested_message": protobuild.Message{
- "a": 2,
- },
- }),
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }.Marshal(),
- },
- {
- desc: "basic repeated types",
- decodeTo: makeMessages(protobuild.Message{
- "repeated_int32": []int32{1001, 2001},
- "repeated_int64": []int64{1002, 2002},
- "repeated_uint32": []uint32{1003, 2003},
- "repeated_uint64": []uint64{1004, 2004},
- "repeated_sint32": []int32{1005, 2005},
- "repeated_sint64": []int64{1006, 2006},
- "repeated_fixed32": []uint32{1007, 2007},
- "repeated_fixed64": []uint64{1008, 2008},
- "repeated_sfixed32": []int32{1009, 2009},
- "repeated_sfixed64": []int64{1010, 2010},
- "repeated_float": []float32{1011.5, 2011.5},
- "repeated_double": []float64{1012.5, 2012.5},
- "repeated_bool": []bool{true, false},
- "repeated_string": []string{"foo", "bar"},
- "repeated_bytes": []string{"FOO", "BAR"},
- "repeated_nested_enum": []string{"FOO", "BAR"},
- }),
- wire: protopack.Message{
- protopack.Tag{31, protopack.VarintType}, protopack.Varint(1001),
- protopack.Tag{31, protopack.VarintType}, protopack.Varint(2001),
- protopack.Tag{32, protopack.VarintType}, protopack.Varint(1002),
- protopack.Tag{32, protopack.VarintType}, protopack.Varint(2002),
- protopack.Tag{33, protopack.VarintType}, protopack.Uvarint(1003),
- protopack.Tag{33, protopack.VarintType}, protopack.Uvarint(2003),
- protopack.Tag{34, protopack.VarintType}, protopack.Uvarint(1004),
- protopack.Tag{34, protopack.VarintType}, protopack.Uvarint(2004),
- protopack.Tag{35, protopack.VarintType}, protopack.Svarint(1005),
- protopack.Tag{35, protopack.VarintType}, protopack.Svarint(2005),
- protopack.Tag{36, protopack.VarintType}, protopack.Svarint(1006),
- protopack.Tag{36, protopack.VarintType}, protopack.Svarint(2006),
- protopack.Tag{37, protopack.Fixed32Type}, protopack.Uint32(1007),
- protopack.Tag{37, protopack.Fixed32Type}, protopack.Uint32(2007),
- protopack.Tag{38, protopack.Fixed64Type}, protopack.Uint64(1008),
- protopack.Tag{38, protopack.Fixed64Type}, protopack.Uint64(2008),
- protopack.Tag{39, protopack.Fixed32Type}, protopack.Int32(1009),
- protopack.Tag{39, protopack.Fixed32Type}, protopack.Int32(2009),
- protopack.Tag{40, protopack.Fixed64Type}, protopack.Int64(1010),
- protopack.Tag{40, protopack.Fixed64Type}, protopack.Int64(2010),
- protopack.Tag{41, protopack.Fixed32Type}, protopack.Float32(1011.5),
- protopack.Tag{41, protopack.Fixed32Type}, protopack.Float32(2011.5),
- protopack.Tag{42, protopack.Fixed64Type}, protopack.Float64(1012.5),
- protopack.Tag{42, protopack.Fixed64Type}, protopack.Float64(2012.5),
- protopack.Tag{43, protopack.VarintType}, protopack.Bool(true),
- protopack.Tag{43, protopack.VarintType}, protopack.Bool(false),
- protopack.Tag{44, protopack.BytesType}, protopack.String("foo"),
- protopack.Tag{44, protopack.BytesType}, protopack.String("bar"),
- protopack.Tag{45, protopack.BytesType}, protopack.Bytes([]byte("FOO")),
- protopack.Tag{45, protopack.BytesType}, protopack.Bytes([]byte("BAR")),
- protopack.Tag{51, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_FOO)),
- protopack.Tag{51, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
- }.Marshal(),
- },
- {
- desc: "basic repeated types (packed encoding)",
- decodeTo: makeMessages(protobuild.Message{
- "repeated_int32": []int32{1001, 2001},
- "repeated_int64": []int64{1002, 2002},
- "repeated_uint32": []uint32{1003, 2003},
- "repeated_uint64": []uint64{1004, 2004},
- "repeated_sint32": []int32{1005, 2005},
- "repeated_sint64": []int64{1006, 2006},
- "repeated_fixed32": []uint32{1007, 2007},
- "repeated_fixed64": []uint64{1008, 2008},
- "repeated_sfixed32": []int32{1009, 2009},
- "repeated_sfixed64": []int64{1010, 2010},
- "repeated_float": []float32{1011.5, 2011.5},
- "repeated_double": []float64{1012.5, 2012.5},
- "repeated_bool": []bool{true, false},
- "repeated_nested_enum": []string{"FOO", "BAR"},
- }),
- wire: protopack.Message{
- protopack.Tag{31, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Varint(1001), protopack.Varint(2001),
- },
- protopack.Tag{32, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Varint(1002), protopack.Varint(2002),
- },
- protopack.Tag{33, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uvarint(1003), protopack.Uvarint(2003),
- },
- protopack.Tag{34, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uvarint(1004), protopack.Uvarint(2004),
- },
- protopack.Tag{35, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Svarint(1005), protopack.Svarint(2005),
- },
- protopack.Tag{36, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Svarint(1006), protopack.Svarint(2006),
- },
- protopack.Tag{37, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uint32(1007), protopack.Uint32(2007),
- },
- protopack.Tag{38, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uint64(1008), protopack.Uint64(2008),
- },
- protopack.Tag{39, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Int32(1009), protopack.Int32(2009),
- },
- protopack.Tag{40, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Int64(1010), protopack.Int64(2010),
- },
- protopack.Tag{41, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Float32(1011.5), protopack.Float32(2011.5),
- },
- protopack.Tag{42, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Float64(1012.5), protopack.Float64(2012.5),
- },
- protopack.Tag{43, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Bool(true), protopack.Bool(false),
- },
- protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Varint(int(testpb.TestAllTypes_FOO)),
- protopack.Varint(int(testpb.TestAllTypes_BAR)),
- },
- }.Marshal(),
- },
- {
- desc: "basic repeated types (zero-length packed encoding)",
- decodeTo: makeMessages(protobuild.Message{
- "repeated_int32": []int32{},
- "repeated_int64": []int64{},
- "repeated_uint32": []uint32{},
- "repeated_uint64": []uint64{},
- "repeated_sint32": []int32{},
- "repeated_sint64": []int64{},
- "repeated_fixed32": []uint32{},
- "repeated_fixed64": []uint64{},
- "repeated_sfixed32": []int32{},
- "repeated_sfixed64": []int64{},
- "repeated_float": []float32{},
- "repeated_double": []float64{},
- "repeated_bool": []bool{},
- "repeated_nested_enum": []string{},
- }),
- wire: protopack.Message{
- protopack.Tag{31, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{32, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{33, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{34, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{35, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{36, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{37, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{38, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{39, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{40, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{41, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{42, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{43, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{},
- }.Marshal(),
- },
- {
- desc: "packed repeated types",
- decodeTo: makeMessages(protobuild.Message{
- "packed_int32": []int32{1001, 2001},
- "packed_int64": []int64{1002, 2002},
- "packed_uint32": []uint32{1003, 2003},
- "packed_uint64": []uint64{1004, 2004},
- "packed_sint32": []int32{1005, 2005},
- "packed_sint64": []int64{1006, 2006},
- "packed_fixed32": []uint32{1007, 2007},
- "packed_fixed64": []uint64{1008, 2008},
- "packed_sfixed32": []int32{1009, 2009},
- "packed_sfixed64": []int64{1010, 2010},
- "packed_float": []float32{1011.5, 2011.5},
- "packed_double": []float64{1012.5, 2012.5},
- "packed_bool": []bool{true, false},
- "packed_enum": []string{"FOREIGN_FOO", "FOREIGN_BAR"},
- }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
- wire: protopack.Message{
- protopack.Tag{90, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Varint(1001), protopack.Varint(2001),
- },
- protopack.Tag{91, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Varint(1002), protopack.Varint(2002),
- },
- protopack.Tag{92, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uvarint(1003), protopack.Uvarint(2003),
- },
- protopack.Tag{93, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uvarint(1004), protopack.Uvarint(2004),
- },
- protopack.Tag{94, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Svarint(1005), protopack.Svarint(2005),
- },
- protopack.Tag{95, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Svarint(1006), protopack.Svarint(2006),
- },
- protopack.Tag{96, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uint32(1007), protopack.Uint32(2007),
- },
- protopack.Tag{97, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Uint64(1008), protopack.Uint64(2008),
- },
- protopack.Tag{98, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Int32(1009), protopack.Int32(2009),
- },
- protopack.Tag{99, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Int64(1010), protopack.Int64(2010),
- },
- protopack.Tag{100, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Float32(1011.5), protopack.Float32(2011.5),
- },
- protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Float64(1012.5), protopack.Float64(2012.5),
- },
- protopack.Tag{102, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Bool(true), protopack.Bool(false),
- },
- protopack.Tag{103, protopack.BytesType}, protopack.LengthPrefix{
- protopack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
- protopack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
- },
- }.Marshal(),
- },
- {
- desc: "packed repeated types (zero length)",
- decodeTo: makeMessages(protobuild.Message{
- "packed_int32": []int32{},
- "packed_int64": []int64{},
- "packed_uint32": []uint32{},
- "packed_uint64": []uint64{},
- "packed_sint32": []int32{},
- "packed_sint64": []int64{},
- "packed_fixed32": []uint32{},
- "packed_fixed64": []uint64{},
- "packed_sfixed32": []int32{},
- "packed_sfixed64": []int64{},
- "packed_float": []float32{},
- "packed_double": []float64{},
- "packed_bool": []bool{},
- "packed_enum": []string{},
- }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
- wire: protopack.Message{
- protopack.Tag{90, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{91, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{92, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{93, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{94, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{95, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{96, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{97, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{98, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{99, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{100, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{102, protopack.BytesType}, protopack.LengthPrefix{},
- protopack.Tag{103, protopack.BytesType}, protopack.LengthPrefix{},
- }.Marshal(),
- },
- {
- desc: "repeated messages",
- decodeTo: makeMessages(protobuild.Message{
- "repeated_nested_message": []protobuild.Message{
- {"a": 1},
- {},
- {"a": 2},
- },
- }),
- wire: protopack.Message{
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }.Marshal(),
- },
- {
- desc: "repeated nil messages",
- decodeTo: []proto.Message{&testpb.TestAllTypes{
- RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
- {A: proto.Int32(1)},
- nil,
- {A: proto.Int32(2)},
- },
- }, &test3pb.TestAllTypes{
- RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
- {A: 1},
- nil,
- {A: 2},
- },
- }, build(
- &testpb.TestAllExtensions{},
- extend(testpb.E_RepeatedNestedMessage, []*testpb.TestAllExtensions_NestedMessage{
- {A: proto.Int32(1)},
- nil,
- {A: proto.Int32(2)},
- }),
- )},
- wire: protopack.Message{
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }.Marshal(),
- },
- {
- desc: "repeated groups",
- decodeTo: makeMessages(protobuild.Message{
- "repeatedgroup": []protobuild.Message{
- {"a": 1017},
- {},
- {"a": 2017},
- },
- }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{47, protopack.VarintType}, protopack.Varint(1017),
- protopack.Tag{46, protopack.EndGroupType},
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{46, protopack.EndGroupType},
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{47, protopack.VarintType}, protopack.Varint(2017),
- protopack.Tag{46, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "repeated nil groups",
- decodeTo: []proto.Message{&testpb.TestAllTypes{
- Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
- {A: proto.Int32(1017)},
- nil,
- {A: proto.Int32(2017)},
- },
- }, build(
- &testpb.TestAllExtensions{},
- extend(testpb.E_Repeatedgroup, []*testpb.RepeatedGroup{
- {A: proto.Int32(1017)},
- nil,
- {A: proto.Int32(2017)},
- }),
- )},
- wire: protopack.Message{
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{47, protopack.VarintType}, protopack.Varint(1017),
- protopack.Tag{46, protopack.EndGroupType},
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{46, protopack.EndGroupType},
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{47, protopack.VarintType}, protopack.Varint(2017),
- protopack.Tag{46, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "maps",
- decodeTo: makeMessages(protobuild.Message{
- "map_int32_int32": map[int32]int32{1056: 1156, 2056: 2156},
- "map_int64_int64": map[int64]int64{1057: 1157, 2057: 2157},
- "map_uint32_uint32": map[uint32]uint32{1058: 1158, 2058: 2158},
- "map_uint64_uint64": map[uint64]uint64{1059: 1159, 2059: 2159},
- "map_sint32_sint32": map[int32]int32{1060: 1160, 2060: 2160},
- "map_sint64_sint64": map[int64]int64{1061: 1161, 2061: 2161},
- "map_fixed32_fixed32": map[uint32]uint32{1062: 1162, 2062: 2162},
- "map_fixed64_fixed64": map[uint64]uint64{1063: 1163, 2063: 2163},
- "map_sfixed32_sfixed32": map[int32]int32{1064: 1164, 2064: 2164},
- "map_sfixed64_sfixed64": map[int64]int64{1065: 1165, 2065: 2165},
- "map_int32_float": map[int32]float32{1066: 1166.5, 2066: 2166.5},
- "map_int32_double": map[int32]float64{1067: 1167.5, 2067: 2167.5},
- "map_bool_bool": map[bool]bool{true: false, false: true},
- "map_string_string": map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
- "map_string_bytes": map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
- "map_string_nested_message": map[string]protobuild.Message{
- "71.1.key": {"a": 1171},
- "71.2.key": {"a": 2171},
- },
- "map_string_nested_enum": map[string]string{"73.1.key": "FOO", "73.2.key": "BAR"},
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
- }),
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2056),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(2156),
- }),
- protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1057),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1157),
- }),
- protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2057),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(2157),
- }),
- protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1058),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1158),
- }),
- protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2058),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(2158),
- }),
- protopack.Tag{59, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1059),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1159),
- }),
- protopack.Tag{59, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2059),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(2159),
- }),
- protopack.Tag{60, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Svarint(1060),
- protopack.Tag{2, protopack.VarintType}, protopack.Svarint(1160),
- }),
- protopack.Tag{60, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Svarint(2060),
- protopack.Tag{2, protopack.VarintType}, protopack.Svarint(2160),
- }),
- protopack.Tag{61, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Svarint(1061),
- protopack.Tag{2, protopack.VarintType}, protopack.Svarint(1161),
- }),
- protopack.Tag{61, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Svarint(2061),
- protopack.Tag{2, protopack.VarintType}, protopack.Svarint(2161),
- }),
- protopack.Tag{62, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1062),
- protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(1162),
- }),
- protopack.Tag{62, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2062),
- protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(2162),
- }),
- protopack.Tag{63, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1063),
- protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(1163),
- }),
- protopack.Tag{63, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(2063),
- protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(2163),
- }),
- protopack.Tag{64, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1064),
- protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(1164),
- }),
- protopack.Tag{64, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2064),
- protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(2164),
- }),
- protopack.Tag{65, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1065),
- protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(1165),
- }),
- protopack.Tag{65, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(2065),
- protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(2165),
- }),
- protopack.Tag{66, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1066),
- protopack.Tag{2, protopack.Fixed32Type}, protopack.Float32(1166.5),
- }),
- protopack.Tag{66, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2066),
- protopack.Tag{2, protopack.Fixed32Type}, protopack.Float32(2166.5),
- }),
- protopack.Tag{67, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1067),
- protopack.Tag{2, protopack.Fixed64Type}, protopack.Float64(1167.5),
- }),
- protopack.Tag{67, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2067),
- protopack.Tag{2, protopack.Fixed64Type}, protopack.Float64(2167.5),
- }),
- protopack.Tag{68, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Bool(true),
- protopack.Tag{2, protopack.VarintType}, protopack.Bool(false),
- }),
- protopack.Tag{68, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Bool(false),
- protopack.Tag{2, protopack.VarintType}, protopack.Bool(true),
- }),
- protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("69.1.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.String("69.1.val"),
- }),
- protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("69.2.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.String("69.2.val"),
- }),
- protopack.Tag{70, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("70.1.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.String("70.1.val"),
- }),
- protopack.Tag{70, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("70.2.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.String("70.2.val"),
- }),
- protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
- }),
- }),
- protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("71.2.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2171),
- }),
- }),
- protopack.Tag{73, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("73.1.key"),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_FOO)),
- }),
- protopack.Tag{73, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("73.2.key"),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
- }),
- }.Marshal(),
- },
- {
- desc: "map with value before key",
- decodeTo: makeMessages(protobuild.Message{
- "map_int32_int32": map[int32]int32{1056: 1156},
- "map_string_nested_message": map[string]protobuild.Message{
- "71.1.key": {"a": 1171},
- },
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
- }),
- protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
- }),
- protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
- }),
- }.Marshal(),
- },
- {
- desc: "map with repeated key and value",
- decodeTo: makeMessages(protobuild.Message{
- "map_int32_int32": map[int32]int32{1056: 1156},
- "map_string_nested_message": map[string]protobuild.Message{
- "71.1.key": {"a": 1171},
- },
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
- }),
- protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String(""),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "oneof (uint32)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_uint32": 1111,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{111, protopack.VarintType}, protopack.Varint(1111)}.Marshal(),
- },
- {
- desc: "oneof (message)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_nested_message": protobuild.Message{
- "a": 1112,
- },
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Message{protopack.Tag{1, protopack.VarintType}, protopack.Varint(1112)},
- })}.Marshal(),
- },
- {
- desc: "oneof (empty message)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_nested_message": protobuild.Message{},
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{})}.Marshal(),
- },
- {
- desc: "oneof (merged message)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_nested_message": protobuild.Message{
- "a": 1,
- "corecursive": protobuild.Message{
- "optional_int32": 43,
- },
- },
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Message{protopack.Tag{1, protopack.VarintType}, protopack.Varint(1)},
- }),
- protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "oneof (group)",
- decodeTo: makeMessages(protobuild.Message{
- "oneofgroup": protobuild.Message{
- "a": 1,
- },
- }, &testpb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{121, protopack.StartGroupType},
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{121, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "oneof (empty group)",
- decodeTo: makeMessages(protobuild.Message{
- "oneofgroup": protobuild.Message{},
- }, &testpb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{121, protopack.StartGroupType},
- protopack.Tag{121, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "oneof (merged group)",
- decodeTo: makeMessages(protobuild.Message{
- "oneofgroup": protobuild.Message{
- "a": 1,
- "b": 2,
- },
- }, &testpb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{121, protopack.StartGroupType},
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{121, protopack.EndGroupType},
- protopack.Tag{121, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(2),
- protopack.Tag{121, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "oneof (string)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_string": "1113",
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{113, protopack.BytesType}, protopack.String("1113")}.Marshal(),
- },
- {
- desc: "oneof (bytes)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_bytes": "1114",
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{114, protopack.BytesType}, protopack.String("1114")}.Marshal(),
- },
- {
- desc: "oneof (bool)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_bool": true,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{115, protopack.VarintType}, protopack.Bool(true)}.Marshal(),
- },
- {
- desc: "oneof (uint64)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_uint64": 116,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{116, protopack.VarintType}, protopack.Varint(116)}.Marshal(),
- },
- {
- desc: "oneof (float)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_float": 117.5,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{117, protopack.Fixed32Type}, protopack.Float32(117.5)}.Marshal(),
- },
- {
- desc: "oneof (double)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_double": 118.5,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{118, protopack.Fixed64Type}, protopack.Float64(118.5)}.Marshal(),
- },
- {
- desc: "oneof (enum)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_enum": "BAR",
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{119, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
- },
- {
- desc: "oneof (zero)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_uint64": 0,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{116, protopack.VarintType}, protopack.Varint(0)}.Marshal(),
- },
- {
- desc: "oneof (overridden value)",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_uint64": 2,
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{111, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{116, protopack.VarintType}, protopack.Varint(2),
- }.Marshal(),
- },
- // TODO: More unknown field tests for ordering, repeated fields, etc.
- //
- // It is currently impossible to produce results that the v1 Equal
- // considers equivalent to those of the v1 decoder. Figure out if
- // that's a problem or not.
- {
- desc: "unknown fields",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- protobuild.Unknown: protopack.Message{
- protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
- }.Marshal(),
- }),
- wire: protopack.Message{
- protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
- }.Marshal(),
- },
- {
- desc: "discarded unknown fields",
- unmarshalOptions: proto.UnmarshalOptions{
- DiscardUnknown: true,
- },
- decodeTo: makeMessages(protobuild.Message{}),
- wire: protopack.Message{
- protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
- }.Marshal(),
- },
- {
- desc: "field type mismatch",
- decodeTo: makeMessages(protobuild.Message{
- protobuild.Unknown: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("string"),
- }.Marshal(),
- }),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("string"),
- }.Marshal(),
- },
- {
- desc: "map field element mismatch",
- decodeTo: makeMessages(protobuild.Message{
- "map_int32_int32": map[int32]int32{1: 0},
- }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{2, protopack.BytesType}, protopack.String("string"),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in nil message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
- },
- {
- desc: "required int32 unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Int32{}),
- },
- {
- desc: "required int32 set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "v": 1,
- }, &requiredpb.Int32{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }.Marshal(),
- },
- {
- desc: "required fixed32 unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed32{}),
- },
- {
- desc: "required fixed32 set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "v": 1,
- }, &requiredpb.Fixed32{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1),
- }.Marshal(),
- },
- {
- desc: "required fixed64 unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed64{}),
- },
- {
- desc: "required fixed64 set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "v": 1,
- }, &requiredpb.Fixed64{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1),
- }.Marshal(),
- },
- {
- desc: "required bytes unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Bytes{}),
- },
- {
- desc: "required bytes set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "v": "",
- }, &requiredpb.Bytes{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.Bytes(nil),
- }.Marshal(),
- },
- {
- desc: "required message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Message{}),
- },
- {
- desc: "required message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "v": protobuild.Message{},
- }, &requiredpb.Message{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- }.Marshal(),
- },
- {
- desc: "required group unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Group{}),
- },
- {
- desc: "required group set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "group": protobuild.Message{},
- }, &requiredpb.Group{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "required field with incompatible wire type",
- checkFastInit: true,
- partial: true,
- decodeTo: []proto.Message{build(
- &testpb.TestRequired{},
- unknown(protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
- }.Marshal()),
- )},
- wire: protopack.Message{
- protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
- }.Marshal(),
- },
- {
- desc: "required field in optional message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "optional_message": protobuild.Message{},
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- }.Marshal(),
- },
- {
- desc: "required field in optional message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "optional_message": protobuild.Message{
- "required_field": 1,
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in optional message set (split across multiple tags)",
- checkFastInit: false, // fast init checks don't handle split messages
- nocheckValidInit: true, // validation doesn't either
- decodeTo: makeMessages(protobuild.Message{
- "optional_message": protobuild.Message{
- "required_field": 1,
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in repeated message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "repeated_message": []protobuild.Message{
- {"required_field": 1},
- {},
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- }.Marshal(),
- },
- {
- desc: "required field in repeated message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "repeated_message": []protobuild.Message{
- {"required_field": 1},
- {"required_field": 2},
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in map message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "map_message": map[int32]protobuild.Message{
- 1: {"required_field": 1},
- 2: {},
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- }),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in absent map message value",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "map_message": map[int32]protobuild.Message{
- 2: {},
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in map message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "map_message": map[int32]protobuild.Message{
- 1: {"required_field": 1},
- 2: {"required_field": 2},
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- }),
- protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in optional group unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "optionalgroup": protobuild.Message{},
- }, &testpb.TestRequiredGroupFields{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "required field in optional group set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "optionalgroup": protobuild.Message{
- "a": 1,
- },
- }, &testpb.TestRequiredGroupFields{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.StartGroupType},
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{1, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "required field in repeated group unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "repeatedgroup": []protobuild.Message{
- {"a": 1},
- {},
- },
- }, &testpb.TestRequiredGroupFields{}),
- wire: protopack.Message{
- protopack.Tag{3, protopack.StartGroupType},
- protopack.Tag{4, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{3, protopack.EndGroupType},
- protopack.Tag{3, protopack.StartGroupType},
- protopack.Tag{3, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "required field in repeated group set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "repeatedgroup": []protobuild.Message{
- {"a": 1},
- {"a": 2},
- },
- }, &testpb.TestRequiredGroupFields{}),
- wire: protopack.Message{
- protopack.Tag{3, protopack.StartGroupType},
- protopack.Tag{4, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{3, protopack.EndGroupType},
- protopack.Tag{3, protopack.StartGroupType},
- protopack.Tag{4, protopack.VarintType}, protopack.Varint(2),
- protopack.Tag{3, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "required field in oneof message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "oneof_message": protobuild.Message{},
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{})}.Marshal(),
- },
- {
- desc: "required field in oneof message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "oneof_message": protobuild.Message{
- "required_field": 1,
- },
- }, &testpb.TestRequiredForeign{}),
- wire: protopack.Message{protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- })}.Marshal(),
- },
- {
- desc: "required field in extension message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "single": protobuild.Message{},
- }, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- }.Marshal(),
- },
- {
- desc: "required field in extension message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "single": protobuild.Message{
- "required_field": 1,
- },
- }, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- }.Marshal(),
- },
- {
- desc: "required field in repeated extension message unset",
- checkFastInit: true,
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "multi": []protobuild.Message{
- {"required_field": 1},
- {},
- },
- }, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
- }.Marshal(),
- },
- {
- desc: "required field in repeated extension message set",
- checkFastInit: true,
- decodeTo: makeMessages(protobuild.Message{
- "multi": []protobuild.Message{
- {"required_field": 1},
- {"required_field": 2},
- },
- }, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- }),
- protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
- }),
- }.Marshal(),
- },
- {
- desc: "nil messages",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*test3pb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- },
- {
- desc: "legacy",
- partial: true,
- decodeTo: makeMessages(protobuild.Message{
- "f1": protobuild.Message{
- "optional_int32": 1,
- "optional_child_enum": "ALPHA",
- "optional_child_message": protobuild.Message{
- "f1": "x",
- },
- "optionalgroup": protobuild.Message{
- "f1": "x",
- },
- "repeated_child_message": []protobuild.Message{
- {"f1": "x"},
- },
- "repeatedgroup": []protobuild.Message{
- {"f1": "x"},
- },
- "map_bool_child_message": map[bool]protobuild.Message{
- true: {"f1": "x"},
- },
- "oneof_child_message": protobuild.Message{
- "f1": "x",
- },
- },
- }, &legacypb.Legacy{}),
- wire: protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{101, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{115, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{116, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
- }),
- protopack.Tag{120, protopack.StartGroupType},
- protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
- protopack.Tag{120, protopack.EndGroupType},
- protopack.Tag{516, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
- }),
- protopack.Tag{520, protopack.StartGroupType},
- protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
- protopack.Tag{520, protopack.EndGroupType},
- protopack.Tag{616, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
- }),
- }),
- protopack.Tag{716, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
- }),
- }),
- }.Marshal(),
- validationStatus: impl.ValidationUnknown,
- },
- {
- desc: "first reserved field number",
- decodeTo: makeMessages(protobuild.Message{
- protobuild.Unknown: protopack.Message{
- protopack.Tag{protopack.FirstReservedNumber, protopack.VarintType}, protopack.Varint(1004),
- }.Marshal(),
- }),
- wire: protopack.Message{
- protopack.Tag{protopack.FirstReservedNumber, protopack.VarintType}, protopack.Varint(1004),
- }.Marshal(),
- },
- {
- desc: "last reserved field number",
- decodeTo: makeMessages(protobuild.Message{
- protobuild.Unknown: protopack.Message{
- protopack.Tag{protopack.LastReservedNumber, protopack.VarintType}, protopack.Varint(1005),
- }.Marshal(),
- }),
- wire: protopack.Message{
- protopack.Tag{protopack.LastReservedNumber, protopack.VarintType}, protopack.Varint(1005),
- }.Marshal(),
- },
- {
- desc: "nested unknown extension",
- unmarshalOptions: proto.UnmarshalOptions{
- DiscardUnknown: true,
- Resolver: filterResolver{
- filter: func(name protoreflect.FullName) bool {
- switch name.Name() {
- case "optional_nested_message",
- "optional_int32":
- return true
- }
- return false
- },
- resolver: protoregistry.GlobalTypes,
- },
- },
- decodeTo: makeMessages(protobuild.Message{
- "optional_nested_message": protobuild.Message{
- "corecursive": protobuild.Message{
- "optional_nested_message": protobuild.Message{
- "corecursive": protobuild.Message{
- "optional_int32": 42,
- },
- },
- },
- },
- }, &testpb.TestAllExtensions{}),
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(43),
- }),
- }),
- }),
- }),
- }.Marshal(),
- },
- }
- var testInvalidMessages = []testProto{
- {
- desc: "invalid UTF-8 in optional string field",
- decodeTo: makeMessages(protobuild.Message{
- "optional_string": "abc\xff",
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{14, protopack.BytesType}, protopack.String("abc\xff"),
- }.Marshal(),
- },
- {
- desc: "invalid UTF-8 in singular string field",
- decodeTo: makeMessages(protobuild.Message{
- "singular_string": "abc\xff",
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{94, protopack.BytesType}, protopack.String("abc\xff"),
- }.Marshal(),
- },
- {
- desc: "invalid UTF-8 in repeated string field",
- decodeTo: makeMessages(protobuild.Message{
- "repeated_string": []string{"foo", "abc\xff"},
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{44, protopack.BytesType}, protopack.String("foo"),
- protopack.Tag{44, protopack.BytesType}, protopack.String("abc\xff"),
- }.Marshal(),
- },
- {
- desc: "invalid UTF-8 in nested message",
- decodeTo: makeMessages(protobuild.Message{
- "optional_nested_message": protobuild.Message{
- "corecursive": protobuild.Message{
- "singular_string": "abc\xff",
- },
- },
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{94, protopack.BytesType}, protopack.String("abc\xff"),
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "invalid UTF-8 in oneof field",
- decodeTo: makeMessages(protobuild.Message{
- "oneof_string": "abc\xff",
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{protopack.Tag{113, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
- },
- {
- desc: "invalid UTF-8 in map key",
- decodeTo: makeMessages(protobuild.Message{
- "map_string_string": map[string]string{"key\xff": "val"},
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("key\xff"),
- protopack.Tag{2, protopack.BytesType}, protopack.String("val"),
- }),
- }.Marshal(),
- },
- {
- desc: "invalid UTF-8 in map value",
- decodeTo: makeMessages(protobuild.Message{
- "map_string_string": map[string]string{"key": "val\xff"},
- }, &test3pb.TestAllTypes{}),
- wire: protopack.Message{
- protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.BytesType}, protopack.String("key"),
- protopack.Tag{2, protopack.BytesType}, protopack.String("val\xff"),
- }),
- }.Marshal(),
- },
- {
- desc: "invalid field number zero",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{protopack.MinValidNumber - 1, protopack.VarintType}, protopack.Varint(1001),
- }.Marshal(),
- },
- {
- desc: "invalid field numbers zero and one",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{protopack.MinValidNumber - 1, protopack.VarintType}, protopack.Varint(1002),
- protopack.Tag{protopack.MinValidNumber, protopack.VarintType}, protopack.Varint(1003),
- }.Marshal(),
- },
- {
- desc: "invalid field numbers max and max+1",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{protopack.MaxValidNumber, protopack.VarintType}, protopack.Varint(1006),
- protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(1007),
- }.Marshal(),
- },
- {
- desc: "invalid field number max+1",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(1008),
- }.Marshal(),
- },
- {
- desc: "invalid field number wraps int32",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Varint(2234993595104), protopack.Varint(0),
- }.Marshal(),
- },
- {
- desc: "invalid field number in map",
- decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
- wire: protopack.Message{
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
- protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(0),
- }),
- }.Marshal(),
- },
- {
- desc: "invalid tag varint",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: []byte{0xff},
- },
- {
- desc: "field number too small",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{0, protopack.VarintType}, protopack.Varint(0),
- }.Marshal(),
- },
- {
- desc: "field number too large",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{protowire.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(0),
- }.Marshal(),
- },
- {
- desc: "invalid tag varint in message field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Raw{0xff},
- }),
- }.Marshal(),
- },
- {
- desc: "invalid tag varint in repeated message field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Raw{0xff},
- }),
- }.Marshal(),
- },
- {
- desc: "invalid varint in group field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{16, protopack.StartGroupType},
- protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Raw{0xff},
- }),
- protopack.Tag{16, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "invalid varint in repeated group field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{46, protopack.StartGroupType},
- protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Raw{0xff},
- }),
- protopack.Tag{46, protopack.EndGroupType},
- }.Marshal(),
- },
- {
- desc: "unterminated repeated group field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{46, protopack.StartGroupType},
- }.Marshal(),
- },
- {
- desc: "invalid tag varint in map item",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
- protopack.Raw{0xff},
- }),
- }.Marshal(),
- },
- {
- desc: "invalid tag varint in map message value",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
- protopack.Raw{0xff},
- }),
- }),
- }.Marshal(),
- },
- {
- desc: "invalid packed int32 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{31, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "invalid packed int64 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{32, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "invalid packed uint32 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{33, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "invalid packed uint64 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{34, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "invalid packed sint32 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{35, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "invalid packed sint64 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{36, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "invalid packed fixed32 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{37, protopack.BytesType}, protopack.Bytes{0x00},
- }.Marshal(),
- },
- {
- desc: "invalid packed fixed64 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{38, protopack.BytesType}, protopack.Bytes{0x00},
- }.Marshal(),
- },
- {
- desc: "invalid packed sfixed32 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{39, protopack.BytesType}, protopack.Bytes{0x00},
- }.Marshal(),
- },
- {
- desc: "invalid packed sfixed64 field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{40, protopack.BytesType}, protopack.Bytes{0x00},
- }.Marshal(),
- },
- {
- desc: "invalid packed float field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{41, protopack.BytesType}, protopack.Bytes{0x00},
- }.Marshal(),
- },
- {
- desc: "invalid packed double field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{42, protopack.BytesType}, protopack.Bytes{0x00},
- }.Marshal(),
- },
- {
- desc: "invalid packed bool field",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{43, protopack.BytesType}, protopack.Bytes{0xff},
- }.Marshal(),
- },
- {
- desc: "bytes field overruns message",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix{protopack.Message{
- protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix{protopack.Message{
- protopack.Tag{15, protopack.BytesType}, protopack.Varint(2),
- }},
- protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
- }},
- }.Marshal(),
- },
- {
- desc: "varint field overruns message",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{1, protopack.VarintType},
- }.Marshal(),
- },
- {
- desc: "bytes field lacks size",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{18, protopack.BytesType},
- }.Marshal(),
- },
- {
- desc: "varint overflow",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{1, protopack.VarintType},
- protopack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"),
- }.Marshal(),
- },
- {
- desc: "varint length overrun",
- decodeTo: []proto.Message{
- (*testpb.TestAllTypes)(nil),
- (*testpb.TestAllExtensions)(nil),
- },
- wire: protopack.Message{
- protopack.Tag{1, protopack.VarintType},
- protopack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
- }.Marshal(),
- },
- }
- type filterResolver struct {
- filter func(name protoreflect.FullName) bool
- resolver protoregistry.ExtensionTypeResolver
- }
- func (f filterResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
- if !f.filter(field) {
- return nil, protoregistry.NotFound
- }
- return f.resolver.FindExtensionByName(field)
- }
- func (f filterResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
- xt, err := f.resolver.FindExtensionByNumber(message, field)
- if err != nil {
- return nil, err
- }
- if !f.filter(xt.TypeDescriptor().FullName()) {
- return nil, protoregistry.NotFound
- }
- return xt, nil
- }
|