decode_test.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. protoV1 "github.com/golang/protobuf/proto"
  10. "github.com/golang/protobuf/v2/encoding/textpb"
  11. "github.com/golang/protobuf/v2/internal/encoding/pack"
  12. "github.com/golang/protobuf/v2/internal/scalar"
  13. "github.com/golang/protobuf/v2/proto"
  14. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  15. testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
  16. test3pb "github.com/golang/protobuf/v2/internal/testprotos/test3"
  17. )
  18. type testProto struct {
  19. desc string
  20. decodeTo []proto.Message
  21. wire []byte
  22. partial bool
  23. invalidExtensions bool
  24. }
  25. func TestDecode(t *testing.T) {
  26. for _, test := range testProtos {
  27. for _, want := range test.decodeTo {
  28. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  29. opts := proto.UnmarshalOptions{
  30. AllowPartial: test.partial,
  31. }
  32. wire := append(([]byte)(nil), test.wire...)
  33. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  34. if err := opts.Unmarshal(wire, got); err != nil {
  35. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, marshalText(want))
  36. return
  37. }
  38. // Aliasing check: Modifying the original wire bytes shouldn't
  39. // affect the unmarshaled message.
  40. for i := range wire {
  41. wire[i] = 0
  42. }
  43. if test.invalidExtensions {
  44. // Equal doesn't work on messages containing invalid extension data.
  45. return
  46. }
  47. if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
  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. if test.invalidExtensions {
  60. // Missing required fields in extensions just end up in the unknown fields.
  61. continue
  62. }
  63. for _, m := range test.decodeTo {
  64. t.Run(fmt.Sprintf("%s (%T)", test.desc, m), func(t *testing.T) {
  65. got := reflect.New(reflect.TypeOf(m).Elem()).Interface().(proto.Message)
  66. if err := proto.Unmarshal(test.wire, got); err == nil {
  67. t.Fatalf("Unmarshal succeeded (want error)\nMessage:\n%v", marshalText(got))
  68. }
  69. })
  70. }
  71. }
  72. }
  73. var testProtos = []testProto{
  74. {
  75. desc: "basic scalar types",
  76. decodeTo: []proto.Message{&testpb.TestAllTypes{
  77. OptionalInt32: scalar.Int32(1001),
  78. OptionalInt64: scalar.Int64(1002),
  79. OptionalUint32: scalar.Uint32(1003),
  80. OptionalUint64: scalar.Uint64(1004),
  81. OptionalSint32: scalar.Int32(1005),
  82. OptionalSint64: scalar.Int64(1006),
  83. OptionalFixed32: scalar.Uint32(1007),
  84. OptionalFixed64: scalar.Uint64(1008),
  85. OptionalSfixed32: scalar.Int32(1009),
  86. OptionalSfixed64: scalar.Int64(1010),
  87. OptionalFloat: scalar.Float32(1011.5),
  88. OptionalDouble: scalar.Float64(1012.5),
  89. OptionalBool: scalar.Bool(true),
  90. OptionalString: scalar.String("string"),
  91. OptionalBytes: []byte("bytes"),
  92. OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
  93. }, &test3pb.TestAllTypes{
  94. OptionalInt32: 1001,
  95. OptionalInt64: 1002,
  96. OptionalUint32: 1003,
  97. OptionalUint64: 1004,
  98. OptionalSint32: 1005,
  99. OptionalSint64: 1006,
  100. OptionalFixed32: 1007,
  101. OptionalFixed64: 1008,
  102. OptionalSfixed32: 1009,
  103. OptionalSfixed64: 1010,
  104. OptionalFloat: 1011.5,
  105. OptionalDouble: 1012.5,
  106. OptionalBool: true,
  107. OptionalString: "string",
  108. OptionalBytes: []byte("bytes"),
  109. OptionalNestedEnum: test3pb.TestAllTypes_BAR,
  110. }, build(
  111. &testpb.TestAllExtensions{},
  112. extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
  113. extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
  114. extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
  115. extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
  116. extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
  117. extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
  118. extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
  119. extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
  120. extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
  121. extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
  122. extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
  123. extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
  124. extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
  125. extend(testpb.E_OptionalStringExtension, scalar.String("string")),
  126. extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
  127. extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
  128. )},
  129. wire: pack.Message{
  130. pack.Tag{1, pack.VarintType}, pack.Varint(1001),
  131. pack.Tag{2, pack.VarintType}, pack.Varint(1002),
  132. pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
  133. pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
  134. pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
  135. pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
  136. pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
  137. pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
  138. pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
  139. pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
  140. pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
  141. pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
  142. pack.Tag{13, pack.VarintType}, pack.Bool(true),
  143. pack.Tag{14, pack.BytesType}, pack.String("string"),
  144. pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
  145. pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  146. }.Marshal(),
  147. },
  148. {
  149. desc: "groups",
  150. decodeTo: []proto.Message{&testpb.TestAllTypes{
  151. Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
  152. A: scalar.Int32(1017),
  153. },
  154. }, build(
  155. &testpb.TestAllExtensions{},
  156. extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
  157. A: scalar.Int32(1017),
  158. }),
  159. )},
  160. wire: pack.Message{
  161. pack.Tag{16, pack.StartGroupType},
  162. pack.Tag{17, pack.VarintType}, pack.Varint(1017),
  163. pack.Tag{16, pack.EndGroupType},
  164. }.Marshal(),
  165. },
  166. {
  167. desc: "groups (field overridden)",
  168. decodeTo: []proto.Message{&testpb.TestAllTypes{
  169. Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
  170. A: scalar.Int32(2),
  171. },
  172. }, build(
  173. &testpb.TestAllExtensions{},
  174. extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
  175. A: scalar.Int32(2),
  176. }),
  177. )},
  178. wire: pack.Message{
  179. pack.Tag{16, pack.StartGroupType},
  180. pack.Tag{17, pack.VarintType}, pack.Varint(1),
  181. pack.Tag{16, pack.EndGroupType},
  182. pack.Tag{16, pack.StartGroupType},
  183. pack.Tag{17, pack.VarintType}, pack.Varint(2),
  184. pack.Tag{16, pack.EndGroupType},
  185. }.Marshal(),
  186. },
  187. {
  188. desc: "messages",
  189. decodeTo: []proto.Message{&testpb.TestAllTypes{
  190. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  191. A: scalar.Int32(42),
  192. Corecursive: &testpb.TestAllTypes{
  193. OptionalInt32: scalar.Int32(43),
  194. },
  195. },
  196. }, &test3pb.TestAllTypes{
  197. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  198. A: 42,
  199. Corecursive: &test3pb.TestAllTypes{
  200. OptionalInt32: 43,
  201. },
  202. },
  203. }, build(
  204. &testpb.TestAllExtensions{},
  205. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  206. A: scalar.Int32(42),
  207. Corecursive: &testpb.TestAllTypes{
  208. OptionalInt32: scalar.Int32(43),
  209. },
  210. }),
  211. )},
  212. wire: pack.Message{
  213. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  214. pack.Tag{1, pack.VarintType}, pack.Varint(42),
  215. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  216. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  217. }),
  218. }),
  219. }.Marshal(),
  220. },
  221. {
  222. desc: "messages (split across multiple tags)",
  223. decodeTo: []proto.Message{&testpb.TestAllTypes{
  224. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  225. A: scalar.Int32(42),
  226. Corecursive: &testpb.TestAllTypes{
  227. OptionalInt32: scalar.Int32(43),
  228. },
  229. },
  230. }, &test3pb.TestAllTypes{
  231. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  232. A: 42,
  233. Corecursive: &test3pb.TestAllTypes{
  234. OptionalInt32: 43,
  235. },
  236. },
  237. }, build(
  238. &testpb.TestAllExtensions{},
  239. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  240. A: scalar.Int32(42),
  241. Corecursive: &testpb.TestAllTypes{
  242. OptionalInt32: scalar.Int32(43),
  243. },
  244. }),
  245. )},
  246. wire: pack.Message{
  247. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  248. pack.Tag{1, pack.VarintType}, pack.Varint(42),
  249. }),
  250. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  251. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  252. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  253. }),
  254. }),
  255. }.Marshal(),
  256. },
  257. {
  258. desc: "messages (field overridden)",
  259. decodeTo: []proto.Message{&testpb.TestAllTypes{
  260. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  261. A: scalar.Int32(2),
  262. },
  263. }, &test3pb.TestAllTypes{
  264. OptionalNestedMessage: &test3pb.TestAllTypes_NestedMessage{
  265. A: 2,
  266. },
  267. }, build(
  268. &testpb.TestAllExtensions{},
  269. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  270. A: scalar.Int32(2),
  271. }),
  272. )},
  273. wire: pack.Message{
  274. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  275. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  276. }),
  277. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  278. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  279. }),
  280. }.Marshal(),
  281. },
  282. {
  283. desc: "basic repeated types",
  284. decodeTo: []proto.Message{&testpb.TestAllTypes{
  285. RepeatedInt32: []int32{1001, 2001},
  286. RepeatedInt64: []int64{1002, 2002},
  287. RepeatedUint32: []uint32{1003, 2003},
  288. RepeatedUint64: []uint64{1004, 2004},
  289. RepeatedSint32: []int32{1005, 2005},
  290. RepeatedSint64: []int64{1006, 2006},
  291. RepeatedFixed32: []uint32{1007, 2007},
  292. RepeatedFixed64: []uint64{1008, 2008},
  293. RepeatedSfixed32: []int32{1009, 2009},
  294. RepeatedSfixed64: []int64{1010, 2010},
  295. RepeatedFloat: []float32{1011.5, 2011.5},
  296. RepeatedDouble: []float64{1012.5, 2012.5},
  297. RepeatedBool: []bool{true, false},
  298. RepeatedString: []string{"foo", "bar"},
  299. RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
  300. RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
  301. testpb.TestAllTypes_FOO,
  302. testpb.TestAllTypes_BAR,
  303. },
  304. }, &test3pb.TestAllTypes{
  305. RepeatedInt32: []int32{1001, 2001},
  306. RepeatedInt64: []int64{1002, 2002},
  307. RepeatedUint32: []uint32{1003, 2003},
  308. RepeatedUint64: []uint64{1004, 2004},
  309. RepeatedSint32: []int32{1005, 2005},
  310. RepeatedSint64: []int64{1006, 2006},
  311. RepeatedFixed32: []uint32{1007, 2007},
  312. RepeatedFixed64: []uint64{1008, 2008},
  313. RepeatedSfixed32: []int32{1009, 2009},
  314. RepeatedSfixed64: []int64{1010, 2010},
  315. RepeatedFloat: []float32{1011.5, 2011.5},
  316. RepeatedDouble: []float64{1012.5, 2012.5},
  317. RepeatedBool: []bool{true, false},
  318. RepeatedString: []string{"foo", "bar"},
  319. RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
  320. RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
  321. test3pb.TestAllTypes_FOO,
  322. test3pb.TestAllTypes_BAR,
  323. },
  324. }, build(
  325. &testpb.TestAllExtensions{},
  326. extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
  327. extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
  328. extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
  329. extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
  330. extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
  331. extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
  332. extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
  333. extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
  334. extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
  335. extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
  336. extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
  337. extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
  338. extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
  339. extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
  340. extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
  341. extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
  342. testpb.TestAllTypes_FOO,
  343. testpb.TestAllTypes_BAR,
  344. }),
  345. )},
  346. wire: pack.Message{
  347. pack.Tag{31, pack.VarintType}, pack.Varint(1001),
  348. pack.Tag{31, pack.VarintType}, pack.Varint(2001),
  349. pack.Tag{32, pack.VarintType}, pack.Varint(1002),
  350. pack.Tag{32, pack.VarintType}, pack.Varint(2002),
  351. pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
  352. pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
  353. pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
  354. pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
  355. pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
  356. pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
  357. pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
  358. pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
  359. pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
  360. pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
  361. pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
  362. pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
  363. pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
  364. pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
  365. pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
  366. pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
  367. pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
  368. pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
  369. pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
  370. pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
  371. pack.Tag{43, pack.VarintType}, pack.Bool(true),
  372. pack.Tag{43, pack.VarintType}, pack.Bool(false),
  373. pack.Tag{44, pack.BytesType}, pack.String("foo"),
  374. pack.Tag{44, pack.BytesType}, pack.String("bar"),
  375. pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
  376. pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
  377. pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
  378. pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  379. }.Marshal(),
  380. },
  381. {
  382. desc: "basic repeated types (packed encoding)",
  383. decodeTo: []proto.Message{&testpb.TestAllTypes{
  384. RepeatedInt32: []int32{1001, 2001},
  385. RepeatedInt64: []int64{1002, 2002},
  386. RepeatedUint32: []uint32{1003, 2003},
  387. RepeatedUint64: []uint64{1004, 2004},
  388. RepeatedSint32: []int32{1005, 2005},
  389. RepeatedSint64: []int64{1006, 2006},
  390. RepeatedFixed32: []uint32{1007, 2007},
  391. RepeatedFixed64: []uint64{1008, 2008},
  392. RepeatedSfixed32: []int32{1009, 2009},
  393. RepeatedSfixed64: []int64{1010, 2010},
  394. RepeatedFloat: []float32{1011.5, 2011.5},
  395. RepeatedDouble: []float64{1012.5, 2012.5},
  396. RepeatedBool: []bool{true, false},
  397. RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
  398. testpb.TestAllTypes_FOO,
  399. testpb.TestAllTypes_BAR,
  400. },
  401. }, &test3pb.TestAllTypes{
  402. RepeatedInt32: []int32{1001, 2001},
  403. RepeatedInt64: []int64{1002, 2002},
  404. RepeatedUint32: []uint32{1003, 2003},
  405. RepeatedUint64: []uint64{1004, 2004},
  406. RepeatedSint32: []int32{1005, 2005},
  407. RepeatedSint64: []int64{1006, 2006},
  408. RepeatedFixed32: []uint32{1007, 2007},
  409. RepeatedFixed64: []uint64{1008, 2008},
  410. RepeatedSfixed32: []int32{1009, 2009},
  411. RepeatedSfixed64: []int64{1010, 2010},
  412. RepeatedFloat: []float32{1011.5, 2011.5},
  413. RepeatedDouble: []float64{1012.5, 2012.5},
  414. RepeatedBool: []bool{true, false},
  415. RepeatedNestedEnum: []test3pb.TestAllTypes_NestedEnum{
  416. test3pb.TestAllTypes_FOO,
  417. test3pb.TestAllTypes_BAR,
  418. },
  419. }, build(
  420. &testpb.TestAllExtensions{},
  421. extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
  422. extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
  423. extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
  424. extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
  425. extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
  426. extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
  427. extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
  428. extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
  429. extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
  430. extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
  431. extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
  432. extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
  433. extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
  434. extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
  435. testpb.TestAllTypes_FOO,
  436. testpb.TestAllTypes_BAR,
  437. }),
  438. )},
  439. wire: pack.Message{
  440. pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
  441. pack.Varint(1001), pack.Varint(2001),
  442. },
  443. pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
  444. pack.Varint(1002), pack.Varint(2002),
  445. },
  446. pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
  447. pack.Uvarint(1003), pack.Uvarint(2003),
  448. },
  449. pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
  450. pack.Uvarint(1004), pack.Uvarint(2004),
  451. },
  452. pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
  453. pack.Svarint(1005), pack.Svarint(2005),
  454. },
  455. pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
  456. pack.Svarint(1006), pack.Svarint(2006),
  457. },
  458. pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
  459. pack.Uint32(1007), pack.Uint32(2007),
  460. },
  461. pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
  462. pack.Uint64(1008), pack.Uint64(2008),
  463. },
  464. pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
  465. pack.Int32(1009), pack.Int32(2009),
  466. },
  467. pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
  468. pack.Int64(1010), pack.Int64(2010),
  469. },
  470. pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
  471. pack.Float32(1011.5), pack.Float32(2011.5),
  472. },
  473. pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
  474. pack.Float64(1012.5), pack.Float64(2012.5),
  475. },
  476. pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
  477. pack.Bool(true), pack.Bool(false),
  478. },
  479. pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
  480. pack.Varint(int(testpb.TestAllTypes_FOO)),
  481. pack.Varint(int(testpb.TestAllTypes_BAR)),
  482. },
  483. }.Marshal(),
  484. },
  485. {
  486. desc: "repeated messages",
  487. decodeTo: []proto.Message{&testpb.TestAllTypes{
  488. RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
  489. {A: scalar.Int32(1)},
  490. {A: scalar.Int32(2)},
  491. },
  492. }, &test3pb.TestAllTypes{
  493. RepeatedNestedMessage: []*test3pb.TestAllTypes_NestedMessage{
  494. {A: 1},
  495. {A: 2},
  496. },
  497. }, build(
  498. &testpb.TestAllExtensions{},
  499. extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
  500. {A: scalar.Int32(1)},
  501. {A: scalar.Int32(2)},
  502. }),
  503. )},
  504. wire: pack.Message{
  505. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
  506. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  507. }),
  508. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
  509. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  510. }),
  511. }.Marshal(),
  512. },
  513. {
  514. desc: "repeated groups",
  515. decodeTo: []proto.Message{&testpb.TestAllTypes{
  516. Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
  517. {A: scalar.Int32(1017)},
  518. {A: scalar.Int32(2017)},
  519. },
  520. }, build(
  521. &testpb.TestAllExtensions{},
  522. extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
  523. {A: scalar.Int32(1017)},
  524. {A: scalar.Int32(2017)},
  525. }),
  526. )},
  527. wire: pack.Message{
  528. pack.Tag{46, pack.StartGroupType},
  529. pack.Tag{47, pack.VarintType}, pack.Varint(1017),
  530. pack.Tag{46, pack.EndGroupType},
  531. pack.Tag{46, pack.StartGroupType},
  532. pack.Tag{47, pack.VarintType}, pack.Varint(2017),
  533. pack.Tag{46, pack.EndGroupType},
  534. }.Marshal(),
  535. },
  536. {
  537. desc: "maps",
  538. decodeTo: []proto.Message{&testpb.TestAllTypes{
  539. MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
  540. MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
  541. MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
  542. MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
  543. MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
  544. MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
  545. MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
  546. MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
  547. MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
  548. MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
  549. MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
  550. MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
  551. MapBoolBool: map[bool]bool{true: false, false: true},
  552. MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
  553. MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
  554. MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
  555. "71.1.key": {A: scalar.Int32(1171)},
  556. "71.2.key": {A: scalar.Int32(2171)},
  557. },
  558. MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
  559. "73.1.key": testpb.TestAllTypes_FOO,
  560. "73.2.key": testpb.TestAllTypes_BAR,
  561. },
  562. }, &test3pb.TestAllTypes{
  563. MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
  564. MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
  565. MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
  566. MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
  567. MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
  568. MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
  569. MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
  570. MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
  571. MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
  572. MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
  573. MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
  574. MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
  575. MapBoolBool: map[bool]bool{true: false, false: true},
  576. MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
  577. MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
  578. MapStringNestedMessage: map[string]*test3pb.TestAllTypes_NestedMessage{
  579. "71.1.key": {A: 1171},
  580. "71.2.key": {A: 2171},
  581. },
  582. MapStringNestedEnum: map[string]test3pb.TestAllTypes_NestedEnum{
  583. "73.1.key": test3pb.TestAllTypes_FOO,
  584. "73.2.key": test3pb.TestAllTypes_BAR,
  585. },
  586. }},
  587. wire: pack.Message{
  588. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  589. pack.Tag{1, pack.VarintType}, pack.Varint(1056),
  590. pack.Tag{2, pack.VarintType}, pack.Varint(1156),
  591. }),
  592. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  593. pack.Tag{1, pack.VarintType}, pack.Varint(2056),
  594. pack.Tag{2, pack.VarintType}, pack.Varint(2156),
  595. }),
  596. pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
  597. pack.Tag{1, pack.VarintType}, pack.Varint(1057),
  598. pack.Tag{2, pack.VarintType}, pack.Varint(1157),
  599. }),
  600. pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
  601. pack.Tag{1, pack.VarintType}, pack.Varint(2057),
  602. pack.Tag{2, pack.VarintType}, pack.Varint(2157),
  603. }),
  604. pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
  605. pack.Tag{1, pack.VarintType}, pack.Varint(1058),
  606. pack.Tag{2, pack.VarintType}, pack.Varint(1158),
  607. }),
  608. pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
  609. pack.Tag{1, pack.VarintType}, pack.Varint(2058),
  610. pack.Tag{2, pack.VarintType}, pack.Varint(2158),
  611. }),
  612. pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
  613. pack.Tag{1, pack.VarintType}, pack.Varint(1059),
  614. pack.Tag{2, pack.VarintType}, pack.Varint(1159),
  615. }),
  616. pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
  617. pack.Tag{1, pack.VarintType}, pack.Varint(2059),
  618. pack.Tag{2, pack.VarintType}, pack.Varint(2159),
  619. }),
  620. pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
  621. pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
  622. pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
  623. }),
  624. pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
  625. pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
  626. pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
  627. }),
  628. pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
  629. pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
  630. pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
  631. }),
  632. pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
  633. pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
  634. pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
  635. }),
  636. pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
  637. pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
  638. pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
  639. }),
  640. pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
  641. pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
  642. pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
  643. }),
  644. pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
  645. pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
  646. pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
  647. }),
  648. pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
  649. pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
  650. pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
  651. }),
  652. pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
  653. pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
  654. pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
  655. }),
  656. pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
  657. pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
  658. pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
  659. }),
  660. pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
  661. pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
  662. pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
  663. }),
  664. pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
  665. pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
  666. pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
  667. }),
  668. pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
  669. pack.Tag{1, pack.VarintType}, pack.Varint(1066),
  670. pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
  671. }),
  672. pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
  673. pack.Tag{1, pack.VarintType}, pack.Varint(2066),
  674. pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
  675. }),
  676. pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
  677. pack.Tag{1, pack.VarintType}, pack.Varint(1067),
  678. pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
  679. }),
  680. pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
  681. pack.Tag{1, pack.VarintType}, pack.Varint(2067),
  682. pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
  683. }),
  684. pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
  685. pack.Tag{1, pack.VarintType}, pack.Bool(true),
  686. pack.Tag{2, pack.VarintType}, pack.Bool(false),
  687. }),
  688. pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
  689. pack.Tag{1, pack.VarintType}, pack.Bool(false),
  690. pack.Tag{2, pack.VarintType}, pack.Bool(true),
  691. }),
  692. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  693. pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
  694. pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
  695. }),
  696. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  697. pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
  698. pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
  699. }),
  700. pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
  701. pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
  702. pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
  703. }),
  704. pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
  705. pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
  706. pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
  707. }),
  708. pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
  709. pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
  710. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  711. pack.Tag{1, pack.VarintType}, pack.Varint(1171),
  712. }),
  713. }),
  714. pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
  715. pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
  716. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  717. pack.Tag{1, pack.VarintType}, pack.Varint(2171),
  718. }),
  719. }),
  720. pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
  721. pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
  722. pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
  723. }),
  724. pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
  725. pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
  726. pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  727. }),
  728. }.Marshal(),
  729. },
  730. {
  731. desc: "oneof (uint32)",
  732. decodeTo: []proto.Message{
  733. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}},
  734. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint32{1111}},
  735. },
  736. wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
  737. },
  738. {
  739. desc: "oneof (message)",
  740. decodeTo: []proto.Message{
  741. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  742. &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
  743. }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
  744. &test3pb.TestAllTypes_NestedMessage{A: 1112},
  745. }},
  746. },
  747. wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  748. pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
  749. })}.Marshal(),
  750. },
  751. {
  752. desc: "oneof (overridden message)",
  753. decodeTo: []proto.Message{
  754. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  755. &testpb.TestAllTypes_NestedMessage{
  756. Corecursive: &testpb.TestAllTypes{
  757. OptionalInt32: scalar.Int32(43),
  758. },
  759. },
  760. }}, &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofNestedMessage{
  761. &test3pb.TestAllTypes_NestedMessage{
  762. Corecursive: &test3pb.TestAllTypes{
  763. OptionalInt32: 43,
  764. },
  765. },
  766. }}},
  767. wire: pack.Message{
  768. pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  769. pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
  770. }),
  771. pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  772. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  773. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  774. }),
  775. }),
  776. }.Marshal(),
  777. },
  778. {
  779. desc: "oneof (string)",
  780. decodeTo: []proto.Message{
  781. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}},
  782. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofString{"1113"}},
  783. },
  784. wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
  785. },
  786. {
  787. desc: "oneof (bytes)",
  788. decodeTo: []proto.Message{
  789. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}},
  790. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBytes{[]byte("1114")}},
  791. },
  792. wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
  793. },
  794. {
  795. desc: "oneof (bool)",
  796. decodeTo: []proto.Message{
  797. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}},
  798. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofBool{true}},
  799. },
  800. wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
  801. },
  802. {
  803. desc: "oneof (uint64)",
  804. decodeTo: []proto.Message{
  805. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}},
  806. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{116}},
  807. },
  808. wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
  809. },
  810. {
  811. desc: "oneof (float)",
  812. decodeTo: []proto.Message{
  813. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}},
  814. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofFloat{117.5}},
  815. },
  816. wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
  817. },
  818. {
  819. desc: "oneof (double)",
  820. decodeTo: []proto.Message{
  821. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}},
  822. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofDouble{118.5}},
  823. },
  824. wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
  825. },
  826. {
  827. desc: "oneof (enum)",
  828. decodeTo: []proto.Message{
  829. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}},
  830. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofEnum{test3pb.TestAllTypes_BAR}},
  831. },
  832. wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
  833. },
  834. {
  835. desc: "oneof (overridden value)",
  836. decodeTo: []proto.Message{
  837. &testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}},
  838. &test3pb.TestAllTypes{OneofField: &test3pb.TestAllTypes_OneofUint64{2}},
  839. },
  840. wire: pack.Message{
  841. pack.Tag{111, pack.VarintType}, pack.Varint(1),
  842. pack.Tag{116, pack.VarintType}, pack.Varint(2),
  843. }.Marshal(),
  844. },
  845. // TODO: More unknown field tests for ordering, repeated fields, etc.
  846. //
  847. // It is currently impossible to produce results that the v1 Equal
  848. // considers equivalent to those of the v1 decoder. Figure out if
  849. // that's a problem or not.
  850. {
  851. desc: "unknown fields",
  852. decodeTo: []proto.Message{build(
  853. &testpb.TestAllTypes{},
  854. unknown(100000, pack.Message{
  855. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  856. }.Marshal()),
  857. ), build(
  858. &test3pb.TestAllTypes{},
  859. unknown(100000, pack.Message{
  860. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  861. }.Marshal()),
  862. )},
  863. wire: pack.Message{
  864. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  865. }.Marshal(),
  866. },
  867. {
  868. desc: "field type mismatch",
  869. decodeTo: []proto.Message{build(
  870. &testpb.TestAllTypes{},
  871. unknown(1, pack.Message{
  872. pack.Tag{1, pack.BytesType}, pack.String("string"),
  873. }.Marshal()),
  874. ), build(
  875. &test3pb.TestAllTypes{},
  876. unknown(1, pack.Message{
  877. pack.Tag{1, pack.BytesType}, pack.String("string"),
  878. }.Marshal()),
  879. )},
  880. wire: pack.Message{
  881. pack.Tag{1, pack.BytesType}, pack.String("string"),
  882. }.Marshal(),
  883. },
  884. {
  885. desc: "map field element mismatch",
  886. decodeTo: []proto.Message{
  887. &testpb.TestAllTypes{
  888. MapInt32Int32: map[int32]int32{1: 0},
  889. }, &test3pb.TestAllTypes{
  890. MapInt32Int32: map[int32]int32{1: 0},
  891. },
  892. },
  893. wire: pack.Message{
  894. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  895. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  896. pack.Tag{2, pack.BytesType}, pack.String("string"),
  897. }),
  898. }.Marshal(),
  899. },
  900. {
  901. desc: "required field unset",
  902. partial: true,
  903. decodeTo: []proto.Message{&testpb.TestRequired{}},
  904. },
  905. {
  906. desc: "required field set",
  907. decodeTo: []proto.Message{&testpb.TestRequired{
  908. RequiredField: scalar.Int32(1),
  909. }},
  910. wire: pack.Message{
  911. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  912. }.Marshal(),
  913. },
  914. {
  915. desc: "required field in optional message unset",
  916. partial: true,
  917. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  918. OptionalMessage: &testpb.TestRequired{},
  919. }},
  920. wire: pack.Message{
  921. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  922. }.Marshal(),
  923. },
  924. {
  925. desc: "required field in optional message set",
  926. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  927. OptionalMessage: &testpb.TestRequired{
  928. RequiredField: scalar.Int32(1),
  929. },
  930. }},
  931. wire: pack.Message{
  932. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
  933. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  934. }),
  935. }.Marshal(),
  936. },
  937. {
  938. desc: "required field in optional message set (split across multiple tags)",
  939. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  940. OptionalMessage: &testpb.TestRequired{
  941. RequiredField: scalar.Int32(1),
  942. },
  943. }},
  944. wire: pack.Message{
  945. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  946. pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{
  947. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  948. }),
  949. }.Marshal(),
  950. },
  951. {
  952. desc: "required field in repeated message unset",
  953. partial: true,
  954. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  955. RepeatedMessage: []*testpb.TestRequired{
  956. {RequiredField: scalar.Int32(1)},
  957. {},
  958. },
  959. }},
  960. wire: pack.Message{
  961. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  962. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  963. }),
  964. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  965. }.Marshal(),
  966. },
  967. {
  968. desc: "required field in repeated message set",
  969. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  970. RepeatedMessage: []*testpb.TestRequired{
  971. {RequiredField: scalar.Int32(1)},
  972. {RequiredField: scalar.Int32(2)},
  973. },
  974. }},
  975. wire: pack.Message{
  976. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  977. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  978. }),
  979. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  980. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  981. }),
  982. }.Marshal(),
  983. },
  984. {
  985. desc: "required field in map message unset",
  986. partial: true,
  987. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  988. MapMessage: map[int32]*testpb.TestRequired{
  989. 1: {RequiredField: scalar.Int32(1)},
  990. 2: {},
  991. },
  992. }},
  993. wire: pack.Message{
  994. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  995. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  996. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  997. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  998. }),
  999. }),
  1000. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1001. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1002. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1003. }),
  1004. }.Marshal(),
  1005. },
  1006. {
  1007. desc: "required field in map message set",
  1008. decodeTo: []proto.Message{&testpb.TestRequiredForeign{
  1009. MapMessage: map[int32]*testpb.TestRequired{
  1010. 1: {RequiredField: scalar.Int32(1)},
  1011. 2: {RequiredField: scalar.Int32(2)},
  1012. },
  1013. }},
  1014. wire: pack.Message{
  1015. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1016. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1017. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1018. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1019. }),
  1020. }),
  1021. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1022. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1023. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1024. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1025. }),
  1026. }),
  1027. }.Marshal(),
  1028. },
  1029. {
  1030. desc: "required field in optional group unset",
  1031. partial: true,
  1032. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1033. Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{},
  1034. }},
  1035. wire: pack.Message{
  1036. pack.Tag{1, pack.StartGroupType},
  1037. pack.Tag{1, pack.EndGroupType},
  1038. }.Marshal(),
  1039. },
  1040. {
  1041. desc: "required field in optional group set",
  1042. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1043. Optionalgroup: &testpb.TestRequiredGroupFields_OptionalGroup{
  1044. A: scalar.Int32(1),
  1045. },
  1046. }},
  1047. wire: pack.Message{
  1048. pack.Tag{1, pack.StartGroupType},
  1049. pack.Tag{2, pack.VarintType}, pack.Varint(1),
  1050. pack.Tag{1, pack.EndGroupType},
  1051. }.Marshal(),
  1052. },
  1053. {
  1054. desc: "required field in repeated group unset",
  1055. partial: true,
  1056. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1057. Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
  1058. {A: scalar.Int32(1)},
  1059. {},
  1060. },
  1061. }},
  1062. wire: pack.Message{
  1063. pack.Tag{3, pack.StartGroupType},
  1064. pack.Tag{4, pack.VarintType}, pack.Varint(1),
  1065. pack.Tag{3, pack.EndGroupType},
  1066. pack.Tag{3, pack.StartGroupType},
  1067. pack.Tag{3, pack.EndGroupType},
  1068. }.Marshal(),
  1069. },
  1070. {
  1071. desc: "required field in repeated group set",
  1072. decodeTo: []proto.Message{&testpb.TestRequiredGroupFields{
  1073. Repeatedgroup: []*testpb.TestRequiredGroupFields_RepeatedGroup{
  1074. {A: scalar.Int32(1)},
  1075. {A: scalar.Int32(2)},
  1076. },
  1077. }},
  1078. wire: pack.Message{
  1079. pack.Tag{3, pack.StartGroupType},
  1080. pack.Tag{4, pack.VarintType}, pack.Varint(1),
  1081. pack.Tag{3, pack.EndGroupType},
  1082. pack.Tag{3, pack.StartGroupType},
  1083. pack.Tag{4, pack.VarintType}, pack.Varint(2),
  1084. pack.Tag{3, pack.EndGroupType},
  1085. }.Marshal(),
  1086. },
  1087. {
  1088. desc: "required field in extension message unset",
  1089. partial: true,
  1090. invalidExtensions: true,
  1091. decodeTo: []proto.Message{build(
  1092. &testpb.TestAllExtensions{},
  1093. extend(testpb.E_TestRequired_Single, &testpb.TestRequired{}),
  1094. )},
  1095. wire: pack.Message{
  1096. pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1097. }.Marshal(),
  1098. },
  1099. {
  1100. desc: "required field in extension message set",
  1101. decodeTo: []proto.Message{build(
  1102. &testpb.TestAllExtensions{},
  1103. extend(testpb.E_TestRequired_Single, &testpb.TestRequired{
  1104. RequiredField: scalar.Int32(1),
  1105. }),
  1106. )},
  1107. wire: pack.Message{
  1108. pack.Tag{1000, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1109. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1110. }),
  1111. }.Marshal(),
  1112. },
  1113. {
  1114. desc: "required field in repeated extension message unset",
  1115. partial: true,
  1116. invalidExtensions: true,
  1117. decodeTo: []proto.Message{build(
  1118. &testpb.TestAllExtensions{},
  1119. extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
  1120. {RequiredField: scalar.Int32(1)},
  1121. {},
  1122. }),
  1123. )},
  1124. wire: pack.Message{
  1125. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1126. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1127. }),
  1128. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{}),
  1129. }.Marshal(),
  1130. },
  1131. {
  1132. desc: "required field in repeated extension message set",
  1133. decodeTo: []proto.Message{build(
  1134. &testpb.TestAllExtensions{},
  1135. extend(testpb.E_TestRequired_Multi, []*testpb.TestRequired{
  1136. {RequiredField: scalar.Int32(1)},
  1137. {RequiredField: scalar.Int32(2)},
  1138. }),
  1139. )},
  1140. wire: pack.Message{
  1141. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1142. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  1143. }),
  1144. pack.Tag{1001, pack.BytesType}, pack.LengthPrefix(pack.Message{
  1145. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  1146. }),
  1147. }.Marshal(),
  1148. },
  1149. }
  1150. func build(m proto.Message, opts ...buildOpt) proto.Message {
  1151. for _, opt := range opts {
  1152. opt(m)
  1153. }
  1154. return m
  1155. }
  1156. type buildOpt func(proto.Message)
  1157. func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
  1158. return func(m proto.Message) {
  1159. m.ProtoReflect().UnknownFields().Set(num, raw)
  1160. }
  1161. }
  1162. func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
  1163. return func(m proto.Message) {
  1164. if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
  1165. panic(err)
  1166. }
  1167. }
  1168. }
  1169. func marshalText(m proto.Message) string {
  1170. b, _ := textpb.Marshal(m)
  1171. return string(b)
  1172. }