decode_test.go 48 KB

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