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