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