decode_test.go 48 KB

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