decode_test.go 48 KB

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