testmessages_test.go 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "google.golang.org/protobuf/encoding/protowire"
  7. "google.golang.org/protobuf/internal/impl"
  8. "google.golang.org/protobuf/internal/protobuild"
  9. "google.golang.org/protobuf/proto"
  10. "google.golang.org/protobuf/reflect/protoreflect"
  11. "google.golang.org/protobuf/reflect/protoregistry"
  12. "google.golang.org/protobuf/testing/protopack"
  13. legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
  14. requiredpb "google.golang.org/protobuf/internal/testprotos/required"
  15. testpb "google.golang.org/protobuf/internal/testprotos/test"
  16. test3pb "google.golang.org/protobuf/internal/testprotos/test3"
  17. )
  18. type testProto struct {
  19. desc string
  20. decodeTo []proto.Message
  21. wire []byte
  22. partial bool
  23. noEncode bool
  24. checkFastInit bool
  25. unmarshalOptions proto.UnmarshalOptions
  26. validationStatus impl.ValidationStatus
  27. nocheckValidInit bool
  28. }
  29. func makeMessages(in protobuild.Message, messages ...proto.Message) []proto.Message {
  30. if len(messages) == 0 {
  31. messages = []proto.Message{
  32. &testpb.TestAllTypes{},
  33. &test3pb.TestAllTypes{},
  34. &testpb.TestAllExtensions{},
  35. }
  36. }
  37. for _, m := range messages {
  38. in.Build(m.ProtoReflect())
  39. }
  40. return messages
  41. }
  42. func templateMessages(messages ...proto.Message) []protoreflect.MessageType {
  43. if len(messages) == 0 {
  44. messages = []proto.Message{
  45. (*testpb.TestAllTypes)(nil),
  46. (*test3pb.TestAllTypes)(nil),
  47. (*testpb.TestAllExtensions)(nil),
  48. }
  49. }
  50. var out []protoreflect.MessageType
  51. for _, m := range messages {
  52. out = append(out, m.ProtoReflect().Type())
  53. }
  54. return out
  55. }
  56. var testValidMessages = []testProto{
  57. {
  58. desc: "basic scalar types",
  59. checkFastInit: true,
  60. decodeTo: makeMessages(protobuild.Message{
  61. "optional_int32": 1001,
  62. "optional_int64": 1002,
  63. "optional_uint32": 1003,
  64. "optional_uint64": 1004,
  65. "optional_sint32": 1005,
  66. "optional_sint64": 1006,
  67. "optional_fixed32": 1007,
  68. "optional_fixed64": 1008,
  69. "optional_sfixed32": 1009,
  70. "optional_sfixed64": 1010,
  71. "optional_float": 1011.5,
  72. "optional_double": 1012.5,
  73. "optional_bool": true,
  74. "optional_string": "string",
  75. "optional_bytes": []byte("bytes"),
  76. "optional_nested_enum": "BAR",
  77. }),
  78. wire: protopack.Message{
  79. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1001),
  80. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
  81. protopack.Tag{3, protopack.VarintType}, protopack.Uvarint(1003),
  82. protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(1004),
  83. protopack.Tag{5, protopack.VarintType}, protopack.Svarint(1005),
  84. protopack.Tag{6, protopack.VarintType}, protopack.Svarint(1006),
  85. protopack.Tag{7, protopack.Fixed32Type}, protopack.Uint32(1007),
  86. protopack.Tag{8, protopack.Fixed64Type}, protopack.Uint64(1008),
  87. protopack.Tag{9, protopack.Fixed32Type}, protopack.Int32(1009),
  88. protopack.Tag{10, protopack.Fixed64Type}, protopack.Int64(1010),
  89. protopack.Tag{11, protopack.Fixed32Type}, protopack.Float32(1011.5),
  90. protopack.Tag{12, protopack.Fixed64Type}, protopack.Float64(1012.5),
  91. protopack.Tag{13, protopack.VarintType}, protopack.Bool(true),
  92. protopack.Tag{14, protopack.BytesType}, protopack.String("string"),
  93. protopack.Tag{15, protopack.BytesType}, protopack.Bytes([]byte("bytes")),
  94. protopack.Tag{21, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
  95. }.Marshal(),
  96. },
  97. {
  98. desc: "zero values",
  99. decodeTo: makeMessages(protobuild.Message{
  100. "optional_int32": 0,
  101. "optional_int64": 0,
  102. "optional_uint32": 0,
  103. "optional_uint64": 0,
  104. "optional_sint32": 0,
  105. "optional_sint64": 0,
  106. "optional_fixed32": 0,
  107. "optional_fixed64": 0,
  108. "optional_sfixed32": 0,
  109. "optional_sfixed64": 0,
  110. "optional_float": 0,
  111. "optional_double": 0,
  112. "optional_bool": false,
  113. "optional_string": "",
  114. "optional_bytes": []byte{},
  115. }),
  116. wire: protopack.Message{
  117. protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
  118. protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
  119. protopack.Tag{3, protopack.VarintType}, protopack.Uvarint(0),
  120. protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(0),
  121. protopack.Tag{5, protopack.VarintType}, protopack.Svarint(0),
  122. protopack.Tag{6, protopack.VarintType}, protopack.Svarint(0),
  123. protopack.Tag{7, protopack.Fixed32Type}, protopack.Uint32(0),
  124. protopack.Tag{8, protopack.Fixed64Type}, protopack.Uint64(0),
  125. protopack.Tag{9, protopack.Fixed32Type}, protopack.Int32(0),
  126. protopack.Tag{10, protopack.Fixed64Type}, protopack.Int64(0),
  127. protopack.Tag{11, protopack.Fixed32Type}, protopack.Float32(0),
  128. protopack.Tag{12, protopack.Fixed64Type}, protopack.Float64(0),
  129. protopack.Tag{13, protopack.VarintType}, protopack.Bool(false),
  130. protopack.Tag{14, protopack.BytesType}, protopack.String(""),
  131. protopack.Tag{15, protopack.BytesType}, protopack.Bytes(nil),
  132. }.Marshal(),
  133. },
  134. {
  135. desc: "proto3 zero values",
  136. decodeTo: makeMessages(protobuild.Message{
  137. "singular_int32": 0,
  138. "singular_int64": 0,
  139. "singular_uint32": 0,
  140. "singular_uint64": 0,
  141. "singular_sint32": 0,
  142. "singular_sint64": 0,
  143. "singular_fixed32": 0,
  144. "singular_fixed64": 0,
  145. "singular_sfixed32": 0,
  146. "singular_sfixed64": 0,
  147. "singular_float": 0,
  148. "singular_double": 0,
  149. "singular_bool": false,
  150. "singular_string": "",
  151. "singular_bytes": []byte{},
  152. }, &test3pb.TestAllTypes{}),
  153. wire: protopack.Message{
  154. protopack.Tag{81, protopack.VarintType}, protopack.Varint(0),
  155. protopack.Tag{82, protopack.VarintType}, protopack.Varint(0),
  156. protopack.Tag{83, protopack.VarintType}, protopack.Uvarint(0),
  157. protopack.Tag{84, protopack.VarintType}, protopack.Uvarint(0),
  158. protopack.Tag{85, protopack.VarintType}, protopack.Svarint(0),
  159. protopack.Tag{86, protopack.VarintType}, protopack.Svarint(0),
  160. protopack.Tag{87, protopack.Fixed32Type}, protopack.Uint32(0),
  161. protopack.Tag{88, protopack.Fixed64Type}, protopack.Uint64(0),
  162. protopack.Tag{89, protopack.Fixed32Type}, protopack.Int32(0),
  163. protopack.Tag{90, protopack.Fixed64Type}, protopack.Int64(0),
  164. protopack.Tag{91, protopack.Fixed32Type}, protopack.Float32(0),
  165. protopack.Tag{92, protopack.Fixed64Type}, protopack.Float64(0),
  166. protopack.Tag{93, protopack.VarintType}, protopack.Bool(false),
  167. protopack.Tag{94, protopack.BytesType}, protopack.String(""),
  168. protopack.Tag{95, protopack.BytesType}, protopack.Bytes(nil),
  169. }.Marshal(),
  170. },
  171. {
  172. desc: "groups",
  173. decodeTo: makeMessages(protobuild.Message{
  174. "optionalgroup": protobuild.Message{
  175. "a": 1017,
  176. "same_field_number": 1016,
  177. },
  178. }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
  179. wire: protopack.Message{
  180. protopack.Tag{16, protopack.StartGroupType},
  181. protopack.Tag{17, protopack.VarintType}, protopack.Varint(1017),
  182. protopack.Tag{16, protopack.VarintType}, protopack.Varint(1016),
  183. protopack.Tag{16, protopack.EndGroupType},
  184. }.Marshal(),
  185. },
  186. {
  187. desc: "groups (field overridden)",
  188. decodeTo: makeMessages(protobuild.Message{
  189. "optionalgroup": protobuild.Message{
  190. "a": 2,
  191. },
  192. }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
  193. wire: protopack.Message{
  194. protopack.Tag{16, protopack.StartGroupType},
  195. protopack.Tag{17, protopack.VarintType}, protopack.Varint(1),
  196. protopack.Tag{16, protopack.EndGroupType},
  197. protopack.Tag{16, protopack.StartGroupType},
  198. protopack.Tag{17, protopack.VarintType}, protopack.Varint(2),
  199. protopack.Tag{16, protopack.EndGroupType},
  200. }.Marshal(),
  201. },
  202. {
  203. desc: "messages",
  204. decodeTo: makeMessages(protobuild.Message{
  205. "optional_nested_message": protobuild.Message{
  206. "a": 42,
  207. "corecursive": protobuild.Message{
  208. "optional_int32": 43,
  209. },
  210. },
  211. }),
  212. wire: protopack.Message{
  213. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  214. protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
  215. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  216. protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
  217. }),
  218. }),
  219. }.Marshal(),
  220. },
  221. {
  222. desc: "messages (split across multiple tags)",
  223. decodeTo: makeMessages(protobuild.Message{
  224. "optional_nested_message": protobuild.Message{
  225. "a": 42,
  226. "corecursive": protobuild.Message{
  227. "optional_int32": 43,
  228. },
  229. },
  230. }),
  231. wire: protopack.Message{
  232. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  233. protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
  234. }),
  235. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  236. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  237. protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
  238. }),
  239. }),
  240. }.Marshal(),
  241. },
  242. {
  243. desc: "messages (field overridden)",
  244. decodeTo: makeMessages(protobuild.Message{
  245. "optional_nested_message": protobuild.Message{
  246. "a": 2,
  247. },
  248. }),
  249. wire: protopack.Message{
  250. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  251. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  252. }),
  253. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  254. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  255. }),
  256. }.Marshal(),
  257. },
  258. {
  259. desc: "basic repeated types",
  260. decodeTo: makeMessages(protobuild.Message{
  261. "repeated_int32": []int32{1001, 2001},
  262. "repeated_int64": []int64{1002, 2002},
  263. "repeated_uint32": []uint32{1003, 2003},
  264. "repeated_uint64": []uint64{1004, 2004},
  265. "repeated_sint32": []int32{1005, 2005},
  266. "repeated_sint64": []int64{1006, 2006},
  267. "repeated_fixed32": []uint32{1007, 2007},
  268. "repeated_fixed64": []uint64{1008, 2008},
  269. "repeated_sfixed32": []int32{1009, 2009},
  270. "repeated_sfixed64": []int64{1010, 2010},
  271. "repeated_float": []float32{1011.5, 2011.5},
  272. "repeated_double": []float64{1012.5, 2012.5},
  273. "repeated_bool": []bool{true, false},
  274. "repeated_string": []string{"foo", "bar"},
  275. "repeated_bytes": []string{"FOO", "BAR"},
  276. "repeated_nested_enum": []string{"FOO", "BAR"},
  277. }),
  278. wire: protopack.Message{
  279. protopack.Tag{31, protopack.VarintType}, protopack.Varint(1001),
  280. protopack.Tag{31, protopack.VarintType}, protopack.Varint(2001),
  281. protopack.Tag{32, protopack.VarintType}, protopack.Varint(1002),
  282. protopack.Tag{32, protopack.VarintType}, protopack.Varint(2002),
  283. protopack.Tag{33, protopack.VarintType}, protopack.Uvarint(1003),
  284. protopack.Tag{33, protopack.VarintType}, protopack.Uvarint(2003),
  285. protopack.Tag{34, protopack.VarintType}, protopack.Uvarint(1004),
  286. protopack.Tag{34, protopack.VarintType}, protopack.Uvarint(2004),
  287. protopack.Tag{35, protopack.VarintType}, protopack.Svarint(1005),
  288. protopack.Tag{35, protopack.VarintType}, protopack.Svarint(2005),
  289. protopack.Tag{36, protopack.VarintType}, protopack.Svarint(1006),
  290. protopack.Tag{36, protopack.VarintType}, protopack.Svarint(2006),
  291. protopack.Tag{37, protopack.Fixed32Type}, protopack.Uint32(1007),
  292. protopack.Tag{37, protopack.Fixed32Type}, protopack.Uint32(2007),
  293. protopack.Tag{38, protopack.Fixed64Type}, protopack.Uint64(1008),
  294. protopack.Tag{38, protopack.Fixed64Type}, protopack.Uint64(2008),
  295. protopack.Tag{39, protopack.Fixed32Type}, protopack.Int32(1009),
  296. protopack.Tag{39, protopack.Fixed32Type}, protopack.Int32(2009),
  297. protopack.Tag{40, protopack.Fixed64Type}, protopack.Int64(1010),
  298. protopack.Tag{40, protopack.Fixed64Type}, protopack.Int64(2010),
  299. protopack.Tag{41, protopack.Fixed32Type}, protopack.Float32(1011.5),
  300. protopack.Tag{41, protopack.Fixed32Type}, protopack.Float32(2011.5),
  301. protopack.Tag{42, protopack.Fixed64Type}, protopack.Float64(1012.5),
  302. protopack.Tag{42, protopack.Fixed64Type}, protopack.Float64(2012.5),
  303. protopack.Tag{43, protopack.VarintType}, protopack.Bool(true),
  304. protopack.Tag{43, protopack.VarintType}, protopack.Bool(false),
  305. protopack.Tag{44, protopack.BytesType}, protopack.String("foo"),
  306. protopack.Tag{44, protopack.BytesType}, protopack.String("bar"),
  307. protopack.Tag{45, protopack.BytesType}, protopack.Bytes([]byte("FOO")),
  308. protopack.Tag{45, protopack.BytesType}, protopack.Bytes([]byte("BAR")),
  309. protopack.Tag{51, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_FOO)),
  310. protopack.Tag{51, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
  311. }.Marshal(),
  312. },
  313. {
  314. desc: "basic repeated types (packed encoding)",
  315. decodeTo: makeMessages(protobuild.Message{
  316. "repeated_int32": []int32{1001, 2001},
  317. "repeated_int64": []int64{1002, 2002},
  318. "repeated_uint32": []uint32{1003, 2003},
  319. "repeated_uint64": []uint64{1004, 2004},
  320. "repeated_sint32": []int32{1005, 2005},
  321. "repeated_sint64": []int64{1006, 2006},
  322. "repeated_fixed32": []uint32{1007, 2007},
  323. "repeated_fixed64": []uint64{1008, 2008},
  324. "repeated_sfixed32": []int32{1009, 2009},
  325. "repeated_sfixed64": []int64{1010, 2010},
  326. "repeated_float": []float32{1011.5, 2011.5},
  327. "repeated_double": []float64{1012.5, 2012.5},
  328. "repeated_bool": []bool{true, false},
  329. "repeated_nested_enum": []string{"FOO", "BAR"},
  330. }),
  331. wire: protopack.Message{
  332. protopack.Tag{31, protopack.BytesType}, protopack.LengthPrefix{
  333. protopack.Varint(1001), protopack.Varint(2001),
  334. },
  335. protopack.Tag{32, protopack.BytesType}, protopack.LengthPrefix{
  336. protopack.Varint(1002), protopack.Varint(2002),
  337. },
  338. protopack.Tag{33, protopack.BytesType}, protopack.LengthPrefix{
  339. protopack.Uvarint(1003), protopack.Uvarint(2003),
  340. },
  341. protopack.Tag{34, protopack.BytesType}, protopack.LengthPrefix{
  342. protopack.Uvarint(1004), protopack.Uvarint(2004),
  343. },
  344. protopack.Tag{35, protopack.BytesType}, protopack.LengthPrefix{
  345. protopack.Svarint(1005), protopack.Svarint(2005),
  346. },
  347. protopack.Tag{36, protopack.BytesType}, protopack.LengthPrefix{
  348. protopack.Svarint(1006), protopack.Svarint(2006),
  349. },
  350. protopack.Tag{37, protopack.BytesType}, protopack.LengthPrefix{
  351. protopack.Uint32(1007), protopack.Uint32(2007),
  352. },
  353. protopack.Tag{38, protopack.BytesType}, protopack.LengthPrefix{
  354. protopack.Uint64(1008), protopack.Uint64(2008),
  355. },
  356. protopack.Tag{39, protopack.BytesType}, protopack.LengthPrefix{
  357. protopack.Int32(1009), protopack.Int32(2009),
  358. },
  359. protopack.Tag{40, protopack.BytesType}, protopack.LengthPrefix{
  360. protopack.Int64(1010), protopack.Int64(2010),
  361. },
  362. protopack.Tag{41, protopack.BytesType}, protopack.LengthPrefix{
  363. protopack.Float32(1011.5), protopack.Float32(2011.5),
  364. },
  365. protopack.Tag{42, protopack.BytesType}, protopack.LengthPrefix{
  366. protopack.Float64(1012.5), protopack.Float64(2012.5),
  367. },
  368. protopack.Tag{43, protopack.BytesType}, protopack.LengthPrefix{
  369. protopack.Bool(true), protopack.Bool(false),
  370. },
  371. protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{
  372. protopack.Varint(int(testpb.TestAllTypes_FOO)),
  373. protopack.Varint(int(testpb.TestAllTypes_BAR)),
  374. },
  375. }.Marshal(),
  376. },
  377. {
  378. desc: "basic repeated types (zero-length packed encoding)",
  379. decodeTo: makeMessages(protobuild.Message{
  380. "repeated_int32": []int32{},
  381. "repeated_int64": []int64{},
  382. "repeated_uint32": []uint32{},
  383. "repeated_uint64": []uint64{},
  384. "repeated_sint32": []int32{},
  385. "repeated_sint64": []int64{},
  386. "repeated_fixed32": []uint32{},
  387. "repeated_fixed64": []uint64{},
  388. "repeated_sfixed32": []int32{},
  389. "repeated_sfixed64": []int64{},
  390. "repeated_float": []float32{},
  391. "repeated_double": []float64{},
  392. "repeated_bool": []bool{},
  393. "repeated_nested_enum": []string{},
  394. }),
  395. wire: protopack.Message{
  396. protopack.Tag{31, protopack.BytesType}, protopack.LengthPrefix{},
  397. protopack.Tag{32, protopack.BytesType}, protopack.LengthPrefix{},
  398. protopack.Tag{33, protopack.BytesType}, protopack.LengthPrefix{},
  399. protopack.Tag{34, protopack.BytesType}, protopack.LengthPrefix{},
  400. protopack.Tag{35, protopack.BytesType}, protopack.LengthPrefix{},
  401. protopack.Tag{36, protopack.BytesType}, protopack.LengthPrefix{},
  402. protopack.Tag{37, protopack.BytesType}, protopack.LengthPrefix{},
  403. protopack.Tag{38, protopack.BytesType}, protopack.LengthPrefix{},
  404. protopack.Tag{39, protopack.BytesType}, protopack.LengthPrefix{},
  405. protopack.Tag{40, protopack.BytesType}, protopack.LengthPrefix{},
  406. protopack.Tag{41, protopack.BytesType}, protopack.LengthPrefix{},
  407. protopack.Tag{42, protopack.BytesType}, protopack.LengthPrefix{},
  408. protopack.Tag{43, protopack.BytesType}, protopack.LengthPrefix{},
  409. protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{},
  410. }.Marshal(),
  411. },
  412. {
  413. desc: "packed repeated types",
  414. decodeTo: makeMessages(protobuild.Message{
  415. "packed_int32": []int32{1001, 2001},
  416. "packed_int64": []int64{1002, 2002},
  417. "packed_uint32": []uint32{1003, 2003},
  418. "packed_uint64": []uint64{1004, 2004},
  419. "packed_sint32": []int32{1005, 2005},
  420. "packed_sint64": []int64{1006, 2006},
  421. "packed_fixed32": []uint32{1007, 2007},
  422. "packed_fixed64": []uint64{1008, 2008},
  423. "packed_sfixed32": []int32{1009, 2009},
  424. "packed_sfixed64": []int64{1010, 2010},
  425. "packed_float": []float32{1011.5, 2011.5},
  426. "packed_double": []float64{1012.5, 2012.5},
  427. "packed_bool": []bool{true, false},
  428. "packed_enum": []string{"FOREIGN_FOO", "FOREIGN_BAR"},
  429. }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
  430. wire: protopack.Message{
  431. protopack.Tag{90, protopack.BytesType}, protopack.LengthPrefix{
  432. protopack.Varint(1001), protopack.Varint(2001),
  433. },
  434. protopack.Tag{91, protopack.BytesType}, protopack.LengthPrefix{
  435. protopack.Varint(1002), protopack.Varint(2002),
  436. },
  437. protopack.Tag{92, protopack.BytesType}, protopack.LengthPrefix{
  438. protopack.Uvarint(1003), protopack.Uvarint(2003),
  439. },
  440. protopack.Tag{93, protopack.BytesType}, protopack.LengthPrefix{
  441. protopack.Uvarint(1004), protopack.Uvarint(2004),
  442. },
  443. protopack.Tag{94, protopack.BytesType}, protopack.LengthPrefix{
  444. protopack.Svarint(1005), protopack.Svarint(2005),
  445. },
  446. protopack.Tag{95, protopack.BytesType}, protopack.LengthPrefix{
  447. protopack.Svarint(1006), protopack.Svarint(2006),
  448. },
  449. protopack.Tag{96, protopack.BytesType}, protopack.LengthPrefix{
  450. protopack.Uint32(1007), protopack.Uint32(2007),
  451. },
  452. protopack.Tag{97, protopack.BytesType}, protopack.LengthPrefix{
  453. protopack.Uint64(1008), protopack.Uint64(2008),
  454. },
  455. protopack.Tag{98, protopack.BytesType}, protopack.LengthPrefix{
  456. protopack.Int32(1009), protopack.Int32(2009),
  457. },
  458. protopack.Tag{99, protopack.BytesType}, protopack.LengthPrefix{
  459. protopack.Int64(1010), protopack.Int64(2010),
  460. },
  461. protopack.Tag{100, protopack.BytesType}, protopack.LengthPrefix{
  462. protopack.Float32(1011.5), protopack.Float32(2011.5),
  463. },
  464. protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{
  465. protopack.Float64(1012.5), protopack.Float64(2012.5),
  466. },
  467. protopack.Tag{102, protopack.BytesType}, protopack.LengthPrefix{
  468. protopack.Bool(true), protopack.Bool(false),
  469. },
  470. protopack.Tag{103, protopack.BytesType}, protopack.LengthPrefix{
  471. protopack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
  472. protopack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
  473. },
  474. }.Marshal(),
  475. },
  476. {
  477. desc: "packed repeated types (zero length)",
  478. decodeTo: makeMessages(protobuild.Message{
  479. "packed_int32": []int32{},
  480. "packed_int64": []int64{},
  481. "packed_uint32": []uint32{},
  482. "packed_uint64": []uint64{},
  483. "packed_sint32": []int32{},
  484. "packed_sint64": []int64{},
  485. "packed_fixed32": []uint32{},
  486. "packed_fixed64": []uint64{},
  487. "packed_sfixed32": []int32{},
  488. "packed_sfixed64": []int64{},
  489. "packed_float": []float32{},
  490. "packed_double": []float64{},
  491. "packed_bool": []bool{},
  492. "packed_enum": []string{},
  493. }, &testpb.TestPackedTypes{}, &testpb.TestPackedExtensions{}),
  494. wire: protopack.Message{
  495. protopack.Tag{90, protopack.BytesType}, protopack.LengthPrefix{},
  496. protopack.Tag{91, protopack.BytesType}, protopack.LengthPrefix{},
  497. protopack.Tag{92, protopack.BytesType}, protopack.LengthPrefix{},
  498. protopack.Tag{93, protopack.BytesType}, protopack.LengthPrefix{},
  499. protopack.Tag{94, protopack.BytesType}, protopack.LengthPrefix{},
  500. protopack.Tag{95, protopack.BytesType}, protopack.LengthPrefix{},
  501. protopack.Tag{96, protopack.BytesType}, protopack.LengthPrefix{},
  502. protopack.Tag{97, protopack.BytesType}, protopack.LengthPrefix{},
  503. protopack.Tag{98, protopack.BytesType}, protopack.LengthPrefix{},
  504. protopack.Tag{99, protopack.BytesType}, protopack.LengthPrefix{},
  505. protopack.Tag{100, protopack.BytesType}, protopack.LengthPrefix{},
  506. protopack.Tag{101, protopack.BytesType}, protopack.LengthPrefix{},
  507. protopack.Tag{102, protopack.BytesType}, protopack.LengthPrefix{},
  508. protopack.Tag{103, protopack.BytesType}, protopack.LengthPrefix{},
  509. }.Marshal(),
  510. },
  511. {
  512. desc: "repeated messages",
  513. decodeTo: makeMessages(protobuild.Message{
  514. "repeated_nested_message": []protobuild.Message{
  515. {"a": 1},
  516. {},
  517. {"a": 2},
  518. },
  519. }),
  520. wire: protopack.Message{
  521. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  522. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  523. }),
  524. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  525. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  526. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  527. }),
  528. }.Marshal(),
  529. },
  530. {
  531. desc: "repeated nil messages",
  532. decodeTo: []proto.Message{&testpb.TestAllTypes{
  533. RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
  534. {A: proto.Int32(1)},
  535. nil,
  536. {A: proto.Int32(2)},
  537. },
  538. }, &test3pb.TestAllTypes{
  539. RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
  540. {A: 1},
  541. nil,
  542. {A: 2},
  543. },
  544. }, build(
  545. &testpb.TestAllExtensions{},
  546. extend(testpb.E_RepeatedNestedMessage, []*testpb.TestAllExtensions_NestedMessage{
  547. {A: proto.Int32(1)},
  548. nil,
  549. {A: proto.Int32(2)},
  550. }),
  551. )},
  552. wire: protopack.Message{
  553. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  554. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  555. }),
  556. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  557. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  558. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  559. }),
  560. }.Marshal(),
  561. },
  562. {
  563. desc: "repeated groups",
  564. decodeTo: makeMessages(protobuild.Message{
  565. "repeatedgroup": []protobuild.Message{
  566. {"a": 1017},
  567. {},
  568. {"a": 2017},
  569. },
  570. }, &testpb.TestAllTypes{}, &testpb.TestAllExtensions{}),
  571. wire: protopack.Message{
  572. protopack.Tag{46, protopack.StartGroupType},
  573. protopack.Tag{47, protopack.VarintType}, protopack.Varint(1017),
  574. protopack.Tag{46, protopack.EndGroupType},
  575. protopack.Tag{46, protopack.StartGroupType},
  576. protopack.Tag{46, protopack.EndGroupType},
  577. protopack.Tag{46, protopack.StartGroupType},
  578. protopack.Tag{47, protopack.VarintType}, protopack.Varint(2017),
  579. protopack.Tag{46, protopack.EndGroupType},
  580. }.Marshal(),
  581. },
  582. {
  583. desc: "repeated nil groups",
  584. decodeTo: []proto.Message{&testpb.TestAllTypes{
  585. Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
  586. {A: proto.Int32(1017)},
  587. nil,
  588. {A: proto.Int32(2017)},
  589. },
  590. }, build(
  591. &testpb.TestAllExtensions{},
  592. extend(testpb.E_Repeatedgroup, []*testpb.RepeatedGroup{
  593. {A: proto.Int32(1017)},
  594. nil,
  595. {A: proto.Int32(2017)},
  596. }),
  597. )},
  598. wire: protopack.Message{
  599. protopack.Tag{46, protopack.StartGroupType},
  600. protopack.Tag{47, protopack.VarintType}, protopack.Varint(1017),
  601. protopack.Tag{46, protopack.EndGroupType},
  602. protopack.Tag{46, protopack.StartGroupType},
  603. protopack.Tag{46, protopack.EndGroupType},
  604. protopack.Tag{46, protopack.StartGroupType},
  605. protopack.Tag{47, protopack.VarintType}, protopack.Varint(2017),
  606. protopack.Tag{46, protopack.EndGroupType},
  607. }.Marshal(),
  608. },
  609. {
  610. desc: "maps",
  611. decodeTo: makeMessages(protobuild.Message{
  612. "map_int32_int32": map[int32]int32{1056: 1156, 2056: 2156},
  613. "map_int64_int64": map[int64]int64{1057: 1157, 2057: 2157},
  614. "map_uint32_uint32": map[uint32]uint32{1058: 1158, 2058: 2158},
  615. "map_uint64_uint64": map[uint64]uint64{1059: 1159, 2059: 2159},
  616. "map_sint32_sint32": map[int32]int32{1060: 1160, 2060: 2160},
  617. "map_sint64_sint64": map[int64]int64{1061: 1161, 2061: 2161},
  618. "map_fixed32_fixed32": map[uint32]uint32{1062: 1162, 2062: 2162},
  619. "map_fixed64_fixed64": map[uint64]uint64{1063: 1163, 2063: 2163},
  620. "map_sfixed32_sfixed32": map[int32]int32{1064: 1164, 2064: 2164},
  621. "map_sfixed64_sfixed64": map[int64]int64{1065: 1165, 2065: 2165},
  622. "map_int32_float": map[int32]float32{1066: 1166.5, 2066: 2166.5},
  623. "map_int32_double": map[int32]float64{1067: 1167.5, 2067: 2167.5},
  624. "map_bool_bool": map[bool]bool{true: false, false: true},
  625. "map_string_string": map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
  626. "map_string_bytes": map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
  627. "map_string_nested_message": map[string]protobuild.Message{
  628. "71.1.key": {"a": 1171},
  629. "71.2.key": {"a": 2171},
  630. },
  631. "map_string_nested_enum": map[string]string{"73.1.key": "FOO", "73.2.key": "BAR"},
  632. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  633. wire: protopack.Message{
  634. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  635. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
  636. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
  637. }),
  638. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  639. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2056),
  640. protopack.Tag{2, protopack.VarintType}, protopack.Varint(2156),
  641. }),
  642. protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  643. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1057),
  644. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1157),
  645. }),
  646. protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  647. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2057),
  648. protopack.Tag{2, protopack.VarintType}, protopack.Varint(2157),
  649. }),
  650. protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  651. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1058),
  652. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1158),
  653. }),
  654. protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  655. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2058),
  656. protopack.Tag{2, protopack.VarintType}, protopack.Varint(2158),
  657. }),
  658. protopack.Tag{59, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  659. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1059),
  660. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1159),
  661. }),
  662. protopack.Tag{59, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  663. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2059),
  664. protopack.Tag{2, protopack.VarintType}, protopack.Varint(2159),
  665. }),
  666. protopack.Tag{60, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  667. protopack.Tag{1, protopack.VarintType}, protopack.Svarint(1060),
  668. protopack.Tag{2, protopack.VarintType}, protopack.Svarint(1160),
  669. }),
  670. protopack.Tag{60, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  671. protopack.Tag{1, protopack.VarintType}, protopack.Svarint(2060),
  672. protopack.Tag{2, protopack.VarintType}, protopack.Svarint(2160),
  673. }),
  674. protopack.Tag{61, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  675. protopack.Tag{1, protopack.VarintType}, protopack.Svarint(1061),
  676. protopack.Tag{2, protopack.VarintType}, protopack.Svarint(1161),
  677. }),
  678. protopack.Tag{61, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  679. protopack.Tag{1, protopack.VarintType}, protopack.Svarint(2061),
  680. protopack.Tag{2, protopack.VarintType}, protopack.Svarint(2161),
  681. }),
  682. protopack.Tag{62, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  683. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1062),
  684. protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(1162),
  685. }),
  686. protopack.Tag{62, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  687. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2062),
  688. protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(2162),
  689. }),
  690. protopack.Tag{63, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  691. protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1063),
  692. protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(1163),
  693. }),
  694. protopack.Tag{63, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  695. protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(2063),
  696. protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(2163),
  697. }),
  698. protopack.Tag{64, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  699. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1064),
  700. protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(1164),
  701. }),
  702. protopack.Tag{64, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  703. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2064),
  704. protopack.Tag{2, protopack.Fixed32Type}, protopack.Int32(2164),
  705. }),
  706. protopack.Tag{65, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  707. protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1065),
  708. protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(1165),
  709. }),
  710. protopack.Tag{65, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  711. protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(2065),
  712. protopack.Tag{2, protopack.Fixed64Type}, protopack.Int64(2165),
  713. }),
  714. protopack.Tag{66, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  715. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1066),
  716. protopack.Tag{2, protopack.Fixed32Type}, protopack.Float32(1166.5),
  717. }),
  718. protopack.Tag{66, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  719. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2066),
  720. protopack.Tag{2, protopack.Fixed32Type}, protopack.Float32(2166.5),
  721. }),
  722. protopack.Tag{67, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  723. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1067),
  724. protopack.Tag{2, protopack.Fixed64Type}, protopack.Float64(1167.5),
  725. }),
  726. protopack.Tag{67, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  727. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2067),
  728. protopack.Tag{2, protopack.Fixed64Type}, protopack.Float64(2167.5),
  729. }),
  730. protopack.Tag{68, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  731. protopack.Tag{1, protopack.VarintType}, protopack.Bool(true),
  732. protopack.Tag{2, protopack.VarintType}, protopack.Bool(false),
  733. }),
  734. protopack.Tag{68, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  735. protopack.Tag{1, protopack.VarintType}, protopack.Bool(false),
  736. protopack.Tag{2, protopack.VarintType}, protopack.Bool(true),
  737. }),
  738. protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  739. protopack.Tag{1, protopack.BytesType}, protopack.String("69.1.key"),
  740. protopack.Tag{2, protopack.BytesType}, protopack.String("69.1.val"),
  741. }),
  742. protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  743. protopack.Tag{1, protopack.BytesType}, protopack.String("69.2.key"),
  744. protopack.Tag{2, protopack.BytesType}, protopack.String("69.2.val"),
  745. }),
  746. protopack.Tag{70, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  747. protopack.Tag{1, protopack.BytesType}, protopack.String("70.1.key"),
  748. protopack.Tag{2, protopack.BytesType}, protopack.String("70.1.val"),
  749. }),
  750. protopack.Tag{70, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  751. protopack.Tag{1, protopack.BytesType}, protopack.String("70.2.key"),
  752. protopack.Tag{2, protopack.BytesType}, protopack.String("70.2.val"),
  753. }),
  754. protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  755. protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
  756. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  757. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
  758. }),
  759. }),
  760. protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  761. protopack.Tag{1, protopack.BytesType}, protopack.String("71.2.key"),
  762. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  763. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2171),
  764. }),
  765. }),
  766. protopack.Tag{73, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  767. protopack.Tag{1, protopack.BytesType}, protopack.String("73.1.key"),
  768. protopack.Tag{2, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_FOO)),
  769. }),
  770. protopack.Tag{73, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  771. protopack.Tag{1, protopack.BytesType}, protopack.String("73.2.key"),
  772. protopack.Tag{2, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR)),
  773. }),
  774. }.Marshal(),
  775. },
  776. {
  777. desc: "map with value before key",
  778. decodeTo: makeMessages(protobuild.Message{
  779. "map_int32_int32": map[int32]int32{1056: 1156},
  780. "map_string_nested_message": map[string]protobuild.Message{
  781. "71.1.key": {"a": 1171},
  782. },
  783. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  784. wire: protopack.Message{
  785. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  786. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
  787. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
  788. }),
  789. protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  790. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  791. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
  792. }),
  793. protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
  794. }),
  795. }.Marshal(),
  796. },
  797. {
  798. desc: "map with repeated key and value",
  799. decodeTo: makeMessages(protobuild.Message{
  800. "map_int32_int32": map[int32]int32{1056: 1156},
  801. "map_string_nested_message": map[string]protobuild.Message{
  802. "71.1.key": {"a": 1171},
  803. },
  804. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  805. wire: protopack.Message{
  806. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  807. protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
  808. protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
  809. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
  810. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
  811. }),
  812. protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  813. protopack.Tag{1, protopack.BytesType}, protopack.String(""),
  814. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  815. protopack.Tag{1, protopack.BytesType}, protopack.String("71.1.key"),
  816. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  817. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1171),
  818. }),
  819. }),
  820. }.Marshal(),
  821. },
  822. {
  823. desc: "oneof (uint32)",
  824. decodeTo: makeMessages(protobuild.Message{
  825. "oneof_uint32": 1111,
  826. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  827. wire: protopack.Message{protopack.Tag{111, protopack.VarintType}, protopack.Varint(1111)}.Marshal(),
  828. },
  829. {
  830. desc: "oneof (message)",
  831. decodeTo: makeMessages(protobuild.Message{
  832. "oneof_nested_message": protobuild.Message{
  833. "a": 1112,
  834. },
  835. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  836. wire: protopack.Message{protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  837. protopack.Message{protopack.Tag{1, protopack.VarintType}, protopack.Varint(1112)},
  838. })}.Marshal(),
  839. },
  840. {
  841. desc: "oneof (empty message)",
  842. decodeTo: makeMessages(protobuild.Message{
  843. "oneof_nested_message": protobuild.Message{},
  844. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  845. wire: protopack.Message{protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{})}.Marshal(),
  846. },
  847. {
  848. desc: "oneof (merged message)",
  849. decodeTo: makeMessages(protobuild.Message{
  850. "oneof_nested_message": protobuild.Message{
  851. "a": 1,
  852. "corecursive": protobuild.Message{
  853. "optional_int32": 43,
  854. },
  855. },
  856. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  857. wire: protopack.Message{
  858. protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  859. protopack.Message{protopack.Tag{1, protopack.VarintType}, protopack.Varint(1)},
  860. }),
  861. protopack.Tag{112, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  862. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  863. protopack.Tag{1, protopack.VarintType}, protopack.Varint(43),
  864. }),
  865. }),
  866. }.Marshal(),
  867. },
  868. {
  869. desc: "oneof (group)",
  870. decodeTo: makeMessages(protobuild.Message{
  871. "oneofgroup": protobuild.Message{
  872. "a": 1,
  873. },
  874. }, &testpb.TestAllTypes{}),
  875. wire: protopack.Message{
  876. protopack.Tag{121, protopack.StartGroupType},
  877. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  878. protopack.Tag{121, protopack.EndGroupType},
  879. }.Marshal(),
  880. },
  881. {
  882. desc: "oneof (empty group)",
  883. decodeTo: makeMessages(protobuild.Message{
  884. "oneofgroup": protobuild.Message{},
  885. }, &testpb.TestAllTypes{}),
  886. wire: protopack.Message{
  887. protopack.Tag{121, protopack.StartGroupType},
  888. protopack.Tag{121, protopack.EndGroupType},
  889. }.Marshal(),
  890. },
  891. {
  892. desc: "oneof (merged group)",
  893. decodeTo: makeMessages(protobuild.Message{
  894. "oneofgroup": protobuild.Message{
  895. "a": 1,
  896. "b": 2,
  897. },
  898. }, &testpb.TestAllTypes{}),
  899. wire: protopack.Message{
  900. protopack.Tag{121, protopack.StartGroupType},
  901. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  902. protopack.Tag{121, protopack.EndGroupType},
  903. protopack.Tag{121, protopack.StartGroupType},
  904. protopack.Tag{2, protopack.VarintType}, protopack.Varint(2),
  905. protopack.Tag{121, protopack.EndGroupType},
  906. }.Marshal(),
  907. },
  908. {
  909. desc: "oneof (string)",
  910. decodeTo: makeMessages(protobuild.Message{
  911. "oneof_string": "1113",
  912. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  913. wire: protopack.Message{protopack.Tag{113, protopack.BytesType}, protopack.String("1113")}.Marshal(),
  914. },
  915. {
  916. desc: "oneof (bytes)",
  917. decodeTo: makeMessages(protobuild.Message{
  918. "oneof_bytes": "1114",
  919. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  920. wire: protopack.Message{protopack.Tag{114, protopack.BytesType}, protopack.String("1114")}.Marshal(),
  921. },
  922. {
  923. desc: "oneof (bool)",
  924. decodeTo: makeMessages(protobuild.Message{
  925. "oneof_bool": true,
  926. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  927. wire: protopack.Message{protopack.Tag{115, protopack.VarintType}, protopack.Bool(true)}.Marshal(),
  928. },
  929. {
  930. desc: "oneof (uint64)",
  931. decodeTo: makeMessages(protobuild.Message{
  932. "oneof_uint64": 116,
  933. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  934. wire: protopack.Message{protopack.Tag{116, protopack.VarintType}, protopack.Varint(116)}.Marshal(),
  935. },
  936. {
  937. desc: "oneof (float)",
  938. decodeTo: makeMessages(protobuild.Message{
  939. "oneof_float": 117.5,
  940. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  941. wire: protopack.Message{protopack.Tag{117, protopack.Fixed32Type}, protopack.Float32(117.5)}.Marshal(),
  942. },
  943. {
  944. desc: "oneof (double)",
  945. decodeTo: makeMessages(protobuild.Message{
  946. "oneof_double": 118.5,
  947. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  948. wire: protopack.Message{protopack.Tag{118, protopack.Fixed64Type}, protopack.Float64(118.5)}.Marshal(),
  949. },
  950. {
  951. desc: "oneof (enum)",
  952. decodeTo: makeMessages(protobuild.Message{
  953. "oneof_enum": "BAR",
  954. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  955. wire: protopack.Message{protopack.Tag{119, protopack.VarintType}, protopack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
  956. },
  957. {
  958. desc: "oneof (zero)",
  959. decodeTo: makeMessages(protobuild.Message{
  960. "oneof_uint64": 0,
  961. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  962. wire: protopack.Message{protopack.Tag{116, protopack.VarintType}, protopack.Varint(0)}.Marshal(),
  963. },
  964. {
  965. desc: "oneof (overridden value)",
  966. decodeTo: makeMessages(protobuild.Message{
  967. "oneof_uint64": 2,
  968. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  969. wire: protopack.Message{
  970. protopack.Tag{111, protopack.VarintType}, protopack.Varint(1),
  971. protopack.Tag{116, protopack.VarintType}, protopack.Varint(2),
  972. }.Marshal(),
  973. },
  974. // TODO: More unknown field tests for ordering, repeated fields, etc.
  975. //
  976. // It is currently impossible to produce results that the v1 Equal
  977. // considers equivalent to those of the v1 decoder. Figure out if
  978. // that's a problem or not.
  979. {
  980. desc: "unknown fields",
  981. checkFastInit: true,
  982. decodeTo: makeMessages(protobuild.Message{
  983. protobuild.Unknown: protopack.Message{
  984. protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
  985. }.Marshal(),
  986. }),
  987. wire: protopack.Message{
  988. protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
  989. }.Marshal(),
  990. },
  991. {
  992. desc: "discarded unknown fields",
  993. unmarshalOptions: proto.UnmarshalOptions{
  994. DiscardUnknown: true,
  995. },
  996. decodeTo: makeMessages(protobuild.Message{}),
  997. wire: protopack.Message{
  998. protopack.Tag{100000, protopack.VarintType}, protopack.Varint(1),
  999. }.Marshal(),
  1000. },
  1001. {
  1002. desc: "field type mismatch",
  1003. decodeTo: makeMessages(protobuild.Message{
  1004. protobuild.Unknown: protopack.Message{
  1005. protopack.Tag{1, protopack.BytesType}, protopack.String("string"),
  1006. }.Marshal(),
  1007. }),
  1008. wire: protopack.Message{
  1009. protopack.Tag{1, protopack.BytesType}, protopack.String("string"),
  1010. }.Marshal(),
  1011. },
  1012. {
  1013. desc: "map field element mismatch",
  1014. decodeTo: makeMessages(protobuild.Message{
  1015. "map_int32_int32": map[int32]int32{1: 0},
  1016. }, &testpb.TestAllTypes{}, &test3pb.TestAllTypes{}),
  1017. wire: protopack.Message{
  1018. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1019. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1020. protopack.Tag{2, protopack.BytesType}, protopack.String("string"),
  1021. }),
  1022. }.Marshal(),
  1023. },
  1024. {
  1025. desc: "required field in nil message unset",
  1026. checkFastInit: true,
  1027. partial: true,
  1028. decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
  1029. },
  1030. {
  1031. desc: "required int32 unset",
  1032. checkFastInit: true,
  1033. partial: true,
  1034. decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Int32{}),
  1035. },
  1036. {
  1037. desc: "required int32 set",
  1038. checkFastInit: true,
  1039. decodeTo: makeMessages(protobuild.Message{
  1040. "v": 1,
  1041. }, &requiredpb.Int32{}),
  1042. wire: protopack.Message{
  1043. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1044. }.Marshal(),
  1045. },
  1046. {
  1047. desc: "required fixed32 unset",
  1048. checkFastInit: true,
  1049. partial: true,
  1050. decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed32{}),
  1051. },
  1052. {
  1053. desc: "required fixed32 set",
  1054. checkFastInit: true,
  1055. decodeTo: makeMessages(protobuild.Message{
  1056. "v": 1,
  1057. }, &requiredpb.Fixed32{}),
  1058. wire: protopack.Message{
  1059. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(1),
  1060. }.Marshal(),
  1061. },
  1062. {
  1063. desc: "required fixed64 unset",
  1064. checkFastInit: true,
  1065. partial: true,
  1066. decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Fixed64{}),
  1067. },
  1068. {
  1069. desc: "required fixed64 set",
  1070. checkFastInit: true,
  1071. decodeTo: makeMessages(protobuild.Message{
  1072. "v": 1,
  1073. }, &requiredpb.Fixed64{}),
  1074. wire: protopack.Message{
  1075. protopack.Tag{1, protopack.Fixed64Type}, protopack.Int64(1),
  1076. }.Marshal(),
  1077. },
  1078. {
  1079. desc: "required bytes unset",
  1080. checkFastInit: true,
  1081. partial: true,
  1082. decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Bytes{}),
  1083. },
  1084. {
  1085. desc: "required bytes set",
  1086. checkFastInit: true,
  1087. decodeTo: makeMessages(protobuild.Message{
  1088. "v": "",
  1089. }, &requiredpb.Bytes{}),
  1090. wire: protopack.Message{
  1091. protopack.Tag{1, protopack.BytesType}, protopack.Bytes(nil),
  1092. }.Marshal(),
  1093. },
  1094. {
  1095. desc: "required message unset",
  1096. checkFastInit: true,
  1097. partial: true,
  1098. decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Message{}),
  1099. },
  1100. {
  1101. desc: "required message set",
  1102. checkFastInit: true,
  1103. decodeTo: makeMessages(protobuild.Message{
  1104. "v": protobuild.Message{},
  1105. }, &requiredpb.Message{}),
  1106. wire: protopack.Message{
  1107. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1108. }.Marshal(),
  1109. },
  1110. {
  1111. desc: "required group unset",
  1112. checkFastInit: true,
  1113. partial: true,
  1114. decodeTo: makeMessages(protobuild.Message{}, &requiredpb.Group{}),
  1115. },
  1116. {
  1117. desc: "required group set",
  1118. checkFastInit: true,
  1119. decodeTo: makeMessages(protobuild.Message{
  1120. "group": protobuild.Message{},
  1121. }, &requiredpb.Group{}),
  1122. wire: protopack.Message{
  1123. protopack.Tag{1, protopack.StartGroupType},
  1124. protopack.Tag{1, protopack.EndGroupType},
  1125. }.Marshal(),
  1126. },
  1127. {
  1128. desc: "required field with incompatible wire type",
  1129. checkFastInit: true,
  1130. partial: true,
  1131. decodeTo: []proto.Message{build(
  1132. &testpb.TestRequired{},
  1133. unknown(protopack.Message{
  1134. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
  1135. }.Marshal()),
  1136. )},
  1137. wire: protopack.Message{
  1138. protopack.Tag{1, protopack.Fixed32Type}, protopack.Int32(2),
  1139. }.Marshal(),
  1140. },
  1141. {
  1142. desc: "required field in optional message unset",
  1143. checkFastInit: true,
  1144. partial: true,
  1145. decodeTo: makeMessages(protobuild.Message{
  1146. "optional_message": protobuild.Message{},
  1147. }, &testpb.TestRequiredForeign{}),
  1148. wire: protopack.Message{
  1149. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1150. }.Marshal(),
  1151. },
  1152. {
  1153. desc: "required field in optional message set",
  1154. checkFastInit: true,
  1155. decodeTo: makeMessages(protobuild.Message{
  1156. "optional_message": protobuild.Message{
  1157. "required_field": 1,
  1158. },
  1159. }, &testpb.TestRequiredForeign{}),
  1160. wire: protopack.Message{
  1161. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1162. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1163. }),
  1164. }.Marshal(),
  1165. },
  1166. {
  1167. desc: "required field in optional message set (split across multiple tags)",
  1168. checkFastInit: false, // fast init checks don't handle split messages
  1169. nocheckValidInit: true, // validation doesn't either
  1170. decodeTo: makeMessages(protobuild.Message{
  1171. "optional_message": protobuild.Message{
  1172. "required_field": 1,
  1173. },
  1174. }, &testpb.TestRequiredForeign{}),
  1175. wire: protopack.Message{
  1176. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1177. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1178. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1179. }),
  1180. }.Marshal(),
  1181. },
  1182. {
  1183. desc: "required field in repeated message unset",
  1184. checkFastInit: true,
  1185. partial: true,
  1186. decodeTo: makeMessages(protobuild.Message{
  1187. "repeated_message": []protobuild.Message{
  1188. {"required_field": 1},
  1189. {},
  1190. },
  1191. }, &testpb.TestRequiredForeign{}),
  1192. wire: protopack.Message{
  1193. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1194. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1195. }),
  1196. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1197. }.Marshal(),
  1198. },
  1199. {
  1200. desc: "required field in repeated message set",
  1201. checkFastInit: true,
  1202. decodeTo: makeMessages(protobuild.Message{
  1203. "repeated_message": []protobuild.Message{
  1204. {"required_field": 1},
  1205. {"required_field": 2},
  1206. },
  1207. }, &testpb.TestRequiredForeign{}),
  1208. wire: protopack.Message{
  1209. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1210. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1211. }),
  1212. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1213. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  1214. }),
  1215. }.Marshal(),
  1216. },
  1217. {
  1218. desc: "required field in map message unset",
  1219. checkFastInit: true,
  1220. partial: true,
  1221. decodeTo: makeMessages(protobuild.Message{
  1222. "map_message": map[int32]protobuild.Message{
  1223. 1: {"required_field": 1},
  1224. 2: {},
  1225. },
  1226. }, &testpb.TestRequiredForeign{}),
  1227. wire: protopack.Message{
  1228. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1229. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1230. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1231. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1232. }),
  1233. }),
  1234. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1235. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  1236. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1237. }),
  1238. }.Marshal(),
  1239. },
  1240. {
  1241. desc: "required field in absent map message value",
  1242. checkFastInit: true,
  1243. partial: true,
  1244. decodeTo: makeMessages(protobuild.Message{
  1245. "map_message": map[int32]protobuild.Message{
  1246. 2: {},
  1247. },
  1248. }, &testpb.TestRequiredForeign{}),
  1249. wire: protopack.Message{
  1250. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1251. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  1252. }),
  1253. }.Marshal(),
  1254. },
  1255. {
  1256. desc: "required field in map message set",
  1257. checkFastInit: true,
  1258. decodeTo: makeMessages(protobuild.Message{
  1259. "map_message": map[int32]protobuild.Message{
  1260. 1: {"required_field": 1},
  1261. 2: {"required_field": 2},
  1262. },
  1263. }, &testpb.TestRequiredForeign{}),
  1264. wire: protopack.Message{
  1265. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1266. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1267. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1268. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1269. }),
  1270. }),
  1271. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1272. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  1273. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1274. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  1275. }),
  1276. }),
  1277. }.Marshal(),
  1278. },
  1279. {
  1280. desc: "required field in optional group unset",
  1281. checkFastInit: true,
  1282. partial: true,
  1283. decodeTo: makeMessages(protobuild.Message{
  1284. "optionalgroup": protobuild.Message{},
  1285. }, &testpb.TestRequiredGroupFields{}),
  1286. wire: protopack.Message{
  1287. protopack.Tag{1, protopack.StartGroupType},
  1288. protopack.Tag{1, protopack.EndGroupType},
  1289. }.Marshal(),
  1290. },
  1291. {
  1292. desc: "required field in optional group set",
  1293. checkFastInit: true,
  1294. decodeTo: makeMessages(protobuild.Message{
  1295. "optionalgroup": protobuild.Message{
  1296. "a": 1,
  1297. },
  1298. }, &testpb.TestRequiredGroupFields{}),
  1299. wire: protopack.Message{
  1300. protopack.Tag{1, protopack.StartGroupType},
  1301. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1),
  1302. protopack.Tag{1, protopack.EndGroupType},
  1303. }.Marshal(),
  1304. },
  1305. {
  1306. desc: "required field in repeated group unset",
  1307. checkFastInit: true,
  1308. partial: true,
  1309. decodeTo: makeMessages(protobuild.Message{
  1310. "repeatedgroup": []protobuild.Message{
  1311. {"a": 1},
  1312. {},
  1313. },
  1314. }, &testpb.TestRequiredGroupFields{}),
  1315. wire: protopack.Message{
  1316. protopack.Tag{3, protopack.StartGroupType},
  1317. protopack.Tag{4, protopack.VarintType}, protopack.Varint(1),
  1318. protopack.Tag{3, protopack.EndGroupType},
  1319. protopack.Tag{3, protopack.StartGroupType},
  1320. protopack.Tag{3, protopack.EndGroupType},
  1321. }.Marshal(),
  1322. },
  1323. {
  1324. desc: "required field in repeated group set",
  1325. checkFastInit: true,
  1326. decodeTo: makeMessages(protobuild.Message{
  1327. "repeatedgroup": []protobuild.Message{
  1328. {"a": 1},
  1329. {"a": 2},
  1330. },
  1331. }, &testpb.TestRequiredGroupFields{}),
  1332. wire: protopack.Message{
  1333. protopack.Tag{3, protopack.StartGroupType},
  1334. protopack.Tag{4, protopack.VarintType}, protopack.Varint(1),
  1335. protopack.Tag{3, protopack.EndGroupType},
  1336. protopack.Tag{3, protopack.StartGroupType},
  1337. protopack.Tag{4, protopack.VarintType}, protopack.Varint(2),
  1338. protopack.Tag{3, protopack.EndGroupType},
  1339. }.Marshal(),
  1340. },
  1341. {
  1342. desc: "required field in oneof message unset",
  1343. checkFastInit: true,
  1344. partial: true,
  1345. decodeTo: makeMessages(protobuild.Message{
  1346. "oneof_message": protobuild.Message{},
  1347. }, &testpb.TestRequiredForeign{}),
  1348. wire: protopack.Message{protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{})}.Marshal(),
  1349. },
  1350. {
  1351. desc: "required field in oneof message set",
  1352. checkFastInit: true,
  1353. decodeTo: makeMessages(protobuild.Message{
  1354. "oneof_message": protobuild.Message{
  1355. "required_field": 1,
  1356. },
  1357. }, &testpb.TestRequiredForeign{}),
  1358. wire: protopack.Message{protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1359. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1360. })}.Marshal(),
  1361. },
  1362. {
  1363. desc: "required field in extension message unset",
  1364. checkFastInit: true,
  1365. partial: true,
  1366. decodeTo: makeMessages(protobuild.Message{
  1367. "single": protobuild.Message{},
  1368. }, &testpb.TestAllExtensions{}),
  1369. wire: protopack.Message{
  1370. protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1371. }.Marshal(),
  1372. },
  1373. {
  1374. desc: "required field in extension message set",
  1375. checkFastInit: true,
  1376. decodeTo: makeMessages(protobuild.Message{
  1377. "single": protobuild.Message{
  1378. "required_field": 1,
  1379. },
  1380. }, &testpb.TestAllExtensions{}),
  1381. wire: protopack.Message{
  1382. protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1383. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1384. }),
  1385. }.Marshal(),
  1386. },
  1387. {
  1388. desc: "required field in repeated extension message unset",
  1389. checkFastInit: true,
  1390. partial: true,
  1391. decodeTo: makeMessages(protobuild.Message{
  1392. "multi": []protobuild.Message{
  1393. {"required_field": 1},
  1394. {},
  1395. },
  1396. }, &testpb.TestAllExtensions{}),
  1397. wire: protopack.Message{
  1398. protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1399. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1400. }),
  1401. protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  1402. }.Marshal(),
  1403. },
  1404. {
  1405. desc: "required field in repeated extension message set",
  1406. checkFastInit: true,
  1407. decodeTo: makeMessages(protobuild.Message{
  1408. "multi": []protobuild.Message{
  1409. {"required_field": 1},
  1410. {"required_field": 2},
  1411. },
  1412. }, &testpb.TestAllExtensions{}),
  1413. wire: protopack.Message{
  1414. protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1415. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1416. }),
  1417. protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1418. protopack.Tag{1, protopack.VarintType}, protopack.Varint(2),
  1419. }),
  1420. }.Marshal(),
  1421. },
  1422. {
  1423. desc: "nil messages",
  1424. decodeTo: []proto.Message{
  1425. (*testpb.TestAllTypes)(nil),
  1426. (*test3pb.TestAllTypes)(nil),
  1427. (*testpb.TestAllExtensions)(nil),
  1428. },
  1429. },
  1430. {
  1431. desc: "legacy",
  1432. partial: true,
  1433. decodeTo: makeMessages(protobuild.Message{
  1434. "f1": protobuild.Message{
  1435. "optional_int32": 1,
  1436. "optional_child_enum": "ALPHA",
  1437. "optional_child_message": protobuild.Message{
  1438. "f1": "x",
  1439. },
  1440. "optionalgroup": protobuild.Message{
  1441. "f1": "x",
  1442. },
  1443. "repeated_child_message": []protobuild.Message{
  1444. {"f1": "x"},
  1445. },
  1446. "repeatedgroup": []protobuild.Message{
  1447. {"f1": "x"},
  1448. },
  1449. "map_bool_child_message": map[bool]protobuild.Message{
  1450. true: {"f1": "x"},
  1451. },
  1452. "oneof_child_message": protobuild.Message{
  1453. "f1": "x",
  1454. },
  1455. },
  1456. }, &legacypb.Legacy{}),
  1457. wire: protopack.Message{
  1458. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1459. protopack.Tag{101, protopack.VarintType}, protopack.Varint(1),
  1460. protopack.Tag{115, protopack.VarintType}, protopack.Varint(0),
  1461. protopack.Tag{116, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1462. protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
  1463. }),
  1464. protopack.Tag{120, protopack.StartGroupType},
  1465. protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
  1466. protopack.Tag{120, protopack.EndGroupType},
  1467. protopack.Tag{516, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1468. protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
  1469. }),
  1470. protopack.Tag{520, protopack.StartGroupType},
  1471. protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
  1472. protopack.Tag{520, protopack.EndGroupType},
  1473. protopack.Tag{616, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1474. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  1475. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1476. protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
  1477. }),
  1478. }),
  1479. protopack.Tag{716, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1480. protopack.Tag{1, protopack.BytesType}, protopack.String("x"),
  1481. }),
  1482. }),
  1483. }.Marshal(),
  1484. validationStatus: impl.ValidationUnknown,
  1485. },
  1486. {
  1487. desc: "first reserved field number",
  1488. decodeTo: makeMessages(protobuild.Message{
  1489. protobuild.Unknown: protopack.Message{
  1490. protopack.Tag{protopack.FirstReservedNumber, protopack.VarintType}, protopack.Varint(1004),
  1491. }.Marshal(),
  1492. }),
  1493. wire: protopack.Message{
  1494. protopack.Tag{protopack.FirstReservedNumber, protopack.VarintType}, protopack.Varint(1004),
  1495. }.Marshal(),
  1496. },
  1497. {
  1498. desc: "last reserved field number",
  1499. decodeTo: makeMessages(protobuild.Message{
  1500. protobuild.Unknown: protopack.Message{
  1501. protopack.Tag{protopack.LastReservedNumber, protopack.VarintType}, protopack.Varint(1005),
  1502. }.Marshal(),
  1503. }),
  1504. wire: protopack.Message{
  1505. protopack.Tag{protopack.LastReservedNumber, protopack.VarintType}, protopack.Varint(1005),
  1506. }.Marshal(),
  1507. },
  1508. {
  1509. desc: "nested unknown extension",
  1510. unmarshalOptions: proto.UnmarshalOptions{
  1511. DiscardUnknown: true,
  1512. Resolver: filterResolver{
  1513. filter: func(name protoreflect.FullName) bool {
  1514. switch name.Name() {
  1515. case "optional_nested_message",
  1516. "optional_int32":
  1517. return true
  1518. }
  1519. return false
  1520. },
  1521. resolver: protoregistry.GlobalTypes,
  1522. },
  1523. },
  1524. decodeTo: makeMessages(protobuild.Message{
  1525. "optional_nested_message": protobuild.Message{
  1526. "corecursive": protobuild.Message{
  1527. "optional_nested_message": protobuild.Message{
  1528. "corecursive": protobuild.Message{
  1529. "optional_int32": 42,
  1530. },
  1531. },
  1532. },
  1533. },
  1534. }, &testpb.TestAllExtensions{}),
  1535. wire: protopack.Message{
  1536. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1537. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1538. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1539. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1540. protopack.Tag{1, protopack.VarintType}, protopack.Varint(42),
  1541. protopack.Tag{2, protopack.VarintType}, protopack.Varint(43),
  1542. }),
  1543. }),
  1544. }),
  1545. }),
  1546. }.Marshal(),
  1547. },
  1548. }
  1549. var testInvalidMessages = []testProto{
  1550. {
  1551. desc: "invalid UTF-8 in optional string field",
  1552. decodeTo: makeMessages(protobuild.Message{
  1553. "optional_string": "abc\xff",
  1554. }, &test3pb.TestAllTypes{}),
  1555. wire: protopack.Message{
  1556. protopack.Tag{14, protopack.BytesType}, protopack.String("abc\xff"),
  1557. }.Marshal(),
  1558. },
  1559. {
  1560. desc: "invalid UTF-8 in singular string field",
  1561. decodeTo: makeMessages(protobuild.Message{
  1562. "singular_string": "abc\xff",
  1563. }, &test3pb.TestAllTypes{}),
  1564. wire: protopack.Message{
  1565. protopack.Tag{94, protopack.BytesType}, protopack.String("abc\xff"),
  1566. }.Marshal(),
  1567. },
  1568. {
  1569. desc: "invalid UTF-8 in repeated string field",
  1570. decodeTo: makeMessages(protobuild.Message{
  1571. "repeated_string": []string{"foo", "abc\xff"},
  1572. }, &test3pb.TestAllTypes{}),
  1573. wire: protopack.Message{
  1574. protopack.Tag{44, protopack.BytesType}, protopack.String("foo"),
  1575. protopack.Tag{44, protopack.BytesType}, protopack.String("abc\xff"),
  1576. }.Marshal(),
  1577. },
  1578. {
  1579. desc: "invalid UTF-8 in nested message",
  1580. decodeTo: makeMessages(protobuild.Message{
  1581. "optional_nested_message": protobuild.Message{
  1582. "corecursive": protobuild.Message{
  1583. "singular_string": "abc\xff",
  1584. },
  1585. },
  1586. }, &test3pb.TestAllTypes{}),
  1587. wire: protopack.Message{
  1588. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1589. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1590. protopack.Tag{94, protopack.BytesType}, protopack.String("abc\xff"),
  1591. }),
  1592. }),
  1593. }.Marshal(),
  1594. },
  1595. {
  1596. desc: "invalid UTF-8 in oneof field",
  1597. decodeTo: makeMessages(protobuild.Message{
  1598. "oneof_string": "abc\xff",
  1599. }, &test3pb.TestAllTypes{}),
  1600. wire: protopack.Message{protopack.Tag{113, protopack.BytesType}, protopack.String("abc\xff")}.Marshal(),
  1601. },
  1602. {
  1603. desc: "invalid UTF-8 in map key",
  1604. decodeTo: makeMessages(protobuild.Message{
  1605. "map_string_string": map[string]string{"key\xff": "val"},
  1606. }, &test3pb.TestAllTypes{}),
  1607. wire: protopack.Message{
  1608. protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1609. protopack.Tag{1, protopack.BytesType}, protopack.String("key\xff"),
  1610. protopack.Tag{2, protopack.BytesType}, protopack.String("val"),
  1611. }),
  1612. }.Marshal(),
  1613. },
  1614. {
  1615. desc: "invalid UTF-8 in map value",
  1616. decodeTo: makeMessages(protobuild.Message{
  1617. "map_string_string": map[string]string{"key": "val\xff"},
  1618. }, &test3pb.TestAllTypes{}),
  1619. wire: protopack.Message{
  1620. protopack.Tag{69, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1621. protopack.Tag{1, protopack.BytesType}, protopack.String("key"),
  1622. protopack.Tag{2, protopack.BytesType}, protopack.String("val\xff"),
  1623. }),
  1624. }.Marshal(),
  1625. },
  1626. {
  1627. desc: "invalid field number zero",
  1628. decodeTo: []proto.Message{
  1629. (*testpb.TestAllTypes)(nil),
  1630. (*testpb.TestAllExtensions)(nil),
  1631. },
  1632. wire: protopack.Message{
  1633. protopack.Tag{protopack.MinValidNumber - 1, protopack.VarintType}, protopack.Varint(1001),
  1634. }.Marshal(),
  1635. },
  1636. {
  1637. desc: "invalid field numbers zero and one",
  1638. decodeTo: []proto.Message{
  1639. (*testpb.TestAllTypes)(nil),
  1640. (*testpb.TestAllExtensions)(nil),
  1641. },
  1642. wire: protopack.Message{
  1643. protopack.Tag{protopack.MinValidNumber - 1, protopack.VarintType}, protopack.Varint(1002),
  1644. protopack.Tag{protopack.MinValidNumber, protopack.VarintType}, protopack.Varint(1003),
  1645. }.Marshal(),
  1646. },
  1647. {
  1648. desc: "invalid field numbers max and max+1",
  1649. decodeTo: []proto.Message{
  1650. (*testpb.TestAllTypes)(nil),
  1651. (*testpb.TestAllExtensions)(nil),
  1652. },
  1653. wire: protopack.Message{
  1654. protopack.Tag{protopack.MaxValidNumber, protopack.VarintType}, protopack.Varint(1006),
  1655. protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(1007),
  1656. }.Marshal(),
  1657. },
  1658. {
  1659. desc: "invalid field number max+1",
  1660. decodeTo: []proto.Message{
  1661. (*testpb.TestAllTypes)(nil),
  1662. (*testpb.TestAllExtensions)(nil),
  1663. },
  1664. wire: protopack.Message{
  1665. protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(1008),
  1666. }.Marshal(),
  1667. },
  1668. {
  1669. desc: "invalid field number wraps int32",
  1670. decodeTo: []proto.Message{
  1671. (*testpb.TestAllTypes)(nil),
  1672. (*testpb.TestAllExtensions)(nil),
  1673. },
  1674. wire: protopack.Message{
  1675. protopack.Varint(2234993595104), protopack.Varint(0),
  1676. }.Marshal(),
  1677. },
  1678. {
  1679. desc: "invalid field number in map",
  1680. decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
  1681. wire: protopack.Message{
  1682. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1683. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1056),
  1684. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1156),
  1685. protopack.Tag{protopack.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(0),
  1686. }),
  1687. }.Marshal(),
  1688. },
  1689. {
  1690. desc: "invalid tag varint",
  1691. decodeTo: []proto.Message{
  1692. (*testpb.TestAllTypes)(nil),
  1693. (*testpb.TestAllExtensions)(nil),
  1694. },
  1695. wire: []byte{0xff},
  1696. },
  1697. {
  1698. desc: "field number too small",
  1699. decodeTo: []proto.Message{
  1700. (*testpb.TestAllTypes)(nil),
  1701. (*testpb.TestAllExtensions)(nil),
  1702. },
  1703. wire: protopack.Message{
  1704. protopack.Tag{0, protopack.VarintType}, protopack.Varint(0),
  1705. }.Marshal(),
  1706. },
  1707. {
  1708. desc: "field number too large",
  1709. decodeTo: []proto.Message{
  1710. (*testpb.TestAllTypes)(nil),
  1711. (*testpb.TestAllExtensions)(nil),
  1712. },
  1713. wire: protopack.Message{
  1714. protopack.Tag{protowire.MaxValidNumber + 1, protopack.VarintType}, protopack.Varint(0),
  1715. }.Marshal(),
  1716. },
  1717. {
  1718. desc: "invalid tag varint in message field",
  1719. decodeTo: []proto.Message{
  1720. (*testpb.TestAllTypes)(nil),
  1721. (*testpb.TestAllExtensions)(nil),
  1722. },
  1723. wire: protopack.Message{
  1724. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1725. protopack.Raw{0xff},
  1726. }),
  1727. }.Marshal(),
  1728. },
  1729. {
  1730. desc: "invalid tag varint in repeated message field",
  1731. decodeTo: []proto.Message{
  1732. (*testpb.TestAllTypes)(nil),
  1733. (*testpb.TestAllExtensions)(nil),
  1734. },
  1735. wire: protopack.Message{
  1736. protopack.Tag{48, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1737. protopack.Raw{0xff},
  1738. }),
  1739. }.Marshal(),
  1740. },
  1741. {
  1742. desc: "invalid varint in group field",
  1743. decodeTo: []proto.Message{
  1744. (*testpb.TestAllTypes)(nil),
  1745. (*testpb.TestAllExtensions)(nil),
  1746. },
  1747. wire: protopack.Message{
  1748. protopack.Tag{16, protopack.StartGroupType},
  1749. protopack.Tag{1000, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1750. protopack.Raw{0xff},
  1751. }),
  1752. protopack.Tag{16, protopack.EndGroupType},
  1753. }.Marshal(),
  1754. },
  1755. {
  1756. desc: "invalid varint in repeated group field",
  1757. decodeTo: []proto.Message{
  1758. (*testpb.TestAllTypes)(nil),
  1759. (*testpb.TestAllExtensions)(nil),
  1760. },
  1761. wire: protopack.Message{
  1762. protopack.Tag{46, protopack.StartGroupType},
  1763. protopack.Tag{1001, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1764. protopack.Raw{0xff},
  1765. }),
  1766. protopack.Tag{46, protopack.EndGroupType},
  1767. }.Marshal(),
  1768. },
  1769. {
  1770. desc: "unterminated repeated group field",
  1771. decodeTo: []proto.Message{
  1772. (*testpb.TestAllTypes)(nil),
  1773. (*testpb.TestAllExtensions)(nil),
  1774. },
  1775. wire: protopack.Message{
  1776. protopack.Tag{46, protopack.StartGroupType},
  1777. }.Marshal(),
  1778. },
  1779. {
  1780. desc: "invalid tag varint in map item",
  1781. decodeTo: []proto.Message{
  1782. (*testpb.TestAllTypes)(nil),
  1783. },
  1784. wire: protopack.Message{
  1785. protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1786. protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
  1787. protopack.Tag{2, protopack.VarintType}, protopack.Varint(0),
  1788. protopack.Raw{0xff},
  1789. }),
  1790. }.Marshal(),
  1791. },
  1792. {
  1793. desc: "invalid tag varint in map message value",
  1794. decodeTo: []proto.Message{
  1795. (*testpb.TestAllTypes)(nil),
  1796. },
  1797. wire: protopack.Message{
  1798. protopack.Tag{71, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1799. protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
  1800. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  1801. protopack.Raw{0xff},
  1802. }),
  1803. }),
  1804. }.Marshal(),
  1805. },
  1806. {
  1807. desc: "invalid packed int32 field",
  1808. decodeTo: []proto.Message{
  1809. (*testpb.TestAllTypes)(nil),
  1810. (*testpb.TestAllExtensions)(nil),
  1811. },
  1812. wire: protopack.Message{
  1813. protopack.Tag{31, protopack.BytesType}, protopack.Bytes{0xff},
  1814. }.Marshal(),
  1815. },
  1816. {
  1817. desc: "invalid packed int64 field",
  1818. decodeTo: []proto.Message{
  1819. (*testpb.TestAllTypes)(nil),
  1820. (*testpb.TestAllExtensions)(nil),
  1821. },
  1822. wire: protopack.Message{
  1823. protopack.Tag{32, protopack.BytesType}, protopack.Bytes{0xff},
  1824. }.Marshal(),
  1825. },
  1826. {
  1827. desc: "invalid packed uint32 field",
  1828. decodeTo: []proto.Message{
  1829. (*testpb.TestAllTypes)(nil),
  1830. (*testpb.TestAllExtensions)(nil),
  1831. },
  1832. wire: protopack.Message{
  1833. protopack.Tag{33, protopack.BytesType}, protopack.Bytes{0xff},
  1834. }.Marshal(),
  1835. },
  1836. {
  1837. desc: "invalid packed uint64 field",
  1838. decodeTo: []proto.Message{
  1839. (*testpb.TestAllTypes)(nil),
  1840. (*testpb.TestAllExtensions)(nil),
  1841. },
  1842. wire: protopack.Message{
  1843. protopack.Tag{34, protopack.BytesType}, protopack.Bytes{0xff},
  1844. }.Marshal(),
  1845. },
  1846. {
  1847. desc: "invalid packed sint32 field",
  1848. decodeTo: []proto.Message{
  1849. (*testpb.TestAllTypes)(nil),
  1850. (*testpb.TestAllExtensions)(nil),
  1851. },
  1852. wire: protopack.Message{
  1853. protopack.Tag{35, protopack.BytesType}, protopack.Bytes{0xff},
  1854. }.Marshal(),
  1855. },
  1856. {
  1857. desc: "invalid packed sint64 field",
  1858. decodeTo: []proto.Message{
  1859. (*testpb.TestAllTypes)(nil),
  1860. (*testpb.TestAllExtensions)(nil),
  1861. },
  1862. wire: protopack.Message{
  1863. protopack.Tag{36, protopack.BytesType}, protopack.Bytes{0xff},
  1864. }.Marshal(),
  1865. },
  1866. {
  1867. desc: "invalid packed fixed32 field",
  1868. decodeTo: []proto.Message{
  1869. (*testpb.TestAllTypes)(nil),
  1870. (*testpb.TestAllExtensions)(nil),
  1871. },
  1872. wire: protopack.Message{
  1873. protopack.Tag{37, protopack.BytesType}, protopack.Bytes{0x00},
  1874. }.Marshal(),
  1875. },
  1876. {
  1877. desc: "invalid packed fixed64 field",
  1878. decodeTo: []proto.Message{
  1879. (*testpb.TestAllTypes)(nil),
  1880. (*testpb.TestAllExtensions)(nil),
  1881. },
  1882. wire: protopack.Message{
  1883. protopack.Tag{38, protopack.BytesType}, protopack.Bytes{0x00},
  1884. }.Marshal(),
  1885. },
  1886. {
  1887. desc: "invalid packed sfixed32 field",
  1888. decodeTo: []proto.Message{
  1889. (*testpb.TestAllTypes)(nil),
  1890. (*testpb.TestAllExtensions)(nil),
  1891. },
  1892. wire: protopack.Message{
  1893. protopack.Tag{39, protopack.BytesType}, protopack.Bytes{0x00},
  1894. }.Marshal(),
  1895. },
  1896. {
  1897. desc: "invalid packed sfixed64 field",
  1898. decodeTo: []proto.Message{
  1899. (*testpb.TestAllTypes)(nil),
  1900. (*testpb.TestAllExtensions)(nil),
  1901. },
  1902. wire: protopack.Message{
  1903. protopack.Tag{40, protopack.BytesType}, protopack.Bytes{0x00},
  1904. }.Marshal(),
  1905. },
  1906. {
  1907. desc: "invalid packed float field",
  1908. decodeTo: []proto.Message{
  1909. (*testpb.TestAllTypes)(nil),
  1910. (*testpb.TestAllExtensions)(nil),
  1911. },
  1912. wire: protopack.Message{
  1913. protopack.Tag{41, protopack.BytesType}, protopack.Bytes{0x00},
  1914. }.Marshal(),
  1915. },
  1916. {
  1917. desc: "invalid packed double field",
  1918. decodeTo: []proto.Message{
  1919. (*testpb.TestAllTypes)(nil),
  1920. (*testpb.TestAllExtensions)(nil),
  1921. },
  1922. wire: protopack.Message{
  1923. protopack.Tag{42, protopack.BytesType}, protopack.Bytes{0x00},
  1924. }.Marshal(),
  1925. },
  1926. {
  1927. desc: "invalid packed bool field",
  1928. decodeTo: []proto.Message{
  1929. (*testpb.TestAllTypes)(nil),
  1930. (*testpb.TestAllExtensions)(nil),
  1931. },
  1932. wire: protopack.Message{
  1933. protopack.Tag{43, protopack.BytesType}, protopack.Bytes{0xff},
  1934. }.Marshal(),
  1935. },
  1936. {
  1937. desc: "bytes field overruns message",
  1938. decodeTo: []proto.Message{
  1939. (*testpb.TestAllTypes)(nil),
  1940. (*testpb.TestAllExtensions)(nil),
  1941. },
  1942. wire: protopack.Message{
  1943. protopack.Tag{18, protopack.BytesType}, protopack.LengthPrefix{protopack.Message{
  1944. protopack.Tag{2, protopack.BytesType}, protopack.LengthPrefix{protopack.Message{
  1945. protopack.Tag{15, protopack.BytesType}, protopack.Varint(2),
  1946. }},
  1947. protopack.Tag{1, protopack.VarintType}, protopack.Varint(0),
  1948. }},
  1949. }.Marshal(),
  1950. },
  1951. {
  1952. desc: "varint field overruns message",
  1953. decodeTo: []proto.Message{
  1954. (*testpb.TestAllTypes)(nil),
  1955. (*testpb.TestAllExtensions)(nil),
  1956. },
  1957. wire: protopack.Message{
  1958. protopack.Tag{1, protopack.VarintType},
  1959. }.Marshal(),
  1960. },
  1961. {
  1962. desc: "bytes field lacks size",
  1963. decodeTo: []proto.Message{
  1964. (*testpb.TestAllTypes)(nil),
  1965. (*testpb.TestAllExtensions)(nil),
  1966. },
  1967. wire: protopack.Message{
  1968. protopack.Tag{18, protopack.BytesType},
  1969. }.Marshal(),
  1970. },
  1971. {
  1972. desc: "varint overflow",
  1973. decodeTo: []proto.Message{
  1974. (*testpb.TestAllTypes)(nil),
  1975. (*testpb.TestAllExtensions)(nil),
  1976. },
  1977. wire: protopack.Message{
  1978. protopack.Tag{1, protopack.VarintType},
  1979. protopack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x02"),
  1980. }.Marshal(),
  1981. },
  1982. {
  1983. desc: "varint length overrun",
  1984. decodeTo: []proto.Message{
  1985. (*testpb.TestAllTypes)(nil),
  1986. (*testpb.TestAllExtensions)(nil),
  1987. },
  1988. wire: protopack.Message{
  1989. protopack.Tag{1, protopack.VarintType},
  1990. protopack.Raw("\xff\xff\xff\xff\xff\xff\xff\xff\xff"),
  1991. }.Marshal(),
  1992. },
  1993. }
  1994. type filterResolver struct {
  1995. filter func(name protoreflect.FullName) bool
  1996. resolver protoregistry.ExtensionTypeResolver
  1997. }
  1998. func (f filterResolver) FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error) {
  1999. if !f.filter(field) {
  2000. return nil, protoregistry.NotFound
  2001. }
  2002. return f.resolver.FindExtensionByName(field)
  2003. }
  2004. func (f filterResolver) FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error) {
  2005. xt, err := f.resolver.FindExtensionByNumber(message, field)
  2006. if err != nil {
  2007. return nil, err
  2008. }
  2009. if !f.filter(xt.TypeDescriptor().FullName()) {
  2010. return nil, protoregistry.NotFound
  2011. }
  2012. return xt, nil
  2013. }