Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 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 | |
| 5 | package proto_test |
| 6 | |
| 7 | import ( |
| 8 | "google.golang.org/protobuf/internal/encoding/pack" |
Damien Neil | fe15dd4 | 2019-12-06 15:36:03 -0800 | [diff] [blame^] | 9 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 10 | "google.golang.org/protobuf/internal/flags" |
| 11 | "google.golang.org/protobuf/proto" |
| 12 | |
| 13 | messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb" |
| 14 | msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb" |
| 15 | ) |
| 16 | |
| 17 | func init() { |
Joe Tsai | 1799d11 | 2019-08-08 13:31:59 -0700 | [diff] [blame] | 18 | if flags.ProtoLegacy { |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 19 | testProtos = append(testProtos, messageSetTestProtos...) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | var messageSetTestProtos = []testProto{ |
| 24 | { |
| 25 | desc: "MessageSet type_id before message content", |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 26 | decodeTo: []proto.Message{func() proto.Message { |
| 27 | m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}} |
| 28 | proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{ |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 29 | Ext1Field1: proto.Int32(10), |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 30 | }) |
| 31 | return m |
| 32 | }()}, |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 33 | wire: pack.Message{ |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 34 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 35 | pack.Tag{1, pack.StartGroupType}, |
| 36 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 37 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 38 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 39 | }), |
| 40 | pack.Tag{1, pack.EndGroupType}, |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 41 | }), |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 42 | }.Marshal(), |
| 43 | }, |
| 44 | { |
| 45 | desc: "MessageSet type_id after message content", |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 46 | decodeTo: []proto.Message{func() proto.Message { |
| 47 | m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}} |
| 48 | proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{ |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 49 | Ext1Field1: proto.Int32(10), |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 50 | }) |
| 51 | return m |
| 52 | }()}, |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 53 | wire: pack.Message{ |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 54 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 55 | pack.Tag{1, pack.StartGroupType}, |
| 56 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 57 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 58 | }), |
| 59 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 60 | pack.Tag{1, pack.EndGroupType}, |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 61 | }), |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 62 | }.Marshal(), |
| 63 | }, |
| 64 | { |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 65 | desc: "MessageSet does not preserve unknown field", |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 66 | decodeTo: []proto.Message{build( |
| 67 | &messagesetpb.MessageSet{}, |
| 68 | extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{ |
| 69 | Ext1Field1: proto.Int32(10), |
| 70 | }), |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 71 | )}, |
| 72 | wire: pack.Message{ |
| 73 | pack.Tag{1, pack.StartGroupType}, |
| 74 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 75 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 76 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 77 | }), |
| 78 | pack.Tag{1, pack.EndGroupType}, |
| 79 | // Unknown field |
| 80 | pack.Tag{4, pack.VarintType}, pack.Varint(30), |
| 81 | }.Marshal(), |
| 82 | }, |
| 83 | { |
| 84 | desc: "MessageSet with unknown type_id", |
| 85 | decodeTo: []proto.Message{build( |
| 86 | &messagesetpb.MessageSet{}, |
| 87 | unknown(pack.Message{ |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 88 | pack.Tag{1002, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 89 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 90 | }), |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 91 | }.Marshal()), |
| 92 | )}, |
| 93 | wire: pack.Message{ |
| 94 | pack.Tag{1, pack.StartGroupType}, |
| 95 | pack.Tag{2, pack.VarintType}, pack.Varint(1002), |
| 96 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 97 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 98 | }), |
| 99 | pack.Tag{1, pack.EndGroupType}, |
| 100 | }.Marshal(), |
| 101 | }, |
| 102 | { |
| 103 | desc: "MessageSet merges repeated message fields in item", |
| 104 | decodeTo: []proto.Message{build( |
| 105 | &messagesetpb.MessageSet{}, |
| 106 | extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{ |
| 107 | Ext1Field1: proto.Int32(10), |
| 108 | Ext1Field2: proto.Int32(20), |
| 109 | }), |
| 110 | )}, |
| 111 | wire: pack.Message{ |
| 112 | pack.Tag{1, pack.StartGroupType}, |
| 113 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 114 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 115 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 116 | }), |
| 117 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 118 | pack.Tag{2, pack.VarintType}, pack.Varint(20), |
| 119 | }), |
| 120 | pack.Tag{1, pack.EndGroupType}, |
| 121 | }.Marshal(), |
| 122 | }, |
| 123 | { |
| 124 | desc: "MessageSet merges message fields in repeated items", |
| 125 | decodeTo: []proto.Message{build( |
| 126 | &messagesetpb.MessageSet{}, |
| 127 | extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{ |
| 128 | Ext1Field1: proto.Int32(10), |
| 129 | Ext1Field2: proto.Int32(20), |
| 130 | }), |
| 131 | extend(msetextpb.E_Ext2_MessageSetExtension, &msetextpb.Ext2{ |
| 132 | Ext2Field1: proto.Int32(30), |
| 133 | }), |
| 134 | )}, |
| 135 | wire: pack.Message{ |
| 136 | // Ext1, field1 |
| 137 | pack.Tag{1, pack.StartGroupType}, |
| 138 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 139 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 140 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 141 | }), |
| 142 | pack.Tag{1, pack.EndGroupType}, |
| 143 | // Ext2, field1 |
| 144 | pack.Tag{1, pack.StartGroupType}, |
| 145 | pack.Tag{2, pack.VarintType}, pack.Varint(1001), |
| 146 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 147 | pack.Tag{1, pack.VarintType}, pack.Varint(30), |
| 148 | }), |
| 149 | pack.Tag{1, pack.EndGroupType}, |
| 150 | // Ext2, field2 |
| 151 | pack.Tag{1, pack.StartGroupType}, |
| 152 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 153 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 154 | pack.Tag{2, pack.VarintType}, pack.Varint(20), |
| 155 | }), |
| 156 | pack.Tag{1, pack.EndGroupType}, |
| 157 | }.Marshal(), |
| 158 | }, |
| 159 | { |
| 160 | desc: "MessageSet with missing type_id", |
| 161 | decodeTo: []proto.Message{build( |
| 162 | &messagesetpb.MessageSet{}, |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 163 | )}, |
| 164 | wire: pack.Message{ |
| 165 | pack.Tag{1, pack.StartGroupType}, |
| 166 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 167 | pack.Tag{1, pack.VarintType}, pack.Varint(10), |
| 168 | }), |
| 169 | pack.Tag{1, pack.EndGroupType}, |
| 170 | }.Marshal(), |
| 171 | }, |
| 172 | { |
| 173 | desc: "MessageSet with missing message", |
| 174 | decodeTo: []proto.Message{build( |
| 175 | &messagesetpb.MessageSet{}, |
| 176 | extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}), |
| 177 | )}, |
| 178 | wire: pack.Message{ |
| 179 | pack.Tag{1, pack.StartGroupType}, |
| 180 | pack.Tag{2, pack.VarintType}, pack.Varint(1000), |
| 181 | pack.Tag{1, pack.EndGroupType}, |
| 182 | }.Marshal(), |
| 183 | }, |
Damien Neil | fe15dd4 | 2019-12-06 15:36:03 -0800 | [diff] [blame^] | 184 | { |
| 185 | desc: "MessageSet with type id out of valid field number range", |
| 186 | decodeTo: []proto.Message{func() proto.Message { |
| 187 | m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}} |
| 188 | proto.SetExtension(m.MessageSet, msetextpb.E_ExtLargeNumber_MessageSetExtension, &msetextpb.ExtLargeNumber{}) |
| 189 | return m |
| 190 | }()}, |
| 191 | wire: pack.Message{ |
| 192 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 193 | pack.Tag{1, pack.StartGroupType}, |
| 194 | pack.Tag{2, pack.VarintType}, pack.Varint(wire.MaxValidNumber + 1), |
| 195 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 196 | pack.Tag{1, pack.EndGroupType}, |
| 197 | }), |
| 198 | }.Marshal(), |
| 199 | }, |
| 200 | { |
| 201 | desc: "MessageSet with unknown type id out of valid field number range", |
| 202 | decodeTo: []proto.Message{func() proto.Message { |
| 203 | m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}} |
| 204 | m.MessageSet.ProtoReflect().SetUnknown( |
| 205 | pack.Message{ |
| 206 | pack.Tag{wire.MaxValidNumber + 2, pack.BytesType}, pack.LengthPrefix{}, |
| 207 | }.Marshal(), |
| 208 | ) |
| 209 | return m |
| 210 | }()}, |
| 211 | wire: pack.Message{ |
| 212 | pack.Tag{1, pack.BytesType}, pack.LengthPrefix(pack.Message{ |
| 213 | pack.Tag{1, pack.StartGroupType}, |
| 214 | pack.Tag{2, pack.VarintType}, pack.Varint(wire.MaxValidNumber + 2), |
| 215 | pack.Tag{3, pack.BytesType}, pack.LengthPrefix(pack.Message{}), |
| 216 | pack.Tag{1, pack.EndGroupType}, |
| 217 | }), |
| 218 | }.Marshal(), |
| 219 | }, |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 220 | } |