messageset_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Copyright 2019 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. "google.golang.org/protobuf/encoding/protowire"
  7. "google.golang.org/protobuf/internal/flags"
  8. "google.golang.org/protobuf/proto"
  9. "google.golang.org/protobuf/testing/protopack"
  10. messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb"
  11. msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb"
  12. )
  13. func init() {
  14. if flags.ProtoLegacy {
  15. testValidMessages = append(testValidMessages, messageSetTestProtos...)
  16. testInvalidMessages = append(testInvalidMessages, messageSetInvalidTestProtos...)
  17. }
  18. }
  19. var messageSetTestProtos = []testProto{
  20. {
  21. desc: "MessageSet type_id before message content",
  22. decodeTo: []proto.Message{func() proto.Message {
  23. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  24. proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  25. Ext1Field1: proto.Int32(10),
  26. })
  27. return m
  28. }()},
  29. wire: protopack.Message{
  30. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  31. protopack.Tag{1, protopack.StartGroupType},
  32. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  33. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  34. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  35. }),
  36. protopack.Tag{1, protopack.EndGroupType},
  37. }),
  38. }.Marshal(),
  39. },
  40. {
  41. desc: "MessageSet type_id after message content",
  42. decodeTo: []proto.Message{func() proto.Message {
  43. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  44. proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  45. Ext1Field1: proto.Int32(10),
  46. })
  47. return m
  48. }()},
  49. wire: protopack.Message{
  50. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  51. protopack.Tag{1, protopack.StartGroupType},
  52. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  53. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  54. }),
  55. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  56. protopack.Tag{1, protopack.EndGroupType},
  57. }),
  58. }.Marshal(),
  59. },
  60. {
  61. desc: "MessageSet does not preserve unknown field",
  62. decodeTo: []proto.Message{build(
  63. &messagesetpb.MessageSet{},
  64. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  65. Ext1Field1: proto.Int32(10),
  66. }),
  67. )},
  68. wire: protopack.Message{
  69. protopack.Tag{1, protopack.StartGroupType},
  70. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  71. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  72. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  73. }),
  74. protopack.Tag{1, protopack.EndGroupType},
  75. // Unknown field
  76. protopack.Tag{4, protopack.VarintType}, protopack.Varint(30),
  77. }.Marshal(),
  78. },
  79. {
  80. desc: "MessageSet with unknown type_id",
  81. decodeTo: []proto.Message{build(
  82. &messagesetpb.MessageSet{},
  83. unknown(protopack.Message{
  84. protopack.Tag{999, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  85. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  86. }),
  87. }.Marshal()),
  88. )},
  89. wire: protopack.Message{
  90. protopack.Tag{1, protopack.StartGroupType},
  91. protopack.Tag{2, protopack.VarintType}, protopack.Varint(999),
  92. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  93. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  94. }),
  95. protopack.Tag{1, protopack.EndGroupType},
  96. }.Marshal(),
  97. },
  98. {
  99. desc: "MessageSet merges repeated message fields in item",
  100. decodeTo: []proto.Message{build(
  101. &messagesetpb.MessageSet{},
  102. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  103. Ext1Field1: proto.Int32(10),
  104. Ext1Field2: proto.Int32(20),
  105. }),
  106. )},
  107. wire: protopack.Message{
  108. protopack.Tag{1, protopack.StartGroupType},
  109. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  110. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  111. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  112. }),
  113. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  114. protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
  115. }),
  116. protopack.Tag{1, protopack.EndGroupType},
  117. }.Marshal(),
  118. },
  119. {
  120. desc: "MessageSet merges message fields in repeated items",
  121. decodeTo: []proto.Message{build(
  122. &messagesetpb.MessageSet{},
  123. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  124. Ext1Field1: proto.Int32(10),
  125. Ext1Field2: proto.Int32(20),
  126. }),
  127. extend(msetextpb.E_Ext2_MessageSetExtension, &msetextpb.Ext2{
  128. Ext2Field1: proto.Int32(30),
  129. }),
  130. )},
  131. wire: protopack.Message{
  132. // Ext1, field1
  133. protopack.Tag{1, protopack.StartGroupType},
  134. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  135. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  136. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  137. }),
  138. protopack.Tag{1, protopack.EndGroupType},
  139. // Ext2, field1
  140. protopack.Tag{1, protopack.StartGroupType},
  141. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1001),
  142. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  143. protopack.Tag{1, protopack.VarintType}, protopack.Varint(30),
  144. }),
  145. protopack.Tag{1, protopack.EndGroupType},
  146. // Ext2, field2
  147. protopack.Tag{1, protopack.StartGroupType},
  148. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  149. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  150. protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
  151. }),
  152. protopack.Tag{1, protopack.EndGroupType},
  153. }.Marshal(),
  154. },
  155. {
  156. desc: "MessageSet with missing type_id",
  157. decodeTo: []proto.Message{build(
  158. &messagesetpb.MessageSet{},
  159. )},
  160. wire: protopack.Message{
  161. protopack.Tag{1, protopack.StartGroupType},
  162. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  163. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  164. }),
  165. protopack.Tag{1, protopack.EndGroupType},
  166. }.Marshal(),
  167. },
  168. {
  169. desc: "MessageSet with missing message",
  170. decodeTo: []proto.Message{build(
  171. &messagesetpb.MessageSet{},
  172. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}),
  173. )},
  174. wire: protopack.Message{
  175. protopack.Tag{1, protopack.StartGroupType},
  176. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  177. protopack.Tag{1, protopack.EndGroupType},
  178. }.Marshal(),
  179. },
  180. {
  181. desc: "MessageSet with type id out of valid field number range",
  182. decodeTo: []proto.Message{func() proto.Message {
  183. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  184. proto.SetExtension(m.MessageSet, msetextpb.E_ExtLargeNumber_MessageSetExtension, &msetextpb.ExtLargeNumber{})
  185. return m
  186. }()},
  187. wire: protopack.Message{
  188. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  189. protopack.Tag{1, protopack.StartGroupType},
  190. protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 1),
  191. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  192. protopack.Tag{1, protopack.EndGroupType},
  193. }),
  194. }.Marshal(),
  195. },
  196. {
  197. desc: "MessageSet with unknown type id out of valid field number range",
  198. decodeTo: []proto.Message{func() proto.Message {
  199. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  200. m.MessageSet.ProtoReflect().SetUnknown(
  201. protopack.Message{
  202. protopack.Tag{protowire.MaxValidNumber + 2, protopack.BytesType}, protopack.LengthPrefix{},
  203. }.Marshal(),
  204. )
  205. return m
  206. }()},
  207. wire: protopack.Message{
  208. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  209. protopack.Tag{1, protopack.StartGroupType},
  210. protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 2),
  211. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  212. protopack.Tag{1, protopack.EndGroupType},
  213. }),
  214. }.Marshal(),
  215. },
  216. {
  217. desc: "MessageSet with unknown field",
  218. decodeTo: []proto.Message{func() proto.Message {
  219. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  220. proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  221. Ext1Field1: proto.Int32(10),
  222. })
  223. return m
  224. }()},
  225. wire: protopack.Message{
  226. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  227. protopack.Tag{1, protopack.StartGroupType},
  228. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
  229. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  230. protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
  231. }),
  232. protopack.Tag{4, protopack.VarintType}, protopack.Varint(0),
  233. protopack.Tag{1, protopack.EndGroupType},
  234. }),
  235. }.Marshal(),
  236. },
  237. {
  238. desc: "MessageSet with required field set",
  239. checkFastInit: true,
  240. decodeTo: []proto.Message{func() proto.Message {
  241. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  242. proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{
  243. RequiredField1: proto.Int32(1),
  244. })
  245. return m
  246. }()},
  247. wire: protopack.Message{
  248. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  249. protopack.Tag{1, protopack.StartGroupType},
  250. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
  251. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  252. protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
  253. }),
  254. protopack.Tag{1, protopack.EndGroupType},
  255. }),
  256. }.Marshal(),
  257. },
  258. {
  259. desc: "MessageSet with required field unset",
  260. checkFastInit: true,
  261. partial: true,
  262. decodeTo: []proto.Message{func() proto.Message {
  263. m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
  264. proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{})
  265. return m
  266. }()},
  267. wire: protopack.Message{
  268. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  269. protopack.Tag{1, protopack.StartGroupType},
  270. protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
  271. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  272. protopack.Tag{1, protopack.EndGroupType},
  273. }),
  274. }.Marshal(),
  275. },
  276. }
  277. var messageSetInvalidTestProtos = []testProto{
  278. {
  279. desc: "MessageSet with type id 0",
  280. decodeTo: []proto.Message{
  281. (*messagesetpb.MessageSetContainer)(nil),
  282. },
  283. wire: protopack.Message{
  284. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  285. protopack.Tag{1, protopack.StartGroupType},
  286. protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0),
  287. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  288. protopack.Tag{1, protopack.EndGroupType},
  289. }),
  290. }.Marshal(),
  291. },
  292. {
  293. desc: "MessageSet with type id overflowing int32",
  294. decodeTo: []proto.Message{
  295. (*messagesetpb.MessageSetContainer)(nil),
  296. },
  297. wire: protopack.Message{
  298. protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
  299. protopack.Tag{1, protopack.StartGroupType},
  300. protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0x80000000),
  301. protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
  302. protopack.Tag{1, protopack.EndGroupType},
  303. }),
  304. }.Marshal(),
  305. },
  306. }