decode_test.go 45 KB

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