messageset_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/internal/encoding/pack"
  7. "google.golang.org/protobuf/internal/flags"
  8. "google.golang.org/protobuf/proto"
  9. messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb"
  10. msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb"
  11. )
  12. func init() {
  13. if flags.ProtoLegacy {
  14. testProtos = append(testProtos, messageSetTestProtos...)
  15. }
  16. }
  17. var messageSetTestProtos = []testProto{
  18. {
  19. desc: "MessageSet type_id before message content",
  20. decodeTo: []proto.Message{build(
  21. &messagesetpb.MessageSet{},
  22. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  23. Ext1Field1: proto.Int32(10),
  24. }),
  25. )},
  26. wire: pack.Message{
  27. pack.Tag{1, pack.StartGroupType},
  28. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  29. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  30. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  31. }),
  32. pack.Tag{1, pack.EndGroupType},
  33. }.Marshal(),
  34. },
  35. {
  36. desc: "MessageSet type_id after message content",
  37. decodeTo: []proto.Message{build(
  38. &messagesetpb.MessageSet{},
  39. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  40. Ext1Field1: proto.Int32(10),
  41. }),
  42. )},
  43. wire: pack.Message{
  44. pack.Tag{1, pack.StartGroupType},
  45. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  46. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  47. }),
  48. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  49. pack.Tag{1, pack.EndGroupType},
  50. }.Marshal(),
  51. },
  52. {
  53. desc: "MessageSet preserves unknown field",
  54. decodeTo: []proto.Message{build(
  55. &messagesetpb.MessageSet{},
  56. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  57. Ext1Field1: proto.Int32(10),
  58. }),
  59. unknown(pack.Message{
  60. pack.Tag{4, pack.VarintType}, pack.Varint(30),
  61. }.Marshal()),
  62. )},
  63. wire: pack.Message{
  64. pack.Tag{1, pack.StartGroupType},
  65. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  66. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  67. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  68. }),
  69. pack.Tag{1, pack.EndGroupType},
  70. // Unknown field
  71. pack.Tag{4, pack.VarintType}, pack.Varint(30),
  72. }.Marshal(),
  73. },
  74. {
  75. desc: "MessageSet with unknown type_id",
  76. decodeTo: []proto.Message{build(
  77. &messagesetpb.MessageSet{},
  78. unknown(pack.Message{
  79. pack.Tag{1, pack.StartGroupType},
  80. pack.Tag{2, pack.VarintType}, pack.Varint(1002),
  81. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  82. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  83. }),
  84. pack.Tag{1, pack.EndGroupType},
  85. }.Marshal()),
  86. )},
  87. wire: pack.Message{
  88. pack.Tag{1, pack.StartGroupType},
  89. pack.Tag{2, pack.VarintType}, pack.Varint(1002),
  90. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  91. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  92. }),
  93. pack.Tag{1, pack.EndGroupType},
  94. }.Marshal(),
  95. },
  96. {
  97. desc: "MessageSet merges repeated message fields in item",
  98. decodeTo: []proto.Message{build(
  99. &messagesetpb.MessageSet{},
  100. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  101. Ext1Field1: proto.Int32(10),
  102. Ext1Field2: proto.Int32(20),
  103. }),
  104. )},
  105. wire: pack.Message{
  106. pack.Tag{1, pack.StartGroupType},
  107. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  108. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  109. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  110. }),
  111. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  112. pack.Tag{2, pack.VarintType}, pack.Varint(20),
  113. }),
  114. pack.Tag{1, pack.EndGroupType},
  115. }.Marshal(),
  116. },
  117. {
  118. desc: "MessageSet merges message fields in repeated items",
  119. decodeTo: []proto.Message{build(
  120. &messagesetpb.MessageSet{},
  121. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
  122. Ext1Field1: proto.Int32(10),
  123. Ext1Field2: proto.Int32(20),
  124. }),
  125. extend(msetextpb.E_Ext2_MessageSetExtension, &msetextpb.Ext2{
  126. Ext2Field1: proto.Int32(30),
  127. }),
  128. )},
  129. wire: pack.Message{
  130. // Ext1, field1
  131. pack.Tag{1, pack.StartGroupType},
  132. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  133. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  134. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  135. }),
  136. pack.Tag{1, pack.EndGroupType},
  137. // Ext2, field1
  138. pack.Tag{1, pack.StartGroupType},
  139. pack.Tag{2, pack.VarintType}, pack.Varint(1001),
  140. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  141. pack.Tag{1, pack.VarintType}, pack.Varint(30),
  142. }),
  143. pack.Tag{1, pack.EndGroupType},
  144. // Ext2, field2
  145. pack.Tag{1, pack.StartGroupType},
  146. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  147. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  148. pack.Tag{2, pack.VarintType}, pack.Varint(20),
  149. }),
  150. pack.Tag{1, pack.EndGroupType},
  151. }.Marshal(),
  152. },
  153. {
  154. desc: "MessageSet with missing type_id",
  155. decodeTo: []proto.Message{build(
  156. &messagesetpb.MessageSet{},
  157. unknown(pack.Message{
  158. pack.Tag{1, pack.StartGroupType},
  159. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  160. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  161. }),
  162. pack.Tag{1, pack.EndGroupType},
  163. }.Marshal()),
  164. )},
  165. wire: pack.Message{
  166. pack.Tag{1, pack.StartGroupType},
  167. pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{
  168. pack.Tag{1, pack.VarintType}, pack.Varint(10),
  169. }),
  170. pack.Tag{1, pack.EndGroupType},
  171. }.Marshal(),
  172. },
  173. {
  174. desc: "MessageSet with missing message",
  175. decodeTo: []proto.Message{build(
  176. &messagesetpb.MessageSet{},
  177. extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}),
  178. )},
  179. wire: pack.Message{
  180. pack.Tag{1, pack.StartGroupType},
  181. pack.Tag{2, pack.VarintType}, pack.Varint(1000),
  182. pack.Tag{1, pack.EndGroupType},
  183. }.Marshal(),
  184. },
  185. }