decode_test.go 59 KB

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