decode_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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/internal/encoding/pack"
  11. _ "github.com/golang/protobuf/v2/internal/legacy"
  12. "github.com/golang/protobuf/v2/internal/scalar"
  13. testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
  14. "github.com/golang/protobuf/v2/proto"
  15. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  16. )
  17. type testProto struct {
  18. desc string
  19. decodeTo []proto.Message
  20. wire []byte
  21. }
  22. func TestDecode(t *testing.T) {
  23. for _, test := range testProtos {
  24. for _, want := range test.decodeTo {
  25. t.Run(fmt.Sprintf("%s (%T)", test.desc, want), func(t *testing.T) {
  26. wire := append(([]byte)(nil), test.wire...)
  27. got := reflect.New(reflect.TypeOf(want).Elem()).Interface().(proto.Message)
  28. if err := proto.Unmarshal(wire, got); err != nil {
  29. t.Errorf("Unmarshal error: %v\nMessage:\n%v", err, protoV1.MarshalTextString(want.(protoV1.Message)))
  30. return
  31. }
  32. // Aliasing check: Modifying the original wire bytes shouldn't
  33. // affect the unmarshaled message.
  34. for i := range wire {
  35. wire[i] = 0
  36. }
  37. if !protoV1.Equal(got.(protoV1.Message), want.(protoV1.Message)) {
  38. t.Errorf("Unmarshal returned unexpected result; got:\n%v\nwant:\n%v", protoV1.MarshalTextString(got.(protoV1.Message)), protoV1.MarshalTextString(want.(protoV1.Message)))
  39. }
  40. })
  41. }
  42. }
  43. }
  44. var testProtos = []testProto{
  45. {
  46. desc: "basic scalar types",
  47. decodeTo: []proto.Message{&testpb.TestAllTypes{
  48. OptionalInt32: scalar.Int32(1001),
  49. OptionalInt64: scalar.Int64(1002),
  50. OptionalUint32: scalar.Uint32(1003),
  51. OptionalUint64: scalar.Uint64(1004),
  52. OptionalSint32: scalar.Int32(1005),
  53. OptionalSint64: scalar.Int64(1006),
  54. OptionalFixed32: scalar.Uint32(1007),
  55. OptionalFixed64: scalar.Uint64(1008),
  56. OptionalSfixed32: scalar.Int32(1009),
  57. OptionalSfixed64: scalar.Int64(1010),
  58. OptionalFloat: scalar.Float32(1011.5),
  59. OptionalDouble: scalar.Float64(1012.5),
  60. OptionalBool: scalar.Bool(true),
  61. OptionalString: scalar.String("string"),
  62. OptionalBytes: []byte("bytes"),
  63. OptionalNestedEnum: testpb.TestAllTypes_BAR.Enum(),
  64. }, build(
  65. &testpb.TestAllExtensions{},
  66. extend(testpb.E_OptionalInt32Extension, scalar.Int32(1001)),
  67. extend(testpb.E_OptionalInt64Extension, scalar.Int64(1002)),
  68. extend(testpb.E_OptionalUint32Extension, scalar.Uint32(1003)),
  69. extend(testpb.E_OptionalUint64Extension, scalar.Uint64(1004)),
  70. extend(testpb.E_OptionalSint32Extension, scalar.Int32(1005)),
  71. extend(testpb.E_OptionalSint64Extension, scalar.Int64(1006)),
  72. extend(testpb.E_OptionalFixed32Extension, scalar.Uint32(1007)),
  73. extend(testpb.E_OptionalFixed64Extension, scalar.Uint64(1008)),
  74. extend(testpb.E_OptionalSfixed32Extension, scalar.Int32(1009)),
  75. extend(testpb.E_OptionalSfixed64Extension, scalar.Int64(1010)),
  76. extend(testpb.E_OptionalFloatExtension, scalar.Float32(1011.5)),
  77. extend(testpb.E_OptionalDoubleExtension, scalar.Float64(1012.5)),
  78. extend(testpb.E_OptionalBoolExtension, scalar.Bool(true)),
  79. extend(testpb.E_OptionalStringExtension, scalar.String("string")),
  80. extend(testpb.E_OptionalBytesExtension, []byte("bytes")),
  81. extend(testpb.E_OptionalNestedEnumExtension, testpb.TestAllTypes_BAR.Enum()),
  82. )},
  83. wire: pack.Message{
  84. pack.Tag{1, pack.VarintType}, pack.Varint(1001),
  85. pack.Tag{2, pack.VarintType}, pack.Varint(1002),
  86. pack.Tag{3, pack.VarintType}, pack.Uvarint(1003),
  87. pack.Tag{4, pack.VarintType}, pack.Uvarint(1004),
  88. pack.Tag{5, pack.VarintType}, pack.Svarint(1005),
  89. pack.Tag{6, pack.VarintType}, pack.Svarint(1006),
  90. pack.Tag{7, pack.Fixed32Type}, pack.Uint32(1007),
  91. pack.Tag{8, pack.Fixed64Type}, pack.Uint64(1008),
  92. pack.Tag{9, pack.Fixed32Type}, pack.Int32(1009),
  93. pack.Tag{10, pack.Fixed64Type}, pack.Int64(1010),
  94. pack.Tag{11, pack.Fixed32Type}, pack.Float32(1011.5),
  95. pack.Tag{12, pack.Fixed64Type}, pack.Float64(1012.5),
  96. pack.Tag{13, pack.VarintType}, pack.Bool(true),
  97. pack.Tag{14, pack.BytesType}, pack.String("string"),
  98. pack.Tag{15, pack.BytesType}, pack.Bytes([]byte("bytes")),
  99. pack.Tag{21, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  100. }.Marshal(),
  101. },
  102. {
  103. desc: "groups",
  104. decodeTo: []proto.Message{&testpb.TestAllTypes{
  105. Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
  106. A: scalar.Int32(1017),
  107. },
  108. }, build(
  109. &testpb.TestAllExtensions{},
  110. extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
  111. A: scalar.Int32(1017),
  112. }),
  113. )},
  114. wire: pack.Message{
  115. pack.Tag{16, pack.StartGroupType},
  116. pack.Tag{17, pack.VarintType}, pack.Varint(1017),
  117. pack.Tag{16, pack.EndGroupType},
  118. }.Marshal(),
  119. },
  120. {
  121. desc: "groups (field overridden)",
  122. decodeTo: []proto.Message{&testpb.TestAllTypes{
  123. Optionalgroup: &testpb.TestAllTypes_OptionalGroup{
  124. A: scalar.Int32(2),
  125. },
  126. }, build(
  127. &testpb.TestAllExtensions{},
  128. extend(testpb.E_OptionalgroupExtension, &testpb.OptionalGroupExtension{
  129. A: scalar.Int32(2),
  130. }),
  131. )},
  132. wire: pack.Message{
  133. pack.Tag{16, pack.StartGroupType},
  134. pack.Tag{17, pack.VarintType}, pack.Varint(1),
  135. pack.Tag{16, pack.EndGroupType},
  136. pack.Tag{16, pack.StartGroupType},
  137. pack.Tag{17, pack.VarintType}, pack.Varint(2),
  138. pack.Tag{16, pack.EndGroupType},
  139. }.Marshal(),
  140. },
  141. {
  142. desc: "messages",
  143. decodeTo: []proto.Message{&testpb.TestAllTypes{
  144. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  145. A: scalar.Int32(42),
  146. Corecursive: &testpb.TestAllTypes{
  147. OptionalInt32: scalar.Int32(43),
  148. },
  149. },
  150. }, build(
  151. &testpb.TestAllExtensions{},
  152. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  153. A: scalar.Int32(42),
  154. Corecursive: &testpb.TestAllTypes{
  155. OptionalInt32: scalar.Int32(43),
  156. },
  157. }),
  158. )},
  159. wire: pack.Message{
  160. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  161. pack.Tag{1, pack.VarintType}, pack.Varint(42),
  162. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  163. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  164. }),
  165. }),
  166. }.Marshal(),
  167. },
  168. {
  169. desc: "messages (split across multiple tags)",
  170. decodeTo: []proto.Message{&testpb.TestAllTypes{
  171. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  172. A: scalar.Int32(42),
  173. Corecursive: &testpb.TestAllTypes{
  174. OptionalInt32: scalar.Int32(43),
  175. },
  176. },
  177. }, build(
  178. &testpb.TestAllExtensions{},
  179. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  180. A: scalar.Int32(42),
  181. Corecursive: &testpb.TestAllTypes{
  182. OptionalInt32: scalar.Int32(43),
  183. },
  184. }),
  185. )},
  186. wire: pack.Message{
  187. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  188. pack.Tag{1, pack.VarintType}, pack.Varint(42),
  189. }),
  190. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  191. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  192. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  193. }),
  194. }),
  195. }.Marshal(),
  196. },
  197. {
  198. desc: "messages (field overridden)",
  199. decodeTo: []proto.Message{&testpb.TestAllTypes{
  200. OptionalNestedMessage: &testpb.TestAllTypes_NestedMessage{
  201. A: scalar.Int32(2),
  202. },
  203. }, build(
  204. &testpb.TestAllExtensions{},
  205. extend(testpb.E_OptionalNestedMessageExtension, &testpb.TestAllTypes_NestedMessage{
  206. A: scalar.Int32(2),
  207. }),
  208. )},
  209. wire: pack.Message{
  210. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  211. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  212. }),
  213. pack.Tag{18, pack.BytesType}, pack.LengthPrefix(pack.Message{
  214. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  215. }),
  216. }.Marshal(),
  217. },
  218. {
  219. desc: "basic repeated types",
  220. decodeTo: []proto.Message{&testpb.TestAllTypes{
  221. RepeatedInt32: []int32{1001, 2001},
  222. RepeatedInt64: []int64{1002, 2002},
  223. RepeatedUint32: []uint32{1003, 2003},
  224. RepeatedUint64: []uint64{1004, 2004},
  225. RepeatedSint32: []int32{1005, 2005},
  226. RepeatedSint64: []int64{1006, 2006},
  227. RepeatedFixed32: []uint32{1007, 2007},
  228. RepeatedFixed64: []uint64{1008, 2008},
  229. RepeatedSfixed32: []int32{1009, 2009},
  230. RepeatedSfixed64: []int64{1010, 2010},
  231. RepeatedFloat: []float32{1011.5, 2011.5},
  232. RepeatedDouble: []float64{1012.5, 2012.5},
  233. RepeatedBool: []bool{true, false},
  234. RepeatedString: []string{"foo", "bar"},
  235. RepeatedBytes: [][]byte{[]byte("FOO"), []byte("BAR")},
  236. RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
  237. testpb.TestAllTypes_FOO,
  238. testpb.TestAllTypes_BAR,
  239. },
  240. }, build(
  241. &testpb.TestAllExtensions{},
  242. extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
  243. extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
  244. extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
  245. extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
  246. extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
  247. extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
  248. extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
  249. extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
  250. extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
  251. extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
  252. extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
  253. extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
  254. extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
  255. extend(testpb.E_RepeatedStringExtension, []string{"foo", "bar"}),
  256. extend(testpb.E_RepeatedBytesExtension, [][]byte{[]byte("FOO"), []byte("BAR")}),
  257. extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
  258. testpb.TestAllTypes_FOO,
  259. testpb.TestAllTypes_BAR,
  260. }),
  261. )},
  262. wire: pack.Message{
  263. pack.Tag{31, pack.VarintType}, pack.Varint(1001),
  264. pack.Tag{31, pack.VarintType}, pack.Varint(2001),
  265. pack.Tag{32, pack.VarintType}, pack.Varint(1002),
  266. pack.Tag{32, pack.VarintType}, pack.Varint(2002),
  267. pack.Tag{33, pack.VarintType}, pack.Uvarint(1003),
  268. pack.Tag{33, pack.VarintType}, pack.Uvarint(2003),
  269. pack.Tag{34, pack.VarintType}, pack.Uvarint(1004),
  270. pack.Tag{34, pack.VarintType}, pack.Uvarint(2004),
  271. pack.Tag{35, pack.VarintType}, pack.Svarint(1005),
  272. pack.Tag{35, pack.VarintType}, pack.Svarint(2005),
  273. pack.Tag{36, pack.VarintType}, pack.Svarint(1006),
  274. pack.Tag{36, pack.VarintType}, pack.Svarint(2006),
  275. pack.Tag{37, pack.Fixed32Type}, pack.Uint32(1007),
  276. pack.Tag{37, pack.Fixed32Type}, pack.Uint32(2007),
  277. pack.Tag{38, pack.Fixed64Type}, pack.Uint64(1008),
  278. pack.Tag{38, pack.Fixed64Type}, pack.Uint64(2008),
  279. pack.Tag{39, pack.Fixed32Type}, pack.Int32(1009),
  280. pack.Tag{39, pack.Fixed32Type}, pack.Int32(2009),
  281. pack.Tag{40, pack.Fixed64Type}, pack.Int64(1010),
  282. pack.Tag{40, pack.Fixed64Type}, pack.Int64(2010),
  283. pack.Tag{41, pack.Fixed32Type}, pack.Float32(1011.5),
  284. pack.Tag{41, pack.Fixed32Type}, pack.Float32(2011.5),
  285. pack.Tag{42, pack.Fixed64Type}, pack.Float64(1012.5),
  286. pack.Tag{42, pack.Fixed64Type}, pack.Float64(2012.5),
  287. pack.Tag{43, pack.VarintType}, pack.Bool(true),
  288. pack.Tag{43, pack.VarintType}, pack.Bool(false),
  289. pack.Tag{44, pack.BytesType}, pack.String("foo"),
  290. pack.Tag{44, pack.BytesType}, pack.String("bar"),
  291. pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("FOO")),
  292. pack.Tag{45, pack.BytesType}, pack.Bytes([]byte("BAR")),
  293. pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
  294. pack.Tag{51, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  295. }.Marshal(),
  296. },
  297. {
  298. desc: "basic repeated types (packed encoding)",
  299. decodeTo: []proto.Message{&testpb.TestAllTypes{
  300. RepeatedInt32: []int32{1001, 2001},
  301. RepeatedInt64: []int64{1002, 2002},
  302. RepeatedUint32: []uint32{1003, 2003},
  303. RepeatedUint64: []uint64{1004, 2004},
  304. RepeatedSint32: []int32{1005, 2005},
  305. RepeatedSint64: []int64{1006, 2006},
  306. RepeatedFixed32: []uint32{1007, 2007},
  307. RepeatedFixed64: []uint64{1008, 2008},
  308. RepeatedSfixed32: []int32{1009, 2009},
  309. RepeatedSfixed64: []int64{1010, 2010},
  310. RepeatedFloat: []float32{1011.5, 2011.5},
  311. RepeatedDouble: []float64{1012.5, 2012.5},
  312. RepeatedBool: []bool{true, false},
  313. RepeatedNestedEnum: []testpb.TestAllTypes_NestedEnum{
  314. testpb.TestAllTypes_FOO,
  315. testpb.TestAllTypes_BAR,
  316. },
  317. }, build(
  318. &testpb.TestAllExtensions{},
  319. extend(testpb.E_RepeatedInt32Extension, []int32{1001, 2001}),
  320. extend(testpb.E_RepeatedInt64Extension, []int64{1002, 2002}),
  321. extend(testpb.E_RepeatedUint32Extension, []uint32{1003, 2003}),
  322. extend(testpb.E_RepeatedUint64Extension, []uint64{1004, 2004}),
  323. extend(testpb.E_RepeatedSint32Extension, []int32{1005, 2005}),
  324. extend(testpb.E_RepeatedSint64Extension, []int64{1006, 2006}),
  325. extend(testpb.E_RepeatedFixed32Extension, []uint32{1007, 2007}),
  326. extend(testpb.E_RepeatedFixed64Extension, []uint64{1008, 2008}),
  327. extend(testpb.E_RepeatedSfixed32Extension, []int32{1009, 2009}),
  328. extend(testpb.E_RepeatedSfixed64Extension, []int64{1010, 2010}),
  329. extend(testpb.E_RepeatedFloatExtension, []float32{1011.5, 2011.5}),
  330. extend(testpb.E_RepeatedDoubleExtension, []float64{1012.5, 2012.5}),
  331. extend(testpb.E_RepeatedBoolExtension, []bool{true, false}),
  332. extend(testpb.E_RepeatedNestedEnumExtension, []testpb.TestAllTypes_NestedEnum{
  333. testpb.TestAllTypes_FOO,
  334. testpb.TestAllTypes_BAR,
  335. }),
  336. )},
  337. wire: pack.Message{
  338. pack.Tag{31, pack.BytesType}, pack.LengthPrefix{
  339. pack.Varint(1001), pack.Varint(2001),
  340. },
  341. pack.Tag{32, pack.BytesType}, pack.LengthPrefix{
  342. pack.Varint(1002), pack.Varint(2002),
  343. },
  344. pack.Tag{33, pack.BytesType}, pack.LengthPrefix{
  345. pack.Uvarint(1003), pack.Uvarint(2003),
  346. },
  347. pack.Tag{34, pack.BytesType}, pack.LengthPrefix{
  348. pack.Uvarint(1004), pack.Uvarint(2004),
  349. },
  350. pack.Tag{35, pack.BytesType}, pack.LengthPrefix{
  351. pack.Svarint(1005), pack.Svarint(2005),
  352. },
  353. pack.Tag{36, pack.BytesType}, pack.LengthPrefix{
  354. pack.Svarint(1006), pack.Svarint(2006),
  355. },
  356. pack.Tag{37, pack.BytesType}, pack.LengthPrefix{
  357. pack.Uint32(1007), pack.Uint32(2007),
  358. },
  359. pack.Tag{38, pack.BytesType}, pack.LengthPrefix{
  360. pack.Uint64(1008), pack.Uint64(2008),
  361. },
  362. pack.Tag{39, pack.BytesType}, pack.LengthPrefix{
  363. pack.Int32(1009), pack.Int32(2009),
  364. },
  365. pack.Tag{40, pack.BytesType}, pack.LengthPrefix{
  366. pack.Int64(1010), pack.Int64(2010),
  367. },
  368. pack.Tag{41, pack.BytesType}, pack.LengthPrefix{
  369. pack.Float32(1011.5), pack.Float32(2011.5),
  370. },
  371. pack.Tag{42, pack.BytesType}, pack.LengthPrefix{
  372. pack.Float64(1012.5), pack.Float64(2012.5),
  373. },
  374. pack.Tag{43, pack.BytesType}, pack.LengthPrefix{
  375. pack.Bool(true), pack.Bool(false),
  376. },
  377. pack.Tag{51, pack.BytesType}, pack.LengthPrefix{
  378. pack.Varint(int(testpb.TestAllTypes_FOO)),
  379. pack.Varint(int(testpb.TestAllTypes_BAR)),
  380. },
  381. }.Marshal(),
  382. },
  383. {
  384. desc: "repeated messages",
  385. decodeTo: []proto.Message{&testpb.TestAllTypes{
  386. RepeatedNestedMessage: []*testpb.TestAllTypes_NestedMessage{
  387. {A: scalar.Int32(1)},
  388. {A: scalar.Int32(2)},
  389. },
  390. }, build(
  391. &testpb.TestAllExtensions{},
  392. extend(testpb.E_RepeatedNestedMessageExtension, []*testpb.TestAllTypes_NestedMessage{
  393. {A: scalar.Int32(1)},
  394. {A: scalar.Int32(2)},
  395. }),
  396. )},
  397. wire: pack.Message{
  398. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
  399. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  400. }),
  401. pack.Tag{48, pack.BytesType}, pack.LengthPrefix(pack.Message{
  402. pack.Tag{1, pack.VarintType}, pack.Varint(2),
  403. }),
  404. }.Marshal(),
  405. },
  406. {
  407. desc: "repeated groups",
  408. decodeTo: []proto.Message{&testpb.TestAllTypes{
  409. Repeatedgroup: []*testpb.TestAllTypes_RepeatedGroup{
  410. {A: scalar.Int32(1017)},
  411. {A: scalar.Int32(2017)},
  412. },
  413. }, build(
  414. &testpb.TestAllExtensions{},
  415. extend(testpb.E_RepeatedgroupExtension, []*testpb.RepeatedGroupExtension{
  416. {A: scalar.Int32(1017)},
  417. {A: scalar.Int32(2017)},
  418. }),
  419. )},
  420. wire: pack.Message{
  421. pack.Tag{46, pack.StartGroupType},
  422. pack.Tag{47, pack.VarintType}, pack.Varint(1017),
  423. pack.Tag{46, pack.EndGroupType},
  424. pack.Tag{46, pack.StartGroupType},
  425. pack.Tag{47, pack.VarintType}, pack.Varint(2017),
  426. pack.Tag{46, pack.EndGroupType},
  427. }.Marshal(),
  428. },
  429. {
  430. desc: "maps",
  431. decodeTo: []proto.Message{&testpb.TestAllTypes{
  432. MapInt32Int32: map[int32]int32{1056: 1156, 2056: 2156},
  433. MapInt64Int64: map[int64]int64{1057: 1157, 2057: 2157},
  434. MapUint32Uint32: map[uint32]uint32{1058: 1158, 2058: 2158},
  435. MapUint64Uint64: map[uint64]uint64{1059: 1159, 2059: 2159},
  436. MapSint32Sint32: map[int32]int32{1060: 1160, 2060: 2160},
  437. MapSint64Sint64: map[int64]int64{1061: 1161, 2061: 2161},
  438. MapFixed32Fixed32: map[uint32]uint32{1062: 1162, 2062: 2162},
  439. MapFixed64Fixed64: map[uint64]uint64{1063: 1163, 2063: 2163},
  440. MapSfixed32Sfixed32: map[int32]int32{1064: 1164, 2064: 2164},
  441. MapSfixed64Sfixed64: map[int64]int64{1065: 1165, 2065: 2165},
  442. MapInt32Float: map[int32]float32{1066: 1166.5, 2066: 2166.5},
  443. MapInt32Double: map[int32]float64{1067: 1167.5, 2067: 2167.5},
  444. MapBoolBool: map[bool]bool{true: false, false: true},
  445. MapStringString: map[string]string{"69.1.key": "69.1.val", "69.2.key": "69.2.val"},
  446. MapStringBytes: map[string][]byte{"70.1.key": []byte("70.1.val"), "70.2.key": []byte("70.2.val")},
  447. MapStringNestedMessage: map[string]*testpb.TestAllTypes_NestedMessage{
  448. "71.1.key": {A: scalar.Int32(1171)},
  449. "71.2.key": {A: scalar.Int32(2171)},
  450. },
  451. MapStringNestedEnum: map[string]testpb.TestAllTypes_NestedEnum{
  452. "73.1.key": testpb.TestAllTypes_FOO,
  453. "73.2.key": testpb.TestAllTypes_BAR,
  454. },
  455. }},
  456. wire: pack.Message{
  457. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  458. pack.Tag{1, pack.VarintType}, pack.Varint(1056),
  459. pack.Tag{2, pack.VarintType}, pack.Varint(1156),
  460. }),
  461. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  462. pack.Tag{1, pack.VarintType}, pack.Varint(2056),
  463. pack.Tag{2, pack.VarintType}, pack.Varint(2156),
  464. }),
  465. pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
  466. pack.Tag{1, pack.VarintType}, pack.Varint(1057),
  467. pack.Tag{2, pack.VarintType}, pack.Varint(1157),
  468. }),
  469. pack.Tag{57, pack.BytesType}, pack.LengthPrefix(pack.Message{
  470. pack.Tag{1, pack.VarintType}, pack.Varint(2057),
  471. pack.Tag{2, pack.VarintType}, pack.Varint(2157),
  472. }),
  473. pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
  474. pack.Tag{1, pack.VarintType}, pack.Varint(1058),
  475. pack.Tag{2, pack.VarintType}, pack.Varint(1158),
  476. }),
  477. pack.Tag{58, pack.BytesType}, pack.LengthPrefix(pack.Message{
  478. pack.Tag{1, pack.VarintType}, pack.Varint(2058),
  479. pack.Tag{2, pack.VarintType}, pack.Varint(2158),
  480. }),
  481. pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
  482. pack.Tag{1, pack.VarintType}, pack.Varint(1059),
  483. pack.Tag{2, pack.VarintType}, pack.Varint(1159),
  484. }),
  485. pack.Tag{59, pack.BytesType}, pack.LengthPrefix(pack.Message{
  486. pack.Tag{1, pack.VarintType}, pack.Varint(2059),
  487. pack.Tag{2, pack.VarintType}, pack.Varint(2159),
  488. }),
  489. pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
  490. pack.Tag{1, pack.VarintType}, pack.Svarint(1060),
  491. pack.Tag{2, pack.VarintType}, pack.Svarint(1160),
  492. }),
  493. pack.Tag{60, pack.BytesType}, pack.LengthPrefix(pack.Message{
  494. pack.Tag{1, pack.VarintType}, pack.Svarint(2060),
  495. pack.Tag{2, pack.VarintType}, pack.Svarint(2160),
  496. }),
  497. pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
  498. pack.Tag{1, pack.VarintType}, pack.Svarint(1061),
  499. pack.Tag{2, pack.VarintType}, pack.Svarint(1161),
  500. }),
  501. pack.Tag{61, pack.BytesType}, pack.LengthPrefix(pack.Message{
  502. pack.Tag{1, pack.VarintType}, pack.Svarint(2061),
  503. pack.Tag{2, pack.VarintType}, pack.Svarint(2161),
  504. }),
  505. pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
  506. pack.Tag{1, pack.Fixed32Type}, pack.Int32(1062),
  507. pack.Tag{2, pack.Fixed32Type}, pack.Int32(1162),
  508. }),
  509. pack.Tag{62, pack.BytesType}, pack.LengthPrefix(pack.Message{
  510. pack.Tag{1, pack.Fixed32Type}, pack.Int32(2062),
  511. pack.Tag{2, pack.Fixed32Type}, pack.Int32(2162),
  512. }),
  513. pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
  514. pack.Tag{1, pack.Fixed64Type}, pack.Int64(1063),
  515. pack.Tag{2, pack.Fixed64Type}, pack.Int64(1163),
  516. }),
  517. pack.Tag{63, pack.BytesType}, pack.LengthPrefix(pack.Message{
  518. pack.Tag{1, pack.Fixed64Type}, pack.Int64(2063),
  519. pack.Tag{2, pack.Fixed64Type}, pack.Int64(2163),
  520. }),
  521. pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
  522. pack.Tag{1, pack.Fixed32Type}, pack.Int32(1064),
  523. pack.Tag{2, pack.Fixed32Type}, pack.Int32(1164),
  524. }),
  525. pack.Tag{64, pack.BytesType}, pack.LengthPrefix(pack.Message{
  526. pack.Tag{1, pack.Fixed32Type}, pack.Int32(2064),
  527. pack.Tag{2, pack.Fixed32Type}, pack.Int32(2164),
  528. }),
  529. pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
  530. pack.Tag{1, pack.Fixed64Type}, pack.Int64(1065),
  531. pack.Tag{2, pack.Fixed64Type}, pack.Int64(1165),
  532. }),
  533. pack.Tag{65, pack.BytesType}, pack.LengthPrefix(pack.Message{
  534. pack.Tag{1, pack.Fixed64Type}, pack.Int64(2065),
  535. pack.Tag{2, pack.Fixed64Type}, pack.Int64(2165),
  536. }),
  537. pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
  538. pack.Tag{1, pack.VarintType}, pack.Varint(1066),
  539. pack.Tag{2, pack.Fixed32Type}, pack.Float32(1166.5),
  540. }),
  541. pack.Tag{66, pack.BytesType}, pack.LengthPrefix(pack.Message{
  542. pack.Tag{1, pack.VarintType}, pack.Varint(2066),
  543. pack.Tag{2, pack.Fixed32Type}, pack.Float32(2166.5),
  544. }),
  545. pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
  546. pack.Tag{1, pack.VarintType}, pack.Varint(1067),
  547. pack.Tag{2, pack.Fixed64Type}, pack.Float64(1167.5),
  548. }),
  549. pack.Tag{67, pack.BytesType}, pack.LengthPrefix(pack.Message{
  550. pack.Tag{1, pack.VarintType}, pack.Varint(2067),
  551. pack.Tag{2, pack.Fixed64Type}, pack.Float64(2167.5),
  552. }),
  553. pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
  554. pack.Tag{1, pack.VarintType}, pack.Bool(true),
  555. pack.Tag{2, pack.VarintType}, pack.Bool(false),
  556. }),
  557. pack.Tag{68, pack.BytesType}, pack.LengthPrefix(pack.Message{
  558. pack.Tag{1, pack.VarintType}, pack.Bool(false),
  559. pack.Tag{2, pack.VarintType}, pack.Bool(true),
  560. }),
  561. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  562. pack.Tag{1, pack.BytesType}, pack.String("69.1.key"),
  563. pack.Tag{2, pack.BytesType}, pack.String("69.1.val"),
  564. }),
  565. pack.Tag{69, pack.BytesType}, pack.LengthPrefix(pack.Message{
  566. pack.Tag{1, pack.BytesType}, pack.String("69.2.key"),
  567. pack.Tag{2, pack.BytesType}, pack.String("69.2.val"),
  568. }),
  569. pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
  570. pack.Tag{1, pack.BytesType}, pack.String("70.1.key"),
  571. pack.Tag{2, pack.BytesType}, pack.String("70.1.val"),
  572. }),
  573. pack.Tag{70, pack.BytesType}, pack.LengthPrefix(pack.Message{
  574. pack.Tag{1, pack.BytesType}, pack.String("70.2.key"),
  575. pack.Tag{2, pack.BytesType}, pack.String("70.2.val"),
  576. }),
  577. pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
  578. pack.Tag{1, pack.BytesType}, pack.String("71.1.key"),
  579. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  580. pack.Tag{1, pack.VarintType}, pack.Varint(1171),
  581. }),
  582. }),
  583. pack.Tag{71, pack.BytesType}, pack.LengthPrefix(pack.Message{
  584. pack.Tag{1, pack.BytesType}, pack.String("71.2.key"),
  585. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  586. pack.Tag{1, pack.VarintType}, pack.Varint(2171),
  587. }),
  588. }),
  589. pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
  590. pack.Tag{1, pack.BytesType}, pack.String("73.1.key"),
  591. pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_FOO)),
  592. }),
  593. pack.Tag{73, pack.BytesType}, pack.LengthPrefix(pack.Message{
  594. pack.Tag{1, pack.BytesType}, pack.String("73.2.key"),
  595. pack.Tag{2, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR)),
  596. }),
  597. }.Marshal(),
  598. },
  599. {
  600. desc: "oneof (uint32)",
  601. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint32{1111}}},
  602. wire: pack.Message{pack.Tag{111, pack.VarintType}, pack.Varint(1111)}.Marshal(),
  603. },
  604. {
  605. desc: "oneof (message)",
  606. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  607. &testpb.TestAllTypes_NestedMessage{A: scalar.Int32(1112)},
  608. }}},
  609. wire: pack.Message{pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  610. pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1112)},
  611. })}.Marshal(),
  612. },
  613. {
  614. desc: "oneof (overridden message)",
  615. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofNestedMessage{
  616. &testpb.TestAllTypes_NestedMessage{
  617. Corecursive: &testpb.TestAllTypes{
  618. OptionalInt32: scalar.Int32(43),
  619. },
  620. },
  621. }}},
  622. wire: pack.Message{
  623. pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  624. pack.Message{pack.Tag{1, pack.VarintType}, pack.Varint(1)},
  625. }),
  626. pack.Tag{112, pack.BytesType}, pack.LengthPrefix(pack.Message{
  627. pack.Tag{2, pack.BytesType}, pack.LengthPrefix(pack.Message{
  628. pack.Tag{1, pack.VarintType}, pack.Varint(43),
  629. }),
  630. }),
  631. }.Marshal(),
  632. },
  633. {
  634. desc: "oneof (string)",
  635. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofString{"1113"}}},
  636. wire: pack.Message{pack.Tag{113, pack.BytesType}, pack.String("1113")}.Marshal(),
  637. },
  638. {
  639. desc: "oneof (bytes)",
  640. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBytes{[]byte("1114")}}},
  641. wire: pack.Message{pack.Tag{114, pack.BytesType}, pack.String("1114")}.Marshal(),
  642. },
  643. {
  644. desc: "oneof (bool)",
  645. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofBool{true}}},
  646. wire: pack.Message{pack.Tag{115, pack.VarintType}, pack.Bool(true)}.Marshal(),
  647. },
  648. {
  649. desc: "oneof (uint64)",
  650. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{116}}},
  651. wire: pack.Message{pack.Tag{116, pack.VarintType}, pack.Varint(116)}.Marshal(),
  652. },
  653. {
  654. desc: "oneof (float)",
  655. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofFloat{117.5}}},
  656. wire: pack.Message{pack.Tag{117, pack.Fixed32Type}, pack.Float32(117.5)}.Marshal(),
  657. },
  658. {
  659. desc: "oneof (double)",
  660. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofDouble{118.5}}},
  661. wire: pack.Message{pack.Tag{118, pack.Fixed64Type}, pack.Float64(118.5)}.Marshal(),
  662. },
  663. {
  664. desc: "oneof (enum)",
  665. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofEnum{testpb.TestAllTypes_BAR}}},
  666. wire: pack.Message{pack.Tag{119, pack.VarintType}, pack.Varint(int(testpb.TestAllTypes_BAR))}.Marshal(),
  667. },
  668. {
  669. desc: "oneof (overridden value)",
  670. decodeTo: []proto.Message{&testpb.TestAllTypes{OneofField: &testpb.TestAllTypes_OneofUint64{2}}},
  671. wire: pack.Message{
  672. pack.Tag{111, pack.VarintType}, pack.Varint(1),
  673. pack.Tag{116, pack.VarintType}, pack.Varint(2),
  674. }.Marshal(),
  675. },
  676. // TODO: More unknown field tests for ordering, repeated fields, etc.
  677. //
  678. // It is currently impossible to produce results that the v1 Equal
  679. // considers equivalent to those of the v1 decoder. Figure out if
  680. // that's a problem or not.
  681. {
  682. desc: "unknown fields",
  683. decodeTo: []proto.Message{build(
  684. &testpb.TestAllTypes{},
  685. unknown(100000, pack.Message{
  686. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  687. }.Marshal()),
  688. )},
  689. wire: pack.Message{
  690. pack.Tag{100000, pack.VarintType}, pack.Varint(1),
  691. }.Marshal(),
  692. },
  693. {
  694. desc: "field type mismatch",
  695. decodeTo: []proto.Message{build(
  696. &testpb.TestAllTypes{},
  697. unknown(1, pack.Message{
  698. pack.Tag{1, pack.BytesType}, pack.String("string"),
  699. }.Marshal()),
  700. )},
  701. wire: pack.Message{
  702. pack.Tag{1, pack.BytesType}, pack.String("string"),
  703. }.Marshal(),
  704. },
  705. {
  706. desc: "map field element mismatch",
  707. decodeTo: []proto.Message{
  708. &testpb.TestAllTypes{
  709. MapInt32Int32: map[int32]int32{1: 0},
  710. },
  711. },
  712. wire: pack.Message{
  713. pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
  714. pack.Tag{1, pack.VarintType}, pack.Varint(1),
  715. pack.Tag{2, pack.BytesType}, pack.String("string"),
  716. }),
  717. }.Marshal(),
  718. },
  719. }
  720. func build(m proto.Message, opts ...buildOpt) proto.Message {
  721. for _, opt := range opts {
  722. opt(m)
  723. }
  724. return m
  725. }
  726. type buildOpt func(proto.Message)
  727. func unknown(num pref.FieldNumber, raw pref.RawFields) buildOpt {
  728. return func(m proto.Message) {
  729. m.ProtoReflect().UnknownFields().Set(num, raw)
  730. }
  731. }
  732. func extend(desc *protoV1.ExtensionDesc, value interface{}) buildOpt {
  733. return func(m proto.Message) {
  734. if err := protoV1.SetExtension(m.(protoV1.Message), desc, value); err != nil {
  735. panic(err)
  736. }
  737. }
  738. }