decode_test.go 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  1. // Copyright 2018 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. "fmt"
  7. "reflect"
  8. "testing"
  9. "google.golang.org/protobuf/encoding/prototext"
  10. "google.golang.org/protobuf/internal/encoding/pack"
  11. "google.golang.org/protobuf/internal/filedesc"
  12. "google.golang.org/protobuf/internal/flags"
  13. "google.golang.org/protobuf/proto"
  14. "google.golang.org/protobuf/reflect/protodesc"
  15. "google.golang.org/protobuf/reflect/protoreflect"
  16. "google.golang.org/protobuf/runtime/protoimpl"
  17. legacypb "google.golang.org/protobuf/internal/testprotos/legacy"
  18. legacy1pb "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
  19. testpb "google.golang.org/protobuf/internal/testprotos/test"
  20. test3pb "google.golang.org/protobuf/internal/testprotos/test3"
  21. "google.golang.org/protobuf/types/descriptorpb"
  22. )
  23. type testProto struct {
  24. desc string
  25. decodeTo []proto.Message
  26. wire []byte
  27. partial bool
  28. }
  29. func TestDecode(t *testing.T) {
  30. for _, test := range testProtos {
  31. for _, want := range test.decodeTo {
  32. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  33. opts := proto.UnmarshalOptions{
  34. AllowPartial: test.partial,
  35. }
  36. wire := append(([]byte)(nil), test.wire...)
  37. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  38. if err := opts.Unmarshal(wire, got); err != nil {
  39. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  40. return
  41. }
  42. // Aliasing check: Modifying the original wire bytes shouldn't
  43. // affect the unmarshaled message.
  44. for i := range wire {
  45. wire[i] = 0
  46. }
  47. if !proto.Equal(got, want) {
  48. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", marshalText(got), marshalText(want))
  49. }
  50. })
  51. }
  52. }
  53. }
  54. func TestDecodeRequiredFieldChecks(t *testing.T) {
  55. for _, test := range testProtos {
  56. if !test.partial {
  57. continue
  58. }
  59. for _, m := range test.decodeTo {
  60. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  61. got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
  62. if err := proto.Unmarshal(test.wire, got); err == nil {
  63. t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
  64. }
  65. })
  66. }
  67. }
  68. }
  69. func TestDecodeInvalidUTF8(t *testing.T) {
  70. for _, test := range invalidUTF8TestProtos {
  71. for _, want := range test.decodeTo {
  72. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  73. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  74. err := proto.Unmarshal(test.wire, got)
  75. if err == nil {
  76. t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  77. }
  78. })
  79. }
  80. }
  81. }
  82. func TestDecodeNoEnforceUTF8(t *testing.T) {
  83. for _, test := range noEnforceUTF8TestProtos {
  84. for _, want := range test.decodeTo {
  85. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  86. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  87. err := proto.Unmarshal(test.wire, got)
  88. switch {
  89. case flags.ProtoLegacy && err != nil:
  90. t.Errorf("Unmarshal returned unexpected error: %v\nMessage:\n%v", err, marshalText(want))
  91. case !flags.ProtoLegacy && err == nil:
  92. t.Errorf("Unmarshal did not return expected error for invalid UTF8: %v\nMessage:\n%v", err, marshalText(want))
  93. }
  94. })
  95. }
  96. }
  97. }
  98. func TestDecodeZeroLengthBytes(t *testing.T) {
  99. // Verify that proto3 bytes fields don't give the mistaken
  100. // impression that they preserve presence.
  101. wire := pack.Message{
  102. pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
  103. }.Marshal()
  104. m := &test3pb.TestAllTypes{}
  105. if err := proto.Unmarshal(wire, m); err != nil {
  106. t.Fatal(err)
  107. }
  108. if m.OptionalBytes != nil {
  109. t.Errorf("unmarshal zero-length proto3 bytes field: got %v, want nil", m.OptionalBytes)
  110. }
  111. }
  112. func TestDecodeOneofNilWrapper(t *testing.T) {
  113. wire := pack.Message{
  114. pack.Tag{111, pack.VarintType}, pack.Varint(1111),
  115. }.Marshal()
  116. m := &testpb.TestAllTypes{OneofField: (*testpb.TestAllTypes_OneofUint32)(nil)}
  117. if err := proto.Unmarshal(wire, m); err != nil {
  118. t.Fatal(err)
  119. }
  120. if got := m.GetOneofUint32(); got != 1111 {
  121. t.Errorf("GetOneofUint32() = %v, want %v", got, 1111)
  122. }
  123. }
  124. func TestDecodeInvalidFieldNumbers(t *testing.T) {
  125. for _, test := range invalidFieldNumberTestProtos {
  126. t.Run(test.desc, func(t *testing.T) {
  127. decoded := new(testpb.TestAllTypes) // type doesn't matter since we expect errors
  128. err := proto.Unmarshal(test.wire, decoded)
  129. if err == nil && !test.allowed {
  130. t.Error("unmarshal: got nil want error")
  131. } else if err != nil && test.allowed {
  132. t.Errorf("unmarshal: got %v want nil since %s is allowed by Unmarshal", err, test.desc)
  133. }
  134. })
  135. }
  136. }
  137. var testProtos = []testProto{
  138. {
  139. desc: "basic scalar types",
  140. decodeTo: []proto.Message{&testpb.TestAllTypes{
  141. OptionalInt32: proto.Int32(1001),
  142. OptionalInt64: proto.Int64(1002),
  143. OptionalUint32: proto.Uint32(1003),
  144. OptionalUint64: proto.Uint64(1004),
  145. OptionalSint32: proto.Int32(1005),
  146. OptionalSint64: proto.Int64(1006),
  147. OptionalFixed32: proto.Uint32(1007),
  148. OptionalFixed64: proto.Uint64(1008),
  149. OptionalSfixed32: proto.Int32(1009),
  150. OptionalSfixed64: proto.Int64(1010),
  151. OptionalFloat: proto.Float32(1011.5),
  152. OptionalDouble: proto.Float64(1012.5),
  153. OptionalBool: proto.Bool(true),
  154. OptionalString: proto.String("string"),
  155. OptionalBytes: []byte("bytes"),
  156. OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
  157. }, &test3pb.TestAllTypes{
  158. OptionalInt32: 1001,
  159. OptionalInt64: 1002,
  160. OptionalUint32: 1003,
  161. OptionalUint64: 1004,
  162. OptionalSint32: 1005,
  163. OptionalSint64: 1006,
  164. OptionalFixed32: 1007,
  165. OptionalFixed64: 1008,
  166. OptionalSfixed32: 1009,
  167. OptionalSfixed64: 1010,
  168. OptionalFloat: 1011.5,
  169. OptionalDouble: 1012.5,
  170. OptionalBool: true,
  171. OptionalString: "string",
  172. OptionalBytes: []byte("bytes"),
  173. OptionalNestedEnum: test3pb.TestAllTypes_BAR,
  174. }, build(
  175. &testpb.TestAllExtensions{},
  176. extend(testpb.E_OptionalInt32Extension, int32(1001)),
  177. extend(testpb.E_OptionalInt64Extension, int64(1002)),
  178. extend(testpb.E_OptionalUint32Extension, uint32(1003)),
  179. extend(testpb.E_OptionalUint64Extension, uint64(1004)),
  180. extend(testpb.E_OptionalSint32Extension, int32(1005)),
  181. extend(testpb.E_OptionalSint64Extension, int64(1006)),
  182. extend(testpb.E_OptionalFixed32Extension, uint32(1007)),
  183. extend(testpb.E_OptionalFixed64Extension, uint64(1008)),
  184. extend(testpb.E_OptionalSfixed32Extension, int32(1009)),
  185. extend(testpb.E_OptionalSfixed64Extension, int64(1010)),
  186. extend(testpb.E_OptionalFloatExtension, float32(1011.5)),
  187. extend(testpb.E_OptionalDoubleExtension, float64(1012.5)),
  188. extend(testpb.E_OptionalBoolExtension, bool(true)),
  189. extend(testpb.E_OptionalStringExtension, string("string")),
  190. extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
  191. extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR),
  192. )},
  193. wire: pack.Message{
  194. pack.Tag{1, pack.VarintType}, pack.Varint(1001),
  195. pack.Tag{2, pack.VarintType}, pack.Varint(1002),
  196. pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
  197. pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
  198. pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
  199. pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
  200. pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
  201. pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
  202. pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
  203. pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
  204. pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
  205. pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
  206. pack.Tag{13, pack.VarintType}, pack.Bool(true),
  207. pack.Tag{14, pack.BytesType}, pack.String("string"),
  208. pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
  209. pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  210. }.Marshal(),
  211. },
  212. {
  213. desc: "zero values",
  214. decodeTo: []proto.Message{&testpb.TestAllTypes{
  215. OptionalInt32: proto.Int32(0),
  216. OptionalInt64: proto.Int64(0),
  217. OptionalUint32: proto.Uint32(0),
  218. OptionalUint64: proto.Uint64(0),
  219. OptionalSint32: proto.Int32(0),
  220. OptionalSint64: proto.Int64(0),
  221. OptionalFixed32: proto.Uint32(0),
  222. OptionalFixed64: proto.Uint64(0),
  223. OptionalSfixed32: proto.Int32(0),
  224. OptionalSfixed64: proto.Int64(0),
  225. OptionalFloat: proto.Float32(0),
  226. OptionalDouble: proto.Float64(0),
  227. OptionalBool: proto.Bool(false),
  228. OptionalString: proto.String(""),
  229. OptionalBytes: []byte{},
  230. }, &test3pb.TestAllTypes{}, build(
  231. &testpb.TestAllExtensions{},
  232. extend(testpb.E_OptionalInt32Extension, int32(0)),
  233. extend(testpb.E_OptionalInt64Extension, int64(0)),
  234. extend(testpb.E_OptionalUint32Extension, uint32(0)),
  235. extend(testpb.E_OptionalUint64Extension, uint64(0)),
  236. extend(testpb.E_OptionalSint32Extension, int32(0)),
  237. extend(testpb.E_OptionalSint64Extension, int64(0)),
  238. extend(testpb.E_OptionalFixed32Extension, uint32(0)),
  239. extend(testpb.E_OptionalFixed64Extension, uint64(0)),
  240. extend(testpb.E_OptionalSfixed32Extension, int32(0)),
  241. extend(testpb.E_OptionalSfixed64Extension, int64(0)),
  242. extend(testpb.E_OptionalFloatExtension, float32(0)),
  243. extend(testpb.E_OptionalDoubleExtension, float64(0)),
  244. extend(testpb.E_OptionalBoolExtension, bool(false)),
  245. extend(testpb.E_OptionalStringExtension, string("")),
  246. extend(testpb.E_OptionalBytesExtension, []byte{}),
  247. )},
  248. wire: pack.Message{
  249. pack.Tag{1, pack.VarintType}, pack.Varint(0),
  250. pack.Tag{2, pack.VarintType}, pack.Varint(0),
  251. pack.Tag{3, pack.VarintType}, pack.Uvarint(0),
  252. pack.Tag{4, pack.VarintType}, pack.Uvarint(0),
  253. pack.Tag{5, pack.VarintType}, pack.Svarint(0),
  254. pack.Tag{6, pack.VarintType}, pack.Svarint(0),
  255. pack.Tag{7, pack.Fixed32Type}, pack.Uint32(0),
  256. pack.Tag{8, pack.Fixed64Type}, pack.Uint64(0),
  257. pack.Tag{9, pack.Fixed32Type}, pack.Int32(0),
  258. pack.Tag{10, pack.Fixed64Type}, pack.Int64(0),
  259. pack.Tag{11, pack.Fixed32Type}, pack.Float32(0),
  260. pack.Tag{12, pack.Fixed64Type}, pack.Float64(0),
  261. pack.Tag{13, pack.VarintType}, pack.Bool(false),
  262. pack.Tag{14, pack.BytesType}, pack.String(""),
  263. pack.Tag{15, pack.BytesType}, pack.Bytes(nil),
  264. }.Marshal(),
  265. },
  266. {
  267. desc: "groups",
  268. decodeTo: []proto.Message{&testpb.TestAllTypes{
  269. Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
  270. A: proto.Int32(1017),
  271. },
  272. }, build(
  273. &testpb.TestAllExtensions{},
  274. extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
  275. A: proto.Int32(1017),
  276. }),
  277. )},
  278. wire: pack.Message{
  279. pack.Tag{16, pack.StartGroupType},
  280. pack.Tag{17, pack.VarintType}, pack.Varint(1017),
  281. pack.Tag{16, pack.EndGroupType},
  282. }.Marshal(),
  283. },
  284. {
  285. desc: "groups (field overridden)",
  286. decodeTo: []proto.Message{&testpb.TestAllTypes{
  287. Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
  288. A: proto.Int32(2),
  289. },
  290. }, build(
  291. &testpb.TestAllExtensions{},
  292. extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
  293. A: proto.Int32(2),
  294. }),
  295. )},
  296. wire: pack.Message{
  297. pack.Tag{16, pack.StartGroupType},
  298. pack.Tag{17, pack.VarintType}, pack.Varint(1),
  299. pack.Tag{16, pack.EndGroupType},
  300. pack.Tag{16, pack.StartGroupType},
  301. pack.Tag{17, pack.VarintType}, pack.Varint(2),
  302. pack.Tag{16, pack.EndGroupType},
  303. }.Marshal(),
  304. },
  305. {
  306. desc: "messages",
  307. decodeTo: []proto.Message{&testpb.TestAllTypes{
  308. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  309. A: proto.Int32(42),
  310. Corecursive: &testpb.TestAllTypes{
  311. OptionalInt32: proto.Int32(43),
  312. },
  313. },
  314. }, &test3pb.TestAllTypes{
  315. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  316. A: 42,
  317. Corecursive: &test3pb.TestAllTypes{
  318. OptionalInt32: 43,
  319. },
  320. },
  321. }, build(
  322. &testpb.TestAllExtensions{},
  323. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  324. A: proto.Int32(42),
  325. Corecursive: &testpb.TestAllTypes{
  326. OptionalInt32: proto.Int32(43),
  327. },
  328. }),
  329. )},
  330. wire: pack.Message{
  331. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  332. pack.Tag{1, pack.VarintType}, pack.Varint(42),
  333. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  334. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  335. }),
  336. }),
  337. }.Marshal(),
  338. },
  339. {
  340. desc: "messages (split across multiple tags)",
  341. decodeTo: []proto.Message{&testpb.TestAllTypes{
  342. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  343. A: proto.Int32(42),
  344. Corecursive: &testpb.TestAllTypes{
  345. OptionalInt32: proto.Int32(43),
  346. },
  347. },
  348. }, &test3pb.TestAllTypes{
  349. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  350. A: 42,
  351. Corecursive: &test3pb.TestAllTypes{
  352. OptionalInt32: 43,
  353. },
  354. },
  355. }, build(
  356. &testpb.TestAllExtensions{},
  357. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  358. A: proto.Int32(42),
  359. Corecursive: &testpb.TestAllTypes{
  360. OptionalInt32: proto.Int32(43),
  361. },
  362. }),
  363. )},
  364. wire: pack.Message{
  365. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  366. pack.Tag{1, pack.VarintType}, pack.Varint(42),
  367. }),
  368. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  369. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  370. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  371. }),
  372. }),
  373. }.Marshal(),
  374. },
  375. {
  376. desc: "messages (field overridden)",
  377. decodeTo: []proto.Message{&testpb.TestAllTypes{
  378. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  379. A: proto.Int32(2),
  380. },
  381. }, &test3pb.TestAllTypes{
  382. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  383. A: 2,
  384. },
  385. }, build(
  386. &testpb.TestAllExtensions{},
  387. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  388. A: proto.Int32(2),
  389. }),
  390. )},
  391. wire: pack.Message{
  392. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  393. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  394. }),
  395. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  396. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  397. }),
  398. }.Marshal(),
  399. },
  400. {
  401. desc: "basic repeated types",
  402. decodeTo: []proto.Message{&testpb.TestAllTypes{
  403. RepeatedInt32: []int32{1001, 2001},
  404. RepeatedInt64: []int64{1002, 2002},
  405. RepeatedUint32: []uint32{1003, 2003},
  406. RepeatedUint64: []uint64{1004, 2004},
  407. RepeatedSint32: []int32{1005, 2005},
  408. RepeatedSint64: []int64{1006, 2006},
  409. RepeatedFixed32: []uint32{1007, 2007},
  410. RepeatedFixed64: []uint64{1008, 2008},
  411. RepeatedSfixed32: []int32{1009, 2009},
  412. RepeatedSfixed64: []int64{1010, 2010},
  413. RepeatedFloat: []float32{1011.5, 2011.5},
  414. RepeatedDouble: []float64{1012.5, 2012.5},
  415. RepeatedBool: []bool{true, false},
  416. RepeatedString: []string{"foo", "bar"},
  417. RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
  418. RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
  419. testpb.TestAllTypes_FOO,
  420. testpb.TestAllTypes_BAR,
  421. },
  422. }, &test3pb.TestAllTypes{
  423. RepeatedInt32: []int32{1001, 2001},
  424. RepeatedInt64: []int64{1002, 2002},
  425. RepeatedUint32: []uint32{1003, 2003},
  426. RepeatedUint64: []uint64{1004, 2004},
  427. RepeatedSint32: []int32{1005, 2005},
  428. RepeatedSint64: []int64{1006, 2006},
  429. RepeatedFixed32: []uint32{1007, 2007},
  430. RepeatedFixed64: []uint64{1008, 2008},
  431. RepeatedSfixed32: []int32{1009, 2009},
  432. RepeatedSfixed64: []int64{1010, 2010},
  433. RepeatedFloat: []float32{1011.5, 2011.5},
  434. RepeatedDouble: []float64{1012.5, 2012.5},
  435. RepeatedBool: []bool{true, false},
  436. RepeatedString: []string{"foo", "bar"},
  437. RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
  438. RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
  439. test3pb.TestAllTypes_FOO,
  440. test3pb.TestAllTypes_BAR,
  441. },
  442. }, build(
  443. &testpb.TestAllExtensions{},
  444. extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
  445. extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
  446. extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
  447. extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
  448. extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
  449. extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
  450. extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
  451. extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
  452. extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
  453. extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
  454. extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
  455. extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
  456. extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
  457. extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
  458. extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
  459. extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
  460. testpb.TestAllTypes_FOO,
  461. testpb.TestAllTypes_BAR,
  462. }),
  463. )},
  464. wire: pack.Message{
  465. pack.Tag{31, pack.VarintType}, pack.Varint(1001),
  466. pack.Tag{31, pack.VarintType}, pack.Varint(2001),
  467. pack.Tag{32, pack.VarintType}, pack.Varint(1002),
  468. pack.Tag{32, pack.VarintType}, pack.Varint(2002),
  469. pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
  470. pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
  471. pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
  472. pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
  473. pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
  474. pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
  475. pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
  476. pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
  477. pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
  478. pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
  479. pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
  480. pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
  481. pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
  482. pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
  483. pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
  484. pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
  485. pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
  486. pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
  487. pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
  488. pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
  489. pack.Tag{43, pack.VarintType}, pack.Bool(true),
  490. pack.Tag{43, pack.VarintType}, pack.Bool(false),
  491. pack.Tag{44, pack.BytesType}, pack.String("foo"),
  492. pack.Tag{44, pack.BytesType}, pack.String("bar"),
  493. pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
  494. pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
  495. pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
  496. pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  497. }.Marshal(),
  498. },
  499. {
  500. desc: "basic repeated types (packed encoding)",
  501. decodeTo: []proto.Message{&testpb.TestAllTypes{
  502. RepeatedInt32: []int32{1001, 2001},
  503. RepeatedInt64: []int64{1002, 2002},
  504. RepeatedUint32: []uint32{1003, 2003},
  505. RepeatedUint64: []uint64{1004, 2004},
  506. RepeatedSint32: []int32{1005, 2005},
  507. RepeatedSint64: []int64{1006, 2006},
  508. RepeatedFixed32: []uint32{1007, 2007},
  509. RepeatedFixed64: []uint64{1008, 2008},
  510. RepeatedSfixed32: []int32{1009, 2009},
  511. RepeatedSfixed64: []int64{1010, 2010},
  512. RepeatedFloat: []float32{1011.5, 2011.5},
  513. RepeatedDouble: []float64{1012.5, 2012.5},
  514. RepeatedBool: []bool{true, false},
  515. RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
  516. testpb.TestAllTypes_FOO,
  517. testpb.TestAllTypes_BAR,
  518. },
  519. }, &test3pb.TestAllTypes{
  520. RepeatedInt32: []int32{1001, 2001},
  521. RepeatedInt64: []int64{1002, 2002},
  522. RepeatedUint32: []uint32{1003, 2003},
  523. RepeatedUint64: []uint64{1004, 2004},
  524. RepeatedSint32: []int32{1005, 2005},
  525. RepeatedSint64: []int64{1006, 2006},
  526. RepeatedFixed32: []uint32{1007, 2007},
  527. RepeatedFixed64: []uint64{1008, 2008},
  528. RepeatedSfixed32: []int32{1009, 2009},
  529. RepeatedSfixed64: []int64{1010, 2010},
  530. RepeatedFloat: []float32{1011.5, 2011.5},
  531. RepeatedDouble: []float64{1012.5, 2012.5},
  532. RepeatedBool: []bool{true, false},
  533. RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
  534. test3pb.TestAllTypes_FOO,
  535. test3pb.TestAllTypes_BAR,
  536. },
  537. }, build(
  538. &testpb.TestAllExtensions{},
  539. extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
  540. extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
  541. extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
  542. extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
  543. extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
  544. extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
  545. extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
  546. extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
  547. extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
  548. extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
  549. extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
  550. extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
  551. extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
  552. extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
  553. testpb.TestAllTypes_FOO,
  554. testpb.TestAllTypes_BAR,
  555. }),
  556. )},
  557. wire: pack.Message{
  558. pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
  559. pack.Varint(1001), pack.Varint(2001),
  560. },
  561. pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
  562. pack.Varint(1002), pack.Varint(2002),
  563. },
  564. pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
  565. pack.Uvarint(1003), pack.Uvarint(2003),
  566. },
  567. pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
  568. pack.Uvarint(1004), pack.Uvarint(2004),
  569. },
  570. pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
  571. pack.Svarint(1005), pack.Svarint(2005),
  572. },
  573. pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
  574. pack.Svarint(1006), pack.Svarint(2006),
  575. },
  576. pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
  577. pack.Uint32(1007), pack.Uint32(2007),
  578. },
  579. pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
  580. pack.Uint64(1008), pack.Uint64(2008),
  581. },
  582. pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
  583. pack.Int32(1009), pack.Int32(2009),
  584. },
  585. pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
  586. pack.Int64(1010), pack.Int64(2010),
  587. },
  588. pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
  589. pack.Float32(1011.5), pack.Float32(2011.5),
  590. },
  591. pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
  592. pack.Float64(1012.5), pack.Float64(2012.5),
  593. },
  594. pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
  595. pack.Bool(true), pack.Bool(false),
  596. },
  597. pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
  598. pack.Varint(int(testpb.TestAllTypes_FOO)),
  599. pack.Varint(int(testpb.TestAllTypes_BAR)),
  600. },
  601. }.Marshal(),
  602. },
  603. {
  604. desc: "packed repeated types",
  605. decodeTo: []proto.Message{&testpb.TestPackedTypes{
  606. PackedInt32: []int32{1001, 2001},
  607. PackedInt64: []int64{1002, 2002},
  608. PackedUint32: []uint32{1003, 2003},
  609. PackedUint64: []uint64{1004, 2004},
  610. PackedSint32: []int32{1005, 2005},
  611. PackedSint64: []int64{1006, 2006},
  612. PackedFixed32: []uint32{1007, 2007},
  613. PackedFixed64: []uint64{1008, 2008},
  614. PackedSfixed32: []int32{1009, 2009},
  615. PackedSfixed64: []int64{1010, 2010},
  616. PackedFloat: []float32{1011.5, 2011.5},
  617. PackedDouble: []float64{1012.5, 2012.5},
  618. PackedBool: []bool{true, false},
  619. PackedEnum: []testpb.ForeignEnum{
  620. testpb.ForeignEnum_FOREIGN_FOO,
  621. testpb.ForeignEnum_FOREIGN_BAR,
  622. },
  623. }, build(
  624. &testpb.TestPackedExtensions{},
  625. extend(testpb.E_PackedInt32Extension, []int32{1001, 2001}),
  626. extend(testpb.E_PackedInt64Extension, []int64{1002, 2002}),
  627. extend(testpb.E_PackedUint32Extension, []uint32{1003, 2003}),
  628. extend(testpb.E_PackedUint64Extension, []uint64{1004, 2004}),
  629. extend(testpb.E_PackedSint32Extension, []int32{1005, 2005}),
  630. extend(testpb.E_PackedSint64Extension, []int64{1006, 2006}),
  631. extend(testpb.E_PackedFixed32Extension, []uint32{1007, 2007}),
  632. extend(testpb.E_PackedFixed64Extension, []uint64{1008, 2008}),
  633. extend(testpb.E_PackedSfixed32Extension, []int32{1009, 2009}),
  634. extend(testpb.E_PackedSfixed64Extension, []int64{1010, 2010}),
  635. extend(testpb.E_PackedFloatExtension, []float32{1011.5, 2011.5}),
  636. extend(testpb.E_PackedDoubleExtension, []float64{1012.5, 2012.5}),
  637. extend(testpb.E_PackedBoolExtension, []bool{true, false}),
  638. extend(testpb.E_PackedEnumExtension, []testpb.ForeignEnum{
  639. testpb.ForeignEnum_FOREIGN_FOO,
  640. testpb.ForeignEnum_FOREIGN_BAR,
  641. }),
  642. )},
  643. wire: pack.Message{
  644. pack.Tag{90, pack.BytesType}, pack.LengthPrefix{
  645. pack.Varint(1001), pack.Varint(2001),
  646. },
  647. pack.Tag{91, pack.BytesType}, pack.LengthPrefix{
  648. pack.Varint(1002), pack.Varint(2002),
  649. },
  650. pack.Tag{92, pack.BytesType}, pack.LengthPrefix{
  651. pack.Uvarint(1003), pack.Uvarint(2003),
  652. },
  653. pack.Tag{93, pack.BytesType}, pack.LengthPrefix{
  654. pack.Uvarint(1004), pack.Uvarint(2004),
  655. },
  656. pack.Tag{94, pack.BytesType}, pack.LengthPrefix{
  657. pack.Svarint(1005), pack.Svarint(2005),
  658. },
  659. pack.Tag{95, pack.BytesType}, pack.LengthPrefix{
  660. pack.Svarint(1006), pack.Svarint(2006),
  661. },
  662. pack.Tag{96, pack.BytesType}, pack.LengthPrefix{
  663. pack.Uint32(1007), pack.Uint32(2007),
  664. },
  665. pack.Tag{97, pack.BytesType}, pack.LengthPrefix{
  666. pack.Uint64(1008), pack.Uint64(2008),
  667. },
  668. pack.Tag{98, pack.BytesType}, pack.LengthPrefix{
  669. pack.Int32(1009), pack.Int32(2009),
  670. },
  671. pack.Tag{99, pack.BytesType}, pack.LengthPrefix{
  672. pack.Int64(1010), pack.Int64(2010),
  673. },
  674. pack.Tag{100, pack.BytesType}, pack.LengthPrefix{
  675. pack.Float32(1011.5), pack.Float32(2011.5),
  676. },
  677. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{
  678. pack.Float64(1012.5), pack.Float64(2012.5),
  679. },
  680. pack.Tag{102, pack.BytesType}, pack.LengthPrefix{
  681. pack.Bool(true), pack.Bool(false),
  682. },
  683. pack.Tag{103, pack.BytesType}, pack.LengthPrefix{
  684. pack.Varint(int(testpb.ForeignEnum_FOREIGN_FOO)),
  685. pack.Varint(int(testpb.ForeignEnum_FOREIGN_BAR)),
  686. },
  687. }.Marshal(),
  688. },
  689. {
  690. desc: "repeated messages",
  691. decodeTo: []proto.Message{&testpb.TestAllTypes{
  692. RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
  693. {A: proto.Int32(1)},
  694. nil,
  695. {A: proto.Int32(2)},
  696. },
  697. }, &test3pb.TestAllTypes{
  698. RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
  699. {A: 1},
  700. nil,
  701. {A: 2},
  702. },
  703. }, build(
  704. &testpb.TestAllExtensions{},
  705. extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
  706. {A: proto.Int32(1)},
  707. nil,
  708. {A: proto.Int32(2)},
  709. }),
  710. )},
  711. wire: pack.Message{
  712. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
  713. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  714. }),
  715. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  716. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
  717. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  718. }),
  719. }.Marshal(),
  720. },
  721. {
  722. desc: "repeated groups",
  723. decodeTo: []proto.Message{&testpb.TestAllTypes{
  724. Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
  725. {A: proto.Int32(1017)},
  726. nil,
  727. {A: proto.Int32(2017)},
  728. },
  729. }, build(
  730. &testpb.TestAllExtensions{},
  731. extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
  732. {A: proto.Int32(1017)},
  733. nil,
  734. {A: proto.Int32(2017)},
  735. }),
  736. )},
  737. wire: pack.Message{
  738. pack.Tag{46, pack.StartGroupType},
  739. pack.Tag{47, pack.VarintType}, pack.Varint(1017),
  740. pack.Tag{46, pack.EndGroupType},
  741. pack.Tag{46, pack.StartGroupType},
  742. pack.Tag{46, pack.EndGroupType},
  743. pack.Tag{46, pack.StartGroupType},
  744. pack.Tag{47, pack.VarintType}, pack.Varint(2017),
  745. pack.Tag{46, pack.EndGroupType},
  746. }.Marshal(),
  747. },
  748. {
  749. desc: "maps",
  750. decodeTo: []proto.Message{&testpb.TestAllTypes{
  751. MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
  752. MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
  753. MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
  754. MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
  755. MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
  756. MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
  757. MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
  758. MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
  759. MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
  760. MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
  761. MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
  762. MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
  763. MapBoolBool: map[bool]bool{true: false, false: true},
  764. MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
  765. MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
  766. MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
  767. "71.1.key": {A: proto.Int32(1171)},
  768. "71.2.key": {A: proto.Int32(2171)},
  769. },
  770. MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
  771. "73.1.key": testpb.TestAllTypes_FOO,
  772. "73.2.key": testpb.TestAllTypes_BAR,
  773. },
  774. }, &test3pb.TestAllTypes{
  775. MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
  776. MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
  777. MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
  778. MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
  779. MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
  780. MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
  781. MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
  782. MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
  783. MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
  784. MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
  785. MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
  786. MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
  787. MapBoolBool: map[bool]bool{true: false, false: true},
  788. MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
  789. MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
  790. MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
  791. "71.1.key": {A: 1171},
  792. "71.2.key": {A: 2171},
  793. },
  794. MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
  795. "73.1.key": test3pb.TestAllTypes_FOO,
  796. "73.2.key": test3pb.TestAllTypes_BAR,
  797. },
  798. }},
  799. wire: pack.Message{
  800. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  801. pack.Tag{1, pack.VarintType}, pack.Varint(1056),
  802. pack.Tag{2, pack.VarintType}, pack.Varint(1156),
  803. }),
  804. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  805. pack.Tag{1, pack.VarintType}, pack.Varint(2056),
  806. pack.Tag{2, pack.VarintType}, pack.Varint(2156),
  807. }),
  808. pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
  809. pack.Tag{1, pack.VarintType}, pack.Varint(1057),
  810. pack.Tag{2, pack.VarintType}, pack.Varint(1157),
  811. }),
  812. pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
  813. pack.Tag{1, pack.VarintType}, pack.Varint(2057),
  814. pack.Tag{2, pack.VarintType}, pack.Varint(2157),
  815. }),
  816. pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
  817. pack.Tag{1, pack.VarintType}, pack.Varint(1058),
  818. pack.Tag{2, pack.VarintType}, pack.Varint(1158),
  819. }),
  820. pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
  821. pack.Tag{1, pack.VarintType}, pack.Varint(2058),
  822. pack.Tag{2, pack.VarintType}, pack.Varint(2158),
  823. }),
  824. pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
  825. pack.Tag{1, pack.VarintType}, pack.Varint(1059),
  826. pack.Tag{2, pack.VarintType}, pack.Varint(1159),
  827. }),
  828. pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
  829. pack.Tag{1, pack.VarintType}, pack.Varint(2059),
  830. pack.Tag{2, pack.VarintType}, pack.Varint(2159),
  831. }),
  832. pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
  833. pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
  834. pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
  835. }),
  836. pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
  837. pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
  838. pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
  839. }),
  840. pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
  841. pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
  842. pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
  843. }),
  844. pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
  845. pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
  846. pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
  847. }),
  848. pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
  849. pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
  850. pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
  851. }),
  852. pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
  853. pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
  854. pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
  855. }),
  856. pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
  857. pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
  858. pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
  859. }),
  860. pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
  861. pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
  862. pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
  863. }),
  864. pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
  865. pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
  866. pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
  867. }),
  868. pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
  869. pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
  870. pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
  871. }),
  872. pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
  873. pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
  874. pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
  875. }),
  876. pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
  877. pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
  878. pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
  879. }),
  880. pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
  881. pack.Tag{1, pack.VarintType}, pack.Varint(1066),
  882. pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
  883. }),
  884. pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
  885. pack.Tag{1, pack.VarintType}, pack.Varint(2066),
  886. pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
  887. }),
  888. pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
  889. pack.Tag{1, pack.VarintType}, pack.Varint(1067),
  890. pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
  891. }),
  892. pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
  893. pack.Tag{1, pack.VarintType}, pack.Varint(2067),
  894. pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
  895. }),
  896. pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
  897. pack.Tag{1, pack.VarintType}, pack.Bool(true),
  898. pack.Tag{2, pack.VarintType}, pack.Bool(false),
  899. }),
  900. pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
  901. pack.Tag{1, pack.VarintType}, pack.Bool(false),
  902. pack.Tag{2, pack.VarintType}, pack.Bool(true),
  903. }),
  904. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  905. pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
  906. pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
  907. }),
  908. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  909. pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
  910. pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
  911. }),
  912. pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
  913. pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
  914. pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
  915. }),
  916. pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
  917. pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
  918. pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
  919. }),
  920. pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
  921. pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
  922. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  923. pack.Tag{1, pack.VarintType}, pack.Varint(1171),
  924. }),
  925. }),
  926. pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
  927. pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
  928. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  929. pack.Tag{1, pack.VarintType}, pack.Varint(2171),
  930. }),
  931. }),
  932. pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
  933. pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
  934. pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
  935. }),
  936. pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
  937. pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
  938. pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  939. }),
  940. }.Marshal(),
  941. },
  942. {
  943. desc: "oneof (uint32)",
  944. decodeTo: []proto.Message{
  945. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
  946. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
  947. },
  948. wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
  949. },
  950. {
  951. desc: "oneof (message)",
  952. decodeTo: []proto.Message{
  953. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  954. &testpb.TestAllTypes_NestedMessage{A: proto.Int32(1112)},
  955. }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
  956. &test3pb.TestAllTypes_NestedMessage{A: 1112},
  957. }},
  958. },
  959. wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  960. pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
  961. })}.Marshal(),
  962. },
  963. {
  964. desc: "oneof (empty message)",
  965. decodeTo: []proto.Message{
  966. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  967. &testpb.TestAllTypes_NestedMessage{},
  968. }},
  969. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
  970. &test3pb.TestAllTypes_NestedMessage{},
  971. }},
  972. },
  973. wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
  974. },
  975. {
  976. desc: "oneof (merged message)",
  977. decodeTo: []proto.Message{
  978. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  979. &testpb.TestAllTypes_NestedMessage{
  980. A: proto.Int32(1),
  981. Corecursive: &testpb.TestAllTypes{
  982. OptionalInt32: proto.Int32(43),
  983. },
  984. },
  985. }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
  986. &test3pb.TestAllTypes_NestedMessage{
  987. A: 1,
  988. Corecursive: &test3pb.TestAllTypes{
  989. OptionalInt32: 43,
  990. },
  991. },
  992. }}},
  993. wire: pack.Message{
  994. pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  995. pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
  996. }),
  997. pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  998. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  999. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  1000. }),
  1001. }),
  1002. }.Marshal(),
  1003. },
  1004. {
  1005. desc: "oneof (string)",
  1006. decodeTo: []proto.Message{
  1007. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
  1008. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
  1009. },
  1010. wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
  1011. },
  1012. {
  1013. desc: "oneof (bytes)",
  1014. decodeTo: []proto.Message{
  1015. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
  1016. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
  1017. },
  1018. wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
  1019. },
  1020. {
  1021. desc: "oneof (bool)",
  1022. decodeTo: []proto.Message{
  1023. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
  1024. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
  1025. },
  1026. wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
  1027. },
  1028. {
  1029. desc: "oneof (uint64)",
  1030. decodeTo: []proto.Message{
  1031. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
  1032. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
  1033. },
  1034. wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
  1035. },
  1036. {
  1037. desc: "oneof (float)",
  1038. decodeTo: []proto.Message{
  1039. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
  1040. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
  1041. },
  1042. wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
  1043. },
  1044. {
  1045. desc: "oneof (double)",
  1046. decodeTo: []proto.Message{
  1047. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
  1048. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
  1049. },
  1050. wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
  1051. },
  1052. {
  1053. desc: "oneof (enum)",
  1054. decodeTo: []proto.Message{
  1055. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
  1056. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
  1057. },
  1058. wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
  1059. },
  1060. {
  1061. desc: "oneof (zero)",
  1062. decodeTo: []proto.Message{
  1063. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{0}},
  1064. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{0}},
  1065. },
  1066. wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(0)}.Marshal(),
  1067. },
  1068. {
  1069. desc: "oneof (overridden value)",
  1070. decodeTo: []proto.Message{
  1071. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
  1072. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
  1073. },
  1074. wire: pack.Message{
  1075. pack.Tag{111, pack.VarintType}, pack.Varint(1),
  1076. pack.Tag{116, pack.VarintType}, pack.Varint(2),
  1077. }.Marshal(),
  1078. },
  1079. // TODO: More unknown field tests for ordering, repeated fields, etc.
  1080. //
  1081. // It is currently impossible to produce results that the v1 Equal
  1082. // considers equivalent to those of the v1 decoder. Figure out if
  1083. // that's a problem or not.
  1084. {
  1085. desc: "unknown fields",
  1086. decodeTo: []proto.Message{build(
  1087. &testpb.TestAllTypes{},
  1088. unknown(pack.Message{
  1089. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  1090. }.Marshal()),
  1091. ), build(
  1092. &test3pb.TestAllTypes{},
  1093. unknown(pack.Message{
  1094. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  1095. }.Marshal()),
  1096. )},
  1097. wire: pack.Message{
  1098. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  1099. }.Marshal(),
  1100. },
  1101. {
  1102. desc: "field type mismatch",
  1103. decodeTo: []proto.Message{build(
  1104. &testpb.TestAllTypes{},
  1105. unknown(pack.Message{
  1106. pack.Tag{1, pack.BytesType}, pack.String("string"),
  1107. }.Marshal()),
  1108. ), build(
  1109. &test3pb.TestAllTypes{},
  1110. unknown(pack.Message{
  1111. pack.Tag{1, pack.BytesType}, pack.String("string"),
  1112. }.Marshal()),
  1113. )},
  1114. wire: pack.Message{
  1115. pack.Tag{1, pack.BytesType}, pack.String("string"),
  1116. }.Marshal(),
  1117. },
  1118. {
  1119. desc: "map field element mismatch",
  1120. decodeTo: []proto.Message{
  1121. &testpb.TestAllTypes{
  1122. MapInt32Int32: map[int32]int32{1: 0},
  1123. }, &test3pb.TestAllTypes{
  1124. MapInt32Int32: map[int32]int32{1: 0},
  1125. },
  1126. },
  1127. wire: pack.Message{
  1128. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1129. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1130. pack.Tag{2, pack.BytesType}, pack.String("string"),
  1131. }),
  1132. }.Marshal(),
  1133. },
  1134. {
  1135. desc: "required field in nil message unset",
  1136. partial: true,
  1137. decodeTo: []proto.Message{(*testpb.TestRequired)(nil)},
  1138. },
  1139. {
  1140. desc: "required field unset",
  1141. partial: true,
  1142. decodeTo: []proto.Message{&testpb.TestRequired{}},
  1143. },
  1144. {
  1145. desc: "required field set",
  1146. decodeTo: []proto.Message{&testpb.TestRequired{
  1147. RequiredField: proto.Int32(1),
  1148. }},
  1149. wire: pack.Message{
  1150. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1151. }.Marshal(),
  1152. },
  1153. {
  1154. desc: "required field in optional message unset",
  1155. partial: true,
  1156. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1157. OptionalMessage: &testpb.TestRequired{},
  1158. }},
  1159. wire: pack.Message{
  1160. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1161. }.Marshal(),
  1162. },
  1163. {
  1164. desc: "required field in optional message set",
  1165. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1166. OptionalMessage: &testpb.TestRequired{
  1167. RequiredField: proto.Int32(1),
  1168. },
  1169. }},
  1170. wire: pack.Message{
  1171. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1172. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1173. }),
  1174. }.Marshal(),
  1175. },
  1176. {
  1177. desc: "required field in optional message set (split across multiple tags)",
  1178. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1179. OptionalMessage: &testpb.TestRequired{
  1180. RequiredField: proto.Int32(1),
  1181. },
  1182. }},
  1183. wire: pack.Message{
  1184. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1185. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1186. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1187. }),
  1188. }.Marshal(),
  1189. },
  1190. {
  1191. desc: "required field in repeated message unset",
  1192. partial: true,
  1193. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1194. RepeatedMessage: []*testpb.TestRequired{
  1195. {RequiredField: proto.Int32(1)},
  1196. {},
  1197. },
  1198. }},
  1199. wire: pack.Message{
  1200. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1201. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1202. }),
  1203. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1204. }.Marshal(),
  1205. },
  1206. {
  1207. desc: "required field in repeated message set",
  1208. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1209. RepeatedMessage: []*testpb.TestRequired{
  1210. {RequiredField: proto.Int32(1)},
  1211. {RequiredField: proto.Int32(2)},
  1212. },
  1213. }},
  1214. wire: pack.Message{
  1215. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1216. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1217. }),
  1218. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1219. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1220. }),
  1221. }.Marshal(),
  1222. },
  1223. {
  1224. desc: "required field in map message unset",
  1225. partial: true,
  1226. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1227. MapMessage: map[int32]*testpb.TestRequired{
  1228. 1: {RequiredField: proto.Int32(1)},
  1229. 2: {},
  1230. },
  1231. }},
  1232. wire: pack.Message{
  1233. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1234. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1235. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1236. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1237. }),
  1238. }),
  1239. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1240. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1241. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1242. }),
  1243. }.Marshal(),
  1244. },
  1245. {
  1246. desc: "required field in map message set",
  1247. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1248. MapMessage: map[int32]*testpb.TestRequired{
  1249. 1: {RequiredField: proto.Int32(1)},
  1250. 2: {RequiredField: proto.Int32(2)},
  1251. },
  1252. }},
  1253. wire: pack.Message{
  1254. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1255. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1256. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1257. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1258. }),
  1259. }),
  1260. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1261. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1262. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1263. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1264. }),
  1265. }),
  1266. }.Marshal(),
  1267. },
  1268. {
  1269. desc: "required field in optional group unset",
  1270. partial: true,
  1271. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1272. Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
  1273. }},
  1274. wire: pack.Message{
  1275. pack.Tag{1, pack.StartGroupType},
  1276. pack.Tag{1, pack.EndGroupType},
  1277. }.Marshal(),
  1278. },
  1279. {
  1280. desc: "required field in optional group set",
  1281. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1282. Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
  1283. A: proto.Int32(1),
  1284. },
  1285. }},
  1286. wire: pack.Message{
  1287. pack.Tag{1, pack.StartGroupType},
  1288. pack.Tag{2, pack.VarintType}, pack.Varint(1),
  1289. pack.Tag{1, pack.EndGroupType},
  1290. }.Marshal(),
  1291. },
  1292. {
  1293. desc: "required field in repeated group unset",
  1294. partial: true,
  1295. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1296. Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
  1297. {A: proto.Int32(1)},
  1298. {},
  1299. },
  1300. }},
  1301. wire: pack.Message{
  1302. pack.Tag{3, pack.StartGroupType},
  1303. pack.Tag{4, pack.VarintType}, pack.Varint(1),
  1304. pack.Tag{3, pack.EndGroupType},
  1305. pack.Tag{3, pack.StartGroupType},
  1306. pack.Tag{3, pack.EndGroupType},
  1307. }.Marshal(),
  1308. },
  1309. {
  1310. desc: "required field in repeated group set",
  1311. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1312. Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
  1313. {A: proto.Int32(1)},
  1314. {A: proto.Int32(2)},
  1315. },
  1316. }},
  1317. wire: pack.Message{
  1318. pack.Tag{3, pack.StartGroupType},
  1319. pack.Tag{4, pack.VarintType}, pack.Varint(1),
  1320. pack.Tag{3, pack.EndGroupType},
  1321. pack.Tag{3, pack.StartGroupType},
  1322. pack.Tag{4, pack.VarintType}, pack.Varint(2),
  1323. pack.Tag{3, pack.EndGroupType},
  1324. }.Marshal(),
  1325. },
  1326. {
  1327. desc: "required field in oneof message unset",
  1328. partial: true,
  1329. decodeTo: []proto.Message{
  1330. &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
  1331. &testpb.TestRequired{},
  1332. }},
  1333. },
  1334. wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{})}.Marshal(),
  1335. },
  1336. {
  1337. desc: "required field in oneof message set",
  1338. decodeTo: []proto.Message{
  1339. &testpb.TestRequiredForeign{OneofField: &testpb.TestRequiredForeign_OneofMessage{
  1340. &testpb.TestRequired{
  1341. RequiredField: proto.Int32(1),
  1342. },
  1343. }},
  1344. },
  1345. wire: pack.Message{pack.Tag{4, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1346. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1347. })}.Marshal(),
  1348. },
  1349. {
  1350. desc: "required field in extension message unset",
  1351. partial: true,
  1352. decodeTo: []proto.Message{build(
  1353. &testpb.TestAllExtensions{},
  1354. extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
  1355. )},
  1356. wire: pack.Message{
  1357. pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1358. }.Marshal(),
  1359. },
  1360. {
  1361. desc: "required field in extension message set",
  1362. decodeTo: []proto.Message{build(
  1363. &testpb.TestAllExtensions{},
  1364. extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
  1365. RequiredField: proto.Int32(1),
  1366. }),
  1367. )},
  1368. wire: pack.Message{
  1369. pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1370. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1371. }),
  1372. }.Marshal(),
  1373. },
  1374. {
  1375. desc: "required field in repeated extension message unset",
  1376. partial: true,
  1377. decodeTo: []proto.Message{build(
  1378. &testpb.TestAllExtensions{},
  1379. extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
  1380. {RequiredField: proto.Int32(1)},
  1381. {},
  1382. }),
  1383. )},
  1384. wire: pack.Message{
  1385. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1386. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1387. }),
  1388. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1389. }.Marshal(),
  1390. },
  1391. {
  1392. desc: "required field in repeated extension message set",
  1393. decodeTo: []proto.Message{build(
  1394. &testpb.TestAllExtensions{},
  1395. extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
  1396. {RequiredField: proto.Int32(1)},
  1397. {RequiredField: proto.Int32(2)},
  1398. }),
  1399. )},
  1400. wire: pack.Message{
  1401. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1402. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1403. }),
  1404. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1405. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1406. }),
  1407. }.Marshal(),
  1408. },
  1409. {
  1410. desc: "nil messages",
  1411. decodeTo: []proto.Message{
  1412. (*testpb.TestAllTypes)(nil),
  1413. (*test3pb.TestAllTypes)(nil),
  1414. (*testpb.TestAllExtensions)(nil),
  1415. },
  1416. },
  1417. {
  1418. desc: "legacy",
  1419. partial: true,
  1420. decodeTo: []proto.Message{
  1421. &legacypb.Legacy{
  1422. F1: &legacy1pb.Message{
  1423. OptionalInt32: proto.Int32(1),
  1424. OptionalChildEnum: legacy1pb.Message_ALPHA.Enum(),
  1425. OptionalChildMessage: &legacy1pb.Message_ChildMessage{
  1426. F1: proto.String("x"),
  1427. },
  1428. Optionalgroup: &legacy1pb.Message_OptionalGroup{
  1429. F1: proto.String("x"),
  1430. },
  1431. RepeatedChildMessage: []*legacy1pb.Message_ChildMessage{
  1432. {F1: proto.String("x")},
  1433. },
  1434. Repeatedgroup: []*legacy1pb.Message_RepeatedGroup{
  1435. {F1: proto.String("x")},
  1436. },
  1437. MapBoolChildMessage: map[bool]*legacy1pb.Message_ChildMessage{
  1438. true: {F1: proto.String("x")},
  1439. },
  1440. OneofUnion: &legacy1pb.Message_OneofChildMessage{
  1441. &legacy1pb.Message_ChildMessage{
  1442. F1: proto.String("x"),
  1443. },
  1444. },
  1445. },
  1446. },
  1447. },
  1448. wire: pack.Message{
  1449. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1450. pack.Tag{101, pack.VarintType}, pack.Varint(1),
  1451. pack.Tag{115, pack.VarintType}, pack.Varint(0),
  1452. pack.Tag{116, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1453. pack.Tag{1, pack.BytesType}, pack.String("x"),
  1454. }),
  1455. pack.Tag{120, pack.StartGroupType},
  1456. pack.Tag{1, pack.BytesType}, pack.String("x"),
  1457. pack.Tag{120, pack.EndGroupType},
  1458. pack.Tag{516, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1459. pack.Tag{1, pack.BytesType}, pack.String("x"),
  1460. }),
  1461. pack.Tag{520, pack.StartGroupType},
  1462. pack.Tag{1, pack.BytesType}, pack.String("x"),
  1463. pack.Tag{520, pack.EndGroupType},
  1464. pack.Tag{616, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1465. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1466. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1467. pack.Tag{1, pack.BytesType}, pack.String("x"),
  1468. }),
  1469. }),
  1470. pack.Tag{716, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1471. pack.Tag{1, pack.BytesType}, pack.String("x"),
  1472. }),
  1473. }),
  1474. }.Marshal(),
  1475. },
  1476. }
  1477. var invalidUTF8TestProtos = []testProto{
  1478. {
  1479. desc: "invalid UTF-8 in optional string field",
  1480. decodeTo: []proto.Message{&test3pb.TestAllTypes{
  1481. OptionalString: "abc\xff",
  1482. }},
  1483. wire: pack.Message{
  1484. pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
  1485. }.Marshal(),
  1486. },
  1487. {
  1488. desc: "invalid UTF-8 in repeated string field",
  1489. decodeTo: []proto.Message{&test3pb.TestAllTypes{
  1490. RepeatedString: []string{"foo", "abc\xff"},
  1491. }},
  1492. wire: pack.Message{
  1493. pack.Tag{44, pack.BytesType}, pack.String("foo"),
  1494. pack.Tag{44, pack.BytesType}, pack.String("abc\xff"),
  1495. }.Marshal(),
  1496. },
  1497. {
  1498. desc: "invalid UTF-8 in nested message",
  1499. decodeTo: []proto.Message{&test3pb.TestAllTypes{
  1500. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  1501. Corecursive: &test3pb.TestAllTypes{
  1502. OptionalString: "abc\xff",
  1503. },
  1504. },
  1505. }},
  1506. wire: pack.Message{
  1507. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1508. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1509. pack.Tag{14, pack.BytesType}, pack.String("abc\xff"),
  1510. }),
  1511. }),
  1512. }.Marshal(),
  1513. },
  1514. {
  1515. desc: "invalid UTF-8 in oneof field",
  1516. decodeTo: []proto.Message{
  1517. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"abc\xff"}},
  1518. },
  1519. wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
  1520. },
  1521. {
  1522. desc: "invalid UTF-8 in map key",
  1523. decodeTo: []proto.Message{&test3pb.TestAllTypes{
  1524. MapStringString: map[string]string{"key\xff": "val"},
  1525. }},
  1526. wire: pack.Message{
  1527. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1528. pack.Tag{1, pack.BytesType}, pack.String("key\xff"),
  1529. pack.Tag{2, pack.BytesType}, pack.String("val"),
  1530. }),
  1531. }.Marshal(),
  1532. },
  1533. {
  1534. desc: "invalid UTF-8 in map value",
  1535. decodeTo: []proto.Message{&test3pb.TestAllTypes{
  1536. MapStringString: map[string]string{"key": "val\xff"},
  1537. }},
  1538. wire: pack.Message{
  1539. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1540. pack.Tag{1, pack.BytesType}, pack.String("key"),
  1541. pack.Tag{2, pack.BytesType}, pack.String("val\xff"),
  1542. }),
  1543. }.Marshal(),
  1544. },
  1545. }
  1546. var noEnforceUTF8TestProtos = []testProto{
  1547. {
  1548. desc: "invalid UTF-8 in optional string field",
  1549. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  1550. OptionalString: string("abc\xff"),
  1551. }},
  1552. wire: pack.Message{
  1553. pack.Tag{1, pack.BytesType}, pack.String("abc\xff"),
  1554. }.Marshal(),
  1555. },
  1556. {
  1557. desc: "invalid UTF-8 in optional string field of Go bytes",
  1558. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  1559. OptionalBytes: []byte("abc\xff"),
  1560. }},
  1561. wire: pack.Message{
  1562. pack.Tag{2, pack.BytesType}, pack.String("abc\xff"),
  1563. }.Marshal(),
  1564. },
  1565. {
  1566. desc: "invalid UTF-8 in repeated string field",
  1567. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  1568. RepeatedString: []string{string("foo"), string("abc\xff")},
  1569. }},
  1570. wire: pack.Message{
  1571. pack.Tag{3, pack.BytesType}, pack.String("foo"),
  1572. pack.Tag{3, pack.BytesType}, pack.String("abc\xff"),
  1573. }.Marshal(),
  1574. },
  1575. {
  1576. desc: "invalid UTF-8 in repeated string field of Go bytes",
  1577. decodeTo: []proto.Message{&TestNoEnforceUTF8{
  1578. RepeatedBytes: [][]byte{[]byte("foo"), []byte("abc\xff")},
  1579. }},
  1580. wire: pack.Message{
  1581. pack.Tag{4, pack.BytesType}, pack.String("foo"),
  1582. pack.Tag{4, pack.BytesType}, pack.String("abc\xff"),
  1583. }.Marshal(),
  1584. },
  1585. {
  1586. desc: "invalid UTF-8 in oneof string field",
  1587. decodeTo: []proto.Message{
  1588. &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofString{string("abc\xff")}},
  1589. },
  1590. wire: pack.Message{pack.Tag{5, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
  1591. },
  1592. {
  1593. desc: "invalid UTF-8 in oneof string field of Go bytes",
  1594. decodeTo: []proto.Message{
  1595. &TestNoEnforceUTF8{OneofField: &TestNoEnforceUTF8_OneofBytes{[]byte("abc\xff")}},
  1596. },
  1597. wire: pack.Message{pack.Tag{6, pack.BytesType}, pack.String("abc\xff")}.Marshal(),
  1598. },
  1599. }
  1600. type TestNoEnforceUTF8 struct {
  1601. OptionalString string `protobuf:"bytes,1,opt,name=optional_string"`
  1602. OptionalBytes []byte `protobuf:"bytes,2,opt,name=optional_bytes"`
  1603. RepeatedString []string `protobuf:"bytes,3,rep,name=repeated_string"`
  1604. RepeatedBytes [][]byte `protobuf:"bytes,4,rep,name=repeated_bytes"`
  1605. OneofField isOneofField `protobuf_oneof:"oneof_field"`
  1606. }
  1607. type isOneofField interface{ isOneofField() }
  1608. type TestNoEnforceUTF8_OneofString struct {
  1609. OneofString string `protobuf:"bytes,5,opt,name=oneof_string,oneof"`
  1610. }
  1611. type TestNoEnforceUTF8_OneofBytes struct {
  1612. OneofBytes []byte `protobuf:"bytes,6,opt,name=oneof_bytes,oneof"`
  1613. }
  1614. func (*TestNoEnforceUTF8_OneofString) isOneofField() {}
  1615. func (*TestNoEnforceUTF8_OneofBytes) isOneofField() {}
  1616. func (m *TestNoEnforceUTF8) ProtoReflect() protoreflect.Message {
  1617. return messageInfo_TestNoEnforceUTF8.MessageOf(m)
  1618. }
  1619. var messageInfo_TestNoEnforceUTF8 = protoimpl.MessageInfo{
  1620. GoReflectType: reflect.TypeOf((*TestNoEnforceUTF8)(nil)),
  1621. Desc: func() protoreflect.MessageDescriptor {
  1622. pb := new(descriptorpb.FileDescriptorProto)
  1623. if err := prototext.Unmarshal([]byte(`
  1624. syntax: "proto3"
  1625. name: "test.proto"
  1626. message_type: [{
  1627. name: "TestNoEnforceUTF8"
  1628. field: [
  1629. {name:"optional_string" number:1 label:LABEL_OPTIONAL type:TYPE_STRING},
  1630. {name:"optional_bytes" number:2 label:LABEL_OPTIONAL type:TYPE_STRING},
  1631. {name:"repeated_string" number:3 label:LABEL_REPEATED type:TYPE_STRING},
  1632. {name:"repeated_bytes" number:4 label:LABEL_REPEATED type:TYPE_STRING},
  1633. {name:"oneof_string" number:5 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0},
  1634. {name:"oneof_bytes" number:6 label:LABEL_OPTIONAL type:TYPE_STRING, oneof_index:0}
  1635. ]
  1636. oneof_decl: [{name:"oneof_field"}]
  1637. }]
  1638. `), pb); err != nil {
  1639. panic(err)
  1640. }
  1641. fd, err := protodesc.NewFile(pb, nil)
  1642. if err != nil {
  1643. panic(err)
  1644. }
  1645. md := fd.Messages().Get(0)
  1646. for i := 0; i < md.Fields().Len(); i++ {
  1647. md.Fields().Get(i).(*filedesc.Field).L1.HasEnforceUTF8 = true
  1648. md.Fields().Get(i).(*filedesc.Field).L1.EnforceUTF8 = false
  1649. }
  1650. return md
  1651. }(),
  1652. OneofWrappers: []interface{}{
  1653. (*TestNoEnforceUTF8_OneofString)(nil),
  1654. (*TestNoEnforceUTF8_OneofBytes)(nil),
  1655. },
  1656. }
  1657. var invalidFieldNumberTestProtos = []struct {
  1658. desc string
  1659. wire []byte
  1660. allowed bool
  1661. }{
  1662. {
  1663. desc: "zero",
  1664. wire: pack.Message{
  1665. pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1001),
  1666. }.Marshal(),
  1667. },
  1668. {
  1669. desc: "zero and one",
  1670. wire: pack.Message{
  1671. pack.Tag{pack.MinValidNumber - 1, pack.VarintType}, pack.Varint(1002),
  1672. pack.Tag{pack.MinValidNumber, pack.VarintType}, pack.Varint(1003),
  1673. }.Marshal(),
  1674. },
  1675. {
  1676. desc: "first reserved",
  1677. wire: pack.Message{
  1678. pack.Tag{pack.FirstReservedNumber, pack.VarintType}, pack.Varint(1004),
  1679. }.Marshal(),
  1680. allowed: true,
  1681. },
  1682. {
  1683. desc: "last reserved",
  1684. wire: pack.Message{
  1685. pack.Tag{pack.LastReservedNumber, pack.VarintType}, pack.Varint(1005),
  1686. }.Marshal(),
  1687. allowed: true,
  1688. },
  1689. {
  1690. desc: "max and max+1",
  1691. wire: pack.Message{
  1692. pack.Tag{pack.MaxValidNumber, pack.VarintType}, pack.Varint(1006),
  1693. pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1007),
  1694. }.Marshal(),
  1695. allowed: flags.ProtoLegacy,
  1696. },
  1697. {
  1698. desc: "max+1",
  1699. wire: pack.Message{
  1700. pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
  1701. }.Marshal(),
  1702. allowed: flags.ProtoLegacy,
  1703. },
  1704. }
  1705. func build(m proto.Message, opts ...buildOpt) proto.Message {
  1706. for _, opt := range opts {
  1707. opt(m)
  1708. }
  1709. return m
  1710. }
  1711. type buildOpt func(proto.Message)
  1712. func unknown(raw protoreflect.RawFields) buildOpt {
  1713. return func(m proto.Message) {
  1714. m.ProtoReflect().SetUnknown(raw)
  1715. }
  1716. }
  1717. func extend(desc protoreflect.ExtensionType, value interface{}) buildOpt {
  1718. return func(m proto.Message) {
  1719. proto.SetExtension(m, desc, value)
  1720. }
  1721. }
  1722. func marshalText(m proto.Message) string {
  1723. if m == nil {
  1724. return "<nil>\n"
  1725. }
  1726. b, _ := prototext.MarshalOptions{
  1727. AllowPartial: true,
  1728. EmitUnknown: true,
  1729. Indent: "\t",
  1730. }.Marshal(m)
  1731. return string(b)
  1732. }