aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/google.golang.org/protobuf/proto/messageset_test.go
blob: e634112fa02671bf6687c4e6293a92da78012adf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package proto_test

import (
	"google.golang.org/protobuf/encoding/protowire"
	"google.golang.org/protobuf/internal/flags"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/testing/protopack"

	messagesetpb "google.golang.org/protobuf/internal/testprotos/messageset/messagesetpb"
	msetextpb "google.golang.org/protobuf/internal/testprotos/messageset/msetextpb"
)

func init() {
	if flags.ProtoLegacy {
		testValidMessages = append(testValidMessages, messageSetTestProtos...)
		testInvalidMessages = append(testInvalidMessages, messageSetInvalidTestProtos...)
	}
}

var messageSetTestProtos = []testProto{
	{
		desc: "MessageSet type_id before message content",
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
				Ext1Field1: proto.Int32(10),
			})
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
				}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc: "MessageSet type_id after message content",
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
				Ext1Field1: proto.Int32(10),
			})
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
				}),
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc: "MessageSet does not preserve unknown field",
		decodeTo: []proto.Message{build(
			&messagesetpb.MessageSet{},
			extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
				Ext1Field1: proto.Int32(10),
			}),
		)},
		wire: protopack.Message{
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
			}),
			protopack.Tag{1, protopack.EndGroupType},
			// Unknown field
			protopack.Tag{4, protopack.VarintType}, protopack.Varint(30),
		}.Marshal(),
	},
	{
		desc: "MessageSet with unknown type_id",
		decodeTo: []proto.Message{build(
			&messagesetpb.MessageSet{},
			unknown(protopack.Message{
				protopack.Tag{999, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
				}),
			}.Marshal()),
		)},
		wire: protopack.Message{
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(999),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
			}),
			protopack.Tag{1, protopack.EndGroupType},
		}.Marshal(),
	},
	{
		desc: "MessageSet merges repeated message fields in item",
		decodeTo: []proto.Message{build(
			&messagesetpb.MessageSet{},
			extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
				Ext1Field1: proto.Int32(10),
				Ext1Field2: proto.Int32(20),
			}),
		)},
		wire: protopack.Message{
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
			}),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
			}),
			protopack.Tag{1, protopack.EndGroupType},
		}.Marshal(),
	},
	{
		desc: "MessageSet merges message fields in repeated items",
		decodeTo: []proto.Message{build(
			&messagesetpb.MessageSet{},
			extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
				Ext1Field1: proto.Int32(10),
				Ext1Field2: proto.Int32(20),
			}),
			extend(msetextpb.E_Ext2_MessageSetExtension, &msetextpb.Ext2{
				Ext2Field1: proto.Int32(30),
			}),
		)},
		wire: protopack.Message{
			// Ext1, field1
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
			}),
			protopack.Tag{1, protopack.EndGroupType},
			// Ext2, field1
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(1001),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.VarintType}, protopack.Varint(30),
			}),
			protopack.Tag{1, protopack.EndGroupType},
			// Ext2, field2
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(20),
			}),
			protopack.Tag{1, protopack.EndGroupType},
		}.Marshal(),
	},
	{
		desc: "MessageSet with missing type_id",
		decodeTo: []proto.Message{build(
			&messagesetpb.MessageSet{},
		)},
		wire: protopack.Message{
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
			}),
			protopack.Tag{1, protopack.EndGroupType},
		}.Marshal(),
	},
	{
		desc: "MessageSet with missing message",
		decodeTo: []proto.Message{build(
			&messagesetpb.MessageSet{},
			extend(msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{}),
		)},
		wire: protopack.Message{
			protopack.Tag{1, protopack.StartGroupType},
			protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
			protopack.Tag{1, protopack.EndGroupType},
		}.Marshal(),
	},
	{
		desc: "MessageSet with type id out of valid field number range",
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			proto.SetExtension(m.MessageSet, msetextpb.E_ExtLargeNumber_MessageSetExtension, &msetextpb.ExtLargeNumber{})
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 1),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc: "MessageSet with unknown type id out of valid field number range",
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			m.MessageSet.ProtoReflect().SetUnknown(
				protopack.Message{
					protopack.Tag{protowire.MaxValidNumber + 2, protopack.BytesType}, protopack.LengthPrefix{},
				}.Marshal(),
			)
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(protowire.MaxValidNumber + 2),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc: "MessageSet with unknown field",
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			proto.SetExtension(m.MessageSet, msetextpb.E_Ext1_MessageSetExtension, &msetextpb.Ext1{
				Ext1Field1: proto.Int32(10),
			})
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(1000),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{1, protopack.VarintType}, protopack.Varint(10),
				}),
				protopack.Tag{4, protopack.VarintType}, protopack.Varint(0),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc:          "MessageSet with required field set",
		checkFastInit: true,
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{
				RequiredField1: proto.Int32(1),
			})
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
					protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
				}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc:          "MessageSet with required field unset",
		checkFastInit: true,
		partial:       true,
		decodeTo: []proto.Message{func() proto.Message {
			m := &messagesetpb.MessageSetContainer{MessageSet: &messagesetpb.MessageSet{}}
			proto.SetExtension(m.MessageSet, msetextpb.E_ExtRequired_MessageSetExtension, &msetextpb.ExtRequired{})
			return m
		}()},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Varint(1002),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
}

var messageSetInvalidTestProtos = []testProto{
	{
		desc: "MessageSet with type id 0",
		decodeTo: []proto.Message{
			(*messagesetpb.MessageSetContainer)(nil),
		},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
	{
		desc: "MessageSet with type id overflowing int32",
		decodeTo: []proto.Message{
			(*messagesetpb.MessageSetContainer)(nil),
		},
		wire: protopack.Message{
			protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
				protopack.Tag{1, protopack.StartGroupType},
				protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(0x80000000),
				protopack.Tag{3, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{}),
				protopack.Tag{1, protopack.EndGroupType},
			}),
		}.Marshal(),
	},
}