blob: d6704e9a0e50b33d0026a2827b0ea54f87e19b90 [file] [log] [blame]
Joe Tsai3ab648c2018-08-15 14:41:30 -07001// Copyright 2018 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
5package pack
6
7import (
8 "bytes"
9 "encoding/hex"
10 "fmt"
11 "math"
12 "testing"
13
Joe Tsaid8881392019-06-06 13:01:53 -070014 "github.com/google/go-cmp/cmp"
15 "google.golang.org/protobuf/encoding/prototext"
16 pdesc "google.golang.org/protobuf/reflect/protodesc"
Damien Neile89e6242019-05-13 23:55:40 -070017 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaid8881392019-06-06 13:01:53 -070018
19 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsai3ab648c2018-08-15 14:41:30 -070020)
21
22var msgDesc = func() pref.MessageDescriptor {
Joe Tsaid8881392019-06-06 13:01:53 -070023 const s = `
24 name: "test.proto"
25 syntax: "proto2"
26 message_type: [{
27 name: "Message"
28 field: [
29 {name:"F1" number:1 label:LABEL_REPEATED type:TYPE_BOOL options:{packed:true}},
30 {name:"F2" number:2 label:LABEL_REPEATED type:TYPE_INT64 options:{packed:true}},
31 {name:"F3" number:3 label:LABEL_REPEATED type:TYPE_SINT64 options:{packed:true}},
32 {name:"F4" number:4 label:LABEL_REPEATED type:TYPE_UINT64 options:{packed:true}},
33 {name:"F5" number:5 label:LABEL_REPEATED type:TYPE_FIXED32 options:{packed:true}},
34 {name:"F6" number:6 label:LABEL_REPEATED type:TYPE_SFIXED32 options:{packed:true}},
35 {name:"F7" number:7 label:LABEL_REPEATED type:TYPE_FLOAT options:{packed:true}},
36 {name:"F8" number:8 label:LABEL_REPEATED type:TYPE_FIXED64 options:{packed:true}},
37 {name:"F9" number:9 label:LABEL_REPEATED type:TYPE_SFIXED64 options:{packed:true}},
38 {name:"F10" number:10 label:LABEL_REPEATED type:TYPE_DOUBLE options:{packed:true}},
39 {name:"F11" number:11 label:LABEL_OPTIONAL type:TYPE_STRING},
40 {name:"F12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES},
41 {name:"F13" number:13 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".Message"},
42 {name:"F14" number:14 label:LABEL_OPTIONAL type:TYPE_GROUP type_name:".Message"}
43 ]
44 }]
45 `
46 pb := new(descriptorpb.FileDescriptorProto)
47 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
48 panic(err)
49 }
50 fd, err := pdesc.NewFile(pb, nil)
Joe Tsai3ab648c2018-08-15 14:41:30 -070051 if err != nil {
52 panic(err)
53 }
Joe Tsaid8881392019-06-06 13:01:53 -070054 return fd.Messages().Get(0)
Joe Tsai3ab648c2018-08-15 14:41:30 -070055}()
56
Joe Tsai3ab648c2018-08-15 14:41:30 -070057// dhex decodes a hex-string and returns the bytes and panics if s is invalid.
58func dhex(s string) []byte {
59 b, err := hex.DecodeString(s)
60 if err != nil {
61 panic(err)
62 }
63 return b
64}
65
66func TestPack(t *testing.T) {
Damien Neil3fa6d3f2019-02-12 11:10:29 -080067 nan32 := math.Float32frombits(0x7fc00000)
68 nan64 := math.Float64frombits(0x7FF8000000000001)
Joe Tsai3ab648c2018-08-15 14:41:30 -070069 tests := []struct {
70 raw []byte
71 msg Message
72
73 wantOutCompact string
74 wantOutMulti string
75 wantOutSource string
76 }{{
77 raw: dhex("080088808080800002088280808080000a09010002828080808000"),
78 msg: Message{
79 Tag{1, VarintType}, Bool(false),
80 Denormalized{5, Tag{1, VarintType}}, Uvarint(2),
81 Tag{1, VarintType}, Denormalized{5, Uvarint(2)},
82 Tag{1, BytesType}, LengthPrefix{Bool(true), Bool(false), Uvarint(2), Denormalized{5, Uvarint(2)}},
83 },
84 wantOutSource: `pack.Message{
85 pack.Tag{1, pack.VarintType}, pack.Bool(false),
86 pack.Denormalized{+5, pack.Tag{1, pack.VarintType}}, pack.Uvarint(2),
87 pack.Tag{1, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(2)},
88 pack.Tag{1, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Uvarint(2), pack.Denormalized{+5, pack.Uvarint(2)}},
89}`,
90 }, {
91 raw: dhex("100010828080808000121980808080808080808001ffffffffffffffff7f828080808000"),
92 msg: Message{
93 Tag{2, VarintType}, Varint(0),
94 Tag{2, VarintType}, Denormalized{5, Varint(2)},
95 Tag{2, BytesType}, LengthPrefix{Varint(math.MinInt64), Varint(math.MaxInt64), Denormalized{5, Varint(2)}},
96 },
97 wantOutCompact: `Message{Tag{2, Varint}, Varint(0), Tag{2, Varint}, Denormalized{+5, Varint(2)}, Tag{2, Bytes}, LengthPrefix{Varint(-9223372036854775808), Varint(9223372036854775807), Denormalized{+5, Varint(2)}}}`,
98 }, {
99 raw: dhex("1801188180808080001a1affffffffffffffffff01feffffffffffffffff01818080808000"),
100 msg: Message{
101 Tag{3, VarintType}, Svarint(-1),
102 Tag{3, VarintType}, Denormalized{5, Svarint(-1)},
103 Tag{3, BytesType}, LengthPrefix{Svarint(math.MinInt64), Svarint(math.MaxInt64), Denormalized{5, Svarint(-1)}},
104 },
105 wantOutMulti: `Message{
106 Tag{3, Varint}, Svarint(-1),
107 Tag{3, Varint}, Denormalized{+5, Svarint(-1)},
108 Tag{3, Bytes}, LengthPrefix{Svarint(-9223372036854775808), Svarint(9223372036854775807), Denormalized{+5, Svarint(-1)}},
109}`,
110 }, {
111 raw: dhex("200120818080808000221100ffffffffffffffffff01818080808000"),
112 msg: Message{
113 Tag{4, VarintType}, Uvarint(+1),
114 Tag{4, VarintType}, Denormalized{5, Uvarint(+1)},
115 Tag{4, BytesType}, LengthPrefix{Uvarint(0), Uvarint(math.MaxUint64), Denormalized{5, Uvarint(+1)}},
116 },
117 wantOutSource: `pack.Message{
118 pack.Tag{4, pack.VarintType}, pack.Uvarint(1),
119 pack.Tag{4, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(1)},
120 pack.Tag{4, pack.BytesType}, pack.LengthPrefix{pack.Uvarint(0), pack.Uvarint(18446744073709551615), pack.Denormalized{+5, pack.Uvarint(1)}},
121}`,
122 }, {
123 raw: dhex("2d010000002a0800000000ffffffff"),
124 msg: Message{
125 Tag{5, Fixed32Type}, Uint32(+1),
126 Tag{5, BytesType}, LengthPrefix{Uint32(0), Uint32(math.MaxUint32)},
127 },
128 wantOutCompact: `Message{Tag{5, Fixed32}, Uint32(1), Tag{5, Bytes}, LengthPrefix{Uint32(0), Uint32(4294967295)}}`,
129 }, {
130 raw: dhex("35ffffffff320800000080ffffff7f"),
131 msg: Message{
132 Tag{6, Fixed32Type}, Int32(-1),
133 Tag{6, BytesType}, LengthPrefix{Int32(math.MinInt32), Int32(math.MaxInt32)},
134 },
135 wantOutMulti: `Message{
136 Tag{6, Fixed32}, Int32(-1),
137 Tag{6, Bytes}, LengthPrefix{Int32(-2147483648), Int32(2147483647)},
138}`,
139 }, {
140 raw: dhex("3ddb0f49403a1401000000ffff7f7f0000c07f0000807f000080ff"),
141 msg: Message{
142 Tag{7, Fixed32Type}, Float32(math.Pi),
Damien Neil3fa6d3f2019-02-12 11:10:29 -0800143 Tag{7, BytesType}, LengthPrefix{Float32(math.SmallestNonzeroFloat32), Float32(math.MaxFloat32), Float32(nan32), Float32(math.Inf(+1)), Float32(math.Inf(-1))},
Joe Tsai3ab648c2018-08-15 14:41:30 -0700144 },
145 wantOutSource: `pack.Message{
146 pack.Tag{7, pack.Fixed32Type}, pack.Float32(3.1415927),
147 pack.Tag{7, pack.BytesType}, pack.LengthPrefix{pack.Float32(1e-45), pack.Float32(3.4028235e+38), pack.Float32(math.NaN()), pack.Float32(math.Inf(+1)), pack.Float32(math.Inf(-1))},
148}`,
149 }, {
150 raw: dhex("41010000000000000042100000000000000000ffffffffffffffff"),
151 msg: Message{
152 Tag{8, Fixed64Type}, Uint64(+1),
153 Tag{8, BytesType}, LengthPrefix{Uint64(0), Uint64(math.MaxUint64)},
154 },
155 wantOutCompact: `Message{Tag{8, Fixed64}, Uint64(1), Tag{8, Bytes}, LengthPrefix{Uint64(0), Uint64(18446744073709551615)}}`,
156 }, {
157 raw: dhex("49ffffffffffffffff4a100000000000000080ffffffffffffff7f"),
158 msg: Message{
159 Tag{9, Fixed64Type}, Int64(-1),
160 Tag{9, BytesType}, LengthPrefix{Int64(math.MinInt64), Int64(math.MaxInt64)},
161 },
162 wantOutMulti: `Message{
163 Tag{9, Fixed64}, Int64(-1),
164 Tag{9, Bytes}, LengthPrefix{Int64(-9223372036854775808), Int64(9223372036854775807)},
165}`,
166 }, {
167 raw: dhex("51182d4454fb21094052280100000000000000ffffffffffffef7f010000000000f87f000000000000f07f000000000000f0ff"),
168 msg: Message{
169 Tag{10, Fixed64Type}, Float64(math.Pi),
Damien Neil3fa6d3f2019-02-12 11:10:29 -0800170 Tag{10, BytesType}, LengthPrefix{Float64(math.SmallestNonzeroFloat64), Float64(math.MaxFloat64), Float64(nan64), Float64(math.Inf(+1)), Float64(math.Inf(-1))},
Joe Tsai3ab648c2018-08-15 14:41:30 -0700171 },
172 wantOutMulti: `Message{
173 Tag{10, Fixed64}, Float64(3.141592653589793),
174 Tag{10, Bytes}, LengthPrefix{Float64(5e-324), Float64(1.7976931348623157e+308), Float64(NaN), Float64(+Inf), Float64(-Inf)},
175}`,
176 }, {
177 raw: dhex("5a06737472696e675a868080808000737472696e67"),
178 msg: Message{
179 Tag{11, BytesType}, String("string"),
180 Tag{11, BytesType}, Denormalized{+5, String("string")},
181 },
182 wantOutCompact: `Message{Tag{11, Bytes}, String("string"), Tag{11, Bytes}, Denormalized{+5, String("string")}}`,
183 }, {
184 raw: dhex("62056279746573628580808080006279746573"),
185 msg: Message{
186 Tag{12, BytesType}, Bytes("bytes"),
187 Tag{12, BytesType}, Denormalized{+5, Bytes("bytes")},
188 },
189 wantOutMulti: `Message{
190 Tag{12, Bytes}, Bytes("bytes"),
191 Tag{12, Bytes}, Denormalized{+5, Bytes("bytes")},
192}`,
193 }, {
194 raw: dhex("6a28a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a406"),
195 msg: Message{
196 Tag{13, BytesType}, LengthPrefix(Message{
197 Tag{100, VarintType}, Uvarint(math.MaxUint64),
198 Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
199 Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
200 Tag{100, BytesType}, Bytes("bytes"),
201 Tag{100, StartGroupType}, Tag{100, EndGroupType},
202 }),
203 },
204 wantOutSource: `pack.Message{
205 pack.Tag{13, pack.BytesType}, pack.LengthPrefix(pack.Message{
206 pack.Tag{100, pack.VarintType}, pack.Uvarint(18446744073709551615),
207 pack.Tag{100, pack.Fixed32Type}, pack.Uint32(4294967295),
208 pack.Tag{100, pack.Fixed64Type}, pack.Uint64(18446744073709551615),
209 pack.Tag{100, pack.BytesType}, pack.Bytes("bytes"),
210 pack.Tag{100, pack.StartGroupType},
211 pack.Tag{100, pack.EndGroupType},
212 }),
213}`,
214 }, {
215 raw: dhex("6aa88080808000a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a406"),
216 msg: Message{
217 Tag{13, BytesType}, Denormalized{5, LengthPrefix(Message{
218 Tag{100, VarintType}, Uvarint(math.MaxUint64),
219 Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
220 Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
221 Tag{100, BytesType}, Bytes("bytes"),
222 Tag{100, StartGroupType}, Tag{100, EndGroupType},
223 })},
224 },
225 wantOutCompact: `Message{Tag{13, Bytes}, Denormalized{+5, LengthPrefix(Message{Tag{100, Varint}, Uvarint(18446744073709551615), Tag{100, Fixed32}, Uint32(4294967295), Tag{100, Fixed64}, Uint64(18446744073709551615), Tag{100, Bytes}, Bytes("bytes"), Tag{100, StartGroup}, Tag{100, EndGroup}})}}`,
226 }, {
227 raw: dhex("73a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a40674"),
228 msg: Message{
229 Tag{14, StartGroupType}, Message{
230 Tag{100, VarintType}, Uvarint(math.MaxUint64),
231 Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
232 Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
233 Tag{100, BytesType}, Bytes("bytes"),
234 Tag{100, StartGroupType}, Tag{100, EndGroupType},
235 },
236 Tag{14, EndGroupType},
237 },
238 wantOutMulti: `Message{
239 Tag{14, StartGroup},
240 Message{
241 Tag{100, Varint}, Uvarint(18446744073709551615),
242 Tag{100, Fixed32}, Uint32(4294967295),
243 Tag{100, Fixed64}, Uint64(18446744073709551615),
244 Tag{100, Bytes}, Bytes("bytes"),
245 Tag{100, StartGroup},
246 Tag{100, EndGroup},
247 },
248 Tag{14, EndGroup},
249}`,
250 }, {
251 raw: dhex("d0faa972cd02a5f09051c2d8aa0d6a26a89c311eddef024b423c0f6f47b64227a1600db56e3f73d4113096c9a88e2b99f2d847516853d76a1a6e9811c85a2ab3"),
252 msg: Message{
253 Tag{29970346, VarintType}, Uvarint(333),
254 Tag{21268228, Fixed32Type}, Uint32(229300418),
255 Tag{13, BytesType}, LengthPrefix(Message{
256 Tag{100805, VarintType}, Uvarint(30),
257 Tag{5883, Fixed32Type}, Uint32(255607371),
258 Tag{13, Type(7)},
259 Raw("G\xb6B'\xa1`\r\xb5n?s\xd4\x110\x96ɨ\x8e+\x99\xf2\xd8GQhS"),
260 }),
261 Tag{1706, Type(7)},
262 Raw("\x1an\x98\x11\xc8Z*\xb3"),
263 },
264 }, {
265 raw: dhex("3d08d0e57f"),
266 msg: Message{
267 Tag{7, Fixed32Type}, Float32(math.Float32frombits(
268 // TODO: Remove workaround for compiler bug (see https://golang.org/issues/27193).
269 func() uint32 { return 0x7fe5d008 }(),
270 )),
271 },
272 wantOutSource: `pack.Message{
273 pack.Tag{7, pack.Fixed32Type}, pack.Float32(math.Float32frombits(0x7fe5d008)),
274}`,
275 }, {
276 raw: dhex("51a8d65110771bf97f"),
277 msg: Message{
278 Tag{10, Fixed64Type}, Float64(math.Float64frombits(0x7ff91b771051d6a8)),
279 },
280 wantOutSource: `pack.Message{
281 pack.Tag{10, pack.Fixed64Type}, pack.Float64(math.Float64frombits(0x7ff91b771051d6a8)),
282}`,
283 }, {
284 raw: dhex("ab2c14481ab3e9a76d937fb4dd5e6c616ef311f62b7fe888785fca5609ffe81c1064e50dd7a9edb408d317e2891c0d54c719446938d41ab0ccf8e61dc28b0ebb"),
285 msg: Message{
286 Tag{709, StartGroupType},
287 Tag{2, EndGroupType},
288 Tag{9, VarintType}, Uvarint(26),
289 Tag{28655254, StartGroupType},
290 Message{
291 Tag{2034, StartGroupType},
292 Tag{194006, EndGroupType},
293 },
294 Tag{13, EndGroupType},
295 Tag{12, Fixed64Type}, Uint64(9865274810543764334),
296 Tag{15, VarintType}, Uvarint(95),
297 Tag{1385, BytesType}, Bytes("\xff\xe8\x1c\x10d\xe5\rש"),
298 Tag{17229, Fixed32Type}, Uint32(2313295827),
299 Tag{3, EndGroupType},
300 Tag{1, Fixed32Type}, Uint32(1142540116),
301 Tag{13, Fixed64Type}, Uint64(2154683029754926136),
302 Tag{28856, BytesType},
303 Raw("\xbb"),
304 },
305 }, {
306 raw: dhex("29baa4ac1c1e0a20183393bac434b8d3559337ec940050038770eaa9937f98e4"),
307 msg: Message{
308 Tag{5, Fixed64Type}, Uint64(1738400580611384506),
309 Tag{6, StartGroupType},
310 Message{
311 Tag{13771682, StartGroupType},
312 Message{
313 Tag{175415, VarintType}, Uvarint(7059),
314 },
315 Denormalized{+1, Tag{333, EndGroupType}},
316 Tag{10, VarintType}, Uvarint(3),
317 Tag{1792, Type(7)},
318 Raw("꩓\u007f\x98\xe4"),
319 },
320 },
321 }}
322
323 equateFloatBits := cmp.Options{
324 cmp.Comparer(func(x, y Float32) bool {
325 return math.Float32bits(float32(x)) == math.Float32bits(float32(y))
326 }),
327 cmp.Comparer(func(x, y Float64) bool {
328 return math.Float64bits(float64(x)) == math.Float64bits(float64(y))
329 }),
330 }
331 for _, tt := range tests {
332 t.Run("", func(t *testing.T) {
333 var msg Message
334 raw := tt.msg.Marshal()
335 msg.UnmarshalDescriptor(tt.raw, msgDesc)
336
337 if !bytes.Equal(raw, tt.raw) {
338 t.Errorf("Marshal() mismatch:\ngot %x\nwant %x", raw, tt.raw)
339 }
340 if !cmp.Equal(msg, tt.msg, equateFloatBits) {
341 t.Errorf("Unmarshal() mismatch:\ngot %+v\nwant %+v", msg, tt.msg)
342 }
343 if got, want := tt.msg.Size(), len(tt.raw); got != want {
344 t.Errorf("Size() = %v, want %v", got, want)
345 }
346 if tt.wantOutCompact != "" {
347 gotOut := fmt.Sprintf("%v", tt.msg)
348 if string(gotOut) != tt.wantOutCompact {
349 t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%v", gotOut, tt.wantOutCompact)
350 }
351 }
352 if tt.wantOutMulti != "" {
353 gotOut := fmt.Sprintf("%+v", tt.msg)
354 if string(gotOut) != tt.wantOutMulti {
355 t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%+v", gotOut, tt.wantOutMulti)
356 }
357 }
358 if tt.wantOutSource != "" {
359 gotOut := fmt.Sprintf("%#v", tt.msg)
360 if string(gotOut) != tt.wantOutSource {
361 t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%#v", gotOut, tt.wantOutSource)
362 }
363 }
364 })
365 }
366}