decode_test.go 52 KB

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