decode_test.go 45 KB

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